Module: FN::PDF::Node::CreateTextflow

Extended by:
CreateTextflow
Includes:
Node::Base
Included in:
CreateTextflow
Defined in:
lib/fn/pdf/node/create_textflow.rb

Constant Summary collapse

NL =
/\r|\n/
SPACE =
/\s+/
BASE_FORMAT =
{
  "face" => "Arial",
  "size" => "12",
  "blockindent" => "0",
  "indent" => "0",
  "leftmargin" => "0",
  "rightmargin" => "0",
  "leading" => "2",
  "color" => "#0000FF",
  "align" => "left",
  "bold" => false,
  "italic" => false,
  "underline" => false
}

Constants included from Node::Base

Node::Base::CURRENT_PAGE_HEIGHT, Node::Base::CURRENT_PAGE_WIDTH

Instance Method Summary collapse

Methods included from Node::Base

#classify, #has_no_children, #mixin, #value, #visit_children, #with_attributes_like

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fn/pdf/node/create_textflow.rb', line 44

def empty?
  inner_text.gsub(/<[^>]*>|\s+/, '').empty?
end

#flow_name(node = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fn/pdf/node/create_textflow.rb', line 32

def flow_name(node = nil)
  if node
    if node["id"]
      "flow-#{node["id"]}"
    else
      "flow-#{node["text"]}-#{node["index"]}"
    end
  else
    self["assigns"]
  end
end

#format(node, buffer = "<fontname=Arial encoding=#{Writer.encoding} fontsize=10>", options = BASE_FORMAT) ⇒ Object



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/fn/pdf/node/create_textflow.rb', line 95

def format(node, buffer = "<fontname=Arial encoding=#{Writer.encoding} fontsize=10>", options = BASE_FORMAT)
  return nil unless node
  
  options = options.dup
  if node.text?
    buffer << formatter(options)
    buffer << node.to_s.gsub(NL, '') 
  elsif node.element?
    case node.name.downcase
    when "br"
      buffer << "<nextline>"
    when "b"
      options["bold"] = true
    when "i"
      options["italic"] = true
    when "u"
      options["underline"] = true
    when "li"
      buffer << "&bull;&nbsp;"
    when "font"
      options["size"]   = node["size"]  if node["size"]
      options["face"]   = node["face"]  if node["face"]
      options["color"]  = node["color"] if node["color"]
      
      options["size"]   = node["SIZE"]  if node["SIZE"]
      options["face"]   = node["FACE"]  if node["FACE"]
      options["color"]  = node["COLOR"] if node["COLOR"]
    end
      
    node.children.each{|c| format(c, buffer, options)}
    
    case node.name.downcase
    when "p", "li"
      buffer << "<nextparagraph>"
    end
  else
    raise "unhandled node type"
  end
  return buffer
end

#format_color(hex) ⇒ Object



91
92
93
# File 'lib/fn/pdf/node/create_textflow.rb', line 91

def format_color(hex)
  hex.scan(/[\da-f]{2}/i).map{|s| s.to_i(16) / 255.0 }.join(" ")
end

#format_face(attrs) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/fn/pdf/node/create_textflow.rb', line 83

def format_face(attrs)
  # titlecase
  face = attrs["face"].split(SPACE).map{|s| s.capitalize }.join(" ")
  face += ",Bold" if attrs["bold"]
  face += ",Italic" if attrs["italic"]
  return face
end

#formatter(options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fn/pdf/node/create_textflow.rb', line 57

def formatter(options)
  attrs = options.dup
  attrs["leftindent"] = (attrs["blockindent"].to_f + attrs["leftmargin"].to_f).to_i
  attrs["totalleading"] = (attrs["size"].to_f + attrs["leading"].to_f).to_i
  # puts attrs.inspect
  attrs["formatted_color"] = format_color(attrs["color"])
  attrs["formatted_face"] = format_face(attrs)
  %(<encoding=unicode 
  embedding=true 
  adjustmethod=nofit 
  nofitlimit=100% 
  minspacing=100% 
  maxspacing=100% 
  kerning=true 
  charref=true
  fontname={#{attrs["formatted_face"]}} 
  fontsize=#{attrs['size']} 
  leftindent=#{attrs['leftindent']}
  rightindent=#{attrs['rightmargin']}
  leading=#{attrs["totalleading"]} 
  fillcolor={rgb #{attrs["formatted_color"]}}
  parindent=#{attrs["indent"]}
  underline=#{attrs['underline']}
  alignment=#{attrs["align"]}>).gsub(SPACE, ' ')
end

#inner_textObject



48
49
50
# File 'lib/fn/pdf/node/create_textflow.rb', line 48

def inner_text
  child.to_s.gsub("<![CDATA[", "").gsub("]]>", "")
end

#visit(struct, debug = false) ⇒ Object



52
53
54
55
# File 'lib/fn/pdf/node/create_textflow.rb', line 52

def visit(struct, debug = false)
  tf = struct.create_textflow(inner_text, "")
  struct.assigns self, tf
end