Skip to content

Commit

Permalink
add jigoku mode description.
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Dec 22, 2010
1 parent d3016e9 commit 26c0b6d
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 34 deletions.
65 changes: 39 additions & 26 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ def debug?
@@configurations[:debug]
end

def rendering_mode
@@configurations[:rendering_mode]
end

def jigoku?
rendering_mode == :jigoku
end

def prefix
if jigoku?
"地獄の".force_encoding("ascii-8bit")
else
""
end
end

get '/' do
erb :index
end
Expand Down Expand Up @@ -91,17 +107,19 @@ def make_layout(context, text, width, height, font)
font_description.size = 12 * Pango::SCALE
layout.font_description = font_description
yield(layout) if block_given?
prev_size = font_description.size
loop do
current_width, current_height = layout.pixel_size
if width < current_width or height < current_height
font_description.size = prev_size
if height
prev_size = font_description.size
loop do
current_width, current_height = layout.pixel_size
if width < current_width or height < current_height
font_description.size = prev_size
layout.font_description = font_description
break
end
prev_size = font_description.size
font_description.size *= 1.05
layout.font_description = font_description
break
end
prev_size = font_description.size
font_description.size *= 1.05
layout.font_description = font_description
end
context.update_pango_layout(layout)
layout
Expand Down Expand Up @@ -202,27 +220,25 @@ def prepare_jigoku_description(description)
end
end

def render_witticism(context, position, witticism,
paper, margin, max_height, font)
def render_witticism(context, position, witticism, paper, margin, font)
layout = make_layout(context,
witticism,
paper.width - margin * 2,
max_height,
paper.height - margin * 2,
nil,
font) do |_layout|
_layout.context.base_gravity = :east
description = _layout.font_description
description.size = 16 * Pango::SCALE
_layout.font_description = description
end
description = layout.font_description
description.size = 20 * Pango::SCALE
layout.font_description = description

witticism_margin = margin * 2
case position
when :right
witticism_x = paper.width - witticism_margin
witticism_x = paper.width - margin * 2
when :left
witticism_x = witticism_margin + layout.pixel_size[1]
witticism_x = margin * 2 + layout.pixel_size[1]
end
witticism_y = witticism_margin
witticism_y = margin
context.save do
context.move_to(witticism_x, witticism_y)
context.rotate(Math::PI / 2)
Expand Down Expand Up @@ -272,11 +288,8 @@ def render_to_surface_jigoku(surface, scale, paper, info, font)

description = prepare_jigoku_description(info[:description])
right_witticism, left_witticism, garbages = description.split(/\n\n/, 3)
max_height = paper.height - margin * 3
render_witticism(context, :right, right_witticism,
paper, margin, max_height, font)
render_witticism(context, :left, left_witticism,
paper, margin, max_height, font)
render_witticism(context, :right, right_witticism, paper, margin, font)
render_witticism(context, :left, left_witticism, paper, margin, font)

screen_name = info[:screen_name]
layout = make_layout(context,
Expand All @@ -293,7 +306,7 @@ def render_to_surface_jigoku(surface, scale, paper, info, font)
end

def render_to_surface(surface, scale, paper, info, font)
send("render_to_surface_#{@@configurations[:rendering_mode]}",
send("render_to_surface_#{rendering_mode}",
surface, scale, paper, info, font)
end

Expand Down
2 changes: 1 addition & 1 deletion public/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*
fonts/
Binary file added public/sample/jigoku.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<!-- coding: utf-8 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>名札には名前を大きく書きましょうジェネレータ</title>
<title><%= prefix %>名札には名前を大きく書きましょうジェネレータ</title>
</head>
<body>
<h1>名札には名前を大きく書きましょうジェネレータ</h1>
<a href="http://rubykaigi.tdiary.net/20100815.html#p01" target="_blank">
名札には名前を大きく書きましょう
</a>のような名札画像を生成するジェネレータ。
<h1><%= prefix %>名札には名前を大きく書きましょうジェネレータ</h1>
<p>
<a href="http://rubykaigi.tdiary.net/20100815.html#p01" target="_blank">
名札には名前を大きく書きましょう
</a>のような名札画像を生成する<%= prefix %>ジェネレータ。
</p>

<p>
<form action="./" method="post">
Expand Down
32 changes: 30 additions & 2 deletions views/user.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>名札には名前を大きく書きましょうジェネレータ</title>
<title><%= prefix %>名札には名前を大きく書きましょうジェネレータ</title>
<style>
div.description
{
line-height: 80%;
}

div.description em
{
background-color: red;
border: 1px solid blue;
}

div.description img.sample
{
float: left;
padding-right: 10px;
}

div.note
{
font-size: smaller;
color: #ccc;
line-height: 80%;
clear: both;
}

div.thumbnail
Expand All @@ -30,7 +48,7 @@ hr
</style>
</head>
<body>
<h1><a href="/">名札には名前を大きく書きましょうジェネレータ</a></h1>
<h1><%= prefix %><a href="/">名札には名前を大きく書きましょうジェネレータ</a></h1>
<h2><%= h(@user) %></h2>
<p>
<form action="./" method="post">
Expand All @@ -39,6 +57,16 @@ hr
<input type="submit" value="生成!" />
</form>
</p>
<% if jigoku? %>
<div class="description">
<p><img class="sample" src="sample/jigoku.png" alt="地獄のサンプル" /></p>
<p>名言にはTwitterの説明文を使います。Twitter側の情報は1日くらいキャッシュするので、説明文を変更してから生成してください。</p>
<p>空白を2文字入れたところで右側の名言と左側の名言が区切られます。</p>
<p>例: 「(カチャカチャカチャ…)<em>&nbsp;</em><em>&nbsp;</em>(ッターン!)」 </p>
<p>→ 右側の名言:「(カチャカチャカチャ…)」</p>
<p>→ 左側の名言:「(ッターン!)」</p>
</div>
<% end %>
<div class="note">
<p>注意: アカウント情報は1日くらいキャッシュしています。Twitter側の名前の変更やプロフィール画像の変更は1日くらい反映されません。</p>
<p>(キャッシュしないとすぐにTwitter側から情報を取ってこれなくなるのです。)</p>
Expand Down

0 comments on commit 26c0b6d

Please sign in to comment.