davebenvenuti-prawn 0.11.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +50 -0
  3. data/LICENSE +56 -0
  4. data/README +141 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/page_without_mediabox.pdf +193 -0
  62. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  63. data/data/pdfs/two_hexagons.pdf +90 -0
  64. data/data/pdfs/version_1_6.pdf +61 -0
  65. data/data/shift_jis_text.txt +1 -0
  66. data/examples/bounding_box/bounding_boxes.rb +44 -0
  67. data/examples/bounding_box/indentation.rb +35 -0
  68. data/examples/bounding_box/russian_boxes.rb +37 -0
  69. data/examples/bounding_box/stretched_nesting.rb +68 -0
  70. data/examples/example_helper.rb +8 -0
  71. data/examples/general/background.rb +24 -0
  72. data/examples/general/canvas.rb +16 -0
  73. data/examples/general/context_sensitive_headers.rb +38 -0
  74. data/examples/general/float.rb +12 -0
  75. data/examples/general/margin.rb +37 -0
  76. data/examples/general/measurement_units.rb +52 -0
  77. data/examples/general/metadata-info.rb +17 -0
  78. data/examples/general/multi_page_layout.rb +19 -0
  79. data/examples/general/outlines.rb +67 -0
  80. data/examples/general/page_geometry.rb +32 -0
  81. data/examples/general/page_numbering.rb +16 -0
  82. data/examples/general/repeaters.rb +48 -0
  83. data/examples/general/stamp.rb +42 -0
  84. data/examples/general/templates.rb +14 -0
  85. data/examples/graphics/basic_images.rb +24 -0
  86. data/examples/graphics/cmyk.rb +13 -0
  87. data/examples/graphics/curves.rb +12 -0
  88. data/examples/graphics/hexagon.rb +14 -0
  89. data/examples/graphics/image_fit.rb +16 -0
  90. data/examples/graphics/image_flow.rb +38 -0
  91. data/examples/graphics/image_position.rb +18 -0
  92. data/examples/graphics/line.rb +33 -0
  93. data/examples/graphics/png_types.rb +23 -0
  94. data/examples/graphics/polygons.rb +17 -0
  95. data/examples/graphics/remote_images.rb +13 -0
  96. data/examples/graphics/rounded_polygons.rb +20 -0
  97. data/examples/graphics/rounded_rectangle.rb +21 -0
  98. data/examples/graphics/ruport_style_helpers.rb +20 -0
  99. data/examples/graphics/stroke_bounds.rb +21 -0
  100. data/examples/graphics/stroke_cap_and_join.rb +46 -0
  101. data/examples/graphics/stroke_dash.rb +43 -0
  102. data/examples/graphics/transformations.rb +53 -0
  103. data/examples/graphics/transparency.rb +27 -0
  104. data/examples/grid/bounding_boxes.rb +22 -0
  105. data/examples/grid/column_gutter_grid.rb +21 -0
  106. data/examples/grid/multi_boxes.rb +52 -0
  107. data/examples/grid/show_grid.rb +14 -0
  108. data/examples/grid/simple_grid.rb +21 -0
  109. data/examples/m17n/chinese_text_wrapping.rb +18 -0
  110. data/examples/m17n/euro.rb +16 -0
  111. data/examples/m17n/sjis.rb +29 -0
  112. data/examples/m17n/utf8.rb +14 -0
  113. data/examples/m17n/win_ansi_charset.rb +55 -0
  114. data/examples/security/hello_foo.rb +9 -0
  115. data/examples/table/bill.rb +54 -0
  116. data/examples/table/cell.rb +13 -0
  117. data/examples/table/checkerboard.rb +23 -0
  118. data/examples/table/header.rb +15 -0
  119. data/examples/table/inline_format_table.rb +13 -0
  120. data/examples/table/multi_page_table.rb +10 -0
  121. data/examples/table/simple_table.rb +25 -0
  122. data/examples/table/subtable.rb +13 -0
  123. data/examples/table/widths.rb +21 -0
  124. data/examples/text/alignment.rb +19 -0
  125. data/examples/text/character_spacing.rb +13 -0
  126. data/examples/text/dfont.rb +49 -0
  127. data/examples/text/family_based_styling.rb +25 -0
  128. data/examples/text/font_calculations.rb +92 -0
  129. data/examples/text/font_size.rb +34 -0
  130. data/examples/text/hyphenation.rb +46 -0
  131. data/examples/text/indent_paragraphs.rb +23 -0
  132. data/examples/text/inline_format.rb +104 -0
  133. data/examples/text/kerning.rb +31 -0
  134. data/examples/text/rotated.rb +99 -0
  135. data/examples/text/shaped_text_box.rb +32 -0
  136. data/examples/text/simple_text.rb +18 -0
  137. data/examples/text/simple_text_ttf.rb +18 -0
  138. data/examples/text/span.rb +30 -0
  139. data/examples/text/text_box.rb +89 -0
  140. data/examples/text/text_box_returning_excess.rb +52 -0
  141. data/examples/text/text_flow.rb +68 -0
  142. data/lib/prawn.rb +26 -0
  143. data/lib/prawn/compatibility.rb +51 -0
  144. data/lib/prawn/core.rb +85 -0
  145. data/lib/prawn/core/annotations.rb +61 -0
  146. data/lib/prawn/core/byte_string.rb +9 -0
  147. data/lib/prawn/core/destinations.rb +90 -0
  148. data/lib/prawn/core/document_state.rb +78 -0
  149. data/lib/prawn/core/literal_string.rb +16 -0
  150. data/lib/prawn/core/name_tree.rb +165 -0
  151. data/lib/prawn/core/object_store.rb +236 -0
  152. data/lib/prawn/core/page.rb +199 -0
  153. data/lib/prawn/core/pdf_object.rb +108 -0
  154. data/lib/prawn/core/reference.rb +112 -0
  155. data/lib/prawn/core/text.rb +140 -0
  156. data/lib/prawn/core/text/formatted/arranger.rb +266 -0
  157. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  158. data/lib/prawn/core/text/formatted/wrap.rb +112 -0
  159. data/lib/prawn/core/text/line_wrap.rb +211 -0
  160. data/lib/prawn/core/text/wrap.rb +82 -0
  161. data/lib/prawn/document.rb +575 -0
  162. data/lib/prawn/document/bounding_box.rb +428 -0
  163. data/lib/prawn/document/graphics_state.rb +48 -0
  164. data/lib/prawn/document/internals.rb +170 -0
  165. data/lib/prawn/document/page_geometry.rb +136 -0
  166. data/lib/prawn/document/snapshot.rb +87 -0
  167. data/lib/prawn/document/span.rb +55 -0
  168. data/lib/prawn/encoding.rb +121 -0
  169. data/lib/prawn/errors.rb +86 -0
  170. data/lib/prawn/font.rb +368 -0
  171. data/lib/prawn/font/afm.rb +225 -0
  172. data/lib/prawn/font/dfont.rb +42 -0
  173. data/lib/prawn/font/ttf.rb +350 -0
  174. data/lib/prawn/graphics.rb +325 -0
  175. data/lib/prawn/graphics/cap_style.rb +38 -0
  176. data/lib/prawn/graphics/color.rb +205 -0
  177. data/lib/prawn/graphics/dash.rb +71 -0
  178. data/lib/prawn/graphics/join_style.rb +38 -0
  179. data/lib/prawn/graphics/transformation.rb +156 -0
  180. data/lib/prawn/graphics/transparency.rb +99 -0
  181. data/lib/prawn/images.rb +217 -0
  182. data/lib/prawn/images/jpg.rb +85 -0
  183. data/lib/prawn/images/png.rb +356 -0
  184. data/lib/prawn/layout.rb +20 -0
  185. data/lib/prawn/layout/grid.rb +259 -0
  186. data/lib/prawn/measurement_extensions.rb +46 -0
  187. data/lib/prawn/measurements.rb +71 -0
  188. data/lib/prawn/outline.rb +326 -0
  189. data/lib/prawn/repeater.rb +129 -0
  190. data/lib/prawn/security.rb +262 -0
  191. data/lib/prawn/security/arcfour.rb +51 -0
  192. data/lib/prawn/stamp.rb +126 -0
  193. data/lib/prawn/table.rb +451 -0
  194. data/lib/prawn/table/cell.rb +395 -0
  195. data/lib/prawn/table/cell/in_table.rb +27 -0
  196. data/lib/prawn/table/cell/subtable.rb +65 -0
  197. data/lib/prawn/table/cell/text.rb +135 -0
  198. data/lib/prawn/table/cells.rb +206 -0
  199. data/lib/prawn/text.rb +449 -0
  200. data/lib/prawn/text/box.rb +397 -0
  201. data/lib/prawn/text/formatted.rb +4 -0
  202. data/lib/prawn/text/formatted/box.rb +222 -0
  203. data/lib/prawn/text/formatted/fragment.rb +181 -0
  204. data/lib/prawn/text/formatted/parser.rb +213 -0
  205. data/prawn.gemspec +28 -0
  206. data/spec/annotations_spec.rb +90 -0
  207. data/spec/bounding_box_spec.rb +190 -0
  208. data/spec/cell_spec.rb +430 -0
  209. data/spec/destinations_spec.rb +15 -0
  210. data/spec/document_spec.rb +476 -0
  211. data/spec/extensions/mocha.rb +32 -0
  212. data/spec/font_spec.rb +324 -0
  213. data/spec/formatted_text_arranger_spec.rb +426 -0
  214. data/spec/formatted_text_box_spec.rb +756 -0
  215. data/spec/formatted_text_fragment_spec.rb +211 -0
  216. data/spec/graphics_spec.rb +446 -0
  217. data/spec/grid_spec.rb +85 -0
  218. data/spec/images_spec.rb +119 -0
  219. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  220. data/spec/jpg_spec.rb +25 -0
  221. data/spec/line_wrap_spec.rb +341 -0
  222. data/spec/measurement_units_spec.rb +23 -0
  223. data/spec/name_tree_spec.rb +112 -0
  224. data/spec/object_store_spec.rb +160 -0
  225. data/spec/outline_spec.rb +404 -0
  226. data/spec/pdf_object_spec.rb +170 -0
  227. data/spec/png_spec.rb +237 -0
  228. data/spec/reference_spec.rb +82 -0
  229. data/spec/repeater_spec.rb +96 -0
  230. data/spec/security_spec.rb +120 -0
  231. data/spec/snapshot_spec.rb +154 -0
  232. data/spec/span_spec.rb +49 -0
  233. data/spec/spec_helper.rb +34 -0
  234. data/spec/stamp_spec.rb +108 -0
  235. data/spec/stroke_styles_spec.rb +163 -0
  236. data/spec/table_spec.rb +687 -0
  237. data/spec/template_spec.rb +165 -0
  238. data/spec/text_at_spec.rb +125 -0
  239. data/spec/text_box_spec.rb +777 -0
  240. data/spec/text_spacing_spec.rb +75 -0
  241. data/spec/text_spec.rb +349 -0
  242. data/spec/text_with_inline_formatting_spec.rb +193 -0
  243. data/spec/transparency_spec.rb +89 -0
  244. data/vendor/pdf-inspector/README +18 -0
  245. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  246. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  247. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  248. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  249. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  250. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  251. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  252. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  253. data/vendor/ttfunk/example.rb +45 -0
  254. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  255. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  256. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  257. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  258. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  260. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  261. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  262. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  263. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  264. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  265. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  266. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  267. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  274. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  275. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  276. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  277. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  278. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  279. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  280. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  281. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  282. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  283. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  284. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  285. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  286. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  287. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  288. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  289. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  290. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  291. metadata +379 -0
