module TankaRenderer::Renderer::Base

Attributes

body_color[RW]
font[RW]
height[RW]
text_color[RW]
vertical[RW]
width[RW]

Public Class Methods

new() click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 11
def initialize
  @width = 300
  @height = 500
  @font = "IPAMincho"
  @body_color = :white
  @text_color = :black
  @vertical = true
end

Public Instance Methods

draw(context, text) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 28
def draw(context, text)
  draw_body(context)
  draw_text(context, text)
end
guess_font(part_of_font_name) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 20
def guess_font(part_of_font_name)
  @font = Fontdock::Local.find(part_of_font_name)
end
render(text, output_path) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 24
def render(text, output_path)
  @width, @height = @height, @width unless @vertical
end

Private Instance Methods

calc_font_size(text) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 62
def calc_font_size(text)
  max_length = 0
  text.each_line do |line|
    length = line.scan(/[ -~]/).length
    length += line.scan(/[^ -~]/).length * 2
    max_length = length if length > max_length
  end
  if @vertical
    @height * 1.4 / max_length
  else
    @width * 1.4 / max_length
  end
end
draw_body(context) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 34
def draw_body(context)
  context.set_source_color(@body_color)
  context.paint
end
draw_text(context, text) click to toggle source
# File lib/tanka_renderer/renderer/base.rb, line 39
def draw_text(context, text)
  context.set_source_color(@text_color)
  layout = context.create_pango_layout
  if @vertical
    layout.context.base_gravity = :east
  else
    layout.context.base_gravity = :south
  end
  layout.text = text
  size = calc_font_size(text)
  layout.font_description = Pango::FontDescription.new("#{@font} #{size}")
  if @vertical
    x = @width / 2 + text.each_line.to_a.length * size / 1.5
    y = @height / 30
  else
    x = @width / 30
    y = @height / 2 - text.each_line.to_a.length * size / 1.5
  end
  context.move_to(x, y)
  context.rotate(Math::PI / 2) if @vertical
  context.show_pango_layout(layout)
end