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_label_body(body) ⇒ Object
- .split_pairs(str) ⇒ Object
Class Method Details
.extract_label_boxes(geometry_string) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 57 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
49 50 51 52 53 54 55 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 49 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 |
# 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"] ) end |
.parse_label_body(body) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 67 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
39 40 41 42 43 44 45 46 47 |
# File 'lib/ea/sources/xmi/extension_geometry_parser.rb', line 39 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 |