Module: Ea::Sources::Xmi::ExtensionGeometryParser
- Defined in:
- lib/ea/sources/xmi/extension_geometry_parser.rb
Overview
Parses EA's packed geometry string from
xmi:Extension/
Defined Under Namespace
Constant Summary collapse
- LABEL_BOX_KEYS =
%i[llb llt lrt lrb lmt lmb irhs ilhs].freeze
Class Method Summary collapse
- .extract_label_boxes(geometry_string) ⇒ Object
- .int(value) ⇒ Object
- .parse(geometry_string) ⇒ Object
-
.parse_bend_path(raw) ⇒ Object
EA's Path= field uses the format
x1:y1$x2:y2$...where each pair is a bend point. - .parse_label_body(body) ⇒ Object
- .split_pairs(str) ⇒ Object
Class Method Details
.extract_label_boxes(geometry_string) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 74 def extract_label_boxes(geometry_string) geometry_string.to_s.scan(/\$?([A-Z]{3})=((?:[A-Za-z]{1,4}=-?\d+:)+)/).each_with_object({}) do |(key, body), acc| sym = key.downcase.to_sym next unless LABEL_BOX_KEYS.include?(sym) next if body.nil? || body.empty? acc[sym] = parse_label_body(body) end end |
.int(value) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 66 def int(value) return nil if value.nil? || value.empty? Integer(value) rescue ArgumentError nil end |
.parse(geometry_string) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 16 def parse(geometry_string) return Placement.new if geometry_string.nil? || geometry_string.empty? kv = split_pairs(geometry_string) Placement.new( left: int(kv["Left"]), top: int(kv["Top"]), right: int(kv["Right"]), bottom: int(kv["Bottom"]), img_left: int(kv["imgL"]), img_top: int(kv["imgT"]), img_right: int(kv["imgR"]), img_bottom: int(kv["imgB"]), sx: int(kv["SX"]), sy: int(kv["SY"]), ex: int(kv["EX"]), ey: int(kv["EY"]), edge: int(kv["EDGE"]), label_boxes: extract_label_boxes(geometry_string), path: kv["Path"], bend_points: parse_bend_path(kv["Path"]) ) end |
.parse_bend_path(raw) ⇒ Object
EA's Path= field uses the format x1:y1$x2:y2$... where
each pair is a bend point. Coordinates are absolute pixel
positions on the diagram canvas.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 43 def parse_bend_path(raw) return [] if raw.nil? || raw.empty? raw.split("$").filter_map do |pair| next nil unless pair.include?(":") x_str, y_str = pair.split(":", 2) x = int(x_str) y = int(y_str) [x, y] if x && y end end |
.parse_label_body(body) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 84 def parse_label_body(body) pairs = body.to_s.split(":").filter_map do |pair| next nil unless pair.include?("=") k, v = pair.split("=", 2) [k, int(v)] if k && v end hash = pairs.to_h LabelBox.new( cx: hash["CX"], cy: hash["CY"], ox: hash["OX"], oy: hash["OY"], bold: hash["BLD"] == 1, italic: hash["ITA"] == 1, underline: hash["UND"] == 1, hidden: hash["HDN"] == 1, align: hash["ALN"], direction: hash["DIR"], rotation: hash["ROT"] ) end |
.split_pairs(str) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 56 def split_pairs(str) str.split(";").filter_map do |pair| next nil unless pair.include?("=") next nil if pair.start_with?("$") # $LLB= etc handled separately key, value = pair.split("=", 2) [key, value] if key && value end.to_h end |