File: page_layout_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 (229 lines) | stat: -rw-r--r-- 7,526 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
# typed: false
# coding: utf-8

describe PDF::Reader::PageLayout do
  describe "#to_s" do
    context "with an A4 page" do
      let(:mediabox) { PDF::Reader::Rectangle.new(0, 0, 595.28, 841.89)}

      context "with no words" do
        subject { PDF::Reader::PageLayout.new([], mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("")
        end
      end

      context "with only blank word(s)" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "")
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("")
        end
      end

      context "with one word" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello")
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello")
        end
      end
      context "with one run directly below another" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(30, 687, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello\nWorld")
        end
      end
      context "with one two words on one line, separated by a font size gap" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(92, 700, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello World")
        end
      end

      context "with two words on one line, separated just over the mean glyph width" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(91, 700, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello World")
        end
      end

      context "with one two words on one line, separated just over 2x the mean glyph width" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(101, 700, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello  World")
        end
      end

      context "with one run directly below another and indented by just over 1 font size gap" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(43, 687, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello\n World")
        end
      end

      context "with one run directly below another and the first indented by just over 1x fs gap" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(43, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(30, 687, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq(" Hello\nWorld")
        end
      end

      context "with one run directly below another with 1 font size gap" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello"),
            PDF::Reader::TextRun.new(30, 676, 50, 12, "World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello\n\nWorld")
        end
      end

      context "with one run that has an implausible font size of 0" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 0, "Hello"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello")
        end
      end

      context "with one run that's positioned at 0,0" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(0, 0, 50, 18, "Hello World"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("Hello World")
        end
      end

      context "with two runs that overlap to make fake 'bold', using the same Y offset" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(420.74, 636.0, 6.67, 12, "b"),
            PDF::Reader::TextRun.new(427.41, 636.0, 6.67, 12, "o"),
            PDF::Reader::TextRun.new(434.08, 636.0, 2.66, 12, "l"),
            PDF::Reader::TextRun.new(436.75, 636.0, 6.67, 12, "d"),
            PDF::Reader::TextRun.new(420.84, 636.0, 6.67, 12, "b"),
            PDF::Reader::TextRun.new(427.51, 636.0, 6.67, 12, "o"),
            PDF::Reader::TextRun.new(434.18, 636.0, 2.66, 12, "l"),
            PDF::Reader::TextRun.new(436.85, 636.0, 6.67, 12, "d"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("bold")
        end
      end

      context "with two runs that overlap to make fake 'bold', using different X+Y offset" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(420.74, 635.95, 6.67, 12, "b"),
            PDF::Reader::TextRun.new(427.41, 635.95, 6.67, 12, "o"),
            PDF::Reader::TextRun.new(434.08, 635.95, 2.66, 12, "l"),
            PDF::Reader::TextRun.new(436.75, 635.95, 6.67, 12, "d"),
            PDF::Reader::TextRun.new(420.84, 636.05, 6.67, 12, "b"),
            PDF::Reader::TextRun.new(427.51, 636.05, 6.67, 12, "o"),
            PDF::Reader::TextRun.new(434.18, 636.05, 2.66, 12, "l"),
            PDF::Reader::TextRun.new(436.85, 636.05, 6.67, 12, "d"),
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox)}

        it "returns a correct string" do
          expect(subject.to_s).to eq("bold")
        end
      end
      context "with one word and the mediabox is provided as a 4-element array" do
        let!(:runs) do
          [
            PDF::Reader::TextRun.new(30, 700, 50, 12, "Hello")
          ]
        end
        subject { PDF::Reader::PageLayout.new(runs, mediabox.to_a)}

        it "returns a correct string and prints a deprecation warning" do
          expect(subject.to_s).to eq("Hello")
        end
      end
    end
    context "with a page that's too small to fit any of the text" do
      let(:mediabox) { PDF::Reader::Rectangle.new(0, 0, 46.560, 32.640)}
      let(:font_size) { 72 }

      it "returns an empty string" do
        run = PDF::Reader::TextRun.new(0, 0, 50, font_size, "a")
        layout = PDF::Reader::PageLayout.new([run], mediabox)
        expect(layout.to_s).to eq("")
      end
    end

  end
end