module Prawn::Format

Constants

DEFAULT_TAGS
VERSION

Public Instance Methods

default_style() click to toggle source
# File lib/prawn/format.rb, line 62
def default_style
  { :font_family => font.family || font.name,
    :font_size   => font_size,
    :color       => fill_color }
end
draw_lines(x, y, width, lines, options={}) click to toggle source
# File lib/prawn/format.rb, line 102
def draw_lines(x, y, width, lines, options={})
  real_x, real_y = translate(x, y)

  state = options[:state] || {}
  options[:align] ||= :left

  state = state.merge(:width => width,
    :x => x, :y => y,
    :real_x => real_x, :real_y => real_y,
    :dx => 0, :dy => 0)

  state[:cookies] ||= {}
  state[:pending_effects] ||= []

  return state if lines.empty?

  text_object do |text|
    text.rotate(real_x, real_y, options[:rotate] || 0)
    state[:text] = text
    lines.each { |line| line.draw_on(self, state, options) }
  end

  state.delete(:text)

  #rectangle [x, y+state[:dy]], width, state[:dy]
  #stroke

  return state
end
evaluate_measure(measure, options={}) click to toggle source
# File lib/prawn/format.rb, line 68
def evaluate_measure(measure, options={})
  case measure
  when nil then nil
  when Numeric then return measure
  when Symbol then
    mappings = options[:mappings] || {}
    raise ArgumentError, "unrecognized value #{measure.inspect}" unless mappings.key?(measure)
    return evaluate_measure(mappings[measure], options)
  when String then
    operator, value, unit = measure.match(/^([-+]?)(\d+(?:\.\d+)?)(.*)$/)[1,3]

    value = case unit
      when "%" then
        relative = options[:relative] || 0
        relative * value.to_f / 100
      when "em" then
        # not a true em, but good enough for approximating. patches welcome.
        value.to_f * (options[:em] || font_size)
      when "", "pt" then return value.to_f
      when "pc" then return value.to_f * 12
      when "in" then return value.to_f * 72
      else raise ArgumentError, "unsupport units in style value: #{measure.inspect}"
      end

    current = options[:current] || 0
    case operator
    when "+" then return current + value
    when "-" then return current - value
    else return value
    end
  else return measure.to_f
  end
end
format(text, options={}) click to toggle source
# File lib/prawn/format.rb, line 138
def format(text, options={})
  if options[:at]
    x, y = options[:at]
    format_positioned_text(text, x, y, options)
  else
    format_wrapped_text(text, options)
  end
end
layout(text, options={}) { |helper| ... } click to toggle source
# File lib/prawn/format.rb, line 132
def layout(text, options={})
  helper = Format::LayoutBuilder.new(self, text, options)
  yield helper if block_given?
  return helper
end
styles(update={}) click to toggle source
# File lib/prawn/format.rb, line 57
def styles(update={})
  @styles ||= {}
  @styles.update(update)
end
tags(update={}) click to toggle source
# File lib/prawn/format.rb, line 52
def tags(update={})
  @tags ||= DEFAULT_TAGS.dup
  @tags.update(update)
end
text_object() { |open| ... } click to toggle source
# File lib/prawn/format.rb, line 147
def text_object
  object = TextObject.new

  if block_given?
    yield object.open
    add_content(object.close)
  end

  return object
end

Private Instance Methods

format_positioned_text(text, x, y, options={}) click to toggle source
# File lib/prawn/format.rb, line 176
def format_positioned_text(text, x, y, options={})
  helper = layout(text, options)
  line = helper.next
  draw_lines(x, y+line.ascent, line.width, [line], options)
end
format_wrapped_text(text, options={}) click to toggle source
# File lib/prawn/format.rb, line 182
def format_wrapped_text(text, options={})
  helper = layout(text, options)

  start_new_page if self.y < bounds.absolute_bottom

  until helper.done?
    y = self.y - bounds.absolute_bottom
    height = bounds.stretchy? ? bounds.absolute_top : y

    y = helper.fill(bounds.left, y, bounds.width, options.merge(:height => height))

    if helper.done?
      self.y = y + bounds.absolute_bottom
    else
      start_new_page
    end
  end
end
formatted_height(string, line_width, size=font_size, options={}) click to toggle source
# File lib/prawn/format.rb, line 201
def formatted_height(string, line_width, size=font_size, options={})
  helper = layout(string, options.merge(:size => size))
  lines = helper.word_wrap(line_width)
  return lines.inject(0) { |s, line| s + line.height }
end
formatted_width(string, options={}) click to toggle source
# File lib/prawn/format.rb, line 207
def formatted_width(string, options={})
  helper = layout(string, options)
  helper.next.width
end
unformatted?(text, options={}) click to toggle source
# File lib/prawn/format.rb, line 160
def unformatted?(text, options={})
  # If they have a preference, use it
  if options.key?(:plain)
    return options[:plain]

  # Otherwise, if they're asking for full-justification, we must assume
  # the text is formatted (since Prawn's text() method has no full justification)
  elsif options[:align] == :justify
    return false

  # Otherwise, look for tags or XML entities in the text
  else
    return text !~ /<|&(?:#x?)?\w+;/
  end
end