File: text_run_spec.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (310 lines) | stat: -rw-r--r-- 9,340 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# typed: false
# coding: utf-8

describe PDF::Reader::TextRun do
  describe "#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 "makes x accessible" do
        expect(subject.x).to eq(10)
      end

      it "makes y accessible" do
        expect(subject.y).to eq(20)
      end

      it "makes width accessible" do
        expect(subject.width).to eq(30)
      end

      it "makes font_size accessible" do
        expect(subject.font_size).to eq(12)
      end

      it "makes text accessible" do
        expect(subject.text).to eq("Chunky")
      end
    end
  end

  describe "#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 "equals x + width" do
        expect(subject.endx).to eq(40)
      end
    end
  end

  describe "#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 "returns true" do
          expect(one.mergable?(two)).to be_truthy
        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 "returns false" do
          expect(one.mergable?(two)).to be_falsey
        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 "returns false" do
          expect(one.mergable?(two)).to be_falsey
        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 "returns true" do
          expect(one.mergable?(two)).to be_falsey
        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 "returns false" do
          expect(one.mergable?(two)).to be_falsey
        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 "returns false" do
          expect(one.mergable?(two)).to be_falsey
        end
      end
    end
  end

  describe "#+" 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 "returns a new TextRun with combined data" do
        result = one + two
        expect(result.x).to     eq(10)
        expect(result.y).to     eq(20)
        expect(result.width).to eq(61.2)
        expect(result.text).to  eq("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 "returns a new TextRun with combined data" do
        result = one + two
        expect(result.x).to     eq(10)
        expect(result.y).to     eq(20)
        expect(result.width).to eq(72)
        expect(result.text).to  eq("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 "raises an exception" do
        expect {
          one + two
        }.to raise_error(ArgumentError)
      end
    end
  end

  describe "#<=>" 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 "returns 0" do
        expect(one <=> two).to eq(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 "sorts two before one" do
        expect([one, two].sort).to eq([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 "sorts one before two" do
        expect([one, two].sort).to eq([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 "sorts one before two" do
        expect([one, two].sort).to eq([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 "sorts two before one" do
        expect([one, two].sort).to eq([two, one])
      end
    end

  end

  describe "#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 "returns 5.0" do
        expect(subject.mean_character_width).to eq(5.0)
      end
    end
  end

  describe "#intersect" do
    let(:result) {
      run_one.intersect?(run_two)
    }

    context "with two runs that don't intersect" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 700, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(100, 500, 10, 12, "H") }

      it "returns false" do
        expect(result).to eq(false)
      end
    end

    context "when run_two overlaps the top of run_one" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 100, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(30, 110, 10, 12, "H") }

      it "returns true" do
        expect(result).to eq(true)
      end
    end

    context "when run_two overlaps the bottom of run_one" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 100, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(30, 92, 10, 12, "H") }

      it "returns true" do
        expect(result).to eq(true)
      end
    end

    context "when run_two overlaps the left of run_one" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 100, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(25, 100, 10, 12, "H") }

      it "returns true" do
        expect(result).to eq(true)
      end
    end

    context "when run_two overlaps the right of run_one" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 100, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(35, 100, 10, 12, "H") }

      it "returns true" do
        expect(result).to eq(true)
      end
    end

    context "with two identical runs" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 700, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(30, 700, 10, 12, "H") }

      it "returns true" do
        expect(result).to eq(true)
      end
    end
  end

  describe "#intersection_area_percent" do
    let(:result) {
      run_one.intersection_area_percent(run_two)
    }

    context "with two runs that don't intersect" do
      let(:run_one) { PDF::Reader::TextRun.new(30, 700, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(100, 500, 10, 12, "H") }

      it "returns 0" do
        expect(result).to eq(0)
      end
    end

    context "when run_two overalps with 50% of run_one" do
      let(:run_one) { PDF::Reader::TextRun.new(100, 100, 10, 12, "H") }
      let(:run_two) { PDF::Reader::TextRun.new(105, 100, 10, 12, "H") }

      it "returns 0.5" do
        expect(result).to be_within(0.01).of(0.5)
      end
    end
  end
end