rfpdf 1.17.1

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.
Files changed (107) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +18 -0
  3. data/Gemfile +4 -0
  4. data/Rakefile +2 -0
  5. data/lib/core/rmagick.rb +85 -0
  6. data/lib/fonts/README.z +2 -0
  7. data/lib/fonts/arialunicid0_cw.rb +1738 -0
  8. data/lib/fonts/cid0cs.rb +21 -0
  9. data/lib/fonts/cid0ct.rb +21 -0
  10. data/lib/fonts/cid0jp.rb +21 -0
  11. data/lib/fonts/cid0kr.rb +21 -0
  12. data/lib/fonts/courier.rb +37 -0
  13. data/lib/fonts/dejavu-fonts-ttf-2.33/AUTHORS +53 -0
  14. data/lib/fonts/dejavu-fonts-ttf-2.33/BUGS +3 -0
  15. data/lib/fonts/dejavu-fonts-ttf-2.33/LICENSE +99 -0
  16. data/lib/fonts/dejavu-fonts-ttf-2.33/NEWS +1315 -0
  17. data/lib/fonts/dejavu-fonts-ttf-2.33/README +59 -0
  18. data/lib/fonts/dejavusans.ctg.z +0 -0
  19. data/lib/fonts/dejavusans.rb +338 -0
  20. data/lib/fonts/dejavusans.z +0 -0
  21. data/lib/fonts/dejavusansb.ctg.z +0 -0
  22. data/lib/fonts/dejavusansb.rb +330 -0
  23. data/lib/fonts/dejavusansb.z +0 -0
  24. data/lib/fonts/dejavusansbi.ctg.z +0 -0
  25. data/lib/fonts/dejavusansbi.rb +297 -0
  26. data/lib/fonts/dejavusansbi.z +0 -0
  27. data/lib/fonts/dejavusansi.ctg.z +0 -0
  28. data/lib/fonts/dejavusansi.rb +305 -0
  29. data/lib/fonts/dejavusansi.z +0 -0
  30. data/lib/fonts/freefont-20080912/AUTHORS +191 -0
  31. data/lib/fonts/freefont-20080912/COPYING +341 -0
  32. data/lib/fonts/freefont-20080912/CREDITS +506 -0
  33. data/lib/fonts/freefont-20080912/ChangeLog +3320 -0
  34. data/lib/fonts/freefont-20080912/INSTALL +81 -0
  35. data/lib/fonts/freefont-20080912/README +108 -0
  36. data/lib/fonts/freemono.ctg.z +0 -0
  37. data/lib/fonts/freemono.rb +203 -0
  38. data/lib/fonts/freemono.z +0 -0
  39. data/lib/fonts/freemonob.ctg.z +0 -0
  40. data/lib/fonts/freemonob.rb +120 -0
  41. data/lib/fonts/freemonob.z +0 -0
  42. data/lib/fonts/freemonobi.ctg.z +0 -0
  43. data/lib/fonts/freemonobi.rb +84 -0
  44. data/lib/fonts/freemonobi.z +0 -0
  45. data/lib/fonts/freemonoi.ctg.z +0 -0
  46. data/lib/fonts/freemonoi.rb +136 -0
  47. data/lib/fonts/freemonoi.z +0 -0
  48. data/lib/fonts/freesans.ctg.z +0 -0
  49. data/lib/fonts/freesans.rb +196 -0
  50. data/lib/fonts/freesans.z +0 -0
  51. data/lib/fonts/freesansb.ctg.z +0 -0
  52. data/lib/fonts/freesansb.rb +136 -0
  53. data/lib/fonts/freesansb.z +0 -0
  54. data/lib/fonts/freesansbi.ctg.z +0 -0
  55. data/lib/fonts/freesansbi.rb +108 -0
  56. data/lib/fonts/freesansbi.z +0 -0
  57. data/lib/fonts/freesansi.ctg.z +0 -0
  58. data/lib/fonts/freesansi.rb +136 -0
  59. data/lib/fonts/freesansi.z +0 -0
  60. data/lib/fonts/helvetica.rb +34 -0
  61. data/lib/fonts/helveticab.rb +34 -0
  62. data/lib/fonts/helveticabi.rb +34 -0
  63. data/lib/fonts/helveticai.rb +34 -0
  64. data/lib/fonts/hysmyeongjostdmedium.rb +31 -0
  65. data/lib/fonts/kozgopromedium.rb +47 -0
  66. data/lib/fonts/kozminproregular.rb +46 -0
  67. data/lib/fonts/msungstdlight.rb +23 -0
  68. data/lib/fonts/sjis.rb +834 -0
  69. data/lib/fonts/stsongstdlight.rb +23 -0
  70. data/lib/fonts/symbol.rb +33 -0
  71. data/lib/fonts/times.rb +34 -0
  72. data/lib/fonts/timesb.rb +34 -0
  73. data/lib/fonts/timesbi.rb +34 -0
  74. data/lib/fonts/timesi.rb +34 -0
  75. data/lib/fonts/uni2cid_ac15.rb +23613 -0
  76. data/lib/fonts/uni2cid_ag15.rb +30222 -0
  77. data/lib/fonts/uni2cid_aj16.rb +15705 -0
  78. data/lib/fonts/uni2cid_ak12.rb +17530 -0
  79. data/lib/fonts/zapfdingbats.rb +33 -0
  80. data/lib/htmlcolors.rb +209 -0
  81. data/lib/rfpdf/version.rb +3 -0
  82. data/lib/rfpdf.rb +16 -0
  83. data/lib/tcpdf.rb +14186 -0
  84. data/lib/unicode_data.rb +18317 -0
  85. data/logo_example.png +0 -0
  86. data/rfpdf.gemspec +35 -0
  87. data/test/tcpdf_bidi_test.rb +65 -0
  88. data/test/tcpdf_bookmark_test.rb +57 -0
  89. data/test/tcpdf_cell_test.rb +54 -0
  90. data/test/tcpdf_content_test.rb +117 -0
  91. data/test/tcpdf_css_test.rb +318 -0
  92. data/test/tcpdf_dom_test.rb +113 -0
  93. data/test/tcpdf_font_func_test.rb +27 -0
  94. data/test/tcpdf_font_style_test.rb +34 -0
  95. data/test/tcpdf_font_test.rb +176 -0
  96. data/test/tcpdf_format_test.rb +27 -0
  97. data/test/tcpdf_func_test.rb +126 -0
  98. data/test/tcpdf_html_test.rb +164 -0
  99. data/test/tcpdf_image_test.rb +54 -0
  100. data/test/tcpdf_test.rb +245 -0
  101. data/test/tcpdf_transaction_test.rb +199 -0
  102. data/test/tcpdf_viewerpreferences_test.rb +37 -0
  103. data/test/tcpdf_write_test.rb +173 -0
  104. data/test/test_helper.rb +1 -0
  105. data/test_unicode.rfpdf +115 -0
  106. data/utf8test.txt +135 -0
  107. metadata +202 -0
