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.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



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

def self.included(mod)
  mod.send :alias_method, :text_without_formatting, :text
  mod.send :alias_method, :text, :text_with_formatting

  mod.send :alias_method, :width_of_without_formatting, :width_of
  mod.send :alias_method, :width_of, :width_of_with_formatting

  mod.send :alias_method, :height_of_without_formatting, :height_of
  mod.send :alias_method, :height_of, :height_of_with_formatting
end

Instance Method Details

#default_styleObject



76
77
78
79
80
# File 'lib/prawn/format.rb', line 76

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/prawn/format.rb', line 116

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/prawn/format.rb', line 82

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



152
153
154
155
156
157
158
159
# File 'lib/prawn/format.rb', line 152

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_with_formatting(string, line_width, size = font_size, options = {}) ⇒ Object

Overloaded version of #height_of. Call via #height_of, rather than #height_of_with_formatting (see above, where it aliased to #height_of).



31
32
33
34
35
36
37
# File 'lib/prawn/format.rb', line 31

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

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

Yields:

  • (helper)


146
147
148
149
150
# File 'lib/prawn/format.rb', line 146

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

#styles(update = {}) ⇒ Object



71
72
73
74
# File 'lib/prawn/format.rb', line 71

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

#tags(update = {}) ⇒ Object



66
67
68
69
# File 'lib/prawn/format.rb', line 66

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

#text_objectObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/prawn/format.rb', line 161

def text_object
  object = TextObject.new

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

  return object
end

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

Overloaded version of #text. Call via #text, rather than #text_with_formatting (see above, where it aliased to #text).



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

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

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

Overloaded version of #width_of. Call via #width_of, rather than #width_of_with_formatting (see above, where it aliased to #width_of).



41
42
43
44
45
46
47
# File 'lib/prawn/format.rb', line 41

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