-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibo_draw.rb
137 lines (111 loc) · 2.74 KB
/
libo_draw.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
require "rexml/document"
module LiboDraw
class Document
def initialize(path)
xml = File.read(path)
@doc = REXML::Document.new(xml)
end
def pages
REXML::XPath.match(@doc, "//draw:page")
.map { |page_el| Page.new(page_el) }
end
end
class Page
def initialize(el)
@el = el
end
def rectangles
custom_shape_els = REXML::XPath.match(@el, "draw:custom-shape")
custom_shape_els
.select { |el|
geo_el = REXML::XPath.match(el, "draw:enhanced-geometry")[0]
geo_el["draw:type"] == "rectangle"
}
.map { |el| Rectangle.new(el) }
end
def lines
REXML::XPath.match(@el, "draw:line")
.map { |line_el| Line.new(line_el) }
end
def name
@el["draw:name"]
end
end
class Rectangle
def initialize(el)
@el = el
end
def paragraphs
para_els = REXML::XPath.match(@el, "text:p")
para_els.map { |para_el|
para_el.children
.map { |child_el|
case child_el
when REXML::Text
child_el.value
when REXML::Element
if child_el.name == "line-break"
"\n"
elsif child_el.name == "span"
child_el.text
else
pp child_el.name, child_el
raise "unknown element"
end
else
raise "unknown element"
end
}
.join("")
}
end
def text
paragraphs.join("\n")
end
def inspect
values = [
self.class.name,
"x=" + @el["svg:x"],
"y=" + @el["svg:y"],
"w=" + @el["svg:width"],
"h=" + @el["svg:height"],
"text=" + text.inspect,
]
"(" + values.join(" ") + ")"
end
def x; @el["svg:x"].sub(/cm$/, "").to_f; end
def y; @el["svg:y"].sub(/cm$/, "").to_f; end
def w; @el["svg:width"].sub(/cm$/, "").to_f; end
def h; @el["svg:height"].sub(/cm$/, "").to_f; end
end
class Line
def initialize(el)
@el = el
end
def inspect
values = [
self.class.name,
"x1=" + @el["svg:x1"],
"y1=" + @el["svg:y1"],
"x2=" + @el["svg:x2"],
"y2=" + @el["svg:y2"],
]
"(" + values.join(" ") + ")"
end
def x1; @el["svg:x1"].sub(/cm$/, "").to_f; end
def y1; @el["svg:y1"].sub(/cm$/, "").to_f; end
def x2; @el["svg:x2"].sub(/cm$/, "").to_f; end
def y2; @el["svg:y2"].sub(/cm$/, "").to_f; end
def tate?
x1.floor == x2.floor
end
end
end
# --------------------------------
if $0 == __FILE__
require "pp"
path = ARGV[0]
doc = LiboDraw::Document.new(path)
pp doc.pages[0].rectangles
pp doc.pages[0].lines
end