data/logo_example.png ADDED
Binary file
data/rfpdf.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rfpdf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rfpdf"
8
+ spec.version = Rfpdf::VERSION
9
+ spec.authors = ["NAITOH Jun"]
10
+ spec.email = ["naitoh@gmail.com"]
11
+ spec.summary = %q{TCPDF via RFPDF.}
12
+ spec.description = %q{A template plugin allowing the inclusion of ERB-enabled RFPDF template files.}
13
+ spec.homepage = ""
14
+ spec.license = "LGPL 2.1 or later"
15
+ spec.files = Dir.glob("lib/rfpdf/version.rb") +
16
+ Dir.glob("lib/*.rb") +
17
+ Dir.glob("lib/core/rmagick.rb") +
18
+ Dir.glob("lib/fonts/*.{rb,z}") +
19
+ Dir.glob("lib/fonts/freefont-*/*") +
20
+ Dir.glob("lib/fonts/dejavu-fonts-ttf-*/{AUTHORS,BUGS,LICENSE,NEWS,README}") +
21
+ Dir.glob("test/*") +
22
+ ["Rakefile", "rfpdf.gemspec", "Gemfile",
23
+ "CHANGELOG", "test_unicode.rfpdf",
24
+ "utf8test.txt", "logo_example.png" ]
25
+ spec.rdoc_options += [ '--exclude', 'lib/fonts/',
26
+ '--exclude', 'lib/htmlcolors.rb',
27
+ '--exclude', 'lib/unicode_data.rb' ]
28
+
29
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
30
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.6"
34
+ spec.add_development_dependency "rake"
35
+ end
@@ -0,0 +1,65 @@
1
+ # coding: ASCII-8BIT
2
+ require 'test_helper'
3
+
4
+ class TcpdfTest < ActiveSupport::TestCase
5
+ class MYPDF < TCPDF
6
+ def UTF8StringToArray(str)
7
+ super
8
+ end
9
+ def utf8Bidi(ta, str='', forcertl=false)
10
+ super
11
+ end
12
+ end
13
+
14
+ test "Bidi" do
15
+ pdf = MYPDF.new
16
+
17
+ # UCS4 charactor -> UTF-8 charactor
18
+ utf8_chr = pdf.unichr(0x61)
19
+ assert_equal "a", utf8_chr
20
+ utf8_chr = pdf.unichr(0x5e2)
21
+ assert_equal "\xd7\xa2", utf8_chr
22
+
23
+ # UTF-8 string -> array of UCS4 charactor
24
+ ary_ucs4 = pdf.UTF8StringToArray("abc")
25
+ assert_equal [0x61, 0x62, 0x63], ary_ucs4
26
+ ary_ucs4 = pdf.UTF8StringToArray("\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa")
27
+ assert_equal [0x5e2, 0x5d1, 0x5e8, 0x5d9, 0x5ea], ary_ucs4
28
+
29
+ # Bidirectional Algorithm
30
+ ascii_str = "abc"
31
+ utf8_str_1 = "\xd7\xa2"
32
+ utf8_str_2 = "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa"
33
+
34
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(ascii_str))
35
+ assert_equal [0x61, 0x62, 0x63], ary_ucs4
36
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(ascii_str), ascii_str, 'R')
37
+ assert_equal [0x61, 0x62, 0x63], ary_ucs4
38
+
39
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_1))
40
+ assert_equal [0x5e2], ary_ucs4
41
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_1), utf8_str_1, 'R')
42
+ assert_equal [0x5e2], ary_ucs4
43
+
44
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_2))
45
+ assert_equal [0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_ucs4
46
+ ary_ucs4 = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_2), utf8_str_2, 'R')
47
+ assert_equal [0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_ucs4 ##
48
+
49
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(ascii_str + utf8_str_2), ascii_str + utf8_str_2, 'L')
50
+ assert_equal [0x61, 0x62, 0x63, 0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_str
51
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(ascii_str + utf8_str_2)) # LTR
52
+ assert_equal [0x61, 0x62, 0x63, 0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_str
53
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(ascii_str + utf8_str_2), ascii_str + utf8_str_2, 'R')
54
+
55
+ assert_equal [0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2, 0x61, 0x62, 0x63], ary_str
56
+
57
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_2 + ascii_str), utf8_str_2 + ascii_str, 'L')
58
+ assert_equal [0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2, 0x61, 0x62, 0x63], ary_str
59
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_2 + ascii_str)) # RTL
60
+ assert_equal [0x61, 0x62, 0x63, 0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_str
61
+ ary_str = pdf.utf8Bidi(pdf.UTF8StringToArray(utf8_str_2 + ascii_str), utf8_str_2 + ascii_str, 'R')
62
+ assert_equal [0x61, 0x62, 0x63, 0x5ea, 0x5d9, 0x5e8, 0x5d1, 0x5e2], ary_str
63
+ end
64
+
65
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class TcpdfTest < ActiveSupport::TestCase
4
+
5
+ test "bookmark test" do
6
+ pdf = TCPDF.new
7
+ pdf.add_page()
8
+
9
+ book = pdf.bookmark('Chapter 1', 0, 0)
10
+ assert_equal book, [{:l=>0, :y=>0, :t=>"Chapter 1", :p=>1}]
11
+
12
+ book = pdf.bookmark('Paragraph 1.1', 1, 0)
13
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
14
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1}]
15
+
16
+ pdf.add_page()
17
+
18
+ book = pdf.bookmark('Paragraph 1.2', 1, 0)
19
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
20
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1},
21
+ {:y=>0, :l=>1, :t=>"Paragraph 1.2", :p=>2}]
22
+
23
+ book = pdf.bookmark('Sub-Paragraph 1.2.1', 2, 10)
24
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
25
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1},
26
+ {:y=>0, :l=>1, :t=>"Paragraph 1.2", :p=>2},
27
+ {:y=>10, :l=>2, :t=>"Sub-Paragraph 1.2.1", :p=>2}]
28
+
29
+ pdf.add_page()
30
+
31
+ book = pdf.bookmark('Paragraph 1.3', 1, 0)
32
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
33
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1},
34
+ {:y=>0, :l=>1, :t=>"Paragraph 1.2", :p=>2},
35
+ {:y=>10, :l=>2, :t=>"Sub-Paragraph 1.2.1", :p=>2},
36
+ {:y=>0, :l=>1, :t=>"Paragraph 1.3", :p=>3}]
37
+
38
+ book = pdf.bookmark('Sub-Paragraph 1.1.1', 2, 0, 2)
39
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
40
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1},
41
+ {:y=>0, :l=>1, :t=>"Paragraph 1.2", :p=>2},
42
+ {:y=>10, :l=>2, :t=>"Sub-Paragraph 1.2.1", :p=>2},
43
+ {:y=>0, :l=>1, :t=>"Paragraph 1.3", :p=>3},
44
+ {:y=>0, :l=>2, :t=>"Sub-Paragraph 1.1.1", :p=>2}]
45
+
46
+ pdf.add_page()
47
+
48
+ book = pdf.bookmark('Paragraph 1.4', 1, 20)
49
+ assert_equal book, [{:y=>0, :l=>0, :t=>"Chapter 1", :p=>1},
50
+ {:y=>0, :l=>1, :t=>"Paragraph 1.1", :p=>1},
51
+ {:y=>0, :l=>1, :t=>"Paragraph 1.2", :p=>2},
52
+ {:y=>10, :l=>2, :t=>"Sub-Paragraph 1.2.1", :p=>2},
53
+ {:y=>0, :l=>1, :t=>"Paragraph 1.3", :p=>3},
54
+ {:y=>0, :l=>2, :t=>"Sub-Paragraph 1.1.1", :p=>2},
55
+ {:y=>20, :l=>1, :t=>"Paragraph 1.4", :p=>4}]
56
+ end
57
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class TcpdfTest < ActiveSupport::TestCase
4
+
5
+ class MYPDF < TCPDF
6
+ 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')
7
+ super
8
+ end
9
+ end
10
+
11
+ test "getCellCode" do
12
+ pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
13
+ pdf.add_page()
14
+ code = pdf.getCellCode(10)
15
+ assert_equal code, "0.57 w 0 J 0 j [] 0 d 0 G 0 g\n"
16
+ # 0.57 w 0 J 0 j [] 0 d 0 G 0 rg # getCellCode
17
+ end
18
+
19
+ test "getCellCode link url align test" do
20
+ pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
21
+ pdf.add_page()
22
+ content = []
23
+ contents = pdf.getCellCode(10, 10, 'abc', 'LTRB', 0, '', 0, 'http://example.com')
24
+ contents.each_line {|line| content.push line.chomp }
25
+
26
+ assert_equal content.length, 2
27
+ 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
+ # 28.35 813.82 m 28.35 784.91 l S
29
+ # 28.07 813.54 m 56.98 813.54 l S
30
+ # 56.70 813.82 m 56.70 784.91 l S
31
+ # 28.07 785.19 m 56.98 785.19 l S
32
+ # BT
33
+ # 31.19 795.17 Td
34
+ # 0 Tr 0.00 w
35
+ # [(abc)] TJ
36
+ # ET
37
+ end
38
+
39
+ test "getCellCode link page test" do
40
+ pdf = MYPDF.new('P', 'mm', 'A4', true, "UTF-8", true)
41
+ pdf.add_page()
42
+ content = []
43
+ contents = pdf.getCellCode(10, 10, 'abc', 0, 0, '', 0, 1)
44
+ contents.each_line {|line| content.push line.chomp }
45
+
46
+ assert_equal content.length, 2
47
+ assert_equal content[1], "BT 31.19 795.17 Td 0 Tr 0.00 w [(abc)] TJ ET"
48
+ # BT
49
+ # 31.19 795.17 Td
50
+ # 0 Tr 0.00 w
51
+ # [(abc)] TJ
52
+ # ET
53
+ end
54
+ end
@@ -0,0 +1,117 @@
1
+ require 'test_helper'
2
+
3
+ class TcpdfPageTest < ActiveSupport::TestCase
4
+ class MYPDF < TCPDF
5
+ def getPageBuffer(page)
6
+ super
7
+ end
8
+ end
9
+
10
+ test "Basic Page content test" do
11
+ pdf = MYPDF.new
12
+
13
+ page = pdf.get_page
14
+ assert_equal 0, page
15
+
16
+ width = pdf.get_page_width
17
+
18
+ pdf.set_print_header(false)
19
+ pdf.add_page
20
+ page = pdf.get_page
21
+ assert_equal 1, page
22
+
23
+ content = []
24
+ contents = pdf.getPageBuffer(page)
25
+ contents.each_line {|line| content.push line.chomp }
26
+
27
+ assert_equal content.length, 4
28
+ assert_equal content[0], "0.57 w 0 J 0 j [] 0 d 0 G 0 g"
29
+ assert_equal content[1], "BT /F1 12.00 Tf ET "
30
+ assert_equal content[2], "0.57 w 0 J 0 j [] 0 d 0 G 0 g"
31
+ assert_equal content[3], "BT /F1 12.00 Tf ET "
32
+
33
+ ##################################
34
+ # 0.57 w 0 J 0 j [] 0 d 0 G 0 g # add_page,start_page,setGraphicVars(set_fill_color)
35
+ # BT /F1 12.00 Tf ET #
36
+ # 0.57 w 0 J 0 j [] 0 d 0 G 0 g #
37
+ # BT /F1 12.00 Tf ET #
38
+ ##################################
39
+ # 0.57 w # @linestyle_width : Line width.
40
+ # 0 J # @linestyle_cap : Type of cap to put on the line. [butt:0, round:1, square:2]
41
+ # 0 j # @linestyle_join : Type of join. [miter:0, round:1, bevel:2]
42
+ # [] 0 d # @linestyle_dash : Line dash pattern. (see set_line_style)
43
+ # 0 G # @draw_color : Drawing color. (see set_draw_color)
44
+ # 0 g # Set colors
45
+ ########################
46
+ # BT # Begin Text.
47
+ # /F1 12.00 Tf # 12.00 point size font.
48
+ # ET # End Text.
49
+ ########################
50
+
51
+ pdf.set_font('freesans', 'BI', 18)
52
+ content = []
53
+ contents = pdf.getPageBuffer(page)
54
+ contents.each_line {|line| content.push line.chomp }
55
+
56
+ assert_equal content.length, 5
57
+ assert_equal content[4], "BT /F2 18.00 Tf ET "
58
+
59
+ ########################
60
+ # BT # Begin Text.
61
+ # /F2 18.00 Tf # 18.00 point size font.
62
+ # ET # End Text.
63
+ ########################
64
+ pdf.set_font('freesans', 'B', 20)
65
+ content = []
66
+ contents = pdf.getPageBuffer(page)
67
+ contents.each_line {|line| content.push line.chomp }
68
+
69
+ assert_equal content.length, 6
70
+ assert_equal content[5], "BT /F3 20.00 Tf ET "
71
+
72
+ pdf.cell(0, 10, 'Chapter', 0, 1, 'L')
73
+ content = []
74
+ contents = pdf.getPageBuffer(page)
75
+ contents.each_line {|line| content.push line.chomp }
76
+
77
+ assert_equal content.length, 8
78
+ assert_equal content[6], "0.57 w 0 J 0 j [] 0 d 0 G 0 g"
79
+
80
+ assert_equal content[7], "BT 31.19 792.37 Td 0 Tr 0.00 w [(\x00C\x00h\x00a\x00p\x00t\x00e\x00r)] TJ ET"
81
+
82
+ #################################################
83
+ # 0.57 w 0 J 0 j [] 0 d 0 G 0 g # getCellCode
84
+ # BT
85
+ # 31.19 792.37 Td # Set text offset.
86
+ # 0 Tr 0.00 w # Set stroke outline and clipping mode
87
+ # [(\x00C\x00h\x00a\x00p\x00t\x00e\x00r)] TJ # Write array of characters.
88
+ # ET
89
+ #################################################
90
+ end
91
+
92
+ test "circle content" do
93
+ pdf = MYPDF.new
94
+
95
+ pdf.set_print_header(false)
96
+ pdf.add_page
97
+ pdf.circle(100, 200, 50)
98
+ content = []
99
+ contents = pdf.getPageBuffer(1)
100
+ contents.each_line {|line| content.push line.chomp }
101
+
102
+ assert_equal content.length, 15
103
+ assert_equal content[4], "425.20 274.96 m" # start point : x0, y0
104
+
105
+ assert_equal content[5], '425.20 308.27 413.45 340.54 392.04 366.06 c' # 1/9 circle : x1, y1(control point 1), x2, y2(control point 2), x3, y3(end point and next start point)
106
+ assert_equal content[6], '370.62 391.58 340.88 408.76 308.08 414.54 c' # 2/9 circle
107
+ assert_equal content[7], '275.27 420.32 241.45 414.36 212.60 397.70 c' # 3/9 circle
108
+ assert_equal content[8], '183.75 381.05 161.67 354.74 150.28 323.44 c' # 4/9 circle
109
+ assert_equal content[9], '138.89 292.13 138.89 257.79 150.28 226.49 c' # 5/9 circle
110
+ assert_equal content[10], '161.67 195.18 183.75 168.87 212.60 152.22 c' # 6/9 circle
111
+ assert_equal content[11], '241.45 135.56 275.27 129.60 308.08 135.38 c' # 7/9 circle
112
+ assert_equal content[12], '340.88 141.17 370.62 158.34 392.04 183.86 c' # 8/9 circle
113
+ assert_equal content[13], '413.45 209.38 425.20 241.65 425.20 274.96 c' # 9/9 circle
114
+ assert_equal content[14], 'S'
115
+
116
+ end
117
+ end
@@ -0,0 +1,318 @@
1
+ require 'test_helper'
2
+
3
+ class TcpdfCssTest < ActiveSupport::TestCase
4
+ class MYPDF < TCPDF
5
+ def extractCSSproperties(cssdata)
6
+ super
7
+ end
8
+ def isValidCSSSelectorForTag(dom, key, selector)
9
+ super
10
+ end
11
+ def getTagStyleFromCSS(dom, key, css)
12
+ super
13
+ end
14
+ def getHtmlDomArray(html)
15
+ super
16
+ end
17
+ end
18
+
19
+ test "CSS Basic" do
20
+ pdf = MYPDF.new
21
+
22
+ # empty
23
+ css = pdf.extractCSSproperties('')
24
+ assert_equal css, {}
25
+ # empty blocks
26
+ css = pdf.extractCSSproperties('h1 {}')
27
+ assert_equal css, {}
28
+ # comment
29
+ css = pdf.extractCSSproperties('/* comment */')
30
+ assert_equal css, {}
31
+
32
+ css = pdf.extractCSSproperties('h1 { color: navy; font-family: times; }')
33
+ assert_equal css, {"0001 h1"=>"color:navy;font-family:times;"}
34
+
35
+ css = pdf.extractCSSproperties('h1 { color: navy; font-family: times; } p.first { color: #003300; font-family: helvetica; font-size: 12pt; }')
36
+ assert_equal css, {"0001 h1"=>"color:navy;font-family:times;", "0021 p.first"=>"color:#003300;font-family:helvetica;font-size:12pt;"}
37
+
38
+ css = pdf.extractCSSproperties('h1,h2,h3{background-color:#e0e0e0}')
39
+ assert_equal css, {"0001 h1"=>"background-color:#e0e0e0", "0001 h2"=>"background-color:#e0e0e0", "0001 h3"=>"background-color:#e0e0e0"}
40
+
41
+ css = pdf.extractCSSproperties('p.second { color: rgb(00,63,127); font-family: times; font-size: 12pt; text-align: justify; }')
42
+ assert_equal css, {"0011 p.second"=>"color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;"}
43
+
44
+ css = pdf.extractCSSproperties('p#second { color: rgb(00,63,127); font-family: times; font-size: 12pt; text-align: justify; }')
45
+ assert_equal css, {"0101 p#second"=>"color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;"}
46
+
47
+ css = pdf.extractCSSproperties('p.first { color: rgb(00,63,127); } p.second { font-family: times; }')
48
+ assert_equal css, {"0021 p.first"=>"color:rgb(00,63,127);", "0011 p.second"=>"font-family:times;"}
49
+
50
+ css = pdf.extractCSSproperties('p#first { color: rgb(00,63,127); } p#second { color: rgb(00,63,127); }')
51
+ assert_equal css, {"0111 p#first"=>"color:rgb(00,63,127);", "0101 p#second"=>"color:rgb(00,63,127);"}
52
+
53
+ # media
54
+ css = pdf.extractCSSproperties('@media print { body { font: 10pt serif } }')
55
+ assert_equal css, {"0001 body"=>"font:10pt serif"}
56
+ css = pdf.extractCSSproperties('@media screen { body { font: 12pt sans-serif } }')
57
+ assert_equal css, {}
58
+ css = pdf.extractCSSproperties('@media all { body { line-height: 1.2 } }')
59
+ assert_equal css, {"0001 body"=>"line-height:1.2"}
60
+
61
+ css = pdf.extractCSSproperties('@media print {
62
+ #top-menu, #header, #main-menu, #sidebar, #footer, .contextual, .other-formats { display:none; }
63
+ #main { background: #fff; }
64
+ #content { width: 99%; margin: 0; padding: 0; border: 0; background: #fff; overflow: visible !important;}
65
+ #wiki_add_attachment { display:none; }
66
+ .hide-when-print { display: none; }
67
+ .autoscroll {overflow-x: visible;}
68
+ table.list {margin-top:0.5em;}
69
+ table.list th, table.list td {border: 1px solid #aaa;}
70
+ } @media all { body { line-height: 1.2 } }')
71
+ assert_equal css, {"0100 #top-menu"=>"display:none;",
72
+ "0100 #header"=>"display:none;",
73
+ "0100 #main-menu"=>"display:none;",
74
+ "0100 #sidebar"=>"display:none;",
75
+ "0100 #footer"=>"display:none;",
76
+ "0010 .contextual"=>"display:none;",
77
+ "0010 .other-formats"=>"display:none;",
78
+ "0100 #main"=>"background:#fff;",
79
+ "0100 #content"=>"width:99%;margin:0;padding:0;border:0;background:#fff;overflow:visible !important;",
80
+ "0100 #wiki_add_attachment"=>"display:none;",
81
+ "0010 .hide-when-print"=>"display:none;",
82
+ "0010 .autoscroll"=>"overflow-x:visible;",
83
+ "0011 table.list"=>"margin-top:0.5em;",
84
+ "0012 table.list th"=>"border:1px solid #aaa;",
85
+ "0012 table.list td"=>"border:1px solid #aaa;",
86
+ "0001 body"=>"line-height:1.2"}
87
+ end
88
+
89
+ test "CSS Selector Valid test" do
90
+ pdf = MYPDF.new
91
+
92
+ # Simple CSS
93
+ dom = pdf.getHtmlDomArray('<p>abc</p>')
94
+ assert_equal dom.length, 4
95
+ valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p') # dom, key, css selector
96
+ assert_equal valid, true
97
+
98
+ dom = pdf.getHtmlDomArray('<h1>abc</h1>')
99
+ assert_equal dom.length, 4
100
+ valid = pdf.isValidCSSSelectorForTag(dom, 1, ' h1') # dom, key, css selector
101
+ assert_equal valid, true
102
+
103
+ dom = pdf.getHtmlDomArray('<p class="first">abc</p>')
104
+ assert_equal dom.length, 4
105
+ valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p.first') # dom, key, css selector
106
+ assert_equal valid, true
107
+
108
+ dom = pdf.getHtmlDomArray('<p class="first">abc<span>def</span></p>')
109
+ assert_equal dom.length, 7
110
+ valid = pdf.isValidCSSSelectorForTag(dom, 3, ' p.first span') # dom, key, css selector
111
+ assert_equal valid, true
112
+
113
+ dom = pdf.getHtmlDomArray('<p id="second">abc</p>')
114
+ assert_equal dom.length, 4
115
+ valid = pdf.isValidCSSSelectorForTag(dom, 1, ' p#second') # dom, key, css selector
116
+ assert_equal valid, true
117
+
118
+ dom = pdf.getHtmlDomArray('<p id="second">abc<span>def</span></p>')
119
+ assert_equal dom.length, 7
120
+ valid = pdf.isValidCSSSelectorForTag(dom, 3, ' p#second > span') # dom, key, css selector
121
+ assert_equal valid, true
122
+ end
123
+
124
+ test "CSS Tag Sytle test 1" do
125
+ pdf = MYPDF.new
126
+
127
+ # Simple CSS
128
+ dom = pdf.getHtmlDomArray('<h1>abc</h1>')
129
+ assert_equal dom.length, 4
130
+
131
+ tag = pdf.getTagStyleFromCSS(dom, 1, {'0001 h1'=>'color:navy;font-family:times;'}) # dom, key, css selector
132
+ assert_equal tag, ';color:navy;font-family:times;'
133
+
134
+ tag = pdf.getTagStyleFromCSS(dom, 1, {'0001h1'=>'color:navy;font-family:times;'}) # dom, key, css selector
135
+ assert_equal tag, ''
136
+
137
+ tag = pdf.getTagStyleFromCSS(dom, 1, {'0001 h2'=>'color:navy;font-family:times;'}) # dom, key, css selector
138
+ assert_equal tag, ''
139
+ end
140
+
141
+ test "CSS Tag Sytle test 2" do
142
+ pdf = MYPDF.new
143
+
144
+ dom = pdf.getHtmlDomArray('<p class="first">abc</p>')
145
+ assert_equal dom.length, 4
146
+
147
+ tag = pdf.getTagStyleFromCSS(dom, 1, {'0021 p.first'=>'color:rgb(00,63,127);'})
148
+ assert_equal tag, ';color:rgb(00,63,127);'
149
+
150
+ dom = pdf.getHtmlDomArray('<p id="second">abc</p>')
151
+ assert_equal dom.length, 4
152
+
153
+ tag = pdf.getTagStyleFromCSS(dom, 1, {'0101 p#second'=>'color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;'})
154
+ assert_equal tag, ';color:rgb(00,63,127);font-family:times;font-size:12pt;text-align:justify;'
155
+ end
156
+
157
+ test "CSS Dom test" do
158
+ pdf = MYPDF.new
159
+
160
+ html = '<style> table, td { border: 2px #ff0000 solid; } </style>
161
+ <h2>HTML TABLE:</h2>
162
+ <table> <tr> <th>abc</th> </tr>
163
+ <tr> <td>def</td> </tr> </table>'
164
+ dom = pdf.getHtmlDomArray(html)
165
+ ## remove style tag block (by getHtmlDomArray()) ##
166
+ ## added marker tag (by getHtmlDomArray()) ##
167
+ # '<h2>HTML TABLE:</h2>
168
+ # <table><tr><th>abc<marker style="font-size:0"/></th></tr>
169
+ # <tr><td>def<marker style="font-size:0"/></td></tr></table>'
170
+ assert_equal dom.length, 18
171
+
172
+ assert_equal dom[0]['parent'], 0 # Root
173
+ assert_equal dom[0]['tag'], false
174
+ assert_equal dom[0]['attribute'], {}
175
+
176
+ # <h2>
177
+ assert_equal dom[1]['elkey'], 0
178
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
179
+ assert_equal dom[1]['tag'], true
180
+ assert_equal dom[1]['opening'], true
181
+ assert_equal dom[1]['value'], 'h2'
182
+
183
+ # <table>
184
+ assert_equal dom[4]['value'], 'table'
185
+ assert_equal dom[4]['attribute'], {'border'=>'2px #ff0000 solid', 'style'=>';border:2px #ff0000 solid;'}
186
+ assert_equal dom[4]['style']['border'], '2px #ff0000 solid'
187
+ assert_equal dom[4]['attribute']['border'], '2px #ff0000 solid'
188
+ end
189
+
190
+ test "CSS Dom line-height test normal" do
191
+ pdf = MYPDF.new
192
+
193
+ html = '<style> h2 { line-height: normal; } </style>
194
+ <h2>HTML TEST</h2>'
195
+ dom = pdf.getHtmlDomArray(html)
196
+ ## remove style tag block (by getHtmlDomArray()) ##
197
+ # '<h2>HTML TEST</h2>'
198
+ assert_equal dom.length, 4
199
+
200
+ # <h2>
201
+ assert_equal dom[1]['elkey'], 0
202
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
203
+ assert_equal dom[1]['tag'], true
204
+ assert_equal dom[1]['opening'], true
205
+ assert_equal dom[1]['value'], 'h2'
206
+ assert_equal dom[1]['line-height'], 1.25
207
+ end
208
+
209
+ test "CSS Dom line-height test numeric" do
210
+ pdf = MYPDF.new
211
+
212
+ html = '<style> h2 { line-height: 1.4; } </style>
213
+ <h2>HTML TEST</h2>'
214
+ dom = pdf.getHtmlDomArray(html)
215
+ ## remove style tag block (by getHtmlDomArray()) ##
216
+ # '<h2>HTML TEST</h2>'
217
+ assert_equal dom.length, 4
218
+
219
+ # <h2>
220
+ assert_equal dom[1]['elkey'], 0
221
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
222
+ assert_equal dom[1]['tag'], true
223
+ assert_equal dom[1]['opening'], true
224
+ assert_equal dom[1]['value'], 'h2'
225
+ assert_equal dom[1]['line-height'], 1.4
226
+ end
227
+
228
+ test "CSS Dom line-height test percentage" do
229
+ pdf = MYPDF.new
230
+
231
+ html = '<style> h2 { line-height: 10%; } </style>
232
+ <h2>HTML TEST</h2>'
233
+ dom = pdf.getHtmlDomArray(html)
234
+ ## remove style tag block (by getHtmlDomArray()) ##
235
+ # '<h2>HTML TEST</h2>'
236
+ assert_equal dom.length, 4
237
+
238
+ # <h2>
239
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
240
+ assert_equal dom[1]['elkey'], 0
241
+ assert_equal dom[1]['tag'], true
242
+ assert_equal dom[1]['opening'], true
243
+ assert_equal dom[1]['value'], 'h2'
244
+ assert_equal dom[1]['line-height'], 0.1
245
+ end
246
+
247
+ test "CSS Dom class test" do
248
+ pdf = MYPDF.new
249
+
250
+ html = '<style>p.first { color: #003300; font-family: helvetica; font-size: 12pt; }
251
+ p.first span { color: #006600; font-style: italic; }</style>
252
+ <p class="first">Example <span>Fusce</span></p>'
253
+ dom = pdf.getHtmlDomArray(html)
254
+ ## remove style tag block (by getHtmlDomArray()) ##
255
+ # '<p class="first">Example <span>Fusce</span></p>'
256
+ assert_equal dom.length, 7
257
+
258
+ # <p class="first">
259
+ assert_equal dom[1]['elkey'], 0
260
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
261
+ assert_equal dom[1]['tag'], true
262
+ assert_equal dom[1]['opening'], true
263
+ assert_equal dom[1]['value'], 'p'
264
+ assert_equal dom[1]['attribute']['class'], 'first'
265
+ assert_equal dom[1]['style']['color'], '#003300'
266
+ assert_equal dom[1]['style']['font-family'], 'helvetica'
267
+ assert_equal dom[1]['style']['font-size'], '12pt'
268
+
269
+ # Example
270
+ assert_equal dom[2]['elkey'], 1
271
+ assert_equal dom[2]['parent'], 1
272
+ assert_equal dom[2]['tag'], false
273
+ assert_equal dom[2]['value'], 'Example '
274
+
275
+ # <span>
276
+ assert_equal dom[3]['elkey'], 2
277
+ assert_equal dom[3]['parent'], 1
278
+ assert_equal dom[3]['tag'], true
279
+ assert_equal dom[3]['opening'], true
280
+ assert_equal dom[3]['value'], 'span'
281
+ assert_equal dom[3]['style']['color'], '#006600'
282
+ assert_equal dom[3]['style']['font-style'], 'italic'
283
+
284
+ end
285
+
286
+ test "CSS Dom id test" do
287
+ pdf = MYPDF.new
288
+
289
+ html = '<style> p#second > span { background-color: #FFFFAA; }</style>
290
+ <p id="second">Example <span>Fusce</span></p>'
291
+ dom = pdf.getHtmlDomArray(html)
292
+ ## remove style tag block (by getHtmlDomArray()) ##
293
+ # '<p id="second">Example <span>Fusce</span></p>'
294
+ assert_equal dom.length, 7
295
+
296
+ # <p id="second">
297
+ assert_equal dom[1]['elkey'], 0
298
+ assert_equal dom[1]['parent'], 0 # parent -> parent tag key
299
+ assert_equal dom[1]['tag'], true
300
+ assert_equal dom[1]['opening'], true
301
+ assert_equal dom[1]['value'], 'p'
302
+ assert_equal dom[1]['attribute']['id'], 'second'
303
+
304
+ # Example
305
+ assert_equal dom[2]['elkey'], 1
306
+ assert_equal dom[2]['parent'], 1
307
+ assert_equal dom[2]['tag'], false
308
+ assert_equal dom[2]['value'], 'Example '
309
+
310
+ # <span>
311
+ assert_equal dom[3]['elkey'], 2
312
+ assert_equal dom[3]['parent'], 1
313
+ assert_equal dom[3]['tag'], true
314
+ assert_equal dom[3]['opening'], true
315
+ assert_equal dom[3]['value'], 'span'
316
+ assert_equal dom[3]['style']['background-color'], '#FFFFAA'
317
+ end
318
+ end