File: rbpdf_cell_test.rb

package info (click to toggle)
ruby-rbpdf 1.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,252 kB
  • ctags: 704
  • sloc: ruby: 135,876; makefile: 10
file content (266 lines) | stat: -rw-r--r-- 6,671 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
require 'test_helper'

class RbpdfTest < Test::Unit::TestCase
  class MYPDF < RBPDF
    def getCellCode(w, h=0, txt='', border=0, ln=0, align='', fill=0, link=nil, stretch=0, ignore_min_height=false, calign='T', valign='M')
      super
    end
  end

  test "getCellCode" do
    pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    code = pdf.getCellCode(10)
    assert_equal code, "0.57 w 0 J 0 j [] 0 d 0 G 0 g\n"
    # 0.57 w 0 J 0 j [] 0 d 0 G 0 rg       # getCellCode
  end

  test "getCellCode link url align test" do
    pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.getCellCode(10, 10, 'abc', 'LTRB', 0, '', 0, 'http://example.com')
    contents.each_line {|line| content.push line.chomp }

    assert_equal content.length,  2
    assert_equal content[1], "28.35 813.83 m 28.35 784.91 l S 28.07 813.54 m 56.98 813.54 l S 56.70 813.83 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"
    # 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 page test" do
    pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
    pdf.add_page()
    content = []
    contents = pdf.getCellCode(10, 10, 'abc', 0, 0, '', 0, 1)
    contents.each_line {|line| content.push line.chomp }

    assert_equal content.length,  2
    assert_equal content[1], "BT 31.19 795.17 Td 0 Tr 0.00 w [(abc)] TJ ET"
    # BT
    #    31.19 795.17 Td
    #    0 Tr 0.00 w
    #    [(abc)] TJ
    # ET
  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 pno, 1
    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 line, 1

    w = 20
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal pno, 1
    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 line, 1
  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 pno, 1
    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 line, 3


    w = 5
    y1 = pdf.get_y
    pdf.multi_cell(w, 0, txt)
    pno = pdf.get_page
    assert_equal pno, 1
    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 line, 7
  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 pno, 1
    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 line, 3
  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 pno, 1
    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 line, 16
  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 pno, 1
    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 line, 2
  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 pno, 1
    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 line, 5

    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 pno, 1
    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 line, 41
  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 pno, 1
    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 line, 3
  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 str.encoding.to_s, 'UTF-8'

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