rbpdf 1.19.5 → 1.19.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2639ff84fd70b4fcf84386c2d2d4ec407cfe932e
4
- data.tar.gz: 59a2e7fe5a678bec81f112744dae30d004ae9a00
3
+ metadata.gz: eb07b943fb93bd28c583e7d1725cb594fd38e37e
4
+ data.tar.gz: bc4e241e6d61bf6ff850080c598fe468a0c524a7
5
5
  SHA512:
6
- metadata.gz: 10d521aaf60b79a63a03d4a7e04830ee2cacee71f5d0a6f3f1acafb03a0d0973c48ce8fb77eec9f86747b72f4c52e5d0ccfcc59f55edca70e25804e0fb65302c
7
- data.tar.gz: de13ae61679876a96365899664338a1a54134805eeec6e1d93cf2355c0b2b89c7266ad314a6b9066f306cc210712659914578cafab8f3907729f9d1c24c50692
6
+ metadata.gz: 01bf0690ab57b3248123090f7832826d60247d6361e276187a9efa98855a95733a869a48edd3d85f3b938f78c4ca424f7a830c6c203146830a350e2ee4bf8b9d
7
+ data.tar.gz: 65cc6adaff4ee5271c0f9d006e4dc600d93766c0d807fb90fd6120399ed73316258aee785bff8f963bee85a920705cb4414ef21fbe964785ea6543399e78995f
data/CHANGELOG CHANGED
@@ -1,3 +1,15 @@
1
+ 1.19.6 2018-10-06
2
+ - The problem of the line feed position of Ln() function has been fixed.
3
+ - Fixed a problem of line feed position occurring in the following <pre> tag of HTML in the following cases.
4
+ When a <br> tag is included immediately after the start of the <pre> tag.
5
+ When a <br> tag is included immediately before the end of the <pre> tag.
6
+ When a line feed is included immediately after the start of the tag.
7
+ When a line feed is included immediately before the end of the tag.
8
+ - Fixed a problem of line feed position occurring in the following </pre> and <p> tags of HTML in the following cases.
9
+ When a space is included between the </pre> tag and the <p> tag.
10
+ - Cell link bug fixed.
11
+ - Examples test added.
12
+
1
13
  1.19.5 2018-06-24
2
14
  - Fixed that the handling of Tempfile was wrong.
3
15
  - Fixed compatibility problem with Magick::RGBColorspace and Magick::SRGBColorspace.
data/Gemfile CHANGED
@@ -7,5 +7,4 @@ source 'https://rubygems.org'
7
7
  # Specify your gem's dependencies in rbpdf.gemspec
8
8
  gemspec
9
9
 
10
- gem "actionpack"
11
-
10
+ gem "actionpack", "~> 5.2.0"
@@ -2994,7 +2994,7 @@ class RBPDF
2994
2994
  #
2995
2995
  def AddLink()
2996
2996
  #Create a new internal link
2997
- n = @links.length
2997
+ n = @links.length + 1
2998
2998
  @links[n]=[0,0];
2999
2999
  return n;
3000
3000
  end
@@ -3757,7 +3757,7 @@ class RBPDF
3757
3757
  if (@color_flag)
3758
3758
  s<<' Q';
3759
3759
  end
3760
- if link && ((link.is_a?(String) and !link.empty?) or (link.is_a?(Integer) and link >= 0)) # Integer is @links array index
3760
+ if link && ((link.is_a?(String) and !link.empty?) or (link.is_a?(Integer) and link != 0)) # Integer is PDF file Page No.
3761
3761
  Link(xdx, y + ((h - @font_size) / 2.0), width, @font_size, link, ns)
3762
3762
  end
3763
3763
  end
@@ -3993,7 +3993,11 @@ class RBPDF
3993
3993
  end
3994
3994
  end
3995
3995
  SetX(nx)
3996
+
3997
+ prevLastH = @lasth
3996
3998
  ccode = getCellCode(w, h, '', cborder, 1, '', fill, '', 0, false)
3999
+ @lasth = prevLastH
4000
+
3997
4001
  if (cborder != 0) or (fill == 1)
3998
4002
  pagebuff = getPageBuffer(@page)
3999
4003
  pstart = pagebuff[0, @intmrk[@page]]
@@ -4007,8 +4011,12 @@ class RBPDF
4007
4011
  # put cursor at the beginning of text
4008
4012
  SetY(y)
4009
4013
  SetX(x)
4014
+
4015
+ prevLastH = @lasth
4010
4016
  # design a cell around the text
4011
4017
  ccode = getCellCode(w, h, '', border, 1, '', fill, '', 0, true)
4018
+ @lasth = prevLastH
4019
+
4012
4020
  if (border != 0) or (fill == 1)
4013
4021
  if !@transfmrk[@page].nil?
4014
4022
  pagemark = @transfmrk[@page]
@@ -11242,8 +11250,24 @@ protected
11242
11250
  html_b = html[offset, pos - offset + 6]
11243
11251
  while html_b =~ /<xre([^\>]*)>(.*?)\n(.*?)<\/pre>/mi
11244
11252
  # preserve newlines on <pre> tag
11245
- html_b = html_b.gsub(/<xre([^\>]*)>(.*?)\n(.*?)<\/pre>/mi, "<xre\\1>\\2<br />\\3</pre>")
11253
+ html_b = html_b.gsub(/<xre([^\>]*)>(.*?)\n(.*?)<\/pre>/mi) do
11254
+ if ($2 != '') and ($3 != '')
11255
+ "<xre#{$1}>#{$2}<br />#{$3}</pre>"
11256
+ elsif ($2 == '') and ($3 != '')
11257
+ "<xre#{$1}>#{$3}</pre>"
11258
+ elsif ($2 != '') and ($3 == '')
11259
+ "<xre#{$1}>#{$2}</pre>"
11260
+ else
11261
+ "<xre#{$1}></pre>"
11262
+ end
11263
+ end
11246
11264
  end
