Module: PrawnStyledText

Defined in:
lib/prawn-styled-text/version.rb,
lib/prawn-styled-text/callbacks.rb,
lib/prawn-styled-text/prawn-styled-text.rb

Defined Under Namespace

Classes: HighlightCallback, StrikeThroughCallback

Constant Summary collapse

VERSION =
'0.1.6'
BLOCK_TAGS =
[:br, :div, :h1, :h2, :h3, :h4, :h5, :h6, :hr, :li, :p, :ul].freeze
DEF_BG_MARK =
'ffff00'
DEF_HEADING_T =
16
DEF_HEADING_H =
8
DEF_MARGIN_UL =
15
DEF_SYMBOL_UL =
"\x95 "
HEADINGS =
{ h1: 32, h2: 24, h3: 20, h4: 16, h5: 14, h6: 13 }.freeze
RENAME =
{ 'font-family': :font, 'font-size': :size, 'font-style': :styles, 'letter-spacing': :character_spacing }.freeze
@@margin_ul =
0
@@symbol_ul =
''
@@last_el =
nil

Class Method Summary collapse

Class Method Details

.adjust_values(pdf, values) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/prawn-styled-text/prawn-styled-text.rb', line 21

def self.adjust_values(pdf, values)
  ret = {}
  values.each do |k, v|
    key = k.to_sym
    key = RENAME[key] if RENAME.include?(key)
    ret[key] =
      case key
      when :character_spacing
        v.to_f
      when :color
        v.delete '#'
      when :font
        matches = v.match /'([^']*)'|"([^"]*)"|(.*)/
        matches[3] || matches[2] || matches[1] || ''
      when :height
        i = v.to_i
        v.include?('%') ? (i * pdf.bounds.height * 0.01) : i
      when :size
        v.to_i
      when :styles
        v.split(',').map { |s| s.strip.to_sym }
      when :width
        i = v.to_i
        v.include?('%') ? (i * pdf.bounds.width * 0.01) : i
      else
        v
      end
  end
  ret
end

.closing_tag(pdf, data) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/prawn-styled-text/prawn-styled-text.rb', line 52

def self.closing_tag(pdf, data)
  context = { tag: data[:name], options: {} }
  context[:flush] ||= true if BLOCK_TAGS.include? data[:name]
  # Evalutate tag
  case data[:name]
  when :br # new line
    context[:text] ||= [ { text: "\n" } ] if @@last_el == :br
  when :img # image
    context[:flush] ||= true
    context[:src] = data[:node].get 'src'
  when :ul
    @@margin_ul = 0
  end
  # Evalutate attributes
  attributes = data[:node].get 'style'
  context[:options] = adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/)) if attributes
  context
end

.opening_tag(pdf, data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prawn-styled-text/prawn-styled-text.rb', line 71

def self.opening_tag(pdf, data)
  context = { tag: data[:name], options: {} }
  context[:flush] ||= true if BLOCK_TAGS.include? data[:name]
  # Evalutate attributes
  attributes = data[:node].get 'style'
  context[:options].merge!(adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/))) if attributes
  if data[:name] == :ul
    @@margin_ul += (context[:options][:'margin-left'] ? context[:options][:'margin-left'].to_i : DEF_MARGIN_UL)
    @@symbol_ul = if context[:options][:'list-symbol']
        matches = context[:options][:'list-symbol'].match /'([^']*)'|"([^"]*)"|(.*)/
        matches[3] || matches[2] || matches[1] || ''
      else
        DEF_SYMBOL_UL.dup
      end
  end
  context
end

.text_node(pdf, data) ⇒ Object



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/prawn-styled-text/prawn-styled-text.rb', line 89

def self.text_node(pdf, data)
  context = { pre: '', options: {} }
  styles = []
  font_size = pdf.font_size
  data.each do |part|
    # Evalutate tag
    tag = part[:name]
    case tag
    when :a # link
      link = part[:node].get 'href'
      context[:options][:link] = link if link
    when :b, :strong # bold
      styles.push :bold
    when :del, :s
      @@strike_through ||= StrikeThroughCallback.new(pdf)
      context[:options][:callback] = @@strike_through
    when :h1, :h2, :h3, :h4, :h5, :h6
      context[:options][:size] = HEADINGS[tag]
      context[:options][:'margin-top'] = DEF_HEADING_T
      context[:options][:'line-height'] = DEF_HEADING_H
    when :i, :em # italic
      styles.push :italic
    when :li # list item
      context[:options][:'margin-left'] = @@margin_ul
      context[:pre] = @@symbol_ul.force_encoding('windows-1252').encode('UTF-8')
    when :mark
      @@highlight ||= HighlightCallback.new(pdf)
      @@highlight.set_color nil
      context[:options][:callback] = @@highlight
    when :small
      context[:options][:size] = font_size * 0.66
    when :u, :ins # underline
      styles.push :underline
    end
    context[:options][:styles] = styles if styles.any?
    # Evalutate attributes
    attributes = part[:node].get 'style'
    if attributes
      values = adjust_values(pdf, attributes.scan(/\s*([^:]+):\s*([^;]+)[;]*/))
      @@highlight.set_color(values[:background].delete('#')) if tag == :mark && values[:background]
      context[:options].merge! values
    end
    font_size = context[:options][:size] if font_size
  end
  context
end

.traverse(nodes, context = [], &block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/prawn-styled-text/prawn-styled-text.rb', line 136

def self.traverse(nodes, context = [], &block)
  nodes.each do |node|
    if node.is_a? Oga::XML::Text
      text = node.text.delete("\n\r")
      yield :text_node, text, context
      @@last_el = nil unless text.empty?
    elsif node.is_a? Oga::XML::Element
      element = { name: node.name.to_sym, node: node }
      yield :opening_tag, element[:name], element
      context.push(element)
      traverse(node.children, context, &block) if node.children.count > 0
      yield :closing_tag, element[:name], context.pop
      @@last_el = element[:name]
    end
  end
end