prawn 0.8.4 → 0.11.1.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (290) 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 +43 -0
  67. data/examples/bounding_box/indentation.rb +34 -0
  68. data/examples/bounding_box/russian_boxes.rb +36 -0
  69. data/examples/bounding_box/stretched_nesting.rb +67 -0
  70. data/examples/example_helper.rb +8 -0
  71. data/examples/general/background.rb +23 -0
  72. data/examples/general/canvas.rb +15 -0
  73. data/examples/general/context_sensitive_headers.rb +37 -0
  74. data/examples/general/float.rb +11 -0
  75. data/examples/general/margin.rb +36 -0
  76. data/examples/general/measurement_units.rb +51 -0
  77. data/examples/general/metadata-info.rb +16 -0
  78. data/examples/general/multi_page_layout.rb +18 -0
  79. data/examples/general/outlines.rb +66 -0
  80. data/examples/general/page_geometry.rb +31 -0
  81. data/examples/general/page_numbering.rb +15 -0
  82. data/examples/general/repeaters.rb +47 -0
  83. data/examples/general/stamp.rb +41 -0
  84. data/examples/general/templates.rb +13 -0
  85. data/examples/graphics/basic_images.rb +23 -0
  86. data/examples/graphics/cmyk.rb +12 -0
  87. data/examples/graphics/curves.rb +11 -0
  88. data/examples/graphics/hexagon.rb +13 -0
  89. data/examples/graphics/image_fit.rb +15 -0
  90. data/examples/graphics/image_flow.rb +37 -0
  91. data/examples/graphics/image_position.rb +17 -0
  92. data/examples/graphics/line.rb +32 -0
  93. data/examples/graphics/png_types.rb +22 -0
  94. data/examples/graphics/polygons.rb +16 -0
  95. data/examples/graphics/remote_images.rb +12 -0
  96. data/examples/graphics/rounded_polygons.rb +19 -0
  97. data/examples/graphics/rounded_rectangle.rb +20 -0
  98. data/examples/graphics/ruport_style_helpers.rb +19 -0
  99. data/examples/graphics/stroke_bounds.rb +20 -0
  100. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  101. data/examples/graphics/stroke_dash.rb +42 -0
  102. data/examples/graphics/transformations.rb +52 -0
  103. data/examples/graphics/transparency.rb +26 -0
  104. data/examples/grid/bounding_boxes.rb +21 -0
  105. data/examples/grid/column_gutter_grid.rb +20 -0
  106. data/examples/grid/multi_boxes.rb +51 -0
  107. data/examples/grid/show_grid.rb +13 -0
  108. data/examples/grid/simple_grid.rb +20 -0
  109. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  110. data/examples/m17n/euro.rb +15 -0
  111. data/examples/m17n/sjis.rb +28 -0
  112. data/examples/m17n/utf8.rb +13 -0
  113. data/examples/m17n/win_ansi_charset.rb +54 -0
  114. data/examples/security/hello_foo.rb +8 -0
  115. data/examples/table/bill.rb +53 -0
  116. data/examples/table/cell.rb +12 -0
  117. data/examples/table/checkerboard.rb +22 -0
  118. data/examples/table/header.rb +14 -0
  119. data/examples/table/inline_format_table.rb +12 -0
  120. data/examples/table/multi_page_table.rb +9 -0
  121. data/examples/table/simple_table.rb +24 -0
  122. data/examples/table/subtable.rb +12 -0
  123. data/examples/table/widths.rb +20 -0
  124. data/examples/text/alignment.rb +18 -0
  125. data/examples/text/character_spacing.rb +12 -0
  126. data/examples/text/dfont.rb +48 -0
  127. data/examples/text/family_based_styling.rb +24 -0
  128. data/examples/text/font_calculations.rb +91 -0
  129. data/examples/text/font_size.rb +33 -0
  130. data/examples/text/hyphenation.rb +45 -0
  131. data/examples/text/indent_paragraphs.rb +22 -0
  132. data/examples/text/inline_format.rb +103 -0
  133. data/examples/text/kerning.rb +30 -0
  134. data/examples/text/rotated.rb +98 -0
  135. data/examples/text/shaped_text_box.rb +31 -0
  136. data/examples/text/simple_text.rb +17 -0
  137. data/examples/text/simple_text_ttf.rb +17 -0
  138. data/examples/text/span.rb +29 -0
  139. data/examples/text/text_box.rb +88 -0
  140. data/examples/text/text_box_returning_excess.rb +51 -0
  141. data/examples/text/text_flow.rb +67 -0
  142. data/lib/prawn.rb +25 -1
  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 +574 -0
  162. data/lib/prawn/document/bounding_box.rb +425 -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 +361 -0
  182. data/lib/prawn/images/jpg.rb +46 -0
  183. data/lib/prawn/images/png.rb +226 -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 +421 -0
  194. data/lib/prawn/table/cell.rb +360 -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 +125 -0
  198. data/lib/prawn/table/cells.rb +189 -0
  199. data/lib/prawn/text.rb +449 -0
  200. data/lib/prawn/text/box.rb +385 -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 +359 -0
  209. data/spec/destinations_spec.rb +15 -0
  210. data/spec/document_spec.rb +476 -0
  211. data/spec/font_spec.rb +324 -0
  212. data/spec/formatted_text_arranger_spec.rb +426 -0
  213. data/spec/formatted_text_box_spec.rb +756 -0
  214. data/spec/formatted_text_fragment_spec.rb +211 -0
  215. data/spec/graphics_spec.rb +446 -0
  216. data/spec/grid_spec.rb +85 -0
  217. data/spec/images_spec.rb +117 -0
  218. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  219. data/spec/jpg_spec.rb +25 -0
  220. data/spec/line_wrap_spec.rb +341 -0
  221. data/spec/measurement_units_spec.rb +23 -0
  222. data/spec/name_tree_spec.rb +112 -0
  223. data/spec/object_store_spec.rb +160 -0
  224. data/spec/outline_spec.rb +404 -0
  225. data/spec/pdf_object_spec.rb +170 -0
  226. data/spec/png_spec.rb +237 -0
  227. data/spec/reference_spec.rb +82 -0
  228. data/spec/repeater_spec.rb +96 -0
  229. data/spec/security_spec.rb +120 -0
  230. data/spec/snapshot_spec.rb +154 -0
  231. data/spec/span_spec.rb +49 -0
  232. data/spec/spec_helper.rb +26 -0
  233. data/spec/stamp_spec.rb +108 -0
  234. data/spec/stroke_styles_spec.rb +163 -0
  235. data/spec/table_spec.rb +598 -0
  236. data/spec/template_spec.rb +165 -0
  237. data/spec/text_at_spec.rb +119 -0
  238. data/spec/text_box_spec.rb +742 -0
  239. data/spec/text_spacing_spec.rb +75 -0
  240. data/spec/text_spec.rb +342 -0
  241. data/spec/text_with_inline_formatting_spec.rb +193 -0
  242. data/spec/transparency_spec.rb +89 -0
  243. data/vendor/pdf-inspector/README +18 -0
  244. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  245. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  246. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  247. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  248. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  249. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  250. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  251. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  252. data/vendor/ttfunk/example.rb +45 -0
  253. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  254. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  255. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  256. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  257. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  258. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  259. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  260. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  261. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  262. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  263. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  264. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  265. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  266. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  274. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  275. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  276. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  277. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  278. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  279. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  280. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  281. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  282. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  283. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  284. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  285. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  286. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  287. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  288. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  289. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  290. metadata +337 -48
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "#character_spacing" do
6
+ it "should draw the character spacing to the document" do
7
+ create_pdf
8
+ @pdf.character_spacing(10.555555) do
9
+ @pdf.text("hello world")
10
+ end
11
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
12
+ contents.character_spacing.first.should == 10.556
13
+ end
14
+ it "should not draw the character spacing to the document" +
15
+ " when the new character spacing matches the old" do
16
+ create_pdf
17
+ @pdf.character_spacing(0) do
18
+ @pdf.text("hello world")
19
+ end
20
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
21
+ contents.character_spacing.should.be.empty
22
+ end
23
+ it "should restore character spacing to 0" do
24
+ create_pdf
25
+ @pdf.character_spacing(10.555555) do
26
+ @pdf.text("hello world")
27
+ end
28
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
29
+ contents.character_spacing.last.should == 0
30
+ end
31
+ it "should function as an accessor when no parameter given" do
32
+ create_pdf
33
+ @pdf.character_spacing(10.555555) do
34
+ @pdf.text("hello world")
35
+ @pdf.character_spacing.should == 10.555555
36
+ end
37
+ @pdf.character_spacing.should == 0
38
+ end
39
+ end
40
+
41
+ describe "#word_spacing" do
42
+ it "should draw the word spacing to the document" do
43
+ create_pdf
44
+ @pdf.word_spacing(10.555555) do
45
+ @pdf.text("hello world")
46
+ end
47
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
48
+ contents.word_spacing.first.should == 10.556
49
+ end
50
+ it "should draw the word spacing to the document" +
51
+ " when the new word spacing matches the old" do
52
+ create_pdf
53
+ @pdf.word_spacing(0) do
54
+ @pdf.text("hello world")
55
+ end
56
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
57
+ contents.word_spacing.should.be.empty
58
+ end
59
+ it "should restore word spacing to 0" do
60
+ create_pdf
61
+ @pdf.word_spacing(10.555555) do
62
+ @pdf.text("hello world")
63
+ end
64
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
65
+ contents.word_spacing.last.should == 0
66
+ end
67
+ it "should function as an accessor when no parameter given" do
68
+ create_pdf
69
+ @pdf.word_spacing(10.555555) do
70
+ @pdf.text("hello world")
71
+ @pdf.word_spacing.should == 10.555555
72
+ end
73
+ @pdf.word_spacing.should == 0
74
+ end
75
+ end
@@ -0,0 +1,342 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "Prawn::Text::NBSP" do
6
+ it "should be defined" do
7
+ Prawn::Text::NBSP.should == " "
8
+ end
9
+ end
10
+
11
+ describe "#height_of" do
12
+ before(:each) { create_pdf }
13
+
14
+ it "should return the height that would be required to print a" +
15
+ "particular string of text" do
16
+ original_y = @pdf.y
17
+ @pdf.text("Foo")
18
+ new_y = @pdf.y
19
+ @pdf.height_of("Foo", :width => 300).should.be.close(original_y - new_y, 0.0001)
20
+ end
21
+
22
+ it "should raise CannotFit if a too-small width is given" do
23
+ lambda do
24
+ @pdf.height_of("text", :width => 1)
25
+ end.should.raise(Prawn::Errors::CannotFit)
26
+ end
27
+
28
+ it "should raise NotImplementedError if :indent_paragraphs option is provided" do
29
+ lambda {
30
+ @pdf.height_of("hai", :width => 300,
31
+ :indent_paragraphs => 60)
32
+ }.should.raise(NotImplementedError)
33
+ end
34
+
35
+ it "should not raise Prawn::Errors::UnknownOption if :final_gap option is provided" do
36
+ lambda {
37
+ @pdf.height_of("hai", :width => 300,
38
+ :final_gap => true)
39
+ }.should.not.raise(Prawn::Errors::UnknownOption)
40
+ end
41
+ end
42
+
43
+ describe "#text" do
44
+ before(:each) { create_pdf }
45
+
46
+ it "should not fail when @output is nil when Prawn::Core::Text::LineWrap#finalize_line is called" do
47
+ # need a document with margins for these particulars to produce the
48
+ # condition that was throwing the error
49
+ pdf = Prawn::Document.new
50
+ lambda {
51
+ pdf.text "transparency " * 150, :size => 18
52
+ }.should.not.raise(TypeError)
53
+ end
54
+
55
+ it "should default to use kerning information" do
56
+ @pdf.text "hello world"
57
+ text = PDF::Inspector::Text.analyze(@pdf.render)
58
+ text.kerned[0].should.be true
59
+ end
60
+
61
+ it "should be able to disable kerning with an option" do
62
+ @pdf.text "hello world", :kerning => false
63
+ text = PDF::Inspector::Text.analyze(@pdf.render)
64
+ text.kerned[0].should.be false
65
+ end
66
+
67
+ it "should be able to disable kerning document wide" do
68
+ @pdf.default_kerning(false)
69
+ @pdf.default_kerning = false
70
+ @pdf.text "hello world"
71
+ text = PDF::Inspector::Text.analyze(@pdf.render)
72
+ text.kerned[0].should.be false
73
+ end
74
+
75
+ it "option should be able to override document wide kerning disabling" do
76
+ @pdf.default_kerning = false
77
+ @pdf.text "hello world", :kerning => true
78
+ text = PDF::Inspector::Text.analyze(@pdf.render)
79
+ text.kerned[0].should.be true
80
+ end
81
+
82
+ it "should raise ArgumentError if :at option included" do
83
+ lambda { @pdf.text("hai", :at => [0, 0]) }.should.raise(ArgumentError)
84
+ end
85
+
86
+ it "should advance down the document based on font_height" do
87
+ position = @pdf.y
88
+ @pdf.text "Foo"
89
+
90
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
91
+
92
+ position = @pdf.y
93
+ @pdf.text "Foo\nBar\nBaz"
94
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
95
+ end
96
+
97
+ it "should advance down the document based on font_height" +
98
+ " with size option" do
99
+ position = @pdf.y
100
+ @pdf.text "Foo", :size => 15
101
+
102
+ @pdf.font_size = 15
103
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
104
+
105
+ position = @pdf.y
106
+ @pdf.text "Foo\nBar\nBaz"
107
+ @pdf.y.should.be.close(position - 3 * @pdf.font.height, 0.0001)
108
+ end
109
+
110
+ it "should advance down the document based on font_height" +
111
+ " with leading option" do
112
+ position = @pdf.y
113
+ leading = 2
114
+ @pdf.text "Foo", :leading => leading
115
+
116
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
117
+
118
+ position = @pdf.y
119
+ @pdf.text "Foo\nBar\nBaz"
120
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
121
+ end
122
+
123
+ it "should advance down the document based on font ascender only "+
124
+ "if final_gap is given" do
125
+ position = @pdf.y
126
+ @pdf.text "Foo", :final_gap => false
127
+
128
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
129
+
130
+ position = @pdf.y
131
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false
132
+ @pdf.y.should.be.close(position - 2*@pdf.font.height - @pdf.font.ascender, 0.0001)
133
+ end
134
+
135
+ it "should be able to print text starting at the last line of a page" do
136
+ @pdf.move_cursor_to(@pdf.font.height)
137
+ @pdf.text("hello world")
138
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
139
+ pages.size.should == 1
140
+ end
141
+
142
+ it "should default to 12 point helvetica" do
143
+ @pdf.text "Blah"
144
+ text = PDF::Inspector::Text.analyze(@pdf.render)
145
+ text.font_settings[0][:name].should == :Helvetica
146
+ text.font_settings[0][:size].should == 12
147
+ text.strings.first.should == "Blah"
148
+ end
149
+
150
+ it "should allow setting font size" do
151
+ @pdf.text "Blah", :size => 16
152
+ text = PDF::Inspector::Text.analyze(@pdf.render)
153
+ text.font_settings[0][:size].should == 16
154
+ end
155
+
156
+ it "should allow setting a default font size" do
157
+ @pdf.font_size = 16
158
+ @pdf.text "Blah"
159
+ text = PDF::Inspector::Text.analyze(@pdf.render)
160
+ text.font_settings[0][:size].should == 16
161
+ end
162
+
163
+ it "should allow overriding default font for a single instance" do
164
+ @pdf.font_size = 16
165
+
166
+ @pdf.text "Blah", :size => 11
167
+ @pdf.text "Blaz"
168
+ text = PDF::Inspector::Text.analyze(@pdf.render)
169
+ text.font_settings[0][:size].should == 11
170
+ text.font_settings[1][:size].should == 16
171
+ end
172
+
173
+ it "should allow setting a font size transaction with a block" do
174
+ @pdf.font_size 16 do
175
+ @pdf.text 'Blah'
176
+ end
177
+
178
+ @pdf.text 'blah'
179
+
180
+ text = PDF::Inspector::Text.analyze(@pdf.render)
181
+ text.font_settings[0][:size].should == 16
182
+ text.font_settings[1][:size].should == 12
183
+ end
184
+
185
+ it "should allow manual setting the font size " +
186
+ "when in a font size block" do
187
+ @pdf.font_size(16) do
188
+ @pdf.text 'Foo'
189
+ @pdf.text 'Blah', :size => 11
190
+ @pdf.text 'Blaz'
191
+ end
192
+ text = PDF::Inspector::Text.analyze(@pdf.render)
193
+ text.font_settings[0][:size].should == 16
194
+ text.font_settings[1][:size].should == 11
195
+ text.font_settings[2][:size].should == 16
196
+ end
197
+
198
+ it "should allow registering of built-in font_settings on the fly" do
199
+ @pdf.font "Times-Roman"
200
+ @pdf.text "Blah"
201
+ @pdf.font "Courier"
202
+ @pdf.text "Blaz"
203
+ text = PDF::Inspector::Text.analyze(@pdf.render)
204
+ text.font_settings[0][:name].should == :"Times-Roman"
205
+ text.font_settings[1][:name].should == :Courier
206
+ end
207
+
208
+ it "should utilise the same default font across multiple pages" do
209
+ @pdf.text "Blah"
210
+ @pdf.start_new_page
211
+ @pdf.text "Blaz"
212
+ text = PDF::Inspector::Text.analyze(@pdf.render)
213
+
214
+ text.font_settings.size.should == 2
215
+ text.font_settings[0][:name].should == :Helvetica
216
+ text.font_settings[1][:name].should == :Helvetica
217
+ end
218
+
219
+ it "should raise an exception when an unknown font is used" do
220
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
221
+ end
222
+
223
+ it "should correctly render a utf-8 string when using a built-in font" do
224
+ str = "©" # copyright symbol
225
+ @pdf.text str
226
+
227
+ # grab the text from the rendered PDF and ensure it matches
228
+ text = PDF::Inspector::Text.analyze(@pdf.render)
229
+ text.strings.first.should == str
230
+ end
231
+
232
+ it "should correctly render a utf-8 string when using a TTF font" do
233
+ str = "©" # copyright symbol
234
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
235
+ @pdf.text str
236
+
237
+ # grab the text from the rendered PDF and ensure it matches
238
+ text = PDF::Inspector::Text.analyze(@pdf.render)
239
+ text.strings.first.should == str
240
+ end
241
+
242
+ it "should correctly render a string with higher bit characters across" +
243
+ " a page break when using a built-in font" do
244
+ str = "©"
245
+ @pdf.move_cursor_to(@pdf.font.height)
246
+ @pdf.text(str + "\n" + str)
247
+
248
+ # grab the text from the rendered PDF and ensure it matches
249
+ text = PDF::Inspector::Text.analyze(@pdf.render)
250
+ text.strings[1].should == str.strip
251
+ end
252
+
253
+ it "should correctly render a string with higher bit characters across" +
254
+ " a page break when using a built-in font and :indent_paragraphs option" do
255
+ str = "©"
256
+ @pdf.move_cursor_to(@pdf.font.height)
257
+ @pdf.text(str + "\n" + str, :indent_paragraphs => 20)
258
+
259
+ # grab the text from the rendered PDF and ensure it matches
260
+ text = PDF::Inspector::Text.analyze(@pdf.render)
261
+ text.strings[1].should == str.strip
262
+ end
263
+
264
+ if "spec".respond_to?(:encode!)
265
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
266
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
267
+ str = "Blah \xDD"
268
+ str.force_encoding("ASCII-8BIT")
269
+ lambda { @pdf.text str }.should.raise(ArgumentError)
270
+ end
271
+ it "should not raise an exception when a shift-jis string is rendered" do
272
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
273
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
274
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
275
+ lambda { @pdf.text sjis_str }.should.not.raise(ArgumentError)
276
+ end
277
+ else
278
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
279
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
280
+ str = "Blah \xDD"
281
+ lambda { @pdf.text str }.should.raise(ArgumentError)
282
+ end
283
+ it "should raise an exception when a shift-jis string is rendered" do
284
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
285
+ lambda { @pdf.text sjis_str }.should.raise(ArgumentError)
286
+ end
287
+ end
288
+
289
+ it "should call move_past_bottom when printing more text than can fit" +
290
+ " between the current document.y and bounds.bottom" do
291
+ @pdf.y = @pdf.font.height
292
+ @pdf.text "Hello"
293
+ @pdf.text "World"
294
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
295
+ pages.size.should == 2
296
+ pages[0][:strings].should == ["Hello"]
297
+ pages[1][:strings].should == ["World"]
298
+ end
299
+
300
+ describe "with :indent_paragraphs option" do
301
+ it "should indent the paragraphs" do
302
+ hello = "hello " * 50
303
+ hello2 = "hello " * 50
304
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
305
+ text = PDF::Inspector::Text.analyze(@pdf.render)
306
+ text.strings[0].should == ("hello " * 19).strip
307
+ text.strings[1].should == ("hello " * 21).strip
308
+ text.strings[3].should == ("hello " * 19).strip
309
+ text.strings[4].should == ("hello " * 21).strip
310
+ end
311
+ describe "when wrap to new page, and first line of new page" +
312
+ " is not the start of a new paragraph, that line should" +
313
+ " not be indented" do
314
+ it "should indent the paragraphs" do
315
+ hello = "hello " * 50
316
+ hello2 = "hello " * 50
317
+ @pdf.move_cursor_to(@pdf.font.height)
318
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
319
+ text = PDF::Inspector::Text.analyze(@pdf.render)
320
+ text.strings[0].should == ("hello " * 19).strip
321
+ text.strings[1].should == ("hello " * 21).strip
322
+ text.strings[3].should == ("hello " * 19).strip
323
+ text.strings[4].should == ("hello " * 21).strip
324
+ end
325
+ end
326
+ describe "when wrap to new page, and first line of new page" +
327
+ " is the start of a new paragraph, that line should" +
328
+ " be indented" do
329
+ it "should indent the paragraphs" do
330
+ hello = "hello " * 50
331
+ hello2 = "hello " * 50
332
+ @pdf.move_cursor_to(@pdf.font.height * 3)
333
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
334
+ text = PDF::Inspector::Text.analyze(@pdf.render)
335
+ text.strings[0].should == ("hello " * 19).strip
336
+ text.strings[1].should == ("hello " * 21).strip
337
+ text.strings[3].should == ("hello " * 19).strip
338
+ text.strings[4].should == ("hello " * 21).strip
339
+ end
340
+ end
341
+ end
342
+ end
@@ -0,0 +1,193 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "#height_of_formatted with inline styling" do
6
+ before(:each) { create_pdf }
7
+
8
+ it "should return the height that would be required to print a" +
9
+ "particular string of text" do
10
+ original_y = @pdf.y
11
+ array = [:text => "Foo"]
12
+ @pdf.formatted_text(array)
13
+ new_y = @pdf.y
14
+ @pdf.height_of_formatted(array,
15
+ :width => 300).should.be.close(original_y - new_y, 0.0001)
16
+ end
17
+
18
+ it "should raise CannotFit if a too-small width is given" do
19
+ lambda do
20
+ @pdf.height_of_formatted([:text => "hai"], :width => 1)
21
+ end.should.raise(Prawn::Errors::CannotFit)
22
+ end
23
+
24
+ it "should raise NotImplementedError if :indent_paragraphs option is" +
25
+ "provided" do
26
+ lambda {
27
+ @pdf.height_of_formatted([:text => "hai"], :width => 300,
28
+ :indent_paragraphs => 60)
29
+ }.should.raise(NotImplementedError)
30
+ end
31
+
32
+ it "should not raise Prawn::Errors::UnknownOption if :final_gap option" +
33
+ "is provided" do
34
+ lambda {
35
+ @pdf.height_of_formatted([:text => "hai"], :width => 300,
36
+ :final_gap => true)
37
+ }.should.not.raise(Prawn::Errors::UnknownOption)
38
+ end
39
+ end
40
+
41
+ describe "#formatted_text" do
42
+ it "should draw text" do
43
+ create_pdf
44
+ string = "hello world"
45
+ format_array = [:text => string]
46
+ @pdf.formatted_text(format_array)
47
+ # grab the text from the rendered PDF and ensure it matches
48
+ text = PDF::Inspector::Text.analyze(@pdf.render)
49
+ text.strings.first.should == string
50
+ end
51
+ end
52
+
53
+ describe "#text with inline styling" do
54
+ before(:each) { create_pdf }
55
+
56
+ it "should advance down the document based on font_height" do
57
+ position = @pdf.y
58
+ @pdf.text "Foo", :inline_format => true
59
+
60
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
61
+
62
+ position = @pdf.y
63
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
64
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
65
+ end
66
+
67
+ it "should advance down the document based on font_height" +
68
+ " with size option" do
69
+ position = @pdf.y
70
+ @pdf.text "Foo", :size => 15, :inline_format => true
71
+
72
+ @pdf.font_size = 15
73
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
74
+
75
+ position = @pdf.y
76
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
77
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
78
+ end
79
+
80
+ it "should advance down the document based on font_height" +
81
+ " with leading option" do
82
+ position = @pdf.y
83
+ leading = 2
84
+ @pdf.text "Foo", :leading => leading, :inline_format => true
85
+
86
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
87
+
88
+ position = @pdf.y
89
+ @pdf.text "Foo\nBar\nBaz", :inline_format => true
90
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
91
+ end
92
+
93
+ it "should advance down the document based on font ascender only "+
94
+ "if final_gap is given" do
95
+ position = @pdf.y
96
+ @pdf.text "Foo", :final_gap => false, :inline_format => true
97
+
98
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
99
+
100
+ position = @pdf.y
101
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false, :inline_format => true
102
+ @pdf.y.should.be.close(position -
103
+ 2*@pdf.font.height -
104
+ @pdf.font.ascender, 0.0001)
105
+ end
106
+
107
+ it "should be able to print text starting at the last line of a page" do
108
+ @pdf.move_cursor_to(@pdf.font.height)
109
+ @pdf.text("hello world", :inline_format => true)
110
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
111
+ pages.size.should == 1
112
+ end
113
+
114
+ it "should automatically move to a new page if the tallest fragment" +
115
+ " on the next line won't fit in the available space" do
116
+ create_pdf
117
+ @pdf.move_cursor_to(@pdf.font.height)
118
+ formatted = "this contains <font size='24'>sized</font> text"
119
+ @pdf.text(formatted, :inline_format => true)
120
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
121
+ pages.size.should == 2
122
+ end
123
+
124
+ it "should raise an exception when an unknown font is used" do
125
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
126
+ end
127
+
128
+ it "should correctly render a utf-8 string when using a built-in font" do
129
+ str = "©" # copyright symbol
130
+ @pdf.text str, :inline_format => true
131
+
132
+ # grab the text from the rendered PDF and ensure it matches
133
+ text = PDF::Inspector::Text.analyze(@pdf.render)
134
+ text.strings.first.should == str
135
+ end
136
+
137
+ it "should correctly render a string with higher bit characters across" +
138
+ " a page break when using a built-in font" do
139
+ str = "©"
140
+ @pdf.move_cursor_to(@pdf.font.height)
141
+ @pdf.text(str + "\n" + str, :inline_format => true)
142
+
143
+ # grab the text from the rendered PDF and ensure it matches
144
+ text = PDF::Inspector::Text.analyze(@pdf.render)
145
+ text.strings[1].should == str.strip
146
+ end
147
+
148
+ it "should correctly render a string with higher bit characters across" +
149
+ " a page break when using a built-in font and :indent_paragraphs option" do
150
+ str = "©"
151
+ @pdf.move_cursor_to(@pdf.font.height)
152
+ @pdf.text(str + "\n" + str,
153
+ :indent_paragraphs => 20,
154
+ :inline_format => true)
155
+
156
+ # grab the text from the rendered PDF and ensure it matches
157
+ text = PDF::Inspector::Text.analyze(@pdf.render)
158
+ text.strings[1].should == str.strip
159
+ end
160
+
161
+ if "spec".respond_to?(:encode!)
162
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
163
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
164
+ str = "Blah \xDD"
165
+ str.force_encoding("ASCII-8BIT")
166
+ lambda { @pdf.text str,
167
+ :inline_format => true }.should.raise(ArgumentError)
168
+ end
169
+ it "should not raise an exception when a shift-jis string is rendered" do
170
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
171
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
172
+ @pdf.font_families["gkai00mp"] = {
173
+ :normal => { :file => "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf",
174
+ :font => "gkai00mp" }
175
+ }
176
+ @pdf.font("gkai00mp")
177
+ lambda { @pdf.text sjis_str,
178
+ :inline_format => true }.should.not.raise(ArgumentError)
179
+ end
180
+ else
181
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
182
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
183
+ str = "Blah \xDD"
184
+ lambda { @pdf.text str,
185
+ :inline_format => true }.should.raise(ArgumentError)
186
+ end
187
+ it "should raise an exception when a shift-jis string is rendered" do
188
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
189
+ lambda { @pdf.text sjis_str,
190
+ :inline_format => true }.should.raise(ArgumentError)
191
+ end
192
+ end
193
+ end