class Prawn::Format::State

Constants

HTML_COLORS

Attributes

document[R]
original_style[R]
style[R]

Public Class Methods

new(document, options={}) click to toggle source
# File lib/prawn/format/state.rb, line 8
def initialize(document, options={})
  @document = document
  @previous = options[:previous]

  @original_style = (@previous && @previous.inheritable_style || {}).
    merge(options[:style] || {})

  compute_styles!

  @style[:kerning] = font.has_kerning_data? unless @style.key?(:kerning)
end

Public Instance Methods

apply!(text_object, cookies) click to toggle source
# File lib/prawn/format/state.rb, line 99
def apply!(text_object, cookies)
  if cookies[:color] != color
    cookies[:color] = color
    text_object.fill_color(color)
  end

  if cookies[:vertical_align] != vertical_align
    cookies[:vertical_align] = vertical_align
    text_object.rise(vertical_align)
  end
end
apply_font!(text_object, cookies, subset) click to toggle source
# File lib/prawn/format/state.rb, line 111
def apply_font!(text_object, cookies, subset)
  if cookies[:font] != [font_family, pdf_font_style, font_size, subset]
    cookies[:font] = [font_family, pdf_font_style, font_size, subset]
    font = document.font(font_family, :style => pdf_font_style)
    font.add_to_current_page(subset)
    text_object.font(font.identifier_for(subset), font_size)
  end
end
bold?() click to toggle source
# File lib/prawn/format/state.rb, line 124
def bold?
  font_weight == :bold
end
color() click to toggle source
# File lib/prawn/format/state.rb, line 59
def color
  @style[:color] || "000000"
end
display() click to toggle source
# File lib/prawn/format/state.rb, line 39
def display
  @style[:display] || :inline
end
font() click to toggle source
# File lib/prawn/format/state.rb, line 79
def font
  @font ||= document.find_font(font_family, :style => pdf_font_style)
end
font_family() click to toggle source
# File lib/prawn/format/state.rb, line 47
def font_family
  @style[:font_family] || "Helvetica"
end
font_size() click to toggle source
# File lib/prawn/format/state.rb, line 43
def font_size
  @style[:font_size] || 12
end
font_style() click to toggle source
# File lib/prawn/format/state.rb, line 51
def font_style
  @style[:font_style] || :normal
end
font_weight() click to toggle source
# File lib/prawn/format/state.rb, line 55
def font_weight
  @style[:font_weight] || :normal
end
inheritable_style() click to toggle source
# File lib/prawn/format/state.rb, line 20
def inheritable_style
  @inheritable_style ||= begin
    subset = original_style.dup
    subset.delete(:meta)
    subset.delete(:display)
    subset.delete(:width)

    # explicitly set font-size so that relative font-sizes don't get
    # recomputed upon each nesting.
    subset[:font_size] = font_size

    subset
  end
end
italic?() click to toggle source
# File lib/prawn/format/state.rb, line 120
def italic?
  font_style == :italic
end
kerning?() click to toggle source
# File lib/prawn/format/state.rb, line 35
def kerning?
  @style[:kerning]
end
pdf_font_style() click to toggle source
# File lib/prawn/format/state.rb, line 83
def pdf_font_style
  if bold? && italic?
    :bold_italic
  elsif bold?
    :bold
  elsif italic?
    :italic
  else
    :normal
  end
end
previous(attr=nil, default=nil) click to toggle source
# File lib/prawn/format/state.rb, line 128
def previous(attr=nil, default=nil)
  return @previous unless attr
  return default unless @previous
  return @previous.send(attr) || default
end
text_decoration() click to toggle source
# File lib/prawn/format/state.rb, line 67
def text_decoration
  @style[:text_decoration] || :none
end
vertical_align() click to toggle source
# File lib/prawn/format/state.rb, line 63
def vertical_align
  @style[:vertical_align] || 0
end
white_space() click to toggle source
# File lib/prawn/format/state.rb, line 71
def white_space
  @style[:white_space] || :normal
end
width() click to toggle source
# File lib/prawn/format/state.rb, line 75
def width
  @style[:width] || 0
end
with_style(style) click to toggle source
# File lib/prawn/format/state.rb, line 95
def with_style(style)
  self.class.new(document, :previous => self, :style => style)
end

Private Instance Methods

compute_styles!() click to toggle source
# File lib/prawn/format/state.rb, line 136
def compute_styles!
  @style = @original_style.dup

  evaluate_style(:font_size, 12, :current)
  evaluate_style(:vertical_align, 0, font_size, :super => "+40%", :sub => "-30%")
  evaluate_style(:width, 0, document.bounds.width)

  @style[:color] = evaluate_color(@style[:color])
end
evaluate_color(color) click to toggle source
# File lib/prawn/format/state.rb, line 173
def evaluate_color(color)
  case color
  when nil then nil
  when /^\s*#?([a-f0-9]{3})\s*$/i then
    return $1.gsub(/./, '\&0')
  when /^\s*#?([a-f0-9]+)$\s*/i then
    return $1
  when /^\s*rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/
    return "%02x%02x%02x" % [$1.to_i, $2.to_i, $3.to_i]
  else
    return HTML_COLORS[color.strip.downcase]
  end
end
evaluate_style(which, default, relative_to, mappings={}) click to toggle source
# File lib/prawn/format/state.rb, line 146
def evaluate_style(which, default, relative_to, mappings={})
  current = previous(which, default)
  relative_to = current if relative_to == :current
  @style[which] = document.evaluate_measure(@style[which],
    :em => @previous && @previous.font_size || 12,
    :current => current, :relative => relative_to, :mappings => mappings) || default
end