File: rbpdf_cell_test.rb

package info (click to toggle)
ruby-rbpdf 1.19.5%2Bds.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,312 kB
  • sloc: ruby: 136,020; makefile: 10
file content (346 lines) | stat: -rw-r--r-- 9,407 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# Copyright (c) 2011-2017 NAITOH Jun
# Released under the MIT license
# http://www.opensource.org/licenses/MIT

require 'test_helper'

class RbpdfTest < Test::Unit::TestCase
  test "getCellCode basic test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    code = pdf.send(:getCellCode, 10)
    assert_equal "0.57 w 0 J 0 j [] 0 d 0 G 0 g\n", code
    # 0.57 w 0 J 0 j [] 0 d 0 G 0 rg       # getCellCode
  end

  test "getCellCode text test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.send(:getCellCode, 10, 10, 'abc')
    contents.each_line {|line| content.push line.chomp }

    assert_equal 2, content.length
    assert_equal "0.57 w 0 J 0 j [] 0 d 0 G 0 g", content[0]
    assert_match(/BT 31.1[89] 795.17 Td 0 Tr 0.00 w \[\(abc\)\] TJ ET/, content[1])
    # BT
    #    31.19 795.17 Td
    #    0 Tr 0.00 w
    #    [(abc)] TJ
    # ET
  end

  test "getCellCode back slash text test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.send(:getCellCode, 10, 10, "a\\bc") # use escape() method
    contents.each_line {|line| content.push line.chomp }

    assert_equal 2, content.length
    assert_equal "0.57 w 0 J 0 j [] 0 d 0 G 0 g", content[0]
    assert_match(/BT 31.1[89] 795.17 Td 0 Tr 0.00 w \[\(a\\\\bc\)\] TJ ET/, content[1])
    # BT
    #    31.19 795.17 Td
    #    0 Tr 0.00 w
    #    [(a\\bc)] TJ
    # ET
  end

  test "getCellCode text align test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.send(:getCellCode, 10, 10, 'abc', 'LTRB')
    contents.each_line {|line| content.push line.chomp }

    assert_equal 2, content.length
    assert_equal "0.57 w 0 J 0 j [] 0 d 0 G 0 g", content[0]
    assert_match(/28.35 813.8[23] m 28.35 784.91 l S 28.0[67] 813.54 m 56.98 813.54 l S 56.70 813.8[32] m 56.70 784.91 l S 28.0[67] 785.19 m 56.98 785.19 l S BT 31.1[89] 795.17 Td 0 Tr 0.00 w \[\(abc\)\] TJ ET/, content[1])
    # 28.35 813.82 m 28.35 784.91 l S
    # 28.07 813.54 m 56.98 813.54 l S
    # 56.70 813.82 m 56.70 784.91 l S
    # 28.07 785.19 m 56.98 785.19 l S
    # BT
    #   31.19 795.17 Td
    #   0 Tr 0.00 w 
    #   [(abc)] TJ
    # ET
  end

  test "getCellCode link url test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()

    # Check Initialize Values
    page_annots = pdf.instance_variable_get('@page_annots')
    assert_equal 0, page_annots.length
    annots = pdf.send(:getannotsrefs, 1)
    assert_equal '', annots

    content = []
    contents = pdf.send(:getCellCode, 10, 10, 'abc', '', 0, '', 0, 'http://example.com')
    contents.each_line {|line| content.push line.chomp }

    assert_equal 2, content.length
    assert_match(/BT 31.1[89] 795.17 Td 0 Tr 0.00 w \[\(abc\)\] TJ ET/, content[1])
    # BT
    #    31.19 795.17 Td
    #    0 Tr 0.00 w
    #    [(abc)] TJ
    # ET

    # Check Annots
    page_annots = pdf.instance_variable_get('@page_annots')
    assert_equal 2, page_annots.length
    assert_equal nil, page_annots[0]
    assert_equal 1, page_annots[1].length
    assert_equal 0,                    page_annots[1][0]['numspaces']
    assert_equal({"Subtype"=>"Link"},  page_annots[1][0]['opt'])
    assert_equal 'http://example.com', page_annots[1][0]['txt']

    annots = pdf.send(:getannotsrefs, 1)
    assert_equal " /Annots [ 200001 0 R ]", annots
  end

  test "getCellCode link page test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.send(:getCellCode, 10, 10, 'abc', 0, 0, '', 0, 1)
    contents.each_line {|line| content.push line.chomp }

    assert_equal 2, content.length
    assert_match(/BT 31.1[89] 795.17 Td 0 Tr 0.00 w \[\(abc\)\] TJ ET/, content[1])
    # BT
    #    31.19 795.17 Td
    #    0 Tr 0.00 w
    #    [(abc)] TJ
    # ET

    # Check Annots
    page_annots = pdf.instance_variable_get('@page_annots')
    assert_equal 2, page_annots.length
    assert_equal nil, page_annots[0]
    assert_equal 1, page_annots[1].length
    assert_equal 0,                    page_annots[1][0]['numspaces']
    assert_equal({"Subtype"=>"Link"},  page_annots[1][0]['opt'])
    assert_equal 1,                    page_annots[1][0]['txt']

    annots = pdf.send(:getannotsrefs, 1)
    assert_equal " /Annots [ 200001 0 R ]", annots
  end

  test "getStringHeight Basic test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page

    txt = 'abcdefg'

    w = 50
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 1, line

    w = 20
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 1, line
  end

  test "getStringHeight Line Break test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page

    txt = 'abcdefg'

    w = 10
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 3, line


    w = 5
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 7, line
  end

  test "getStringHeight Multi Line test" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page

    txt = "abc\ndif\nhij"

    w = 100
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 3, line
  end

  test "getStringHeight Minimum Width test 1" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page

    w = pdf.get_string_width('OO')

    txt = "Export to PDF: align is Good."

    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 16, line
  end

 test "getStringHeight Minimum Width test 2" do
    pdf = RBPDF.new('L', 'mm', 'A4', true, "UTF-8", true)
    pdf.set_font('kozminproregular', '', 8)
    pdf.add_page

    margins = pdf.get_margins
    w = pdf.get_string_width('20') + margins['cell'] * 2

    txt = "20"

    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 2, line
  end

  test "getStringHeight Minimum Bidi test 1" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page

    w = pdf.get_string_width('OO')

    txt  = "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa"
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1
    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 5, line

    txt = "? \xd7\x93\xd7\x92 \xd7\xa1\xd7\xa7\xd7\xa8\xd7\x9f \xd7\xa9\xd7\x98 \xd7\x91\xd7\x99\xd7\x9d \xd7\x9e\xd7\x90\xd7\x95\xd7\x9b\xd7\x96\xd7\x91 \xd7\x95\xd7\x9c\xd7\xa4\xd7\xaa\xd7\xa2 \xd7\x9e\xd7\xa6\xd7\x90 \xd7\x9c\xd7\x95 \xd7\x97\xd7\x91\xd7\xa8\xd7\x94 \xd7\x90\xd7\x99\xd7\x9a \xd7\x94\xd7\xa7\xd7\x9c\xd7\x99\xd7\x98\xd7\x94"

    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 41, line
  end

  test "getStringHeight Minimum Bidi test 2" do
    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.set_font('freesans', '')
    pdf.set_rtl(true)
    pdf.set_temp_rtl('R')
    pdf.add_page

    margins = pdf.get_margins
    w = pdf.get_string_width('OO') + margins['cell'] * 2

    txt =  "\xd7\x9c 000"

    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal 1, pno
    y2 = pdf.get_y
    h1 = y2 - y1

    h2 = pdf.getStringHeight(w, txt)
    assert_in_delta h1, h2, 0.01

    line = pdf.get_num_lines(txt, w)
    assert_equal 3, line
  end

  test "removeSHY encoding test" do
    return unless 'test'.respond_to?(:force_encoding)

    pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)

    str = 'test'.force_encoding('UTF-8')
    txt = pdf.removeSHY(str)
    assert_equal 'UTF-8', str.encoding.to_s

    str = 'test'.force_encoding('ASCII-8BIT')
    txt = pdf.removeSHY(str)
    assert_equal 'ASCII-8BIT', str.encoding.to_s
  end
end