Module: Prawn::Format

Defined in:
lib/prawn/format.rb,
lib/prawn/format/line.rb,
lib/prawn/format/lexer.rb,
lib/prawn/format/state.rb,
lib/prawn/format/parser.rb,
lib/prawn/format/version.rb,
lib/prawn/format/text_object.rb,
lib/prawn/format/effects/link.rb,
lib/prawn/format/layout_builder.rb,
lib/prawn/format/effects/underline.rb,
lib/prawn/format/instructions/base.rb,
lib/prawn/format/instructions/text.rb,
lib/prawn/format/instructions/tag_open.rb,
lib/prawn/format/instructions/tag_close.rb

Defined Under Namespace

Modules: Effects, Instructions Classes: LayoutBuilder, Lexer, Line, Parser, State, TextObject

Constant Summary collapse

DEFAULT_TAGS =
{
  :a      => { :meta => { :name => :anchor, :href => :target }, :color => "0000ff", :text_decoration => :underline },
  :b      => { :font_weight => :bold },
  :br     => { :display => :break },
  :code   => { :font_family => "Courier", :font_size => "90%" },
  :em     => { :font_style => :italic },
  :font   => { :meta => { :face => :font_family, :color => :color, :size => :font_size } },
  :i      => { :font_style => :italic },
  :pre    => { :white_space => :pre, :font_family => "Courier", :font_size => "90%" },
  :span   => {},
  :strong => { :font_weight => :bold },
  :sub    => { :vertical_align => :sub, :font_size => "70%" },
  :sup    => { :vertical_align => :super, :font_size => "70%" },
  :tt     => { :font_family => "Courier" },
  :u      => { :text_decoration => :underline },
}.freeze
VERSION =
"0.2.3"

Instance Method Summary collapse

Instance Method Details

#default_styleObject



63
64
65
66
67
# File 'lib/prawn/format.rb', line 63

def default_style
  { :font_family => font.family || font.name,
    :font_size   => font_size,
    :color       => fill_color }
end

#draw_lines(x, y, width, lines, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/prawn/format.rb', line 103

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 = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/prawn/format.rb', line 69

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 = {}) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/prawn/format.rb', line 139

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

#height_of(string, line_width, size = font_size, options = {}) ⇒ Object

Overloaded version of #height_of.



19
20
21
22
23
24
25
# File 'lib/prawn/format.rb', line 19

def height_of(string, line_width, size=font_size, options={}) #:nodoc:
  if unformatted?(string, options)
    super(string, line_width, size)
  else
    formatted_height(string, line_width, size, options)
  end
end

#layout(text, options = {}) {|helper| ... } ⇒ Object

Yields:

  • (helper)


133
134
135
136
137
# File 'lib/prawn/format.rb', line 133

def layout(text, options={})
  helper = Format::LayoutBuilder.new(self, text, options)
  yield helper if block_given?
  return helper
end

#styles(update = {}) ⇒ Object



58
59
60
61
# File 'lib/prawn/format.rb', line 58

def styles(update={})
  @styles ||= {}
  @styles.update(update)
end

#tags(update = {}) ⇒ Object



53
54
55
56
# File 'lib/prawn/format.rb', line 53

def tags(update={})
  @tags ||= DEFAULT_TAGS.dup
  @tags.update(update)
end

#text(text, options = {}) ⇒ Object

Overloaded version of #text.



10
11
12
13
14
15
16
# File 'lib/prawn/format.rb', line 10

def text(text, options={}) #:nodoc:
  if unformatted?(text, options)
    super
  else
    format(text, options)
  end
end

#text_objectObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/prawn/format.rb', line 148

def text_object
  object = TextObject.new

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

  return object
end

#width_of(string, options = {}) ⇒ Object

Overloaded version of #width_of.



28
29
30
31
32
33
34
# File 'lib/prawn/format.rb', line 28

def width_of(string, options={}) #:nodoc:
  if unformatted?(string, options)
    super
  else
    formatted_width(string, options)
  end
end