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,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,349 @@
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 allow drawing empty strings to the page" do
56
+ @pdf.text " "
57
+ text = PDF::Inspector::Text.analyze(@pdf.render)
58
+ # If anything is rendered to the page, it should be whitespace.
59
+ text.strings.each { |str| str.should =~ /\A\s*\z/ }
60
+ end
61
+
62
+ it "should default to use kerning information" do
63
+ @pdf.text "hello world"
64
+ text = PDF::Inspector::Text.analyze(@pdf.render)
65
+ text.kerned[0].should.be true
66
+ end
67
+
68
+ it "should be able to disable kerning with an option" do
69
+ @pdf.text "hello world", :kerning => false
70
+ text = PDF::Inspector::Text.analyze(@pdf.render)
71
+ text.kerned[0].should.be false
72
+ end
73
+
74
+ it "should be able to disable kerning document wide" do
75
+ @pdf.default_kerning(false)
76
+ @pdf.default_kerning = false
77
+ @pdf.text "hello world"
78
+ text = PDF::Inspector::Text.analyze(@pdf.render)
79
+ text.kerned[0].should.be false
80
+ end
81
+
82
+ it "option should be able to override document wide kerning disabling" do
83
+ @pdf.default_kerning = false
84
+ @pdf.text "hello world", :kerning => true
85
+ text = PDF::Inspector::Text.analyze(@pdf.render)
86
+ text.kerned[0].should.be true
87
+ end
88
+
89
+ it "should raise ArgumentError if :at option included" do
90
+ lambda { @pdf.text("hai", :at => [0, 0]) }.should.raise(ArgumentError)
91
+ end
92
+
93
+ it "should advance down the document based on font_height" do
94
+ position = @pdf.y
95
+ @pdf.text "Foo"
96
+
97
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
98
+
99
+ position = @pdf.y
100
+ @pdf.text "Foo\nBar\nBaz"
101
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
102
+ end
103
+
104
+ it "should advance down the document based on font_height" +
105
+ " with size option" do
106
+ position = @pdf.y
107
+ @pdf.text "Foo", :size => 15
108
+
109
+ @pdf.font_size = 15
110
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
111
+
112
+ position = @pdf.y
113
+ @pdf.text "Foo\nBar\nBaz"
114
+ @pdf.y.should.be.close(position - 3 * @pdf.font.height, 0.0001)
115
+ end
116
+
117
+ it "should advance down the document based on font_height" +
118
+ " with leading option" do
119
+ position = @pdf.y
120
+ leading = 2
121
+ @pdf.text "Foo", :leading => leading
122
+
123
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
124
+
125
+ position = @pdf.y
126
+ @pdf.text "Foo\nBar\nBaz"
127
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
128
+ end
129
+
130
+ it "should advance down the document based on font ascender only "+
131
+ "if final_gap is given" do
132
+ position = @pdf.y
133
+ @pdf.text "Foo", :final_gap => false
134
+
135
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
136
+
137
+ position = @pdf.y
138
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false
139
+ @pdf.y.should.be.close(position - 2*@pdf.font.height - @pdf.font.ascender, 0.0001)
140
+ end
141
+
142
+ it "should be able to print text starting at the last line of a page" do
143
+ @pdf.move_cursor_to(@pdf.font.height)
144
+ @pdf.text("hello world")
145
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
146
+ pages.size.should == 1
147
+ end
148
+
149
+ it "should default to 12 point helvetica" do
150
+ @pdf.text "Blah"
151
+ text = PDF::Inspector::Text.analyze(@pdf.render)
152
+ text.font_settings[0][:name].should == :Helvetica
153
+ text.font_settings[0][:size].should == 12
154
+ text.strings.first.should == "Blah"
155
+ end
156
+
157
+ it "should allow setting font size" do
158
+ @pdf.text "Blah", :size => 16
159
+ text = PDF::Inspector::Text.analyze(@pdf.render)
160
+ text.font_settings[0][:size].should == 16
161
+ end
162
+
163
+ it "should allow setting a default font size" do
164
+ @pdf.font_size = 16
165
+ @pdf.text "Blah"
166
+ text = PDF::Inspector::Text.analyze(@pdf.render)
167
+ text.font_settings[0][:size].should == 16
168
+ end
169
+
170
+ it "should allow overriding default font for a single instance" do
171
+ @pdf.font_size = 16
172
+
173
+ @pdf.text "Blah", :size => 11
174
+ @pdf.text "Blaz"
175
+ text = PDF::Inspector::Text.analyze(@pdf.render)
176
+ text.font_settings[0][:size].should == 11
177
+ text.font_settings[1][:size].should == 16
178
+ end
179
+
180
+ it "should allow setting a font size transaction with a block" do
181
+ @pdf.font_size 16 do
182
+ @pdf.text 'Blah'
183
+ end
184
+
185
+ @pdf.text 'blah'
186
+
187
+ text = PDF::Inspector::Text.analyze(@pdf.render)
188
+ text.font_settings[0][:size].should == 16
189
+ text.font_settings[1][:size].should == 12
190
+ end
191
+
192
+ it "should allow manual setting the font size " +
193
+ "when in a font size block" do
194
+ @pdf.font_size(16) do
195
+ @pdf.text 'Foo'
196
+ @pdf.text 'Blah', :size => 11
197
+ @pdf.text 'Blaz'
198
+ end
199
+ text = PDF::Inspector::Text.analyze(@pdf.render)
200
+ text.font_settings[0][:size].should == 16
201
+ text.font_settings[1][:size].should == 11
202
+ text.font_settings[2][:size].should == 16
203
+ end
204
+
205
+ it "should allow registering of built-in font_settings on the fly" do
206
+ @pdf.font "Times-Roman"
207
+ @pdf.text "Blah"
208
+ @pdf.font "Courier"
209
+ @pdf.text "Blaz"
210
+ text = PDF::Inspector::Text.analyze(@pdf.render)
211
+ text.font_settings[0][:name].should == :"Times-Roman"
212
+ text.font_settings[1][:name].should == :Courier
213
+ end
214
+
215
+ it "should utilise the same default font across multiple pages" do
216
+ @pdf.text "Blah"
217
+ @pdf.start_new_page
218
+ @pdf.text "Blaz"
219
+ text = PDF::Inspector::Text.analyze(@pdf.render)
220
+
221
+ text.font_settings.size.should == 2
222
+ text.font_settings[0][:name].should == :Helvetica
223
+ text.font_settings[1][:name].should == :Helvetica
224
+ end
225
+
226
+ it "should raise an exception when an unknown font is used" do
227
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
228
+ end
229
+
230
+ it "should correctly render a utf-8 string when using a built-in font" do
231
+ str = "©" # copyright symbol
232
+ @pdf.text str
233
+
234
+ # grab the text from the rendered PDF and ensure it matches
235
+ text = PDF::Inspector::Text.analyze(@pdf.render)
236
+ text.strings.first.should == str
237
+ end
238
+
239
+ it "should correctly render a utf-8 string when using a TTF font" do
240
+ str = "©" # copyright symbol
241
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
242
+ @pdf.text str
243
+
244
+ # grab the text from the rendered PDF and ensure it matches
245
+ text = PDF::Inspector::Text.analyze(@pdf.render)
246
+ text.strings.first.should == str
247
+ end
248
+
249
+ it "should correctly render a string with higher bit characters across" +
250
+ " a page break when using a built-in font" do
251
+ str = "©"
252
+ @pdf.move_cursor_to(@pdf.font.height)
253
+ @pdf.text(str + "\n" + str)
254
+
255
+ # grab the text from the rendered PDF and ensure it matches
256
+ text = PDF::Inspector::Text.analyze(@pdf.render)
257
+ text.strings[1].should == str.strip
258
+ end
259
+
260
+ it "should correctly render a string with higher bit characters across" +
261
+ " a page break when using a built-in font and :indent_paragraphs option" do
262
+ str = "©"
263
+ @pdf.move_cursor_to(@pdf.font.height)
264
+ @pdf.text(str + "\n" + str, :indent_paragraphs => 20)
265
+
266
+ # grab the text from the rendered PDF and ensure it matches
267
+ text = PDF::Inspector::Text.analyze(@pdf.render)
268
+ text.strings[1].should == str.strip
269
+ end
270
+
271
+ if "spec".respond_to?(:encode!)
272
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
273
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
274
+ str = "Blah \xDD"
275
+ str.force_encoding("ASCII-8BIT")
276
+ lambda { @pdf.text str }.should.raise(ArgumentError)
277
+ end
278
+ it "should not raise an exception when a shift-jis string is rendered" do
279
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
280
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
281
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
282
+ lambda { @pdf.text sjis_str }.should.not.raise(ArgumentError)
283
+ end
284
+ else
285
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
286
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
287
+ str = "Blah \xDD"
288
+ lambda { @pdf.text str }.should.raise(ArgumentError)
289
+ end
290
+ it "should raise an exception when a shift-jis string is rendered" do
291
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
292
+ lambda { @pdf.text sjis_str }.should.raise(ArgumentError)
293
+ end
294
+ end
295
+
296
+ it "should call move_past_bottom when printing more text than can fit" +
297
+ " between the current document.y and bounds.bottom" do
298
+ @pdf.y = @pdf.font.height
299
+ @pdf.text "Hello"
300
+ @pdf.text "World"
301
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
302
+ pages.size.should == 2
303
+ pages[0][:strings].should == ["Hello"]
304
+ pages[1][:strings].should == ["World"]
305
+ end
306
+
307
+ describe "with :indent_paragraphs option" do
308
+ it "should indent the paragraphs" do
309
+ hello = "hello " * 50
310
+ hello2 = "hello " * 50
311
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
312
+ text = PDF::Inspector::Text.analyze(@pdf.render)
313
+ text.strings[0].should == ("hello " * 19).strip
314
+ text.strings[1].should == ("hello " * 21).strip
315
+ text.strings[3].should == ("hello " * 19).strip
316
+ text.strings[4].should == ("hello " * 21).strip
317
+ end
318
+ describe "when wrap to new page, and first line of new page" +
319
+ " is not the start of a new paragraph, that line should" +
320
+ " not be indented" do
321
+ it "should indent the paragraphs" do
322
+ hello = "hello " * 50
323
+ hello2 = "hello " * 50
324
+ @pdf.move_cursor_to(@pdf.font.height)
325
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
326
+ text = PDF::Inspector::Text.analyze(@pdf.render)
327
+ text.strings[0].should == ("hello " * 19).strip
328
+ text.strings[1].should == ("hello " * 21).strip
329
+ text.strings[3].should == ("hello " * 19).strip
330
+ text.strings[4].should == ("hello " * 21).strip
331
+ end
332
+ end
333
+ describe "when wrap to new page, and first line of new page" +
334
+ " is the start of a new paragraph, that line should" +
335
+ " be indented" do
336
+ it "should indent the paragraphs" do
337
+ hello = "hello " * 50
338
+ hello2 = "hello " * 50
339
+ @pdf.move_cursor_to(@pdf.font.height * 3)
340
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
341
+ text = PDF::Inspector::Text.analyze(@pdf.render)
342
+ text.strings[0].should == ("hello " * 19).strip
343
+ text.strings[1].should == ("hello " * 21).strip
344
+ text.strings[3].should == ("hello " * 19).strip
345
+ text.strings[4].should == ("hello " * 21).strip
346
+ end
347
+ end
348
+ end
349
+ 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