@@ -0,0 +1,165 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
2
+
3
+ describe "Document built from a template" do
4
+ it "should have the same page count as the source document" do
5
+ filename = "#{Prawn::BASEDIR}/reference_pdfs/curves.pdf"
6
+ @pdf = Prawn::Document.new(:template => filename)
7
+ page_counter = PDF::Inspector::Page.analyze(@pdf.render)
8
+
9
+ page_counter.pages.size.should == 1
10
+ end
11
+
12
+ it "should have start with the Y cursor at the top of the document" do
13
+ filename = "#{Prawn::BASEDIR}/reference_pdfs/curves.pdf"
14
+
15
+ @pdf = Prawn::Document.new(:template => filename)
16
+ (@pdf.y == nil).should == false
17
+ end
18
+
19
+ it "should respect margins set by Prawn" do
20
+ filename = "#{Prawn::BASEDIR}/reference_pdfs/curves.pdf"
21
+
22
+ @pdf = Prawn::Document.new(:template => filename, :margin => 0)
23
+ assert_equal @pdf.page.margins, { :left => 0,
24
+ :right => 0,
25
+ :top => 0,
26
+ :bottom => 0 }
27
+
28
+ @pdf = Prawn::Document.new(:template => filename, :left_margin => 0)
29
+
30
+ assert_equal @pdf.page.margins, { :left => 0,
31
+ :right => 36,
32
+ :top => 36,
33
+ :bottom => 36 }
34
+
35
+ @pdf.start_new_page(:right_margin => 0)
36
+
37
+ assert_equal @pdf.page.margins, { :left => 0,
38
+ :right => 0,
39
+ :top => 36,
40
+ :bottom => 36 }
41
+
42
+
43
+
44
+ end
45
+
46
+ it "should not add an extra restore_graphics_state operator to the end of any content stream" do
47
+ filename = "#{Prawn::BASEDIR}/reference_pdfs/curves.pdf"
48
+
49
+ @pdf = Prawn::Document.new(:template => filename)
50
+ output = StringIO.new(@pdf.render)
51
+ hash = PDF::Hash.new(output)
52
+
53
+ hash.each_value do |obj|
54
+ next unless obj.kind_of?(PDF::Reader::Stream)
55
+
56
+ data = obj.data.tr(" \n\r","")
57
+ data.include?("QQ").should == false
58
+ end
59
+ end
60
+
61
+ it "should have a single page object if importing a single page template" do
62
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
63
+
64
+ @pdf = Prawn::Document.new(:template => filename)
65
+ output = StringIO.new(@pdf.render)
66
+ hash = PDF::Hash.new(output)
67
+
68
+ pages = hash.values.select { |obj| obj.kind_of?(Hash) && obj[:Type] == :Page }
69
+
70
+ pages.size.should == 1
71
+ end
72
+
73
+ it "should have two content streams if importing a single page template" do
74
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
75
+
76
+ @pdf = Prawn::Document.new(:template => filename)
77
+ output = StringIO.new(@pdf.render)
78
+ hash = PDF::Hash.new(output)
79
+
80
+ streams = hash.values.select { |obj| obj.kind_of?(PDF::Reader::Stream) }
81
+
82
+ streams.size.should == 2
83
+ end
84
+
85
+ it "should not die if using this PDF as a template" do
86
+ filename = "#{Prawn::BASEDIR}/data/pdfs/complex_template.pdf"
87
+
88
+ assert_nothing_raised do
89
+ @pdf = Prawn::Document.new(:template => filename)
90
+ end
91
+ end
92
+
93
+
94
+ it "should have balance q/Q operators on all content streams" do
95
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
96
+
97
+ @pdf = Prawn::Document.new(:template => filename)
98
+ output = StringIO.new(@pdf.render)
99
+ hash = PDF::Hash.new(output)
100
+
101
+ streams = hash.values.select { |obj| obj.kind_of?(PDF::Reader::Stream) }
102
+
103
+ streams.each do |stream|
104
+ data = stream.unfiltered_data
105
+ data.scan("q").size.should == 1
106
+ data.scan("Q").size.should == 1
107
+ end
108
+ end
109
+
110
+ it "should allow text to be added to a single page template" do
111
+ filename = "#{Prawn::BASEDIR}/data/pdfs/hexagon.pdf"
112
+
113
+ @pdf = Prawn::Document.new(:template => filename)
114
+
115
+ @pdf.text "Adding some text"
116
+
117
+ text = PDF::Inspector::Text.analyze(@pdf.render)
118
+ text.strings.first.should == "Adding some text"
119
+ end
120
+
121
+ it "should allow PDFs with page resources behind an indirect object to be used as templates" do
122
+ filename = "#{Prawn::BASEDIR}/data/pdfs/resources_as_indirect_object.pdf"
123
+
124
+ @pdf = Prawn::Document.new(:template => filename)
125
+
126
+ @pdf.text "Adding some text"
127
+
128
+ text = PDF::Inspector::Text.analyze(@pdf.render)
129
+ all_text = text.strings.join("")
130
+ all_text.include?("Adding some text").should == true
131
+ end
132
+
133
+ it "should copy the PDF version from the template file" do
134
+ filename = "#{Prawn::BASEDIR}/data/pdfs/version_1_6.pdf"
135
+
136
+ @pdf = Prawn::Document.new(:template => filename)
137
+ str = @pdf.render
138
+ str[0,8].should == "%PDF-1.6"
139
+ end
140
+
141
+ it "should correctly add a TTF font to a template that has existing fonts" do
142
+ filename = "#{Prawn::BASEDIR}/data/pdfs/contains_ttf_font.pdf"
143
+ @pdf = Prawn::Document.new(:template => filename)
144
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/Chalkboard.ttf"
145
+ @pdf.move_down(40)
146
+ @pdf.text "Hi There"
147
+
148
+ output = StringIO.new(@pdf.render)
149
+ hash = PDF::Hash.new(output)
150
+
151
+ page_dict = hash.values.detect{ |obj| obj.is_a?(Hash) && obj[:Type] == :Page }
152
+ resources = page_dict[:Resources]
153
+ fonts = resources[:Font]
154
+ fonts.size.should == 2
155
+ end
156
+
157
+ it "should correctly import a template file that is missing a MediaBox entry" do
158
+ filename = "#{Prawn::BASEDIR}/data/pdfs/page_without_mediabox.pdf"
159
+
160
+ @pdf = Prawn::Document.new(:template => filename)
161
+ str = @pdf.render
162
+ str[0,4].should == "%PDF"
163
+ end
164
+
165
+ end
@@ -0,0 +1,125 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "#draw_text" do
6
+ before(:each) { create_pdf }
7
+
8
+ it "should raise ArgumentError if :at option omitted" do
9
+ lambda { @pdf.draw_text("hai", { }) }.should.raise(ArgumentError)
10
+ end
11
+
12
+ it "should raise ArgumentError if :align option included" do
13
+ lambda { @pdf.draw_text("hai", :at => [0, 0], :align => :center) }.should.raise(ArgumentError)
14
+ end
15
+
16
+ it "should allow drawing empty strings to the page" do
17
+ @pdf.draw_text(" ", :at => [100,100])
18
+ text = PDF::Inspector::Text.analyze(@pdf.render)
19
+ text.strings.first.should == " "
20
+ end
21
+
22
+ it "should default to 12 point helvetica" do
23
+ @pdf.draw_text("Blah", :at => [100,100])
24
+ text = PDF::Inspector::Text.analyze(@pdf.render)
25
+ text.font_settings[0][:name].should == :Helvetica
26
+ text.font_settings[0][:size].should == 12
27
+ text.strings.first.should == "Blah"
28
+ end
29
+
30
+ it "should allow setting font size" do
31
+ @pdf.draw_text("Blah", :at => [100,100], :size => 16)
32
+ text = PDF::Inspector::Text.analyze(@pdf.render)
33
+ text.font_settings[0][:size].should == 16
34
+ end
35
+
36
+ it "should allow setting a default font size" do
37
+ @pdf.font_size = 16
38
+ @pdf.draw_text("Blah", :at => [0, 0])
39
+ text = PDF::Inspector::Text.analyze(@pdf.render)
40
+ text.font_settings[0][:size].should == 16
41
+ end
42
+
43
+ it "should allow overriding default font for a single instance" do
44
+ @pdf.font_size = 16
45
+
46
+ @pdf.draw_text("Blah", :size => 11, :at => [0, 0])
47
+ @pdf.draw_text("Blaz", :at => [0, 0])
48
+ text = PDF::Inspector::Text.analyze(@pdf.render)
49
+ text.font_settings[0][:size].should == 11
50
+ text.font_settings[1][:size].should == 16
51
+ end
52
+
53
+ it "should allow setting a font size transaction with a block" do
54
+ @pdf.font_size 16 do
55
+ @pdf.draw_text('Blah', :at => [0, 0])
56
+ end
57
+
58
+ @pdf.draw_text('blah', :at => [0, 0])
59
+
60
+ text = PDF::Inspector::Text.analyze(@pdf.render)
61
+ text.font_settings[0][:size].should == 16
62
+ text.font_settings[1][:size].should == 12
63
+ end
64
+
65
+ it "should allow manual setting the font size " +
66
+ "when in a font size block" do
67
+ @pdf.font_size(16) do
68
+ @pdf.draw_text('Foo', :at => [0, 0])
69
+ @pdf.draw_text('Blah', :size => 11, :at => [0, 0])
70
+ @pdf.draw_text('Blaz', :at => [0, 0])
71
+ end
72
+ text = PDF::Inspector::Text.analyze(@pdf.render)
73
+ text.font_settings[0][:size].should == 16
74
+ text.font_settings[1][:size].should == 11
75
+ text.font_settings[2][:size].should == 16
76
+ end
77
+
78
+ it "should allow registering of built-in font_settings on the fly" do
79
+ @pdf.font "Times-Roman"
80
+ @pdf.draw_text("Blah", :at => [100,100], :at => [0, 0])
81
+ @pdf.font "Courier"
82
+ @pdf.draw_text("Blaz", :at => [150,150], :at => [0, 0])
83
+ text = PDF::Inspector::Text.analyze(@pdf.render)
84
+ text.font_settings[0][:name].should == :"Times-Roman"
85
+ text.font_settings[1][:name].should == :Courier
86
+ end
87
+
88
+ it "should raise an exception when an unknown font is used" do
89
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
90
+ end
91
+
92
+ it "should correctly render a utf-8 string when using a built-in font" do
93
+ str = "©" # copyright symbol
94
+ @pdf.draw_text(str, :at => [0, 0])
95
+
96
+ # grab the text from the rendered PDF and ensure it matches
97
+ text = PDF::Inspector::Text.analyze(@pdf.render)
98
+ text.strings.first.should == str
99
+ end
100
+
101
+ if "spec".respond_to?(:encode!)
102
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
103
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
104
+ str = "Blah \xDD"
105
+ str.force_encoding("ASCII-8BIT")
106
+ lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(ArgumentError)
107
+ end
108
+ it "should not raise an exception when a shift-jis string is rendered" do
109
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
110
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
111
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
112
+ lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.not.raise(ArgumentError)
113
+ end
114
+ else
115
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
116
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
117
+ str = "Blah \xDD"
118
+ lambda { @pdf.draw_text(str, :at => [0, 0]) }.should.raise(ArgumentError)
119
+ end
120
+ it "should raise an exception when a shift-jis string is rendered" do
121
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
122
+ lambda { @pdf.draw_text(sjis_str, :at => [0, 0]) }.should.raise(ArgumentError)
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,777 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "Text::Box" do
6
+
7
+ it "should be able to set leading document wide" do
8
+ create_pdf
9
+ @pdf.default_leading(7)
10
+ @pdf.default_leading = 7
11
+ text_box = Prawn::Text::Box.new("hello world",
12
+ :document => @pdf)
13
+ text_box.leading.should == 7
14
+ end
15
+
16
+ it "option should be able to override document wide leading" do
17
+ create_pdf
18
+ @pdf.default_leading = 7
19
+ text_box = Prawn::Text::Box.new("hello world",
20
+ :document => @pdf,
21
+ :leading => 20)
22
+ text_box.leading.should == 20
23
+ end
24
+ it "should default to document-wide leading if no" +
25
+ "leading option is provided" do
26
+
27
+ end
28
+ end
29
+
30
+ describe "Text::Box#extensions" do
31
+ it "should be able to override default line wrapping" do
32
+ create_pdf
33
+ Prawn::Text::Box.extensions << TestWrapOverride
34
+ @pdf.text_box("hello world", {})
35
+ text = PDF::Inspector::Text.analyze(@pdf.render)
36
+ text.strings[0].should == "all your base are belong to us"
37
+ Prawn::Text::Box.extensions.delete(TestWrapOverride)
38
+ end
39
+ end
40
+
41
+ describe "Text::Box#render with :align => :justify" do
42
+ it "should draw the word spacing to the document" do
43
+ create_pdf
44
+ string = "hello world " * 10
45
+ options = { :document => @pdf, :align => :justify }
46
+ text_box = Prawn::Text::Box.new(string, options)
47
+ text_box.render
48
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
49
+ contents.word_spacing[0].should.be > 0
50
+ end
51
+ end
52
+
53
+ describe "Text::Box#height without leading" do
54
+ it "should equal the sum of the height of each line" do
55
+ create_pdf
56
+ text = "Oh hai text rect.\nOh hai text rect."
57
+ options = { :document => @pdf }
58
+ text_box = Prawn::Text::Box.new(text, options)
59
+ text_box.render
60
+ text_box.height.should == @pdf.font.height * 2
61
+ end
62
+ end
63
+
64
+ describe "Text::Box#height with leading" do
65
+ it "should equal the sum of the height of each line" do
66
+ create_pdf
67
+ text = "Oh hai text rect.\nOh hai text rect."
68
+ leading = 12
69
+ options = { :document => @pdf, :leading => leading }
70
+ text_box = Prawn::Text::Box.new(text, options)
71
+ text_box.render
72
+ text_box.height.should == @pdf.font.height * 2 + leading
73
+ end
74
+ end
75
+
76
+ describe "Text::Box#valid_options" do
77
+ it "should return an array" do
78
+ create_pdf
79
+ text_box = Prawn::Text::Box.new("", :document => @pdf)
80
+ text_box.valid_options.should.be.kind_of(Array)
81
+ end
82
+ end
83
+
84
+ describe "Text::Box#render" do
85
+ it "should not fail if height is smaller than 1 line" do
86
+ create_pdf
87
+ @text = "Oh hai text rect. " * 10
88
+ @options = {
89
+ :height => @pdf.font.height * 0.5,
90
+ :document => @pdf
91
+ }
92
+ text_box = Prawn::Text::Box.new(@text, @options)
93
+ text_box.render
94
+ text_box.text.should == ""
95
+ end
96
+ it "should draw content to the page" do
97
+ create_pdf
98
+ @text = "Oh hai text rect. " * 10
99
+ @options = { :document => @pdf }
100
+ text_box = Prawn::Text::Box.new(@text, @options)
101
+ text_box.render
102
+ text = PDF::Inspector::Text.analyze(@pdf.render)
103
+ text.strings.should.not.be.empty
104
+ end
105
+ it "should not draw a transformation matrix" do
106
+ create_pdf
107
+ @text = "Oh hai text rect. " * 10
108
+ @options = { :document => @pdf }
109
+ text_box = Prawn::Text::Box.new(@text, @options)
110
+ text_box.render
111
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
112
+ matrices.matrices.length.should == 0
113
+ end
114
+ end
115
+
116
+ describe "Text::Box#render(:single_line => true)" do
117
+ it "should draw only one line to the page" do
118
+ create_pdf
119
+ @text = "Oh hai text rect. " * 10
120
+ @options = { :document => @pdf,
121
+ :single_line => true }
122
+ text_box = Prawn::Text::Box.new(@text, @options)
123
+ text_box.render
124
+ text = PDF::Inspector::Text.analyze(@pdf.render)
125
+ text.strings.length.should == 1
126
+ end
127
+ end
128
+
129
+ describe "Text::Box#render(:dry_run => true)" do
130
+ it "should not draw any content to the page" do
131
+ create_pdf
132
+ @text = "Oh hai text rect. " * 10
133
+ @options = { :document => @pdf }
134
+ text_box = Prawn::Text::Box.new(@text, @options)
135
+ text_box.render(:dry_run => true)
136
+ text = PDF::Inspector::Text.analyze(@pdf.render)
137
+ text.strings.should.be.empty
138
+ end
139
+ it "subsequent calls to render should not raise an ArgumentError exception" do
140
+ create_pdf
141
+ @text = "™©"
142
+ @options = { :document => @pdf }
143
+ text_box = Prawn::Text::Box.new(@text, @options)
144
+ text_box.render(:dry_run => true)
145
+ lambda { text_box.render }.should.not.raise(ArgumentError)
146
+ end
147
+ end
148
+
149
+ describe "Text::Box#render with :rotate option of 30)" do
150
+ before(:each) do
151
+ create_pdf
152
+ rotate = 30
153
+ @x = 300
154
+ @y = 70
155
+ @width = 100
156
+ @height = 50
157
+ @cos = Math.cos(rotate * Math::PI / 180)
158
+ @sin = Math.sin(rotate * Math::PI / 180)
159
+ @text = "Oh hai text rect. " * 10
160
+ @options = { :document => @pdf,
161
+ :rotate => rotate,
162
+ :at => [@x, @y],
163
+ :width => @width,
164
+ :height => @height }
165
+ end
166
+ context ":rotate_around option of :center" do
167
+ it "should draw content to the page rotated about the center of the text" do
168
+ @options[:rotate_around] = :center
169
+ text_box = Prawn::Text::Box.new(@text, @options)
170
+ text_box.render
171
+
172
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
173
+ x = @x + @width / 2
174
+ y = @y - @height / 2
175
+ x_prime = x * @cos - y * @sin
176
+ y_prime = x * @sin + y * @cos
177
+ matrices.matrices[0].should == [1, 0, 0, 1,
178
+ reduce_precision(x - x_prime),
179
+ reduce_precision(y - y_prime)]
180
+ matrices.matrices[1].should == [reduce_precision(@cos),
181
+ reduce_precision(@sin),
182
+ reduce_precision(-@sin),
183
+ reduce_precision(@cos), 0, 0]
184
+
185
+ text = PDF::Inspector::Text.analyze(@pdf.render)
186
+ text.strings.should.not.be.empty
187
+ end
188
+ end
189
+ context ":rotate_around option of :upper_left" do
190
+ it "should draw content to the page rotated about the upper left corner of the text" do
191
+ @options[:rotate_around] = :upper_left
192
+ text_box = Prawn::Text::Box.new(@text, @options)
193
+ text_box.render
194
+
195
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
196
+ x = @x
197
+ y = @y
198
+ x_prime = x * @cos - y * @sin
199
+ y_prime = x * @sin + y * @cos
200
+ matrices.matrices[0].should == [1, 0, 0, 1,
201
+ reduce_precision(x - x_prime),
202
+ reduce_precision(y - y_prime)]
203
+ matrices.matrices[1].should == [reduce_precision(@cos),
204
+ reduce_precision(@sin),
205
+ reduce_precision(-@sin),
206
+ reduce_precision(@cos), 0, 0]
207
+
208
+ text = PDF::Inspector::Text.analyze(@pdf.render)
209
+ text.strings.should.not.be.empty
210
+ end
211
+ end
212
+ context "default :rotate_around" do
213
+ it "should draw content to the page rotated about the upper left corner of the text" do
214
+ text_box = Prawn::Text::Box.new(@text, @options)
215
+ text_box.render
216
+
217
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
218
+ x = @x
219
+ y = @y
220
+ x_prime = x * @cos - y * @sin
221
+ y_prime = x * @sin + y * @cos
222
+ matrices.matrices[0].should == [1, 0, 0, 1,
223
+ reduce_precision(x - x_prime),
224
+ reduce_precision(y - y_prime)]
225
+ matrices.matrices[1].should == [reduce_precision(@cos),
226
+ reduce_precision(@sin),
227
+ reduce_precision(-@sin),
228
+ reduce_precision(@cos), 0, 0]
229
+
230
+ text = PDF::Inspector::Text.analyze(@pdf.render)
231
+ text.strings.should.not.be.empty
232
+ end
233
+ end
234
+ context ":rotate_around option of :upper_right" do
235
+ it "should draw content to the page rotated about the upper right corner of the text" do
236
+ @options[:rotate_around] = :upper_right
237
+ text_box = Prawn::Text::Box.new(@text, @options)
238
+ text_box.render
239
+
240
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
241
+ x = @x + @width
242
+ y = @y
243
+ x_prime = x * @cos - y * @sin
244
+ y_prime = x * @sin + y * @cos
245
+ matrices.matrices[0].should == [1, 0, 0, 1,
246
+ reduce_precision(x - x_prime),
247
+ reduce_precision(y - y_prime)]
248
+ matrices.matrices[1].should == [reduce_precision(@cos),
249
+ reduce_precision(@sin),
250
+ reduce_precision(-@sin),
251
+ reduce_precision(@cos), 0, 0]
252
+
253
+ text = PDF::Inspector::Text.analyze(@pdf.render)
254
+ text.strings.should.not.be.empty
255
+ end
256
+ end
257
+ context ":rotate_around option of :lower_right" do
258
+ it "should draw content to the page rotated about the lower right corner of the text" do
259
+ @options[:rotate_around] = :lower_right
260
+ text_box = Prawn::Text::Box.new(@text, @options)
261
+ text_box.render
262
+
263
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
264
+ x = @x + @width
265
+ y = @y - @height
266
+ x_prime = x * @cos - y * @sin
267
+ y_prime = x * @sin + y * @cos
268
+ matrices.matrices[0].should == [1, 0, 0, 1,
269
+ reduce_precision(x - x_prime),
270
+ reduce_precision(y - y_prime)]
271
+ matrices.matrices[1].should == [reduce_precision(@cos),
272
+ reduce_precision(@sin),
273
+ reduce_precision(-@sin),
274
+ reduce_precision(@cos), 0, 0]
275
+
276
+ text = PDF::Inspector::Text.analyze(@pdf.render)
277
+ text.strings.should.not.be.empty
278
+ end
279
+ end
280
+ context ":rotate_around option of :lower_left" do
281
+ it "should draw content to the page rotated about the lower left corner of the text" do
282
+ @options[:rotate_around] = :lower_left
283
+ text_box = Prawn::Text::Box.new(@text, @options)
284
+ text_box.render
285
+
286
+ matrices = PDF::Inspector::Graphics::Matrix.analyze(@pdf.render)
287
+ x = @x
288
+ y = @y - @height
289
+ x_prime = x * @cos - y * @sin
290
+ y_prime = x * @sin + y * @cos
291
+ matrices.matrices[0].should == [1, 0, 0, 1,
292
+ reduce_precision(x - x_prime),
293
+ reduce_precision(y - y_prime)]
294
+ matrices.matrices[1].should == [reduce_precision(@cos),
295
+ reduce_precision(@sin),
296
+ reduce_precision(-@sin),
297
+ reduce_precision(@cos), 0, 0]
298
+
299
+ text = PDF::Inspector::Text.analyze(@pdf.render)
300
+ text.strings.should.not.be.empty
301
+ end
302
+ end
303
+ end
304
+
305
+ describe "Text::Box default height" do
306
+ before(:each) { create_pdf }
307
+
308
+ it "should be the height from the bottom bound to document.y" do
309
+ target_height = @pdf.y - @pdf.bounds.bottom
310
+ @text = "Oh hai\n" * 60
311
+ text_box = Prawn::Text::Box.new(@text, :document => @pdf)
312
+ text_box.render
313
+ text_box.height.should.be.close(target_height, @pdf.font.height)
314
+ end
315
+
316
+ it "should use the margin-box bottom if only in a stretchy bbox" do
317
+ @pdf.bounding_box([0, @pdf.cursor], :width => @pdf.bounds.width) do
318
+ target_height = @pdf.y - @pdf.bounds.bottom
319
+ @text = "Oh hai\n" * 60
320
+ text_box = Prawn::Text::Box.new(@text, :document => @pdf)
321
+ text_box.render
322
+ text_box.height.should.be.close(target_height, @pdf.font.height)
323
+ end
324
+ end
325
+
326
+ it "should use the parent-box bottom if in a stretchy bbox and " +
327
+ "overflow is :expand, even with an explicit height"do
328
+ @pdf.bounding_box([0, @pdf.cursor], :width => @pdf.bounds.width) do
329
+ target_height = @pdf.y - @pdf.bounds.bottom
330
+ @text = "Oh hai\n" * 60
331
+ text_box = Prawn::Text::Box.new(@text, :document => @pdf,
332
+ :height => 100, :overflow => :expand)
333
+ text_box.render
334
+ text_box.height.should.be.close(target_height, @pdf.font.height)
335
+ end
336
+ end
337
+
338
+ it "should use the innermost non-stretchy bbox, not the margin box" do
339
+ @pdf.bounding_box([0, @pdf.cursor], :width => @pdf.bounds.width,
340
+ :height => 200) do
341
+ @pdf.bounding_box([0, @pdf.cursor], :width => @pdf.bounds.width) do
342
+ @text = "Oh hai\n" * 60
343
+ text_box = Prawn::Text::Box.new(@text, :document => @pdf)
344
+ text_box.render
345
+ text_box.height.should.be.close(200, @pdf.font.height)
346
+ end
347
+ end
348
+ end
349
+
350
+ end
351
+
352
+ describe "Text::Box default at" do
353
+ it "should be the left corner of the bounds, and the current document.y" do
354
+ create_pdf
355
+ target_at = [@pdf.bounds.left, @pdf.y]
356
+ @text = "Oh hai text rect. " * 100
357
+ @options = { :document => @pdf }
358
+ text_box = Prawn::Text::Box.new(@text, @options)
359
+ text_box.render
360
+ text_box.at.should == target_at
361
+ end
362
+ end
363
+
364
+ describe "Text::Box with text than can fit in the box" do
365
+ before(:each) do
366
+ create_pdf
367
+ @text = "Oh hai text rect. " * 10
368
+ @options = {
369
+ :width => 162.0,
370
+ :height => 162.0,
371
+ :document => @pdf
372
+ }
373
+ end
374
+
375
+ it "printed text should match requested text, except for trailing or leading white space and that spaces may be replaced by newlines" do
376
+ text_box = Prawn::Text::Box.new(@text, @options)
377
+ text_box.render
378
+ text_box.text.gsub("\n", " ").should == @text.strip
379
+ end
380
+
381
+ it "render should return an empty string because no text remains unprinted" do
382
+ text_box = Prawn::Text::Box.new(@text, @options)
383
+ text_box.render.should == ""
384
+ end
385
+
386
+ it "should be truncated when the leading is set high enough to prevent all the lines from being printed" do
387
+ @options[:leading] = 40
388
+ text_box = Prawn::Text::Box.new(@text, @options)
389
+ text_box.render
390
+ text_box.text.gsub("\n", " ").should.not == @text.strip
391
+ end
392
+ end
393
+
394
+ describe "Text::Box with text than can fit in the box with :ellipses overflow and :valign => :bottom" do
395
+ it "should not print ellipses" do
396
+ create_pdf
397
+ @text = "Oh hai text rect. " * 10
398
+ @options = {
399
+ :width => 162.0,
400
+ :height => 162.0,
401
+ :overflow => :ellipses,
402
+ :valign => :bottom,
403
+ :document => @pdf
404
+ }
405
+ @text_box = Prawn::Text::Box.new(@text, @options)
406
+ @text_box.render
407
+ @text_box.text.should.not =~ /\.\.\./
408
+ end
409
+ end
410
+
411
+
412
+
413
+ describe "Text::Box printing UTF-8 string with higher bit characters" do
414
+ before(:each) do
415
+ create_pdf
416
+ @text = "©"
417
+ # not enough height to print any text, so we can directly compare against
418
+ # the input string
419
+ bounding_height = 1.0
420
+ options = {
421
+ :height => bounding_height,
422
+ :document => @pdf
423
+ }
424
+ file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
425
+ @pdf.font_families["Action Man"] = {
426
+ :normal => { :file => file, :font => "ActionMan" },
427
+ :italic => { :file => file, :font => "ActionMan-Italic" },
428
+ :bold => { :file => file, :font => "ActionMan-Bold" },
429
+ :bold_italic => { :file => file, :font => "ActionMan-BoldItalic" }
430
+ }
431
+ @text_box = Prawn::Text::Box.new(@text, options)
432
+ end
433
+ describe "when using a TTF font" do
434
+ it "unprinted text should be in UTF-8 encoding" do
435
+ @pdf.font("Action Man")
436
+ remaining_text = @text_box.render
437
+ remaining_text.should == @text
438
+ end
439
+ it "subsequent calls to Text::Box need not include the" +
440
+ " :skip_encoding => true option" do
441
+ @pdf.font("Action Man")
442
+ remaining_text = @text_box.render
443
+ lambda {
444
+ @pdf.text_box(remaining_text, :document => @pdf)
445
+ }.should.not.raise(ArgumentError)
446
+ end
447
+ end
448
+ describe "when using an AFM font" do
449
+ it "unprinted text should be in WinAnsi encoding" do
450
+ remaining_text = @text_box.render
451
+ remaining_text.should == @pdf.font.normalize_encoding(@text)
452
+ end
453
+ it "subsequent calls to Text::Box must include the" +
454
+ " :skip_encoding => true option" do
455
+ remaining_text = @text_box.render
456
+ lambda {
457
+ @pdf.text_box(remaining_text, :document => @pdf)
458
+ }.should.raise(ArgumentError)
459
+ lambda {
460
+ @pdf.text_box(remaining_text, :skip_encoding => true,
461
+ :document => @pdf)
462
+ }.should.not.raise(ArgumentError)
463
+ end
464
+ end
465
+ end
466
+
467
+
468
+ describe "Text::Box with more text than can fit in the box" do
469
+ before(:each) do
470
+ create_pdf
471
+ @text = "Oh hai text rect. " * 30
472
+ @bounding_height = 162.0
473
+ @options = {
474
+ :width => 162.0,
475
+ :height => @bounding_height,
476
+ :document => @pdf
477
+ }
478
+ end
479
+
480
+ context "truncated overflow" do
481
+ before(:each) do
482
+ @options[:overflow] = :truncate
483
+ @text_box = Prawn::Text::Box.new(@text, @options)
484
+ end
485
+ it "should not display ellipses" do
486
+ @text_box.render
487
+ @text_box.text.should.not =~ /\.\.\./
488
+ end
489
+ it "should be truncated" do
490
+ @text_box.render
491
+ @text_box.text.gsub("\n", " ").should.not == @text.strip
492
+ end
493
+ it "render should not return an empty string because some text remains unprinted" do
494
+ @text_box.render.should.not == ""
495
+ end
496
+ it "#height should be no taller than the specified height" do
497
+ @text_box.render
498
+ @text_box.height.should.be <= @bounding_height
499
+ end
500
+ it "#height should be within one font height of the specified height" do
501
+ @text_box.render
502
+ @bounding_height.should.be.close(@text_box.height, @pdf.font.height)
503
+ end
504
+ context "with :rotate option" do
505
+ it "unrendered text should be the same as when not rotated" do
506
+ remaining_text = @text_box.render
507
+
508
+ rotate = 30
509
+ x = 300
510
+ y = 70
511
+ width = @options[:width]
512
+ height = @options[:height]
513
+ @options[:document] = @pdf
514
+ @options[:rotate] = rotate
515
+ @options[:at] = [x, y]
516
+ rotated_text_box = Prawn::Text::Box.new(@text, @options)
517
+ rotated_text_box.render.should == remaining_text
518
+ end
519
+ end
520
+ end
521
+
522
+ context "ellipses overflow" do
523
+ before(:each) do
524
+ @options[:overflow] = :ellipses
525
+ @text_box = Prawn::Text::Box.new(@text, @options)
526
+ end
527
+ it "should display ellipses" do
528
+ @text_box.render
529
+ @text_box.text.should =~ /\.\.\./
530
+ end
531
+ it "render should not return an empty string because some text remains unprinted" do
532
+ @text_box.render.should.not == ""
533
+ end
534
+ end
535
+
536
+ context "expand overflow" do
537
+ before(:each) do
538
+ @options[:overflow] = :expand
539
+ @text_box = Prawn::Text::Box.new(@text, @options)
540
+ end
541
+ it "height should expand to encompass all the text (but not exceed the height of the page)" do
542
+ @text_box.render
543
+ @text_box.height.should > @bounding_height
544
+ end
545
+ it "should display the entire string (as long as there was space remaining on the page to print all the text)" do
546
+ @text_box.render
547
+ @text_box.text.gsub("\n", " ").should == @text.strip
548
+ end
549
+ it "render should return an empty string because no text remains unprinted(as long as there was space remaining on the page to print all the text)" do
550
+ @text_box.render.should == ""
551
+ end
552
+ end
553
+
554
+ context "shrink_to_fit overflow" do
555
+ before(:each) do
556
+ @options[:overflow] = :shrink_to_fit
557
+ @options[:min_font_size] = 2
558
+ @text_box = Prawn::Text::Box.new(@text, @options)
559
+ end
560
+ it "should display the entire text" do
561
+ @text_box.render
562
+ @text_box.text.gsub("\n", " ").should == @text.strip
563
+ end
564
+ it "render should return an empty string because no text remains unprinted" do
565
+ @text_box.render.should == ""
566
+ end
567
+ end
568
+ end
569
+
570
+ describe "Text::Box with a solid block of Chinese characters" do
571
+ it "printed text should match requested text, except for newlines" do
572
+ create_pdf
573
+ @text = "写中国字" * 10
574
+ @options = {
575
+ :width => 162.0,
576
+ :height => 162.0,
577
+ :document => @pdf
578
+ }
579
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf"
580
+ @options[:overflow] = :truncate
581
+ text_box = Prawn::Text::Box.new(@text, @options)
582
+ text_box.render
583
+ text_box.text.gsub("\n", "").should == @text.strip
584
+ end
585
+ end
586
+
587
+
588
+ describe "drawing bounding boxes" do
589
+
590
+ before(:each) { create_pdf }
591
+
592
+ it "should restore the margin box when bounding box exits" do
593
+ margin_box = @pdf.bounds
594
+
595
+ @pdf.text_box "Oh hai text box. " * 11, :height => @pdf.font.height * 10
596
+
597
+ @pdf.bounds.should == margin_box
598
+
599
+ end
600
+
601
+ end
602
+
603
+
604
+ describe "Text::Box#render with :character_spacing option" do
605
+ it "should draw the character spacing to the document" do
606
+ create_pdf
607
+ string = "hello world"
608
+ options = { :document => @pdf, :character_spacing => 10 }
609
+ text_box = Prawn::Text::Box.new(string, options)
610
+ text_box.render
611
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
612
+ contents.character_spacing[0].should == 10
613
+ end
614
+ it "should take character spacing into account when wrapping" do
615
+ create_pdf
616
+ @pdf.font "Courier"
617
+ text_box = Prawn::Text::Box.new("hello world",
618
+ :width => 100,
619
+ :overflow => :expand,
620
+ :character_spacing => 10,
621
+ :document => @pdf)
622
+ text_box.render
623
+ text_box.text.should == "hello\nworld"
624
+ end
625
+ end
626
+
627
+ describe "Text::Box wrapping" do
628
+ before(:each) do
629
+ create_pdf
630
+ end
631
+
632
+ it "should wrap text" do
633
+ text = "Please wrap this text about HERE. More text that should be wrapped"
634
+ expect = "Please wrap this text about\nHERE. More text that should be\nwrapped"
635
+
636
+ @pdf.font "Courier"
637
+ text_box = Prawn::Text::Box.new(text,
638
+ :width => 220,
639
+ :overflow => :expand,
640
+ :document => @pdf)
641
+ text_box.render
642
+ text_box.text.should == expect
643
+ end
644
+
645
+ # white space was being stripped after the entire line was generated, meaning
646
+ # that leading white space characters reduced the amount of space on the line
647
+ # for other characters, so wrapping "hello hello" resulted in
648
+ # "hello\n\nhello", rather than "hello\nhello"
649
+ #
650
+ it "white space at beginning of line should not be taken into account when" +
651
+ " computing line width" do
652
+ text = "hello hello"
653
+ expect = "hello\nhello"
654
+
655
+ @pdf.font "Courier"
656
+ text_box = Prawn::Text::Box.new(text,
657
+ :width => 40,
658
+ :overflow => :expand,
659
+ :document => @pdf)
660
+ text_box.render
661
+ text_box.text.should == expect
662
+ end
663
+
664
+ it "should respect end of line when wrapping text" do
665
+ text = "Please wrap only before\nTHIS word. Don't wrap this"
666
+ expect = text
667
+
668
+ @pdf.font "Courier"
669
+ text_box = Prawn::Text::Box.new(text,
670
+ :width => 220,
671
+ :overflow => :expand,
672
+ :document => @pdf)
673
+ text_box.render
674
+ text_box.text.should == expect
675
+ end
676
+
677
+ it "should respect multiple newlines when wrapping text" do
678
+ text = "Please wrap only before THIS\n\nword. Don't wrap this"
679
+ expect= "Please wrap only before\nTHIS\n\nword. Don't wrap this"
680
+
681
+ @pdf.font "Courier"
682
+ text_box = Prawn::Text::Box.new(text,
683
+ :width => 200,
684
+ :overflow => :expand,
685
+ :document => @pdf)
686
+ text_box.render
687
+ text_box.text.should == expect
688
+ end
689
+
690
+ it "should respect multiple newlines when wrapping text when those newlines coincide with a line break" do
691
+ text = "Please wrap only before\n\nTHIS word. Don't wrap this"
692
+ expect = text
693
+
694
+ @pdf.font "Courier"
695
+ text_box = Prawn::Text::Box.new(text,
696
+ :width => 220,
697
+ :overflow => :expand,
698
+ :document => @pdf)
699
+ text_box.render
700
+ text_box.text.should == expect
701
+ end
702
+
703
+ it "should respect initial newlines" do
704
+ text = "\nThis should be on line 2"
705
+ expect = text
706
+
707
+ @pdf.font "Courier"
708
+ text_box = Prawn::Text::Box.new(text,
709
+ :width => 220,
710
+ :overflow => :expand,
711
+ :document => @pdf)
712
+ text_box.render
713
+ text_box.text.should == expect
714
+ end
715
+
716
+ it "should wrap lines comprised of a single word of the bounds when wrapping text" do
717
+ text = "You_can_wrap_this_text_HERE"
718
+ expect = "You_can_wrap_this_text_HE\nRE"
719
+
720
+ @pdf.font "Courier"
721
+ text_box = Prawn::Text::Box.new(text,
722
+ :width => 180,
723
+ :overflow => :expand,
724
+ :document => @pdf)
725
+ text_box.render
726
+ text_box.text.should == expect
727
+ end
728
+
729
+ it "should wrap lines comprised of a single word of the bounds when wrapping text" do
730
+ text = "©" * 30
731
+
732
+ @pdf.font "Courier"
733
+ text_box = Prawn::Text::Box.new(text, :width => 180,
734
+ :overflow => :expand,
735
+ :document => @pdf)
736
+
737
+ text_box.render
738
+
739
+ expected = "©" * 25 + "\n" + "©" * 5
740
+ @pdf.font.normalize_encoding!(expected)
741
+
742
+ text_box.text.should == expected
743
+ end
744
+
745
+ it "should wrap non-unicode strings using single-byte word-wrapping" do
746
+ text = "continúa esforzandote " * 5
747
+ text_box = Prawn::Text::Box.new(text, :width => 180,
748
+ :document => @pdf)
749
+ text_box.render
750
+ results_with_accent = text_box.text
751
+
752
+ text = "continua esforzandote " * 5
753
+ text_box = Prawn::Text::Box.new(text, :width => 180,
754
+ :document => @pdf)
755
+ text_box.render
756
+ results_without_accent = text_box.text
757
+
758
+ results_with_accent.first_line.length.should == results_without_accent.first_line.length
759
+ end
760
+
761
+ end
762
+
763
+ def reduce_precision(float)
764
+ ("%.5f" % float).to_f
765
+ end
766
+
767
+ module TestWrapOverride
768
+ def wrap(string)
769
+ @text = nil
770
+ @line_height = @document.font.height
771
+ @descender = @document.font.descender
772
+ @ascender = @document.font.ascender
773
+ @baseline_y = -@ascender
774
+ draw_line("all your base are belong to us")
775
+ ""
776
+ end
777
+ end