class SvgDrawer::TextBox
Public Class Methods
new(text, params = {})
click to toggle source
Calls superclass method
SvgDrawer::Base::new
# File lib/svg_drawer/text_box.rb, line 19 def initialize(text, params = {}) super(params) @text = text.to_s.strip.gsub(/\s+/, ' ') fonts_config = SvgDrawer.configuration @config = fonts_config['default'].merge(fonts_config[param(:font)] || {}) end
Public Instance Methods
height()
click to toggle source
If overflow is true, but we don't have any explicit height set, report the calculated height.
# File lib/svg_drawer/text_box.rb, line 34 def height return @height if @height ensure_complete! @height = param(:overflow) || !param(:wrap) ? param(:height, calculate_height).to_d : [param(:height, 0), calculate_height].max.to_d end
incomplete()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 42 def incomplete false end
width()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 26 def width return @width if @width ensure_complete! @width = param(:width, calculate_width).to_d end
Private Instance Methods
_draw(parent)
click to toggle source
# File lib/svg_drawer/text_box.rb, line 48 def _draw(parent) # need symbol keys due to a bug in Rasem::SVGTag#write_styles style = {} style[:fill] = param(:font_color) style[:'font-family'] = param(:font) style[:'text-anchor'] = align_to_anchor(param(:text_align)) style[:'font-weight'] = param(:font_style).include?('bold') ? 'bold' : param(:font_weight) style[:'font-style'] = 'italic' if param(:font_style).include?('italic') style[:'font-size'] = param(:font_size).is_a?(Numeric) ? "#{param(:font_size)}px" : param(:font_size) svg_params = { x: 0, y: 0, style: style } case param(:text_align) when 'left' then svg_params[:x] += param(:text_padding)[:left] when 'right' then svg_params[:x] += width - param(:text_padding)[:right] when 'center' then svg_params[:x] += width.to_d / 2 end y_deltas = calculate_y_deltas Utils::RasemWrapper.group(parent, class: param(:class)) do |text_box_group| root_y = svg_params[:y] draw_border(text_box_group) lines.zip(y_deltas).each do |line, delta_y| svg_params[:y] = root_y + delta_y escaped = line.encode(xml: :text) Utils::RasemWrapper.text(text_box_group, svg_params) { |txt| txt.raw(escaped) } end end end
align_to_anchor(align)
click to toggle source
# File lib/svg_drawer/text_box.rb, line 82 def align_to_anchor(align) case align when 'left' then 'start' when 'right' then 'end' when 'center' then 'middle' else raise "Bad text_align: #{align}. Valid are: [left, right, center]" end end
calculate_height()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 131 def calculate_height return 0 if @text.empty? ypad = param(:text_padding)[:top] + param(:text_padding)[:bottom] ypad + lines.size * calculate_line_height_px end
calculate_line_height_px()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 99 def calculate_line_height_px font_height_px * param(:font_size) * param(:line_height) end
calculate_width()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 126 def calculate_width font_width = font_width_px * param(:font_size) @text.length * font_width end
calculate_y_deltas()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 137 def calculate_y_deltas lheight = calculate_line_height_px yoffset = calculate_y_offset_px base_deltas = 1.upto(lines.size).map { |lineno| lineno * lheight - yoffset } remaining_height = height - calculate_height case param(:text_valign) when 'top' base_deltas.map { |bd| bd + param(:text_padding)[:top] } when 'bottom' base_deltas.map { |bd| bd + remaining_height } when 'middle' base_deltas.map { |bd| bd + remaining_height / 2 } else raise "Bad text_valign: #{param(:text_valign)}. Valid are: [top, bottom, middle]" end end
calculate_y_offset_px()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 103 def calculate_y_offset_px font_height_px * param(:font_size) * @config['y_offset'] end
chars_per_line()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 117 def chars_per_line font_width = font_width_px.to_d * param(:font_size) font_wrap_policy = @config['wrap_policies'] wrap_coeff = font_wrap_policy[param(:wrap_policy)] adj_font_width = font_width * wrap_coeff xpad = param(:text_padding)[:left] + param(:text_padding)[:right] ((width - xpad) / adj_font_width).to_i end
font_height_px()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 95 def font_height_px @config['height'] end
font_width_px()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 91 def font_width_px @config['width'] end
lines()
click to toggle source
# File lib/svg_drawer/text_box.rb, line 107 def lines return [@text] if param(:overflow) || !param(:wrap) txt = param(:truncate) ? Utils::Text.truncate(@text, chars_per_line, param(:truncate_with)) : @text Utils::Text.word_wrap(txt, chars_per_line, param(:word_pattern)) # Text.word_wrap(txt, chars_per_line, /\b/) end