/
text_run_spec.rb
225 lines (180 loc) · 6.49 KB
/
text_run_spec.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# coding: utf-8
require File.dirname(__FILE__) + "/spec_helper"
describe PDF::Reader::TextRun, "#initilize" do
context "when initialized with valid values" do
let(:x) { 10 }
let(:y) { 20 }
let(:width) { 30 }
let(:font) { 12 }
let(:text) { "Chunky" }
subject { PDF::Reader::TextRun.new(x, y, width, font, text)}
it "should make x accessible" do
subject.x.should == 10
end
it "should make y accessible" do
subject.y.should == 20
end
it "should make width accessible" do
subject.width.should == 30
end
it "should make font_size accessible" do
subject.font_size.should == 12
end
it "should make text accessible" do
subject.text.should == "Chunky"
end
end
end
describe PDF::Reader::TextRun, "#endx" do
context "when initialized with valid values" do
let(:x) { 10 }
let(:y) { 20 }
let(:width) { 30 }
let(:font) { 12 }
let(:text) { "Chunky" }
subject { PDF::Reader::TextRun.new(x, y, width, font, text)}
it "should equal x + width" do
subject.endx.should == 40
end
end
end
describe PDF::Reader::TextRun, "#mergable?" do
let(:x) { 10 }
let(:y) { 20 }
let(:width) { 30 }
let(:font) { 12 }
context "the font_sizes match" do
context "when the two runs are within 1x font_size of each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+12, y, width, font, "B")}
it "should return true" do
one.mergable?(two).should be_true
end
end
context "when the two runs are over 1x font_size away from each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+13, y, width, font, "B")}
it "should return false" do
one.mergable?(two).should be_false
end
end
context "when the two runs have identical X values but different Y" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(x, y + 1, width, font, "B")}
it "should return false" do
one.mergable?(two).should be_false
end
end
end
context "the font_sizes do not match" do
context "when the two runs are within 1x font_size of each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+12, y, width, font+1, "B")}
it "should return true" do
one.mergable?(two).should be_false
end
end
context "when the two runs are over 1x font_size away from each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+13, y, width, font+1, "B")}
it "should return false" do
one.mergable?(two).should be_false
end
end
context "when the two runs have identical X values but different Y" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(x, y + 1, width, font+1, "B")}
it "should return false" do
one.mergable?(two).should be_false
end
end
end
end
describe PDF::Reader::TextRun, "#+" do
let(:x) { 10 }
let(:y) { 20 }
let(:width) { 30 }
let(:font) { 12 }
context "when the two runs are 0.12x font_size of each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+1.2, y, width, font, "B")}
it "should return a new TextRun with combined data" do
result = one + two
result.x.should == 10
result.y.should == 20
result.width.should == 61.2
result.text.should == "AB"
end
end
context "when the two runs are 1x font_size of each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+12, y, width, font, "B")}
it "should return a new TextRun with combined data" do
result = one + two
result.x.should == 10
result.y.should == 20
result.width.should == 72
result.text.should == "A B"
end
end
context "when the two runs are over 12pts away from each other" do
let(:one) { PDF::Reader::TextRun.new(x, y, width, font, "A")}
let(:two) { PDF::Reader::TextRun.new(one.endx+13, y, width, font, "B")}
it "should raise an exception" do
lambda {
one + two
}.should raise_error(ArgumentError)
end
end
end
describe PDF::Reader::TextRun, "#<=>" do
let(:width) { 30 }
let(:font) { 12 }
let(:text) { "Chunky" }
context "when comparing two runs in the same position" do
let!(:one) { PDF::Reader::TextRun.new(10, 20, width, font, text)}
let!(:two) { PDF::Reader::TextRun.new(10, 20, width, font, text)}
it "should return 0" do
(one <=> two).should == 0
end
end
context "when run two is directly above run one" do
let!(:one) { PDF::Reader::TextRun.new(10, 10, width, font, text)}
let!(:two) { PDF::Reader::TextRun.new(10, 20, width, font, text)}
it "should sort two before one" do
[one, two].sort.should == [two, one]
end
end
context "when run two is directly right of run one" do
let!(:one) { PDF::Reader::TextRun.new(10, 10, width, font, text)}
let!(:two) { PDF::Reader::TextRun.new(20, 10, width, font, text)}
it "should sort one before two" do
[one, two].sort.should == [one, two]
end
end
context "when run two is directly below run one" do
let!(:one) { PDF::Reader::TextRun.new(10, 10, width, font, text)}
let!(:two) { PDF::Reader::TextRun.new(10, 05, width, font, text)}
it "should sort one before two" do
[one, two].sort.should == [one, two]
end
end
context "when run two is directly left of run one" do
let!(:one) { PDF::Reader::TextRun.new(10, 10, width, font, text)}
let!(:two) { PDF::Reader::TextRun.new(5, 10, width, font, text)}
it "should sort two before one" do
[one, two].sort.should == [two, one]
end
end
end
describe PDF::Reader::TextRun, "#mean_character_width" do
let(:width) { 30 }
let(:font) { 12 }
let(:text) { "Chunky" }
context "when the run is 30pts wide with 6 characters" do
subject { PDF::Reader::TextRun.new(10, 20, width, font, text)}
it "should return 5.0" do
subject.mean_character_width.should == 5.0
end
end
end