11265
+
11266
+ # Delete the <br />(<br>) tag located immediately after the <pre> tag.
11267
+ html_b = html_b.gsub(/<xre([^\>]*)>((?:<[^\>]+>)*)<br *\/*>/, "<xre\\1>\\2")
11268
+ # Delete the <br />(<br>) tag located immediately before the </pre> tag.
11269
+ html_b = html_b.gsub(/<br *\/*>((?:<\/[^\>]+>)*)<\/pre>/, "\\1</pre>")
11270
+
11247
11271
  while html_b =~ /<xre([^\>]*)>(.*?)[\s](.*?)<\/pre>/mi
11248
11272
  # preserve whitespace on <pre> tag
11249
11273
  html_b = html_b.gsub(/<xre([^\>]*)>(.*?)[\s](.*?)<\/pre>/mi, "<xre\\1>\\2&nbsp;\\3</pre>")
@@ -12794,7 +12818,7 @@ public
12794
12818
  startlinepage = @page
12795
12819
  end
12796
12820
  end
12797
- elsif dom[key]['value'].length > 0
12821
+ elsif dom[key]['value'].strip.length > 0
12798
12822
  # print list-item
12799
12823
  if !empty_string(@lispacer)
12800
12824
  SetFont(pfontname, pfontstyle, pfontsize)
@@ -13596,8 +13620,12 @@ public
13596
13620
  @x += @pagedim[page]['olm'] - @pagedim[startpage]['olm']
13597
13621
  end
13598
13622
  end
13623
+
13624
+ prevLastH = @lasth
13599
13625
  # design a cell around the text
13600
13626
  ccode = @fill_color + "\n" + getCellCode(cw, ch, '', cborder, 1, '', fill, '', 0, true)
13627
+ @lasth = prevLastH
13628
+
13601
13629
  if (cborder != 0) or (fill == 1)
13602
13630
  pagebuff = getPageBuffer(@page)
13603
13631
  pstart = pagebuff[0, @intmrk[@page]]
@@ -13618,8 +13646,12 @@ public
13618
13646
  @y = parent['starty']
13619
13647
  cw = (cellpos['endx'] - cellpos['startx']).abs
13620
13648
  ch = endy - parent['starty']
13649
+
13650
+ prevLastH = @lasth
13621
13651
  # design a cell around the text
13622
13652
  ccode = @fill_color + "\n" + getCellCode(cw, ch, '', border, 1, '', fill, '', 0, true)
13653
+ @lasth = prevLastH
13654
+
13623
13655
  if (border != 0) or (fill == 1)
13624
13656
  if !@transfmrk[@page].nil?
13625
13657
  pagemark = @transfmrk[@page]
@@ -1,7 +1,7 @@
1
- # Copyright (c) 2011-2017 NAITOH Jun
1
+ # Copyright (c) 2011-2018 NAITOH Jun
2
2
  # Released under the MIT license
3
3
  # http://www.opensource.org/licenses/MIT
4
4
 
5
5
  module Rbpdf
6
- VERSION = "1.19.5"
6
+ VERSION = "1.19.6"
7
7
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2011-2017 NAITOH Jun
1
+ # Copyright (c) 2011-2018 NAITOH Jun
2
2
  # Released under the MIT license
3
3
  # http://www.opensource.org/licenses/MIT
4
4
 
@@ -18,6 +18,9 @@ class RbpdfTest < Test::Unit::TestCase
18
18
  pdf.add_page()
19
19
  content = []
20
20
  contents = pdf.send(:getCellCode, 10, 10, 'abc')
21
+ #pdf.send(:getCellCode, 10, 10, 'abc')
22
+ #contents = pdf.output()
23
+
21
24
  contents.each_line {|line| content.push line.chomp }
22
25
 
23
26
  assert_equal 2, content.length
@@ -131,203 +134,85 @@ class RbpdfTest < Test::Unit::TestCase
131
134
  assert_equal " /Annots [ 200001 0 R ]", annots
132
135
  end
133
136
 
134
- test "getStringHeight Basic test" do
135
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
136
- pdf.add_page
137
-
138
- txt = 'abcdefg'
139
-
140
- w = 50
141
- y1 = pdf.get_y
142
- pdf.multi_cell(w, 0, txt)
143
- pno = pdf.get_page
144
- assert_equal 1, pno
145
- y2 = pdf.get_y
146
- h1 = y2 - y1
147
-
148
- h2 = pdf.getStringHeight(w, txt)
149
- assert_in_delta h1, h2, 0.01
150
-
151
- line = pdf.get_num_lines(txt, w)
152
- assert_equal 1, line
153
-
154
- w = 20
155
- y1 = pdf.get_y
156
- pdf.multi_cell(w, 0, txt)
157
- pno = pdf.get_page
158
- assert_equal 1, pno
159
- y2 = pdf.get_y
160
- h1 = y2 - y1
161
-
162
- h2 = pdf.getStringHeight(w, txt)
163
- assert_in_delta h1, h2, 0.01
164
-
165
- line = pdf.get_num_lines(txt, w)
166
- assert_equal 1, line
167
- end
168
-
169
- test "getStringHeight Line Break test" do
170
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
171
- pdf.add_page
172
-
173
- txt = 'abcdefg'
174
-
175
- w = 10
176
- y1 = pdf.get_y
177
- pdf.multi_cell(w, 0, txt)
178
- pno = pdf.get_page
179
- assert_equal 1, pno
180
- y2 = pdf.get_y
181
- h1 = y2 - y1
182
-
183
- h2 = pdf.getStringHeight(w, txt)
184
- assert_in_delta h1, h2, 0.01
185
-
186
- line = pdf.get_num_lines(txt, w)
187
- assert_equal 3, line
188
-
189
-
190
- w = 5
191
- y1 = pdf.get_y
192
- pdf.multi_cell(w, 0, txt)
193
- pno = pdf.get_page
194
- assert_equal 1, pno
195
- y2 = pdf.get_y
196
- h1 = y2 - y1
197
-
198
- h2 = pdf.getStringHeight(w, txt)
199
- assert_in_delta h1, h2, 0.01
200
-
201
- line = pdf.get_num_lines(txt, w)
202
- assert_equal 7, line
203
- end
204
-
205
- test "getStringHeight Multi Line test" do
206
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
207
- pdf.add_page
208
-
209
- txt = "abc\ndif\nhij"
210
-
211
- w = 100
212
- y1 = pdf.get_y
213
- pdf.multi_cell(w, 0, txt)
214
- pno = pdf.get_page
215
- assert_equal 1, pno
216
- y2 = pdf.get_y
217
- h1 = y2 - y1
218
-
219
- h2 = pdf.getStringHeight(w, txt)
220
- assert_in_delta h1, h2, 0.01
221
-
222
- line = pdf.get_num_lines(txt, w)
223
- assert_equal 3, line
224
- end
225
-
226
- test "getStringHeight Minimum Width test 1" do
227
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
228
- pdf.add_page
229
-
230
- w = pdf.get_string_width('OO')
137
+ test "Cell link with Close and output PDF document test" do
138
+ pdf = RBPDF.new
231
139
 
232
- txt = "Export to PDF: align is Good."
233
-
234
- y1 = pdf.get_y
235
- pdf.multi_cell(w, 0, txt)
236
- pno = pdf.get_page
237
- assert_equal 1, pno
238
- y2 = pdf.get_y
239
- h1 = y2 - y1
140
+ # add a page
141
+ pdf.add_page()
240
142
 
241
- h2 = pdf.getStringHeight(w, txt)
242
- assert_in_delta h1, h2, 0.01
143
+ text="DUMMY"
144
+ pdf.cell(0, 0, text, 1, 1, 'L', 1, 0)
243
145
 
244
- line = pdf.get_num_lines(txt, w)
245
- assert_equal 16, line
146
+ # Close and output PDF document
147
+ content = []
148
+ contents = pdf.output()
149
+ contents.each_line {|line| content.push line.chomp }
150
+ assert_match(/3 0 obj/, content[1])
246
151
  end
247
152
 
248
- test "getStringHeight Minimum Width test 2" do
249
- pdf = RBPDF.new('L', 'mm', 'A4', true, "UTF-8", true)
250
- pdf.set_font('kozminproregular', '', 8)
251
- pdf.add_page
252
-
253
- margins = pdf.get_margins
254
- w = pdf.get_string_width('20') + margins['cell'] * 2
255
-
256
- txt = "20"
257
-
258
- y1 = pdf.get_y
259
- pdf.multi_cell(w, 0, txt)
260
- pno = pdf.get_page
261
- assert_equal 1, pno
262
- y2 = pdf.get_y
263
- h1 = y2 - y1
153
+ texts = {
154
+ 'Basic' => {:params => [{:txt => "abcdefg", :w => 50, :pno => 1, :line => 1},
155
+ {:txt => "abcdefg", :w => 20, :pno => 1, :line => 1}]},
156
+ 'Line Break' => {:params => [{:txt => "abcdefg", :w => 10, :pno => 1, :line => 3},
157
+ {:txt => "abcdefg", :w => 5, :pno => 1, :line => 7}]},
158
+ 'Multi Line' => {:params => [{:txt => "abc\ndif\nhij", :w => 100, :pno => 1, :line => 3}]},
159
+ 'Minimum Width' => {:params => [{:txt => "Export to PDF: align is Good.", :w => 'OO', :pno => 1, :line => 16}]},
160
+ 'Minimum Width with font' => {:font => "kozminproregular", :orientation => 'L',
161
+ :params => [{:txt => "20", :w => '20', :pno => 1, :line => 2}]},
162
+ 'Minimum Bidi' => {:params => [{:txt => "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa", :w => 'OO', :pno => 1, :line => 5},
163
+ {: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", :w => 'OO', :pno => 1, :line => 41}]},
164
+ 'Minimum Bidi with font' => {:font => "freesans", :rtl => true,
165
+ :params => [{:txt => "\xd7\x9c 000", :w => 'OO', :pno => 1, :line => 3}]},
166
+ }
167
+
168
+ data(texts)
169
+ test "getStringHeight test" do |data|
170
+ orientation = data[:orientation] ? data[:orientation] : 'P'
171
+ pdf = RBPDF.new(orientation, 'mm', 'A4', true, "UTF-8", true)
172
+ pdf.set_font(data[:font], '', 8) if data[:font]
173
+
174
+ if data[:rtl]
175
+ pdf.set_rtl(true)
176
+ pdf.set_temp_rtl('R')
177
+ end
264
178
 
265
- h2 = pdf.getStringHeight(w, txt)
266
- assert_in_delta h1, h2, 0.01
267
-
268
- line = pdf.get_num_lines(txt, w)
269
- assert_equal 2, line
270
- end
271
-
272
- test "getStringHeight Minimum Bidi test 1" do
273
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
274
179
  pdf.add_page
275
180
 
276
- w = pdf.get_string_width('OO')
277
-
278
- txt = "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa"
279
- y1 = pdf.get_y
280
- pdf.multi_cell(w, 0, txt)
281
- pno = pdf.get_page
282
- assert_equal 1, pno
283
- y2 = pdf.get_y
284
- h1 = y2 - y1
285
- h2 = pdf.getStringHeight(w, txt)
286
- assert_in_delta h1, h2, 0.01
287
-
288
- line = pdf.get_num_lines(txt, w)
289
- assert_equal 5, line
290
-
291
- 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"
292
-
293
- y1 = pdf.get_y
294
- pdf.multi_cell(w, 0, txt)
295
- pno = pdf.get_page
296
- assert_equal 1, pno
297
- y2 = pdf.get_y
298
- h1 = y2 - y1
299
-
300
- h2 = pdf.getStringHeight(w, txt)
301
- assert_in_delta h1, h2, 0.01
302
-
303
- line = pdf.get_num_lines(txt, w)
304
- assert_equal 41, line
305
- end
306
-
307
- test "getStringHeight Minimum Bidi test 2" do
308
- pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
309
- pdf.set_font('freesans', '')
310
- pdf.set_rtl(true)
311
- pdf.set_temp_rtl('R')
312
- pdf.add_page
313
-
314
- margins = pdf.get_margins
315
- w = pdf.get_string_width('OO') + margins['cell'] * 2
316
-
317
- txt = "\xd7\x9c 000"
318
-
319
- y1 = pdf.get_y
320
- pdf.multi_cell(w, 0, txt)
321
- pno = pdf.get_page
322
- assert_equal 1, pno
323
- y2 = pdf.get_y
324
- h1 = y2 - y1
325
-
326
- h2 = pdf.getStringHeight(w, txt)
327
- assert_in_delta h1, h2, 0.01
328
-
329
- line = pdf.get_num_lines(txt, w)
330
- assert_equal 3, line
181
+ data[:params].each_with_index {|param, i|
182
+ txt = param[:txt]
183
+ if param[:w].is_a? String
184
+ if data[:font]
185
+ margins = pdf.get_margins
186
+ w = pdf.get_string_width(param[:w]) + margins['cell'] * 2
187
+ else
188
+ w = pdf.get_string_width(param[:w])
189
+ end
190
+ else
191
+ w = param[:w]
192
+ end
193
+
194
+ lasth_0 = pdf.get_last_h
195
+ y1 = pdf.get_y
196
+ pdf.multi_cell(w, 0, txt)
197
+ lasth_1 = pdf.get_last_h
198
+ pno = pdf.get_page
199
+ assert_equal param[:pno], pno
200
+
201
+ if i == 0
202
+ assert_not_equal 0, lasth_1
203
+ else
204
+ assert_equal lasth_0, lasth_1
205
+ end
206
+
207
+ y2 = pdf.get_y
208
+ h1 = y2 - y1
209
+
210
+ h2 = pdf.getStringHeight(w, txt)
211
+ assert_in_delta h1, h2, 0.01
212
+
213
+ line = pdf.get_num_lines(txt, w)
214
+ assert_equal param[:line], line
215
+ }
331
216
  end
332
217
 
333
218
  test "removeSHY encoding test" do
@@ -6,343 +6,267 @@ require 'test_helper'
6
6
 
7
7
  class RbpdfTest < Test::Unit::TestCase
8
8
  class MYPDF < RBPDF
9
- def getHtmlDomArray(html)
10
- super
11
- end
12
- def openHTMLTagHandler(dom, key, cell)
13
- super
14
- end
15
9
  def get_temp_rtl
16
10
  @tmprtl
17
11
  end
18
12
  end
19
13
 
20
- test "Dom Basic" do
21
- pdf = MYPDF.new
14
+ htmls = {
15
+ 'Simple Text' => {:html => 'abc', :length => 2,
16
+ :params => [{:parent => 0, :tag => false}, # parent -> Root
17
+ {:parent => 0, :tag => false, :value => 'abc', :elkey => 0, :block => false}]},
18
+ 'Back Slash Text' => {:html => 'a\\bc', :length => 2,
19
+ :params => [{:parent => 0, :tag => false}, # parent -> Root
20
+ {:parent => 0, :tag => false, :value => 'a\\bc', :elkey => 0, :block => false}]},
21
+ 'Simple Tag' => {:html => '<b>abc</b>', :length => 4,
22
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
23
+ {:parent => 0, :tag => true, :value => 'b', :elkey => 0, :opening => true, :attribute => {}},
24
+ {:parent => 1, :tag => false, :value => 'abc', :elkey => 1, :block => false}, # parent -> open tag key
25
+ {:parent => 1, :tag => true, :value => 'b', :elkey => 2, :opening => false}]}, # parent -> open tag key
26
+ 'pre Tag' => {:html => "<pre>abc</pre>", :length => 4, # See. 'Dom pre tag test'
27
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
28
+ {:parent => 0, :tag => true, :value => 'pre', :elkey => 0, :opening => true, :attribute => {}},
29
+ {:parent => 1, :tag => false, :value => 'abc', :elkey => 1, :block => false}, # parent -> open tag key
30
+ {:parent => 1, :tag => true, :value => 'pre', :elkey => 2, :opening => false}]}, # parent -> open tag key
31
+ 'pre code Tag' => {:html => '<pre><code class="ruby">abc</code></pre>', :length => 4, # See. 'Dom pre tag test (code)'
32
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
33
+ {:parent => 0, :tag => true, :value => 'pre', :elkey => 0, :opening => true, :attribute => {}},
34
+ {:parent => 1, :tag => false, :value => 'abc', :elkey => 1, :block => false}, # parent -> open tag key
35
+ {:parent => 1, :tag => true, :value => 'pre', :elkey => 2, :opening => false}]}, # parent -> open tag key
36
+ 'pre code span Tag' => {:html => '<pre><code class="ruby syntaxhl"><span class="CodeRay">abc</span></code></pre>', :length => 6, # See. 'Dom pre tag test (code span)'
37
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
38
+ {:parent => 0, :tag => true, :value => 'pre', :elkey => 0, :opening => true, :attribute => {}},
39
+ {:parent => 1, :tag => true, :value => 'span', :elkey => 1, :opening => true},
40
+ {:parent => 2, :tag => false, :value => 'abc', :elkey => 2, :block => false}, # parent -> open tag key
41
+ {:parent => 2, :tag => true, :value => 'span', :elkey => 3, :opening => false}, # parent -> open tag key
42
+ {:parent => 1, :tag => true, :value => 'pre', :elkey => 4, :opening => false}]}, # parent -> open tag key
43
+ 'Error Tag (doble colse tag)' => {:html => '</ul></div>',
44
+ :validated_length => 1, # for Rails 4.2 later (no use Rails 3.x/4.0/4.1)
45
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
46
+ {:parent => 0, :tag => true, :value => 'ul', :elkey => 0, :opening => false}, # parent -> Root key
47
+ {:parent => 0, :tag => true, :value => 'div', :elkey => 1, :opening => false}]}, # parent -> Root key
48
+ 'Attribute' => {:html => '<p style="text-align:justify">abc</p>', :length => 4,
49
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
50
+ {:parent => 0, :tag => true, :value => 'p', :elkey => 0, :opening => true, :align => 'J', :attribute => {:style => 'text-align:justify;'}}]},
51
+ 'Table border' => {:html => '<table border="1"><tr><td>abc</td></tr></table>', :length => 9,
52
+ # -> '<table border="1"><tr><td>abc<marker style="font-size:0"/></td></tr></table>' ## added marker tag (by getHtmlDomArray()) ##
53
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
54
+ {:parent => 0, :tag => true, :value => 'table', :elkey => 0, :opening => true, :attribute => {:border => 1}},
55
+ {},
56
+ {},
57
+ {},
58
+ # marker tag (by getHtmlDomArray())
59
+ {:parent => 3, :tag => true, :value => 'marker', :elkey => 4, :opening => true, :attribute => {:style => 'font-size:0'}}]},
60
+ 'Table td Width' => {:html => '<table><tr><td width="10">abc</td></tr></table>', :length => 9,
61
+ # -> '<table><tr><td width="10">abc<marker style="font-size:0"/></td></tr></table>' ## added marker tag (by getHtmlDomArray()) ##
62
+ :params => [{},
63
+ {},
64
+ {},
65
+ {:parent => 2, :tag => true, :value => 'td', :elkey => 2, :opening => true, :width => '10'}]},
66
+ 'Dom open angled bracket "<"' => {:html => "<p>AAA '<'-BBB << <<< '</' '<//' '<///' CCC.</p>", :length => 4,
67
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
68
+ {:parent => 0, :tag => true, :value => 'p', :elkey => 0, :opening => true, :attribute => {}},
69
+ {:parent => 1, :tag => false, :value => "AAA '<'-BBB << <<< '</' '<//' '<///' CCC.", :elkey => 1},
70
+ {:parent => 1, :tag => true, :value => 'p', :elkey => 2, :opening => false}]},
71
+ 'Dom self close tag' => {:html => '<b>ab<br>c</b>', :length => 6, # See. 'Dom self close tag test (Simple Tag)'
72
+ :params => [{:parent => 0, :tag => false, :attribute => {}}, # parent -> Root
73
+ {:parent => 0, :tag => true, :value => 'b', :elkey => 0, :opening => true, :attribute => {}}, # <b>
74
+ {:parent => 1, :tag => false, :value => 'ab', :elkey => 1, :block => false}, # ab
75
+ {:parent => 1, :tag => true, :value => 'br', :elkey => 2, :opening => true, :attribute => {}}, # <br>
76
+ {:parent => 1, :tag => false, :value => 'c', :elkey => 3, :block => false}, # c
77
+ {:parent => 1, :tag => true, :value => 'b', :elkey => 4, :opening => false}]}, # </b>
78
+ }
79
+
80
+ data(htmls)
81
+ test "Dom Basic test" do |data|
82
+ pdf = RBPDF.new
83
+ dom = pdf.send(:getHtmlDomArray, data[:html])
84
+ assert_equal data[:length], dom.length if data[:length]
85
+ data[:params].each_with_index {|param, i|
86
+ # validated length check (for Rails 4.2 later)
87
+ if dom[i].nil? and i >= data[:validated_length]
88
+ break
89
+ end
90
+
91
+ param.each {|key, val|
92
+ if (key == :attribute) and !val.empty?
93
+ val.each {|k, v|
94
+ if (k == :style) and !v.empty?
95
+ assert_equal("dom[#{i}][attribute]: #{k} => #{v}", "dom[#{i}][attribute]: #{k} => #{dom[i][key.to_s][k.to_s].gsub(' ', '')}")
96
+ else
97
+ assert_equal("dom[#{i}][attribute]: #{k} => #{v}", "dom[#{i}][attribute]: #{k} => #{dom[i][key.to_s][k.to_s]}")
98
+ end
99
+ }
100
+ else
101
+ assert_equal("dom[#{i}]: #{key} => #{val}", "dom[#{i}]: #{key} => #{dom[i][key.to_s]}")
102
+ end
103
+ }
104
+ }
105
+ end
22
106
 
23
- # Simple Text
24
- dom = pdf.getHtmlDomArray('abc')
25
- assert_equal 0, dom[0]['parent'] # Root
26
- assert_equal false, dom[0]['tag']
27
- assert_equal({'tag'=>false, 'value'=>'abc', 'elkey'=>0, 'parent'=>0, 'block'=>false}, dom[1])
107
+ test "Dom pre tag test" do
108
+ pdf = RBPDF.new
28
109
 
29
- # Back Slash Text
30
- dom = pdf.getHtmlDomArray("a\\bc")
31
- assert_equal 0, dom[0]['parent'] # Root
32
- assert_equal false, dom[0]['tag']
33
- assert_equal({'tag'=>false, 'value'=>"a\\bc", 'elkey'=>0, 'parent'=>0, 'block'=>false}, dom[1])
110
+ # No Text
111
+ dom = pdf.send(:getHtmlDomArray, "<pre></pre>")
112
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>\n</pre>")
113
+ assert_equal dom, dom2
34
114
 
35
115
  # Simple Tag
36
- dom = pdf.getHtmlDomArray('<b>abc</b>')
37
- assert_equal 4, dom.length
38
-
39
- assert_equal 0, dom[0]['parent'] # Root
40
- assert_equal false, dom[0]['tag']
41
- assert_equal({}, dom[0]['attribute'])
42
-
43
- assert_equal 0, dom[1]['parent'] # parent -> parent tag key
44
- assert_equal 0, dom[1]['elkey']
45
- assert_equal true, dom[1]['tag']
46
- assert_equal true, dom[1]['opening']
47
- assert_equal 'b', dom[1]['value']
48
- assert_equal({}, dom[1]['attribute'])
49
-
50
- assert_equal({'tag' => false, 'value'=>'abc', 'elkey'=>1, 'parent'=>1, 'block'=>false}, dom[2]) # parent -> open tag key
51
-
52
- assert_equal 1, dom[3]['parent'] # parent -> open tag key
53
- assert_equal 2, dom[3]['elkey']
54
- assert_equal true, dom[3]['tag']
55
- assert_equal false, dom[3]['opening']
56
- assert_equal 'b', dom[3]['value']
57
-
58
- # Error Tag (doble colse tag)
59
- dom = pdf.getHtmlDomArray('</ul></div>')
60
-
61
- assert_equal 0, dom[0]['parent'] # Root
62
- assert_equal false, dom[0]['tag']
63
- assert_equal({}, dom[0]['attribute'])
64
-
65
- if dom.length == 3 # for Rails 3.x/4.0/4.1 (no use Rails 4.2 later)
66
- assert_equal 0, dom[1]['parent'] # parent -> Root key
67
- assert_equal 0, dom[1]['elkey']
68
- assert_equal true, dom[1]['tag']
69
- assert_equal false, dom[1]['opening']
70
- assert_equal 'ul', dom[1]['value']
71
-
72
- assert_equal 0, dom[2]['parent'] # parent -> Root key
73
- assert_equal 1, dom[2]['elkey']
74
- assert_equal true, dom[2]['tag']
75
- assert_equal false, dom[2]['opening']
76
- assert_equal 'div', dom[2]['value']
77
- end
78
-
79
- # Attribute
80
- dom = pdf.getHtmlDomArray('<p style="text-align:justify">abc</p>')
81
- assert_equal 4, dom.length
82
-
83
- assert_equal 0, dom[0]['parent'] # Root
84
- assert_equal false, dom[0]['tag']
85
- assert_equal({}, dom[0]['attribute'])
86
-
87
- assert_equal 0, dom[1]['parent'] # parent -> parent tag key
88
- assert_equal 0, dom[1]['elkey']
89
- assert_equal true, dom[1]['tag']
90
- assert_equal true, dom[1]['opening']
91
- assert_equal 'p', dom[1]['value']
92
- assert_not_nil dom[1]['attribute']
93
- assert_equal 'text-align:justify;', dom[1]['attribute']['style'].gsub(' ', '')
94
- assert_equal 'J', dom[1]['align']
95
-
96
- # Table border
97
- dom = pdf.getHtmlDomArray('<table border="1"><tr><td>abc</td></tr></table>')
98
- ## added marker tag (by getHtmlDomArray()) ##
99
- # '<table border="1"><tr><td>abc<marker style="font-size:0"/></td></tr></table>'
100
- assert_equal 9, dom.length
101
-
102
- assert_equal 0, dom[1]['parent'] # parent -> parent tag key
103
- assert_equal 0, dom[1]['elkey']
104
- assert_equal true, dom[1]['tag']
105
- assert_equal true, dom[1]['opening']
106
- assert_equal 'table', dom[1]['value']
107
- assert_equal '1', dom[1]['attribute']['border']
116
+ dom = pdf.send(:getHtmlDomArray, "<pre>abc</pre>")
117
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>\nabc\n</pre>")
118
+ assert_equal dom, dom2
119
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>\nabc</pre>")
120
+ assert_equal dom, dom2
121
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>abc\n</pre>")
122
+ assert_equal dom, dom2
123
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>abc<br /></pre>")
124
+ assert_equal dom, dom2
108
125
 
109
- ## marker tag (by getHtmlDomArray())
110
- assert_equal 3, dom[5]['parent'] # parent -> parent tag key
111
- assert_equal 4, dom[5]['elkey']
112
- assert_equal true, dom[5]['tag']
113
- assert_equal true, dom[5]['opening']
114
- assert_equal 'marker', dom[5]['value']
115
- assert_equal 'font-size:0', dom[5]['attribute']['style']
126
+ # with line feed
127
+ dom = pdf.send(:getHtmlDomArray, "<pre>abc\ndef</pre>")
128
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>\nabc\ndef\n</pre>")
129
+ assert_equal dom, dom2
130
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>\nabc\ndef</pre>")
131
+ assert_equal dom, dom2
132
+ dom2 = pdf.send(:getHtmlDomArray, "<pre>abc\ndef\n</pre>")
133
+ assert_equal dom, dom2
116
134
 
117
- # Table td Width
118
- dom = pdf.getHtmlDomArray('<table><tr><td width="10">abc</td></tr></table>')
119
- ## added marker tag (by getHtmlDomArray()) ##
120
- # '<table><tr><td width="10">abc<marker style="font-size:0"/></td></tr></table>'
121
- assert_equal 9, dom.length
135
+ # code Tag
136
+ dom = pdf.send(:getHtmlDomArray, '<pre><code class="ruby">abc</code></pre>')
137
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby\">\nabc\n</code></pre>")
138
+ assert_equal dom, dom2
139
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby\">\nabc</code></pre>")
140
+ assert_equal dom, dom2
141
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby\">abc\n</code></pre>")
142
+ assert_equal dom, dom2
122
143
 
123
- assert_equal 2, dom[3]['parent'] # parent -> parent tag key
124
- assert_equal 2, dom[3]['elkey']
125
- assert_equal true, dom[3]['tag']
126
- assert_equal true, dom[3]['opening']
127
- assert_equal 'td', dom[3]['value']
128
- assert_equal '10', dom[3]['width']
144
+ # code span Tag
145
+ dom = pdf.send(:getHtmlDomArray, '<pre><code class="ruby syntaxhl"><span class="CodeRay">abc</span></code></pre>')
146
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby syntaxhl\"><span class=\"CodeRay\">\nabc\n</span></code></pre>")
147
+ assert_equal dom, dom2
148
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby syntaxhl\"><span class=\"CodeRay\">\nabc</span></code></pre>")
149
+ assert_equal dom, dom2
150
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby syntaxhl\"><span class=\"CodeRay\">abc\n</span></code></pre>")
151
+ assert_equal dom, dom2
152
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby syntaxhl\"><span class=\"CodeRay\">abc</span>\n</code></pre>")
153
+ assert_equal dom, dom2
154
+ dom2 = pdf.send(:getHtmlDomArray, "<pre><code class=\"ruby syntaxhl\">\n<span class=\"CodeRay\">\nabc\n</span>\n</code></pre>")
155
+ assert_equal dom, dom2
129
156
  end
130
157
 
131
158
  test "Dom self close tag test" do
132
- pdf = MYPDF.new
159
+ pdf = RBPDF.new
133
160
 
134
161
  # Simple Tag
135
- dom = pdf.getHtmlDomArray('<b>ab<br>c</b>')
136
- assert_equal 6, dom.length
137
-
138
- assert_equal 0, dom[0]['parent'] # Root
139
- assert_equal false, dom[0]['tag']
140
- assert_equal({}, dom[0]['attribute'])
141
-
142
- # <b>
143
- assert_equal 0, dom[1]['parent'] # parent -> parent tag key
144
- assert_equal 0, dom[1]['elkey']
145
- assert_equal true, dom[1]['tag']
146
- assert_equal true, dom[1]['opening']
147
- assert_equal 'b', dom[1]['value']
148
- assert_equal({}, dom[1]['attribute'])
149
-
150
- # ab
151
- assert_equal({'tag' => false, 'value'=>'ab', 'elkey'=>1, 'parent'=>1, 'block'=>false}, dom[2]) # parent -> open tag key
152
-
153
- # <br>
154
- assert_equal 1, dom[3]['parent'] # parent -> open tag key
155
- assert_equal 2, dom[3]['elkey']
156
- assert_equal true, dom[3]['tag']
157
- assert_equal true, dom[3]['opening']
158
- assert_equal 'br', dom[3]['value']
159
- assert_equal({}, dom[3]['attribute'])
160
-
161
- # c
162
- assert_equal({'tag' => false, 'value'=>'c', 'elkey'=>3, 'parent'=>1, 'block'=>false}, dom[4]) # parent -> open tag key
163
-
164
- # </b>
165
- assert_equal 1, dom[5]['parent'] # parent -> open tag key
166
- assert_equal 4, dom[5]['elkey']
167
- assert_equal true, dom[5]['tag']
168
- assert_equal false, dom[5]['opening']
169
- assert_equal 'b', dom[5]['value']
170
-
171
- dom2 = pdf.getHtmlDomArray('<b>ab<br/>c</b>')
162
+ dom = pdf.send(:getHtmlDomArray, '<b>ab<br>c</b>')
163
+ dom2 = pdf.send(:getHtmlDomArray, '<b>ab<br/>c</b>')
172
164
  assert_equal dom, dom2
173
165
 
174
166
  htmlcontent = '<b><img src="/public/ng.png" alt="test alt attribute" width="30" height="30" border="0"/></b>'
175
- dom1 = pdf.getHtmlDomArray(htmlcontent)
167
+ dom1 = pdf.send(:getHtmlDomArray, htmlcontent)
176
168
  htmlcontent = '<b><img src="/public/ng.png" alt="test alt attribute" width="30" height="30" border="0"></b>'
177
- dom2 = pdf.getHtmlDomArray(htmlcontent)
169
+ dom2 = pdf.send(:getHtmlDomArray, htmlcontent)
178
170
  assert_equal dom1, dom2
179
171
 
180
- dom1 = pdf.getHtmlDomArray('<b>ab<hr/>c</b>')
181
- dom2 = pdf.getHtmlDomArray('<b>ab<hr>c</b>')
172
+ dom1 = pdf.send(:getHtmlDomArray, '<b>ab<hr/>c</b>')
173
+ dom2 = pdf.send(:getHtmlDomArray, '<b>ab<hr>c</b>')
182
174
  assert_equal dom1, dom2
183
175
  end
184
176
 
185
177
  test "Dom HTMLTagHandler Basic test" do
186
- pdf = MYPDF.new
178
+ pdf = RBPDF.new
187
179
  pdf.add_page
188
180
 
189
181
  # Simple HTML
190
182
  htmlcontent = '<h1>HTML Example</h1>'
191
- dom1 = pdf.getHtmlDomArray(htmlcontent)
192
- dom2 = pdf.openHTMLTagHandler(dom1, 1, false)
183
+ dom1 = pdf.send(:getHtmlDomArray, htmlcontent)
184
+ dom2 = pdf.send(:openHTMLTagHandler, dom1, 1, false)
193
185
  assert_equal dom1, dom2
194
186
  end
195
187
 
196
- test "Dom HTMLTagHandler DIR RTL test" do
188
+ htmls = {
189
+ 'LTR' => {:lines => [{:html => '<p dir="ltr">HTML Example</p>', :length => 4,
190
+ :params => [{}, {:tag => true, :value => 'p', :opening => true, :attribute => {:dir => 'ltr'}, :temprtl => false}]},
191
+ {:html => '<p dir="rtl">HTML Example</p>', :length => 4,
192
+ :params => [{}, {:tag => true, :value => 'p', :opening => true, :attribute => {:dir => 'rtl'}, :temprtl => 'R'}]},
193
+ {:html => '<p dir="ltr">HTML Example</p>', :length => 4,
194
+ :params => [{}, {:tag => true, :value => 'p', :opening => true, :attribute => {:dir => 'ltr'}, :temprtl => false}]}
195
+ ]},
196
+ 'RTL' => {:rtl => true,
197
+ :lines => [{:html => '<p dir="ltr">HTML Example</p>', :length => 4,
198
+ :params => [{}, {:tag => true, :value => 'p', :opening => true, :attribute => {:dir => 'ltr'}, :temprtl => 'L'}]},
199
+ {:html => '<p dir="rtl">HTML Example</p>', :length => 4,
200
+ :params => [{}, {:tag => true, :value => 'p', :opening => true, :attribute => {:dir => 'rtl'}, :temprtl => false}]}
201
+ ]},
202
+ }
203
+
204
+ data(htmls)
205
+ test "Dom HTMLTagHandler DIR test" do |data|
197
206
  pdf = MYPDF.new
198
207
  pdf.add_page
199
208
  temprtl = pdf.get_temp_rtl
200
209
  assert_equal false, temprtl
201
-
202
- # LTR, ltr
203
- htmlcontent = '<p dir="ltr">HTML Example</p>'
204
- dom = pdf.getHtmlDomArray(htmlcontent)
205
- dom = pdf.openHTMLTagHandler(dom, 1, false)
206
-
207
- assert_equal true, dom[1]['tag']
208
- assert_equal true, dom[1]['opening']
209
- assert_equal 'p', dom[1]['value']
210
- assert_equal 'ltr', dom[1]['attribute']['dir']
211
-
212
- temprtl = pdf.get_temp_rtl
213
- assert_equal false, temprtl
214
-
215
- # LTR, rtl
216
- htmlcontent = '<p dir="rtl">HTML Example</p>'
217
- dom = pdf.getHtmlDomArray(htmlcontent)
218
- dom = pdf.openHTMLTagHandler(dom, 1, false)
219
- assert_equal 4, dom.length
220
-
221
- assert_equal true, dom[1]['tag']
222
- assert_equal true, dom[1]['opening']
223
- assert_equal 'p', dom[1]['value']
224
- assert_equal 'rtl', dom[1]['attribute']['dir']
225
-
226
- temprtl = pdf.get_temp_rtl
227
- assert_equal 'R', temprtl
228
-
229
- # LTR, ltr
230
- htmlcontent = '<p dir="ltr">HTML Example</p>'
231
- dom = pdf.getHtmlDomArray(htmlcontent)
232
- dom = pdf.openHTMLTagHandler(dom, 1, false)
233
-
234
- assert_equal true, dom[1]['tag']
235
- assert_equal true, dom[1]['opening']
236
- assert_equal 'p', dom[1]['value']
237
- assert_equal 'ltr', dom[1]['attribute']['dir']
238
-
239
- temprtl = pdf.get_temp_rtl
240
- assert_equal false, temprtl
241
- end
242
-
243
- test "Dom HTMLTagHandler DIR LTR test" do
244
- pdf = MYPDF.new
245
- pdf.add_page
246
- temprtl = pdf.get_temp_rtl
247
- assert_equal false, temprtl
248
- pdf.set_rtl(true)
249
-
250
- # RTL, ltr
251
- htmlcontent = '<p dir="ltr">HTML Example</p>'
252
- dom = pdf.getHtmlDomArray(htmlcontent)
253
- dom = pdf.openHTMLTagHandler(dom, 1, false)
254
- assert_equal 4, dom.length
255
-
256
- assert_equal true, dom[1]['tag']
257
- assert_equal true, dom[1]['opening']
258
- assert_equal 'p', dom[1]['value']
259
- assert_equal 'ltr', dom[1]['attribute']['dir']
260
-
261
- temprtl = pdf.get_temp_rtl
262
- assert_equal 'L', temprtl
263
-
264
- # RTL, rtl
265
- htmlcontent = '<p dir="rtl">HTML Example</p>'
266
- dom = pdf.getHtmlDomArray(htmlcontent)
267
- dom = pdf.openHTMLTagHandler(dom, 1, false)
268
- assert_equal 4, dom.length
269
-
270
- assert_equal true, dom[1]['tag']
271
- assert_equal true, dom[1]['opening']
272
- assert_equal 'p', dom[1]['value']
273
- assert_equal 'rtl', dom[1]['attribute']['dir']
274
-
275
- temprtl = pdf.get_temp_rtl
276
- assert_equal false, temprtl
210
+ pdf.set_rtl(true) if data[:rtl]
211
+
212
+ data[:lines].each_with_index {|line, l|
213
+ dom = pdf.send(:getHtmlDomArray, line[:html])
214
+ dom = pdf.send(:openHTMLTagHandler, dom, 1, false)
215
+
216
+ assert_equal line[:length], dom.length if line[:length]
217
+ line[:params].each_with_index {|param, i|
218
+ param.each {|key, val|
219
+ if (key == :attribute) and !val.empty?
220
+ val.each {|k, v|
221
+ assert_equal("#{l}: dom[#{i}][attribute]: #{k} => #{v}", "#{l}: dom[#{i}][attribute]: #{k} => #{dom[i][key.to_s][k.to_s]}")
222
+ }
223
+ elsif (key == :temprtl)
224
+ temprtl = pdf.get_temp_rtl
225
+ assert_equal("#{l}: dom[#{i}]: temprtl => #{val}", "#{l}: dom[#{i}]: temprtl => #{temprtl.to_s}")
226
+ else
227
+ assert_equal("#{l}: dom[#{i}]: #{key} => #{val}", "#{l}: dom[#{i}]: #{key} => #{dom[i][key.to_s]}")
228
+ end
229
+ }
230
+ }
231
+ }
277
232
  end
278
233
 
279
234
  test "Dom HTMLTagHandler img y position with height attribute test" do
280
- pdf = MYPDF.new
235
+ pdf = RBPDF.new
281
236
  pdf.add_page
282
237
 
283
238
  # Image Error HTML
284
239
  htmlcontent = '<img src="/public/ng.png" alt="test alt attribute" width="30" height="30" border="0"/>'
285
- dom1 = pdf.getHtmlDomArray(htmlcontent)
240
+ dom1 = pdf.send(:getHtmlDomArray, htmlcontent)
286
241
  #y1 = pdf.get_y
287
242
 
288
- dom2 = pdf.openHTMLTagHandler(dom1, 1, false)
243
+ dom2 = pdf.send(:openHTMLTagHandler, dom1, 1, false)
289
244
  y2 = pdf.get_y
290
245
  assert_equal dom1, dom2
291
246
  assert_equal pdf.get_image_rby - (12 / pdf.get_scale_factor) , y2
292
247
  end
293
248
 
294
249
  test "Dom HTMLTagHandler img y position without height attribute test" do
295
- pdf = MYPDF.new
250
+ pdf = RBPDF.new
296
251
  pdf.add_page
297
252
 
298
253
  # Image Error HTML
299
254
  htmlcontent = '<img src="/public/ng.png" alt="test alt attribute" border="0"/>'
300
- dom1 = pdf.getHtmlDomArray(htmlcontent)
255
+ dom1 = pdf.send(:getHtmlDomArray, htmlcontent)
301
256
  y1 = pdf.get_y
302
257
 
303
- dom2 = pdf.openHTMLTagHandler(dom1, 1, false)
258
+ dom2 = pdf.send(:openHTMLTagHandler, dom1, 1, false)
304
259
  y2 = pdf.get_y
305
260
  assert_equal dom1, dom2
306
261
  assert_equal y1, y2
307
262
  end
308
263
 
309
- test "Dom open angled bracket '<' test" do
310
- pdf = MYPDF.new
311
- pdf.add_page
312
-
313
- htmlcontent = "<p>AAA '<'-BBB << <<< '</' '<//' '<///' CCC.</p>"
314
- dom = pdf.getHtmlDomArray(htmlcontent)
315
- assert_equal 4, dom.length
316
-
317
- assert_equal 0, dom[0]['parent'] # Root
318
- assert_equal false, dom[0]['tag']
319
- assert_equal({}, dom[0]['attribute'])
320
-
321
- assert_equal 0, dom[1]['parent'] # parent -> parent tag key
322
- assert_equal 0, dom[1]['elkey']
323
- assert_equal true, dom[1]['tag']
324
- assert_equal true, dom[1]['opening']
325
- assert_equal 'p', dom[1]['value']
326
- assert_equal({}, dom[1]['attribute'])
327
-
328
- assert_equal 1, dom[2]['parent'] # parent -> open tag key
329
- assert_equal 1, dom[2]['elkey']
330
- assert_equal false, dom[2]['tag']
331
- assert_equal "AAA '<'-BBB << <<< '</' '<//' '<///' CCC.", dom[2]['value']
332
-
333
- assert_equal 1, dom[3]['parent'] # parent -> open tag key
334
- assert_equal 2, dom[3]['elkey']
335
- assert_equal true, dom[3]['tag']
336
- assert_equal false, dom[3]['opening']
337
- assert_equal 'p', dom[3]['value']
338
- end
339
-
340
264
  test "getHtmlDomArray encoding test" do
341
265
  return unless 'test'.respond_to?(:force_encoding)
342
266
 
343
- pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
267
+ pdf = RBPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
344
268
  htmlcontent = 'test'.force_encoding('ASCII-8BIT')
345
- pdf.getHtmlDomArray(htmlcontent)
269
+ pdf.send(:getHtmlDomArray, htmlcontent)
346
270
  assert_equal 'ASCII-8BIT', htmlcontent.encoding.to_s
347
271
  end
348
272
  end
@@ -0,0 +1,83 @@
1
+ # Copyright (c) 2011-2018 NAITOH Jun
2
+ # Released under the MIT license
3
+ # http://www.opensource.org/licenses/MIT
4
+
5
+ require 'test_helper'
6
+
7
+ def logger
8
+ require 'logger'
9
+ return Logger.new(STDOUT)
10
+ end
11
+
12
+ if !defined? ApplicationController
13
+ class ApplicationController
14
+ end
15
+ end
16
+
17
+ class RbpdfTest < Test::Unit::TestCase
18
+ htmls = {
19
+ '001 : Default Header and Footer' => '001',
20
+ '002 : Removing Header and Footer' => '002',
21
+ '003 : Custom Header and Footer' => '003',
22
+ '004 : Cell stretching' => '004',
23
+ '005 : Multicell' => '005',
24
+ '006 : write_html and RTL support' => '006',
25
+ '007 : Two independent columns with write_htmlcell()' => '007',
26
+ '008 : Include external UTF-8 text file' => '008',
27
+ '009 : Test Image' => '009',
28
+ '011 : Colored Table' => '011',
29
+ '012 : Graphic Functions' => '012',
30
+ '015 : Bookmarks (Table of Content)' => '015',
31
+ '017 : Two independent columns with MultiCell' => '017',
32
+ '018 : RTL document with Persian language' => '018',
33
+ '020 : Two columns composed by MultiCell of different' => '020',
34
+ '021 : write_html text flow' => '021',
35
+ '022 : CMYK colors' => '022',
36
+ '023 : Page Groups' => '023',
37
+ '024 : Object Visibility' => '024',
38
+ '025 : Object Transparency' => '025',
39
+ '026 : Text Rendering Modes and Text Clipping' => '026',
40
+ '028 : Changing page formats' => '028',
41
+ '029 : Set PDF viewer display preferences.' => '029',
42
+ '031 : Pie Chart' => '031',
43
+ '033 : Mixed font types' => '033',
44
+ '034 : Clipping' => '034',
45
+ '035 : Line styles with cells and multicells' => '035',
46
+ '036 : Annotations' => '036',
47
+ '038 : CID-0 CJK unembedded font' => '038',
48
+ '039 : HTML justification' => '039',
49
+ '040 : Booklet mode (double-sided pages)' => '040',
50
+ '041 : Annotation - FileAttachment' => '041',
51
+ '042 : Test Image with alpha channel' => '042',
52
+ '043 : Disk caching' => '043',
53
+ '044 : Move, copy and delete pages' => '044',
54
+ '045 : Bookmarks and Table of Content' => '045',
55
+ '047 : Transactions' => '047',
56
+ '048 : HTML tables and table headers' => '048',
57
+ # '051 : Full page background' => '051',
58
+ '055 : Display all characters available on core fonts.' => '055',
59
+ '057 : Cell vertical alignments' => '057',
60
+ '059 : Table Of Content using HTML templates.' => '059',
61
+ '060 : Advanced page settings.' => '060',
62
+ '061 : XHTML + CSS' => '061',
63
+ }
64
+
65
+ data(htmls)
66
+ test "Examples test" do |data|
67
+ $LOAD_PATH << File.join(File.dirname(File.expand_path(__FILE__)), '../example/rails/app/controllers/')
68
+
69
+ require "example#{data}_controller"
70
+
71
+ content = []
72
+ test = Object.const_get("Example#{data}Controller").new
73
+
74
+ contents = test.index
75
+ # contents = assert_nothing_raised do
76
+ # test.index
77
+ # end
78
+ contents.each_line {|line| content.push line.chomp }
79
+
80
+ assert_not_equal 0, content.length
81
+ assert_equal '%PDF-1.7', content[0]
82
+ end
83
+ end
@@ -79,7 +79,7 @@ class RbpdfTest < Test::Unit::TestCase
79
79
  pdf.write_html(htmlcontent, true, 0, true, 0)
80
80
 
81
81
  pdf.send(:mapLinksToHtmlAnchors)
82
- link_position = pdf.instance_variable_get(:@links)[0]
82
+ link_position = pdf.instance_variable_get(:@links)[1]
83
83
  assert_equal [3, 73.50124999999998], link_position
84
84
  end
85
85
 
@@ -98,7 +98,7 @@ class RbpdfTest < Test::Unit::TestCase
98
98
 
99
99
  pdf.send(:mapLinksToHtmlAnchors)
100
100
 
101
- link_position = pdf.instance_variable_get(:@links)[0]
101
+ link_position = pdf.instance_variable_get(:@links)[1]
102
102
  assert_equal [1, 10.001249999999999], link_position
103
103
  end
104
104
 
@@ -1,68 +1,60 @@
1
- # Copyright (c) 2011-2017 NAITOH Jun
1
+ # Copyright (c) 2011-2018 NAITOH Jun
2
2
  # Released under the MIT license
3
3
  # http://www.opensource.org/licenses/MIT
4
4
 
5
5
  require 'test_helper'
6
6
 
7
7
  class RbpdfTest < Test::Unit::TestCase
8
- test "write_html_cell Basic test" do
8
+ htmls = {
9
+ 'Basic' => {:html => '<p>foo</p>', :line => 1,
10
+ :border => 0, :pno => 1, :no => 1},
11
+ 'Page Break no border' => {:html => '<p>foo</p>', :margin => 30,
12
+ :border => 0, :pno => 2, :no => 2},
13
+ 'Page Break border' => {:html => '<p>foo</p>', :margin => 30,
14
+ :border => 'LRBT', :pno => 2, :no => 2},
15
+ 'pre tag y position' => {:html => "<p>test 0</p>\n <pre>test 1\ntest 2\ntest 3</pre>\n <p>test 10</p>", :line => 7,
16
+ :border => 0, :pno => 1, :no => 1},
17
+ }
18
+
19
+ data(htmls)
20
+ test "write_html_cell test" do |data|
9
21
  pdf = RBPDF.new
10
22
  pdf.add_page()
23
+ t_margin = pdf.instance_variable_get('@t_margin')
24
+ y0 = pdf.get_y
25
+ assert_equal t_margin, y0
11
26
 
12
- htmlcontent = '<p>foo</p>'
13
- pdf.write_html_cell(0, 5, 10, '', htmlcontent, 0, 1)
27
+ if data[:margin]
28
+ pdf.set_top_margin(data[:margin])
29
+ y0 = pdf.get_y
30
+ assert_equal data[:margin], y0
14
31
 
15
- pno = pdf.get_page
16
- assert_equal 1, pno
17
-
18
- y = pdf.get_y
19
- assert_in_delta 17.3, y, 0.1
20
-
21
- no = pdf.get_num_pages
22
- assert_equal 1, no
23
- end
24
-
25
- test "write_html_cell Page Break test 1" do
26
- pdf = RBPDF.new
27
- pdf.add_page()
28
-
29
- pdf.set_top_margin(30)
30
-
31
- h = pdf.get_page_height
32
- pdf.set_y(h - 15)
33
-
34
- htmlcontent = '<p>foo</p>'
35
- pdf.write_html_cell(0, 5, 10, '', htmlcontent, 0, 1)
36
-
37
- pno = pdf.get_page
38
- assert_equal 2, pno
39
-
40
- y = pdf.get_y
41
- assert_in_delta 40.0, y, 0.1
42
-
43
- no = pdf.get_num_pages
44
- assert_equal 2, no
45
- end
46
-
47
- test "write_html_cell Page Break test 2" do
48
- pdf = RBPDF.new
49
- pdf.add_page()
32
+ h = pdf.get_page_height
33
+ pdf.set_y(h - 15)
34
+ y0 = pdf.get_y
35
+ end
50
36
 
51
- pdf.set_top_margin(30)
37
+ font_size = pdf.get_font_size
38
+ cell_height_ratio = pdf.get_cell_height_ratio
39
+ min_cell_height = font_size * cell_height_ratio
40
+ h = 5
52
41
 
53
- h = pdf.get_page_height
54
- pdf.set_y(h - 15)
42
+ min_cell_height = h > min_cell_height ? h : min_cell_height
55
43
 
56
- htmlcontent = '<p>foo</p>'
57
- pdf.write_html_cell(0, 5, 10, '', htmlcontent, "LRBT", 1)
44
+ pdf.write_html_cell(0, h, 10, '', data[:html], data[:border], 1, 0, true, '', false)
58
45
 
59
46
  pno = pdf.get_page
60
- assert_equal 2, pno
47
+ assert_equal data[:pno], pno
61
48
 
62
- y = pdf.get_y
63
- assert_in_delta 40.0, y, 0.1
49
+ y1 = pdf.get_y
50
+ if pno == 1
51
+ assert_in_delta(y0 + min_cell_height * data[:line], y1, 0.1)
52
+ else # pno 2, 1 line case only
53
+ page_break_trigger = pdf.instance_variable_get('@page_break_trigger')
54
+ assert_in_delta(data[:margin] + y0 + h - page_break_trigger, y1, 0.1)
55
+ end
64
56
 
65
57
  no = pdf.get_num_pages
66
- assert_equal 2, no
58
+ assert_equal data[:no], no
67
59
  end
68
60
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbpdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.19.5
4
+ version: 1.19.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - NAITOH Jun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
11
+ date: 2018-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities
@@ -122,6 +122,7 @@ files:
122
122
  - test/rbpdf_content_test.rb
123
123
  - test/rbpdf_css_test.rb
124
124
  - test/rbpdf_dom_test.rb
125
+ - test/rbpdf_examples_test.rb
125
126
  - test/rbpdf_font_func_test.rb
126
127
  - test/rbpdf_font_style_test.rb
127
128
  - test/rbpdf_font_test.rb
@@ -194,6 +195,7 @@ test_files:
194
195
  - test/rbpdf_content_test.rb
195
196
  - test/rbpdf_css_test.rb
196
197
  - test/rbpdf_dom_test.rb
198
+ - test/rbpdf_examples_test.rb
197
199
  - test/rbpdf_font_func_test.rb
198
200
  - test/rbpdf_font_style_test.rb
199
201
  - test/rbpdf_font_test.rb