Class: Prawn::Format::State

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/format/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options = {}) ⇒ State

Returns a new instance of State.



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

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

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



6
7
8
# File 'lib/prawn/format/state.rb', line 6

def document
  @document
end

#original_styleObject (readonly)

Returns the value of attribute original_style.



7
8
9
# File 'lib/prawn/format/state.rb', line 7

def original_style
  @original_style
end

#styleObject (readonly)

Returns the value of attribute style.



7
8
9
# File 'lib/prawn/format/state.rb', line 7

def style
  @style
end

Instance Method Details

#apply!(text_object, cookies) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prawn/format/state.rb', line 100

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) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/prawn/format/state.rb', line 112

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?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/prawn/format/state.rb', line 125

def bold?
  font_weight == :bold
end

#colorObject



60
61
62
# File 'lib/prawn/format/state.rb', line 60

def color
  @style[:color] || "000000"
end

#displayObject



40
41
42
# File 'lib/prawn/format/state.rb', line 40

def display
  @style[:display] || :inline
end

#fontObject



80
81
82
# File 'lib/prawn/format/state.rb', line 80

def font
  @font ||= document.find_font(font_family, :style => pdf_font_style)
end

#font_familyObject



48
49
50
# File 'lib/prawn/format/state.rb', line 48

def font_family
  @style[:font_family] || "Helvetica"
end

#font_sizeObject



44
45
46
# File 'lib/prawn/format/state.rb', line 44

def font_size
  @style[:font_size] || 12
end

#font_styleObject



52
53
54
# File 'lib/prawn/format/state.rb', line 52

def font_style
  @style[:font_style] || :normal
end

#font_weightObject



56
57
58
# File 'lib/prawn/format/state.rb', line 56

def font_weight
  @style[:font_weight] || :normal
end

#inheritable_styleObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prawn/format/state.rb', line 21

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?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/prawn/format/state.rb', line 121

def italic?
  font_style == :italic
end

#kerning?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/prawn/format/state.rb', line 36

def kerning?
  @style[:kerning]
end

#pdf_font_styleObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/prawn/format/state.rb', line 84

def pdf_font_style
  if bold? && italic?
    :bold_italic
  elsif bold?
    :bold
  elsif italic?
    :italic
  else
    :normal
  end
end

#previous(attr = nil, default = nil) ⇒ Object



129
130
131
132
133
# File 'lib/prawn/format/state.rb', line 129

def previous(attr=nil, default=nil)
  return @previous unless attr
  return default unless @previous
  return @previous.send(attr) || default
end

#text_decorationObject



68
69
70
# File 'lib/prawn/format/state.rb', line 68

def text_decoration
  @style[:text_decoration] || :none
end

#vertical_alignObject



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

def vertical_align
  @style[:vertical_align] || 0
end

#white_spaceObject



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

def white_space
  @style[:white_space] || :normal
end

#widthObject



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

def width
  @style[:width] || 0
end

#with_style(style) ⇒ Object



96
97
98
# File 'lib/prawn/format/state.rb', line 96

def with_style(style)
  self.class.new(document, :previous => self, :style => style)
end