alphasights-prawn 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) 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/resources_as_indirect_object.pdf +83 -0
  62. data/data/pdfs/two_hexagons.pdf +90 -0
  63. data/data/pdfs/version_1_6.pdf +61 -0
  64. data/data/shift_jis_text.txt +1 -0
  65. data/examples/bounding_box/bounding_boxes.rb +43 -0
  66. data/examples/bounding_box/indentation.rb +34 -0
  67. data/examples/bounding_box/russian_boxes.rb +36 -0
  68. data/examples/bounding_box/stretched_nesting.rb +67 -0
  69. data/examples/builder/simple.rb +28 -0
  70. data/examples/example_helper.rb +4 -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 +50 -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/chunkable.rb +38 -0
  87. data/examples/graphics/cmyk.rb +12 -0
  88. data/examples/graphics/curves.rb +11 -0
  89. data/examples/graphics/hexagon.rb +13 -0
  90. data/examples/graphics/image_fit.rb +15 -0
  91. data/examples/graphics/image_flow.rb +37 -0
  92. data/examples/graphics/image_position.rb +17 -0
  93. data/examples/graphics/line.rb +32 -0
  94. data/examples/graphics/png_types.rb +22 -0
  95. data/examples/graphics/polygons.rb +16 -0
  96. data/examples/graphics/remote_images.rb +12 -0
  97. data/examples/graphics/rounded_polygons.rb +19 -0
  98. data/examples/graphics/rounded_rectangle.rb +20 -0
  99. data/examples/graphics/ruport_style_helpers.rb +19 -0
  100. data/examples/graphics/stroke_bounds.rb +20 -0
  101. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  102. data/examples/graphics/stroke_dash.rb +42 -0
  103. data/examples/graphics/transformations.rb +52 -0
  104. data/examples/graphics/transparency.rb +26 -0
  105. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  106. data/examples/m17n/euro.rb +15 -0
  107. data/examples/m17n/sjis.rb +28 -0
  108. data/examples/m17n/utf8.rb +13 -0
  109. data/examples/m17n/win_ansi_charset.rb +54 -0
  110. data/examples/security/hello_foo.rb +8 -0
  111. data/examples/table/bill.rb +53 -0
  112. data/examples/table/cell.rb +12 -0
  113. data/examples/table/checkerboard.rb +22 -0
  114. data/examples/table/header.rb +14 -0
  115. data/examples/table/inline_format_table.rb +12 -0
  116. data/examples/table/multi_page_table.rb +9 -0
  117. data/examples/table/simple_table.rb +24 -0
  118. data/examples/table/subtable.rb +12 -0
  119. data/examples/table/widths.rb +20 -0
  120. data/examples/text/alignment.rb +18 -0
  121. data/examples/text/character_spacing.rb +12 -0
  122. data/examples/text/dfont.rb +48 -0
  123. data/examples/text/family_based_styling.rb +24 -0
  124. data/examples/text/font_calculations.rb +91 -0
  125. data/examples/text/font_size.rb +33 -0
  126. data/examples/text/hyphenation.rb +45 -0
  127. data/examples/text/indent_paragraphs.rb +22 -0
  128. data/examples/text/inline_format.rb +103 -0
  129. data/examples/text/kerning.rb +30 -0
  130. data/examples/text/rotated.rb +98 -0
  131. data/examples/text/shaped_text_box.rb +31 -0
  132. data/examples/text/simple_text.rb +17 -0
  133. data/examples/text/simple_text_ttf.rb +17 -0
  134. data/examples/text/text_box.rb +88 -0
  135. data/examples/text/text_box_returning_excess.rb +51 -0
  136. data/examples/text/text_flow.rb +67 -0
  137. data/lib/prawn.rb +27 -0
  138. data/lib/prawn/canvas.rb +119 -0
  139. data/lib/prawn/chunkable.rb +37 -0
  140. data/lib/prawn/compatibility.rb +51 -0
  141. data/lib/prawn/core.rb +85 -0
  142. data/lib/prawn/core/annotations.rb +61 -0
  143. data/lib/prawn/core/byte_string.rb +9 -0
  144. data/lib/prawn/core/chunk.rb +36 -0
  145. data/lib/prawn/core/destinations.rb +90 -0
  146. data/lib/prawn/core/document_state.rb +78 -0
  147. data/lib/prawn/core/literal_string.rb +16 -0
  148. data/lib/prawn/core/name_tree.rb +165 -0
  149. data/lib/prawn/core/object_store.rb +236 -0
  150. data/lib/prawn/core/page.rb +179 -0
  151. data/lib/prawn/core/pdf_object.rb +108 -0
  152. data/lib/prawn/core/reference.rb +112 -0
  153. data/lib/prawn/core/text.rb +140 -0
  154. data/lib/prawn/core/text/formatted/arranger.rb +266 -0
  155. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  156. data/lib/prawn/core/text/formatted/wrap.rb +112 -0
  157. data/lib/prawn/core/text/line_wrap.rb +209 -0
  158. data/lib/prawn/core/text/wrap.rb +80 -0
  159. data/lib/prawn/document.rb +573 -0
  160. data/lib/prawn/document/bounding_box.rb +425 -0
  161. data/lib/prawn/document/graphics_state.rb +48 -0
  162. data/lib/prawn/document/internals.rb +170 -0
  163. data/lib/prawn/document/page_geometry.rb +136 -0
  164. data/lib/prawn/document/snapshot.rb +87 -0
  165. data/lib/prawn/document_builder.rb +51 -0
  166. data/lib/prawn/document_builder/command.rb +38 -0
  167. data/lib/prawn/document_builder/constructs.rb +2 -0
  168. data/lib/prawn/document_builder/constructs/flowing_text_construct.rb +18 -0
  169. data/lib/prawn/document_builder/constructs/path_construct.rb +9 -0
  170. data/lib/prawn/document_builder/layout.rb +25 -0
  171. data/lib/prawn/document_builder/modifications.rb +2 -0
  172. data/lib/prawn/document_builder/modifications/layout_modification.rb +9 -0
  173. data/lib/prawn/document_builder/modifications/path_modification.rb +9 -0
  174. data/lib/prawn/encoding.rb +121 -0
  175. data/lib/prawn/errors.rb +94 -0
  176. data/lib/prawn/font.rb +341 -0
  177. data/lib/prawn/font/afm.rb +225 -0
  178. data/lib/prawn/font/dfont.rb +42 -0
  179. data/lib/prawn/font/ttf.rb +350 -0
  180. data/lib/prawn/graphics.rb +325 -0
  181. data/lib/prawn/graphics/cap_style.rb +38 -0
  182. data/lib/prawn/graphics/color.rb +205 -0
  183. data/lib/prawn/graphics/dash.rb +71 -0
  184. data/lib/prawn/graphics/join_style.rb +38 -0
  185. data/lib/prawn/graphics/transformation.rb +156 -0
  186. data/lib/prawn/graphics/transparency.rb +99 -0
  187. data/lib/prawn/images.rb +348 -0
  188. data/lib/prawn/images/jpg.rb +46 -0
  189. data/lib/prawn/images/png.rb +226 -0
  190. data/lib/prawn/measurement_extensions.rb +46 -0
  191. data/lib/prawn/measurements.rb +71 -0
  192. data/lib/prawn/outline.rb +278 -0
  193. data/lib/prawn/repeater.rb +129 -0
  194. data/lib/prawn/security.rb +262 -0
  195. data/lib/prawn/security/arcfour.rb +51 -0
  196. data/lib/prawn/stamp.rb +126 -0
  197. data/lib/prawn/table.rb +421 -0
  198. data/lib/prawn/table/accessors.rb +180 -0
  199. data/lib/prawn/table/cell.rb +350 -0
  200. data/lib/prawn/table/cell/in_table.rb +27 -0
  201. data/lib/prawn/table/cell/subtable.rb +65 -0
  202. data/lib/prawn/table/cell/text.rb +125 -0
  203. data/lib/prawn/text.rb +449 -0
  204. data/lib/prawn/text/box.rb +392 -0
  205. data/lib/prawn/text/formatted.rb +4 -0
  206. data/lib/prawn/text/formatted/box.rb +228 -0
  207. data/lib/prawn/text/formatted/fragment.rb +181 -0
  208. data/lib/prawn/text/formatted/parser.rb +213 -0
  209. data/spec/annotations_spec.rb +90 -0
  210. data/spec/bounding_box_spec.rb +190 -0
  211. data/spec/cell_spec.rb +348 -0
  212. data/spec/destinations_spec.rb +15 -0
  213. data/spec/document_spec.rb +473 -0
  214. data/spec/font_spec.rb +324 -0
  215. data/spec/formatted_text_arranger_spec.rb +426 -0
  216. data/spec/formatted_text_box_spec.rb +756 -0
  217. data/spec/formatted_text_fragment_spec.rb +211 -0
  218. data/spec/graphics_spec.rb +446 -0
  219. data/spec/images_spec.rb +96 -0
  220. data/spec/inline_formatted_text_parser_spec.rb +502 -0
  221. data/spec/jpg_spec.rb +25 -0
  222. data/spec/line_wrap_spec.rb +341 -0
  223. data/spec/measurement_units_spec.rb +23 -0
  224. data/spec/name_tree_spec.rb +112 -0
  225. data/spec/object_store_spec.rb +160 -0
  226. data/spec/outline_spec.rb +269 -0
  227. data/spec/pdf_object_spec.rb +170 -0
  228. data/spec/png_spec.rb +237 -0
  229. data/spec/reference_spec.rb +82 -0
  230. data/spec/repeater_spec.rb +96 -0
  231. data/spec/security_spec.rb +120 -0
  232. data/spec/snapshot_spec.rb +138 -0
  233. data/spec/spec_helper.rb +26 -0
  234. data/spec/stamp_spec.rb +108 -0
  235. data/spec/stroke_styles_spec.rb +163 -0
  236. data/spec/table_spec.rb +598 -0
  237. data/spec/template_spec.rb +158 -0
  238. data/spec/text_at_spec.rb +119 -0
  239. data/spec/text_box_spec.rb +742 -0
  240. data/spec/text_spacing_spec.rb +75 -0
  241. data/spec/text_spec.rb +333 -0
  242. data/spec/text_with_inline_formatting_spec.rb +193 -0
  243. data/spec/transparency_spec.rb +89 -0
  244. metadata +331 -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,333 @@
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 default to use kerning information" do
47
+ @pdf.text "hello world"
48
+ text = PDF::Inspector::Text.analyze(@pdf.render)
49
+ text.kerned[0].should.be true
50
+ end
51
+
52
+ it "should be able to disable kerning with an option" do
53
+ @pdf.text "hello world", :kerning => false
54
+ text = PDF::Inspector::Text.analyze(@pdf.render)
55
+ text.kerned[0].should.be false
56
+ end
57
+
58
+ it "should be able to disable kerning document wide" do
59
+ @pdf.default_kerning(false)
60
+ @pdf.default_kerning = false
61
+ @pdf.text "hello world"
62
+ text = PDF::Inspector::Text.analyze(@pdf.render)
63
+ text.kerned[0].should.be false
64
+ end
65
+
66
+ it "option should be able to override document wide kerning disabling" do
67
+ @pdf.default_kerning = false
68
+ @pdf.text "hello world", :kerning => true
69
+ text = PDF::Inspector::Text.analyze(@pdf.render)
70
+ text.kerned[0].should.be true
71
+ end
72
+
73
+ it "should raise ArgumentError if :at option included" do
74
+ lambda { @pdf.text("hai", :at => [0, 0]) }.should.raise(ArgumentError)
75
+ end
76
+
77
+ it "should advance down the document based on font_height" do
78
+ position = @pdf.y
79
+ @pdf.text "Foo"
80
+
81
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
82
+
83
+ position = @pdf.y
84
+ @pdf.text "Foo\nBar\nBaz"
85
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
86
+ end
87
+
88
+ it "should advance down the document based on font_height" +
89
+ " with size option" do
90
+ position = @pdf.y
91
+ @pdf.text "Foo", :size => 15
92
+
93
+ @pdf.font_size = 15
94
+ @pdf.y.should.be.close(position - @pdf.font.height, 0.0001)
95
+
96
+ position = @pdf.y
97
+ @pdf.text "Foo\nBar\nBaz"
98
+ @pdf.y.should.be.close(position - 3 * @pdf.font.height, 0.0001)
99
+ end
100
+
101
+ it "should advance down the document based on font_height" +
102
+ " with leading option" do
103
+ position = @pdf.y
104
+ leading = 2
105
+ @pdf.text "Foo", :leading => leading
106
+
107
+ @pdf.y.should.be.close(position - @pdf.font.height - leading, 0.0001)
108
+
109
+ position = @pdf.y
110
+ @pdf.text "Foo\nBar\nBaz"
111
+ @pdf.y.should.be.close(position - 3*@pdf.font.height, 0.0001)
112
+ end
113
+
114
+ it "should advance down the document based on font ascender only "+
115
+ "if final_gap is given" do
116
+ position = @pdf.y
117
+ @pdf.text "Foo", :final_gap => false
118
+
119
+ @pdf.y.should.be.close(position - @pdf.font.ascender, 0.0001)
120
+
121
+ position = @pdf.y
122
+ @pdf.text "Foo\nBar\nBaz", :final_gap => false
123
+ @pdf.y.should.be.close(position - 2*@pdf.font.height - @pdf.font.ascender, 0.0001)
124
+ end
125
+
126
+ it "should be able to print text starting at the last line of a page" do
127
+ @pdf.move_cursor_to(@pdf.font.height)
128
+ @pdf.text("hello world")
129
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
130
+ pages.size.should == 1
131
+ end
132
+
133
+ it "should default to 12 point helvetica" do
134
+ @pdf.text "Blah"
135
+ text = PDF::Inspector::Text.analyze(@pdf.render)
136
+ text.font_settings[0][:name].should == :Helvetica
137
+ text.font_settings[0][:size].should == 12
138
+ text.strings.first.should == "Blah"
139
+ end
140
+
141
+ it "should allow setting font size" do
142
+ @pdf.text "Blah", :size => 16
143
+ text = PDF::Inspector::Text.analyze(@pdf.render)
144
+ text.font_settings[0][:size].should == 16
145
+ end
146
+
147
+ it "should allow setting a default font size" do
148
+ @pdf.font_size = 16
149
+ @pdf.text "Blah"
150
+ text = PDF::Inspector::Text.analyze(@pdf.render)
151
+ text.font_settings[0][:size].should == 16
152
+ end
153
+
154
+ it "should allow overriding default font for a single instance" do
155
+ @pdf.font_size = 16
156
+
157
+ @pdf.text "Blah", :size => 11
158
+ @pdf.text "Blaz"
159
+ text = PDF::Inspector::Text.analyze(@pdf.render)
160
+ text.font_settings[0][:size].should == 11
161
+ text.font_settings[1][:size].should == 16
162
+ end
163
+
164
+ it "should allow setting a font size transaction with a block" do
165
+ @pdf.font_size 16 do
166
+ @pdf.text 'Blah'
167
+ end
168
+
169
+ @pdf.text 'blah'
170
+
171
+ text = PDF::Inspector::Text.analyze(@pdf.render)
172
+ text.font_settings[0][:size].should == 16
173
+ text.font_settings[1][:size].should == 12
174
+ end
175
+
176
+ it "should allow manual setting the font size " +
177
+ "when in a font size block" do
178
+ @pdf.font_size(16) do
179
+ @pdf.text 'Foo'
180
+ @pdf.text 'Blah', :size => 11
181
+ @pdf.text 'Blaz'
182
+ end
183
+ text = PDF::Inspector::Text.analyze(@pdf.render)
184
+ text.font_settings[0][:size].should == 16
185
+ text.font_settings[1][:size].should == 11
186
+ text.font_settings[2][:size].should == 16
187
+ end
188
+
189
+ it "should allow registering of built-in font_settings on the fly" do
190
+ @pdf.font "Times-Roman"
191
+ @pdf.text "Blah"
192
+ @pdf.font "Courier"
193
+ @pdf.text "Blaz"
194
+ text = PDF::Inspector::Text.analyze(@pdf.render)
195
+ text.font_settings[0][:name].should == :"Times-Roman"
196
+ text.font_settings[1][:name].should == :Courier
197
+ end
198
+
199
+ it "should utilise the same default font across multiple pages" do
200
+ @pdf.text "Blah"
201
+ @pdf.start_new_page
202
+ @pdf.text "Blaz"
203
+ text = PDF::Inspector::Text.analyze(@pdf.render)
204
+
205
+ text.font_settings.size.should == 2
206
+ text.font_settings[0][:name].should == :Helvetica
207
+ text.font_settings[1][:name].should == :Helvetica
208
+ end
209
+
210
+ it "should raise an exception when an unknown font is used" do
211
+ lambda { @pdf.font "Pao bu" }.should.raise(Prawn::Errors::UnknownFont)
212
+ end
213
+
214
+ it "should correctly render a utf-8 string when using a built-in font" do
215
+ str = "©" # copyright symbol
216
+ @pdf.text str
217
+
218
+ # grab the text from the rendered PDF and ensure it matches
219
+ text = PDF::Inspector::Text.analyze(@pdf.render)
220
+ text.strings.first.should == str
221
+ end
222
+
223
+ it "should correctly render a utf-8 string when using a TTF font" do
224
+ str = "©" # copyright symbol
225
+ @pdf.font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
226
+ @pdf.text str
227
+
228
+ # grab the text from the rendered PDF and ensure it matches
229
+ text = PDF::Inspector::Text.analyze(@pdf.render)
230
+ text.strings.first.should == str
231
+ end
232
+
233
+ it "should correctly render a string with higher bit characters across" +
234
+ " a page break when using a built-in font" do
235
+ str = "©"
236
+ @pdf.move_cursor_to(@pdf.font.height)
237
+ @pdf.text(str + "\n" + str)
238
+
239
+ # grab the text from the rendered PDF and ensure it matches
240
+ text = PDF::Inspector::Text.analyze(@pdf.render)
241
+ text.strings[1].should == str.strip
242
+ end
243
+
244
+ it "should correctly render a string with higher bit characters across" +
245
+ " a page break when using a built-in font and :indent_paragraphs option" do
246
+ str = "©"
247
+ @pdf.move_cursor_to(@pdf.font.height)
248
+ @pdf.text(str + "\n" + str, :indent_paragraphs => 20)
249
+
250
+ # grab the text from the rendered PDF and ensure it matches
251
+ text = PDF::Inspector::Text.analyze(@pdf.render)
252
+ text.strings[1].should == str.strip
253
+ end
254
+
255
+ if "spec".respond_to?(:encode!)
256
+ # Handle non utf-8 string encodings in a sane way on M17N aware VMs
257
+ it "should raise an exception when a utf-8 incompatible string is rendered" do
258
+ str = "Blah \xDD"
259
+ str.force_encoding("ASCII-8BIT")
260
+ lambda { @pdf.text str }.should.raise(ArgumentError)
261
+ end
262
+ it "should not raise an exception when a shift-jis string is rendered" do
263
+ datafile = "#{Prawn::BASEDIR}/data/shift_jis_text.txt"
264
+ sjis_str = File.open(datafile, "r:shift_jis") { |f| f.gets }
265
+ @pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf")
266
+ lambda { @pdf.text sjis_str }.should.not.raise(ArgumentError)
267
+ end
268
+ else
269
+ # Handle non utf-8 string encodings in a sane way on non-M17N aware VMs
270
+ it "should raise an exception when a corrupt utf-8 string is rendered" do
271
+ str = "Blah \xDD"
272
+ lambda { @pdf.text str }.should.raise(ArgumentError)
273
+ end
274
+ it "should raise an exception when a shift-jis string is rendered" do
275
+ sjis_str = File.read("#{Prawn::BASEDIR}/data/shift_jis_text.txt")
276
+ lambda { @pdf.text sjis_str }.should.raise(ArgumentError)
277
+ end
278
+ end
279
+
280
+ it "should call move_past_bottom when printing more text than can fit" +
281
+ " between the current document.y and bounds.bottom" do
282
+ @pdf.y = @pdf.font.height
283
+ @pdf.text "Hello"
284
+ @pdf.text "World"
285
+ pages = PDF::Inspector::Page.analyze(@pdf.render).pages
286
+ pages.size.should == 2
287
+ pages[0][:strings].should == ["Hello"]
288
+ pages[1][:strings].should == ["World"]
289
+ end
290
+
291
+ describe "with :indent_paragraphs option" do
292
+ it "should indent the paragraphs" do
293
+ hello = "hello " * 50
294
+ hello2 = "hello " * 50
295
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
296
+ text = PDF::Inspector::Text.analyze(@pdf.render)
297
+ text.strings[0].should == ("hello " * 19).strip
298
+ text.strings[1].should == ("hello " * 21).strip
299
+ text.strings[3].should == ("hello " * 19).strip
300
+ text.strings[4].should == ("hello " * 21).strip
301
+ end
302
+ describe "when wrap to new page, and first line of new page" +
303
+ " is not the start of a new paragraph, that line should" +
304
+ " not be indented" do
305
+ it "should indent the paragraphs" do
306
+ hello = "hello " * 50
307
+ hello2 = "hello " * 50
308
+ @pdf.move_cursor_to(@pdf.font.height)
309
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
310
+ text = PDF::Inspector::Text.analyze(@pdf.render)
311
+ text.strings[0].should == ("hello " * 19).strip
312
+ text.strings[1].should == ("hello " * 21).strip
313
+ text.strings[3].should == ("hello " * 19).strip
314
+ text.strings[4].should == ("hello " * 21).strip
315
+ end
316
+ end
317
+ describe "when wrap to new page, and first line of new page" +
318
+ " is the start of a new paragraph, that line should" +
319
+ " be indented" do
320
+ it "should indent the paragraphs" do
321
+ hello = "hello " * 50
322
+ hello2 = "hello " * 50
323
+ @pdf.move_cursor_to(@pdf.font.height * 3)
324
+ @pdf.text(hello + "\n" + hello2, :indent_paragraphs => 60)
325
+ text = PDF::Inspector::Text.analyze(@pdf.render)
326
+ text.strings[0].should == ("hello " * 19).strip
327
+ text.strings[1].should == ("hello " * 21).strip
328
+ text.strings[3].should == ("hello " * 19).strip
329
+ text.strings[4].should == ("hello " * 21).strip
330
+ end
331
+ end
332
+ end
333
+ 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