Class: SVGGraph
- Inherits:
-
Object
- Object
- SVGGraph
- Defined in:
- lib/rsyntaxtree/svg_graph.rb
Overview
constant variables are already set in tree_graph.rb
Instance Method Summary collapse
- #calc_children_width(id) ⇒ Object
-
#calc_element_width(e) ⇒ Object
Calculate the width of the element.
-
#calc_level_width(level) ⇒ Object
Calculate the width of all elements in a certain level.
-
#create_tempf(basename, ext, num = 10) ⇒ Object
Create a temporary file and returns only its filename.
-
#draw_element(x, y, w, string, type) ⇒ Object
Add the element into the tree (draw it).
-
#fix_child_size(id, current, target) ⇒ Object
If a node element text is wider than the sum of it’s child elements, then the child elements need to be resized to even out the space.
- #get_children_indent(id) ⇒ Object
- #get_children_width(id) ⇒ Object
-
#initialize(e_list, symmetrize = true, color = true, leafstyle = "triangle", font = "Helvetica", font_size = 10, simple = false) ⇒ SVGGraph
constructor
A new instance of SVGGraph.
-
#line_to_parent(fromX, fromY, fromW, toX, toW) ⇒ Object
Draw a line between child/parent elements.
-
#parse_list ⇒ Object
Parse the elements in the list top to bottom and draw the elements into the image.
-
#row2px(row) ⇒ Object
Calculate top position from row (level).
- #svg_data ⇒ Object
-
#triangle_to_parent(fromX, fromY, fromW, toW, textW) ⇒ Object
Draw a triangle between child/parent elements.
Constructor Details
#initialize(e_list, symmetrize = true, color = true, leafstyle = "triangle", font = "Helvetica", font_size = 10, simple = false) ⇒ SVGGraph
Returns a new instance of SVGGraph.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 38 def initialize(e_list, symmetrize = true, color = true, leafstyle = "triangle", font = "Helvetica", font_size = 10, simple = false) # Store parameters @e_list = e_list @font = font @font_size = font_size @leafstyle = leafstyle @symmetrize = symmetrize # Element dimensions @e_width = E_WIDTH # Calculate image dimensions @e_height = @font_size + E_PADD * 2 h = @e_list.get_level_height w = calc_level_width(0) w_px = w + B_SIDE * 2 h_px = h * @e_height + (h-1) * (V_SPACE + @font_size) + B_TOPBOT * 2 @height = h_px @width = w_px # Initialize the image and colors @col_bg = "none" @col_fg = "black" @col_line = "black" if color @col_node = "blue" @col_leaf = "green" @col_trace = "red" else @col_node = "black" @col_leaf = "black" @col_trace = "black" end @line_styles = "<line style='stroke:black; stroke-width:1;' x1='X1' y1='Y1' x2='X2' y2='Y2' />\n" @polygon_styles = "<polygon style='fill: white; stroke: black; stroke-width:1;' points='X1 Y1 X2 Y2 X3 Y3' />\n" @text_styles = "<text style='fill: COLOR; font-size: FONT_SIZEpx;' x='X_VALUE' y='Y_VALUE'>CONTENT</text>\n" @tree_data = String.new end |
Instance Method Details
#calc_children_width(id) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 297 def calc_children_width(id) left = 0 right = 0 c_list = @e_list.get_children(id) return nil if c_list.empty? c_list.each do |c| left = c.indent if indent == 0 or left > c.indent end c_list.each do |c| right = c.indent + e.width if c.indent + c.width > right end return [left, right] end |
#calc_element_width(e) ⇒ Object
Calculate the width of the element. If the element is
a node, the calculation will be performed recursively
for all child elements.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 259 def calc_element_width(e) w = 0 children = @e_list.get_children(e.id) if(children.length == 0) w = img_get_txt_width(e.content, @font, @font_size) + @font_size else children.each do |child| child_e = @e_list.get_id(child) w += calc_element_width(child_e) end tw = img_get_txt_width(e.content, @font, @font_size) + @font_size if(tw > w) fix_child_size(e.id, w, tw) w = tw end end @e_list.set_element_width(e.id, w) return w end |
#calc_level_width(level) ⇒ Object
Calculate the width of all elements in a certain level
284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 284 def calc_level_width(level) w = 0 e = @e_list.get_first while e if(e.level == level) w += calc_element_width(e) end e = @e_list.get_next end return w end |
#create_tempf(basename, ext, num = 10) ⇒ Object
Create a temporary file and returns only its filename
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 103 def create_tempf(basename, ext, num = 10) flags = File::RDWR | File::CREAT | File::EXCL tfname = "" num.times do |i| begin tfname = "#{basename}.#{$$}.#{i}.#{ext}" tfile = File.open(tfname, flags, 0600) rescue Errno::EEXIST next end tfile.close return tfname end end |
#draw_element(x, y, w, string, type) ⇒ Object
Add the element into the tree (draw it)
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 121 def draw_element(x, y, w, string, type) # Calculate element dimensions and position if (type == ETYPE_LEAF) and @leafstyle == "nothing" top = row2px(y - 1) + (@font_size * 1.5) else top = row2px(y) end left = x + B_SIDE bottom = top + @e_height right = left + w # Split the string into the main part and the # subscript part of the element (if any) main = string sub = "" sub_size = (@font_size * 0.7 ) parts = string.split("_", 2) if(parts.length > 1 ) main = parts[0] sub = parts[1].gsub(/_/, " ") end # Calculate text size for the main and the # subscript part of the element main_width = img_get_txt_width(main, @font, @font_size) if sub != "" sub_width = img_get_txt_width(sub.to_s, @font, sub_size) else sub_width = 0 end # Center text in the element txt_width = main_width + sub_width txt_pos = left + (right - left) / 2 - txt_width / 2 # Select apropriate color if(type == ETYPE_LEAF) col = @col_leaf else col = @col_node end if(main[0].chr == "<" && main[-1].chr == ">") col = @col_trace end # Draw main text main_data = @text_styles.sub(/COLOR/, col) main_data = main_data.sub(/FONT_SIZE/, @font_size.to_s) main_x = txt_pos main_y = top + @e_height - E_PADD main_data = main_data.sub(/X_VALUE/, main_x.to_s) main_data = main_data.sub(/Y_VALUE/, main_y.to_s) @tree_data += main_data.sub(/CONTENT/, main) # Draw subscript text sub_data = @text_styles.sub(/COLOR/, col) sub_data = sub_data.sub(/FONT_SIZE/, @font_size.to_s) sub_x = main_x + main_width + (sub_size/8) sub_y = top + (@e_height - E_PADD + sub_size / 2).ceil if (sub.length > 0 ) sub_data = sub_data.sub(/X_VALUE/, sub_x.ceil.to_s) sub_data = sub_data.sub(/Y_VALUE/, sub_y.ceil.to_s) @tree_data += sub_data.sub(/CONTENT/, sub) end end |
#fix_child_size(id, current, target) ⇒ Object
If a node element text is wider than the sum of it’s
child elements, then the child elements need to
be resized to even out the space. This function
recurses down the a child tree and sizes the
children appropriately.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 241 def fix_child_size(id, current, target) children = @e_list.get_children(id) @e_list.set_element_width(id, target) if(children.length > 0 ) delta = target - current target_delta = delta / children.length children.each do |child| child_width = @e_list.get_element_width(child) fix_child_size(child, child_width, child_width + target_delta) end end end |
#get_children_indent(id) ⇒ Object
312 313 314 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 312 def get_children_indent(id) calc_children_width(id)[0] end |
#get_children_width(id) ⇒ Object
316 317 318 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 316 def get_children_width(id) calc_children_width(id)[1] - get_children_indent(id) end |
#line_to_parent(fromX, fromY, fromW, toX, toW) ⇒ Object
Draw a line between child/parent elements
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 194 def line_to_parent(fromX, fromY, fromW, toX, toW) if (fromY == 0 ) return end fromTop = row2px(fromY) fromLeft = (fromX + fromW / 2 + B_SIDE) toBot = (row2px(fromY - 1 ) + @e_height) toLeft = (toX + toW / 2 + B_SIDE) line_data = @line_styles.sub(/X1/, fromLeft.ceil.to_s.to_s) line_data = line_data.sub(/Y1/, fromTop.ceil.to_s.to_s) line_data = line_data.sub(/X2/, toLeft.ceil.to_s.to_s) @tree_data += line_data.sub(/Y2/, toBot.ceil.to_s.to_s) end |
#parse_list ⇒ Object
Parse the elements in the list top to bottom and
draw the elements into the image.
As we it iterate through the levels, the element
indentation is calculated.
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 324 def parse_list # Calc element list recursively.... e_arr = @e_list.get_elements h = @e_list.get_level_height h.times do |i| x = 0 e_arr.each do |j| if (j.level == i) cw = @e_list.get_element_width(j.id) parent_indent = @e_list.get_indent(j.parent) if (x < parent_indent) x = parent_indent end @e_list.set_indent(j.id, x) if !@symmetrize draw_element(x, i, cw, j.content, j.type) if(j.parent != 0 ) words = j.content.split(" ") unless @leafstyle == "nothing" && ETYPE_LEAF == j.type if (@leafstyle == "triangle" && ETYPE_LEAF == j.type && x == parent_indent && words.length > 1) txt_width = img_get_txt_width(j.content, @font, @font_size) triangle_to_parent(x, i, cw, @e_list.get_element_width(j.parent), txt_width) else line_to_parent(x, i, cw, @e_list.get_indent(j.parent), @e_list.get_element_width(j.parent)) end end end end x += cw end end end return true if !@symmetrize h.times do |i| curlevel = h - i - 1 indent = 0 e_arr.each_with_index do |j, idx| if (j.level == curlevel) # Draw a line to the parent element children = @e_list.get_children(j.id) tw = img_get_txt_width(j.content, @font, @font_size) if children.length > 1 left, right = -1, -1 children.each do |child| k = @e_list.get_id(child) kw = img_get_txt_width(k.content, @font, @font_size) left = k.indent + kw / 2 if k.indent + kw / 2 < left or left == -1 right = k.indent + kw / 2 if k.indent + kw / 2 > right end draw_element(left, curlevel, right - left, j.content, j.type) @e_list.set_indent(j.id, left + (right - left) / 2 - tw / 2) children.each do |child| k = @e_list.get_id(child) words = k.content.split(" ") dw = img_get_txt_width(k.content, @font, @font_size) unless @leafstyle == "nothing" && ETYPE_LEAF == k.type if (@leafstyle == "triangle" && ETYPE_LEAF == k.type && k.indent == j.indent && words.length > 1) txt_width = img_get_txt_width(k.content, @font, @font_size) triangle_to_parent(k.indent, curlevel + 1, dw, tw, txt_width) else line_to_parent(k.indent, curlevel + 1, dw, j.indent, tw) end end end else unless children.empty? k = @e_list.get_id(children[0]) kw = img_get_txt_width(k.content, @font, @font_size) left = k.indent right = k.indent + kw draw_element(left, curlevel, right - left, j.content, j.type) @e_list.set_indent(j.id, left + (right - left) / 2 - tw / 2) else parent = @e_list.get_id(j.parent) pw = img_get_txt_width(parent.content, @font, @font_size) pleft = parent.indent pright = pleft + pw left = j.indent right = left + tw if pw > tw left = pleft right = pright end draw_element(left, curlevel, right - left, j.content, j.type) @e_list.set_indent(j.id, left + (right - left) / 2 - tw / 2) end unless children.empty? k = @e_list.get_id(children[0]) words = k.content.split(" ") dw = img_get_txt_width(k.content, @font, @font_size) unless @leafstyle == "nothing" && ETYPE_LEAF == k.type if (@leafstyle == "triangle" && ETYPE_LEAF == k.type && words.length > 1) txt_width = img_get_txt_width(k.content, @font, @font_size) triangle_to_parent(k.indent, curlevel + 1, dw, @e_list.get_element_width(k.parent), txt_width) else line_to_parent(k.indent, curlevel + 1, dw, j.indent, tw) end end end end end end end end |
#row2px(row) ⇒ Object
Calculate top position from row (level)
442 443 444 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 442 def row2px(row) B_TOPBOT + @e_height * row + (V_SPACE + @font_size) * row end |
#svg_data ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 84 def svg_data parse_list header =<<EOD <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="#{@width}" height="#{@height}" version="1.1" xmlns="http://www.w3.org/2000/svg"> EOD = "</svg>" # File.open(filename, "w") do |f| # f.write header # f.write @tree_data # f.write footer # end header + @tree_data + end |
#triangle_to_parent(fromX, fromY, fromW, toW, textW) ⇒ Object
Draw a triangle between child/parent elements
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/rsyntaxtree/svg_graph.rb', line 213 def triangle_to_parent(fromX, fromY, fromW, toW, textW) if (fromY == 0) return end toX = fromX fromTop = row2px(fromY) fromCenter = (fromX + fromW / 2 + B_SIDE) fromLeft1 = (fromCenter + textW / 2) fromLeft2 = (fromCenter - textW / 2) toBot = (row2px(fromY - 1) + @e_height) toLeft = (toX + toW / 2 + B_SIDE) polygon_data = @polygon_styles.sub(/X1/, fromLeft1.ceil.to_s) polygon_data = polygon_data.sub(/Y1/, fromTop.ceil.to_s) polygon_data = polygon_data.sub(/X2/, fromLeft2.ceil.to_s) polygon_data = polygon_data.sub(/Y2/, fromTop.ceil.to_s) polygon_data = polygon_data.sub(/X3/, toLeft.ceil.to_s) @tree_data += polygon_data.sub(/Y3/, toBot.ceil.to_s) end |