Class: RSyntaxTree::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/rsyntaxtree/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, parent, content, level, fontset, fontsize, global) ⇒ Element

Returns a new instance of Element.



17
18
19
20
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
51
52
53
54
55
# File 'lib/rsyntaxtree/element.rb', line 17

def initialize(id, parent, content, level, fontset, fontsize, global)
  @global = global
  @type = ETYPE_LEAF
  @id = id                 # Unique element id
  @parent = parent         # Parent element id
  @children = []           # Child element ids
  @level = level           # Element level in the tree (0=top etc...)
  @width = 0               # Width of the part of the tree including itself and it governs
  @content_width = 0       # Width of the content
  @horizontal_indent = 0   # Drawing offset
  @vertical_indent = 0     # Drawing offset
  content = content.strip

  @path = if /.+?\^?((?:\+-?>?<?\d+)+)\^?\z/m =~ content
            $1.sub(/\A\+/, "").split("+")
          else
            []
          end

  @fontset = fontset
  @fontsize = fontsize

  parsed = Markup.parse(content)

  if parsed[:status] == :success
    results = parsed[:results]
  else
    error_text = +"Error: input text contains an invalid string"
    error_text += "\n > " + content
    raise RSTError, error_text
  end
  @content = results[:contents]
  @paths = results[:paths]
  @enclosure = results[:enclosure]
  @triangle = results[:triangle]

  @contains_phrase = false
  setup
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def children
  @children
end

#contains_phraseObject

Returns the value of attribute contains_phrase.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def contains_phrase
  @contains_phrase
end

#contentObject

Returns the value of attribute content.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def content
  @content
end

#content_heightObject

Returns the value of attribute content_height.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def content_height
  @content_height
end

#content_widthObject

Returns the value of attribute content_width.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def content_width
  @content_width
end

#enclosureObject

Returns the value of attribute enclosure.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def enclosure
  @enclosure
end

#fontObject

Returns the value of attribute font.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def font
  @font
end

#fontsizeObject

Returns the value of attribute fontsize.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def fontsize
  @fontsize
end

#heightObject

Returns the value of attribute height.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def height
  @height
end

#horizontal_indentObject

Returns the value of attribute horizontal_indent.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def horizontal_indent
  @horizontal_indent
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def id
  @id
end

#levelObject

Returns the value of attribute level.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def level
  @level
end

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def path
  @path
end

#triangleObject

Returns the value of attribute triangle.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def triangle
  @triangle
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def type
  @type
end

#vertical_indentObject

Returns the value of attribute vertical_indent.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def vertical_indent
  @vertical_indent
end

#widthObject

Returns the value of attribute width.



15
16
17
# File 'lib/rsyntaxtree/element.rb', line 15

def width
  @width
end

Instance Method Details

#add_child(child_id) ⇒ Object



158
159
160
# File 'lib/rsyntaxtree/element.rb', line 158

def add_child(child_id)
  @children << child_id
end

#setupObject



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
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
115
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
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rsyntaxtree/element.rb', line 57

def setup
  total_width = 0
  total_height = 0
  one_bvm_given = false
  @content.each do |content|
    content_width = 0
    case content[:type]
    when :border, :bborder
      height = @global[:single_line_height] / 2
      content[:height] = height
      total_height += height
    when :text
      row_width = 0
      elements_height = []
      content[:elements].each do |e|
        text = e[:text]
        e[:text] = text.gsub(" ", WHITESPACE_BLOCK).gsub(">", '&#62;').gsub("<", '&#60;')

        @contains_phrase = true if text.include?(" ")
        decoration = e[:decoration]
        fontsize = decoration.include?(:small) ? @fontsize * SUBSCRIPT_CONST : @fontsize
        fontsize = decoration.include?(:subscript) || decoration.include?(:superscript) ? fontsize * SUBSCRIPT_CONST : fontsize
        style    = decoration.include?(:italic) || decoration.include?(:bolditalic) ? :italic : :normal
        weight   = decoration.include?(:bold) || decoration.include?(:bolditalic) ? :bold : :normal
        font = if decoration.include? :bolditalic
                 @fontset[:bolditalic]
               elsif decoration.include? :bold
                 @fontset[:bold]
               elsif decoration.include? :italic
                 @fontset[:italic]
               else
                 @fontset[:normal]
               end

        standard_metrics = FontMetrics.get_metrics('X', @fontset[:normal], fontsize, :normal, :normal)

        height = standard_metrics.height
        if /\A[<>]+\z/ =~ text
          width = standard_metrics.width * text.size / 2
        elsif text.contains_emoji?
          segments = text.split_by_emoji
          width = 0
          segments.each do |seg|
            ch = if /\s/ =~ seg[:char]
                   't'
                 else
                   seg[:char]
                 end
            this_font = if seg[:type] == :emoji
                          @fontset[:emoji]
                        else
                          font
                        end
            metrics = FontMetrics.get_metrics(ch, this_font, fontsize, style, weight)
            width += metrics.width
          end
        else
          text.gsub!("\\\\", 'i')
          text.gsub!("\\", "")
          text.gsub!(" ", "x")
          text.gsub!("%", "X")
          metrics = FontMetrics.get_metrics(text, font, fontsize, style, weight)
          width = metrics.width
        end

        if e[:decoration].include?(:box) || e[:decoration].include?(:circle) || e[:decoration].include?(:bar)
          e[:content_width] = width
          width += if e[:text].size == 1
                     height - width
                   else
                     @global[:width_half_x]
                   end
        end

        if e[:decoration].include?(:whitespace)
          width = @global[:width_half_x] / 2 * e[:text].size / 4
          e[:text] = ""
        end

        e[:height] = height

        if one_bvm_given
          elements_height << height
        else
          one_bvm_given = true
          elements_height << height + @global[:box_vertical_margin]
        end

        e[:width] = width
        row_width += width
      end

      total_height += elements_height.max
      content_width += row_width
    end
    total_width = content_width if total_width < content_width
  end
  @content_width = total_width
  @content_height = total_height
end