prawn-git 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (252) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +10 -0
  3. data/COPYING +2 -0
  4. data/GPLv2 +340 -0
  5. data/GPLv3 +674 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE +56 -0
  8. data/Rakefile +55 -0
  9. data/data/fonts/Courier-Bold.afm +342 -0
  10. data/data/fonts/Courier-BoldOblique.afm +342 -0
  11. data/data/fonts/Courier-Oblique.afm +342 -0
  12. data/data/fonts/Courier.afm +342 -0
  13. data/data/fonts/Helvetica-Bold.afm +2827 -0
  14. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  15. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  16. data/data/fonts/Helvetica.afm +3051 -0
  17. data/data/fonts/MustRead.html +19 -0
  18. data/data/fonts/Symbol.afm +213 -0
  19. data/data/fonts/Times-Bold.afm +2588 -0
  20. data/data/fonts/Times-BoldItalic.afm +2384 -0
  21. data/data/fonts/Times-Italic.afm +2667 -0
  22. data/data/fonts/Times-Roman.afm +2419 -0
  23. data/data/fonts/ZapfDingbats.afm +225 -0
  24. data/data/images/16bit.alpha +0 -0
  25. data/data/images/16bit.color +0 -0
  26. data/data/images/16bit.png +0 -0
  27. data/data/images/arrow.png +0 -0
  28. data/data/images/arrow2.png +0 -0
  29. data/data/images/dice.alpha +0 -0
  30. data/data/images/dice.color +0 -0
  31. data/data/images/dice.png +0 -0
  32. data/data/images/dice_interlaced.png +0 -0
  33. data/data/images/fractal.jpg +0 -0
  34. data/data/images/indexed_color.dat +0 -0
  35. data/data/images/indexed_color.png +0 -0
  36. data/data/images/letterhead.jpg +0 -0
  37. data/data/images/license.md +8 -0
  38. data/data/images/page_white_text.alpha +0 -0
  39. data/data/images/page_white_text.color +0 -0
  40. data/data/images/page_white_text.png +0 -0
  41. data/data/images/pal_bk.png +0 -0
  42. data/data/images/pigs.jpg +0 -0
  43. data/data/images/prawn.png +0 -0
  44. data/data/images/ruport.png +0 -0
  45. data/data/images/ruport_data.dat +0 -0
  46. data/data/images/ruport_transparent.png +0 -0
  47. data/data/images/ruport_type0.png +0 -0
  48. data/data/images/stef.jpg +0 -0
  49. data/data/images/tru256.bmp +0 -0
  50. data/data/images/web-links.dat +1 -0
  51. data/data/images/web-links.png +0 -0
  52. data/data/pdfs/complex_template.pdf +0 -0
  53. data/data/pdfs/contains_ttf_font.pdf +0 -0
  54. data/data/pdfs/encrypted.pdf +0 -0
  55. data/data/pdfs/form.pdf +820 -0
  56. data/data/pdfs/hexagon.pdf +61 -0
  57. data/data/pdfs/indirect_reference.pdf +86 -0
  58. data/data/pdfs/multipage_template.pdf +127 -0
  59. data/data/pdfs/nested_pages.pdf +118 -0
  60. data/data/pdfs/page_without_mediabox.pdf +193 -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/lib/prawn.rb +89 -0
  66. data/lib/prawn/document.rb +706 -0
  67. data/lib/prawn/document/bounding_box.rb +539 -0
  68. data/lib/prawn/document/column_box.rb +144 -0
  69. data/lib/prawn/document/internals.rb +58 -0
  70. data/lib/prawn/document/span.rb +57 -0
  71. data/lib/prawn/encoding.rb +87 -0
  72. data/lib/prawn/errors.rb +80 -0
  73. data/lib/prawn/font.rb +413 -0
  74. data/lib/prawn/font/afm.rb +256 -0
  75. data/lib/prawn/font/dfont.rb +43 -0
  76. data/lib/prawn/font/ttf.rb +355 -0
  77. data/lib/prawn/font_metric_cache.rb +46 -0
  78. data/lib/prawn/graphics.rb +646 -0
  79. data/lib/prawn/graphics/cap_style.rb +47 -0
  80. data/lib/prawn/graphics/color.rb +232 -0
  81. data/lib/prawn/graphics/dash.rb +109 -0
  82. data/lib/prawn/graphics/join_style.rb +49 -0
  83. data/lib/prawn/graphics/patterns.rb +126 -0
  84. data/lib/prawn/graphics/transformation.rb +157 -0
  85. data/lib/prawn/graphics/transparency.rb +101 -0
  86. data/lib/prawn/grid.rb +279 -0
  87. data/lib/prawn/image_handler.rb +44 -0
  88. data/lib/prawn/images.rb +199 -0
  89. data/lib/prawn/images/image.rb +49 -0
  90. data/lib/prawn/images/jpg.rb +91 -0
  91. data/lib/prawn/images/png.rb +290 -0
  92. data/lib/prawn/measurement_extensions.rb +50 -0
  93. data/lib/prawn/measurements.rb +77 -0
  94. data/lib/prawn/outline.rb +289 -0
  95. data/lib/prawn/repeater.rb +124 -0
  96. data/lib/prawn/security.rb +288 -0
  97. data/lib/prawn/security/arcfour.rb +54 -0
  98. data/lib/prawn/soft_mask.rb +94 -0
  99. data/lib/prawn/stamp.rb +136 -0
  100. data/lib/prawn/text.rb +437 -0
  101. data/lib/prawn/text/box.rb +141 -0
  102. data/lib/prawn/text/formatted.rb +7 -0
  103. data/lib/prawn/text/formatted/arranger.rb +290 -0
  104. data/lib/prawn/text/formatted/box.rb +614 -0
  105. data/lib/prawn/text/formatted/fragment.rb +264 -0
  106. data/lib/prawn/text/formatted/line_wrap.rb +277 -0
  107. data/lib/prawn/text/formatted/parser.rb +224 -0
  108. data/lib/prawn/text/formatted/wrap.rb +160 -0
  109. data/lib/prawn/utilities.rb +46 -0
  110. data/lib/prawn/version.rb +5 -0
  111. data/lib/prawn/view.rb +91 -0
  112. data/manual/absolute_position.pdf +0 -0
  113. data/manual/basic_concepts/adding_pages.rb +27 -0
  114. data/manual/basic_concepts/basic_concepts.rb +36 -0
  115. data/manual/basic_concepts/creation.rb +39 -0
  116. data/manual/basic_concepts/cursor.rb +33 -0
  117. data/manual/basic_concepts/measurement.rb +25 -0
  118. data/manual/basic_concepts/origin.rb +38 -0
  119. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  120. data/manual/basic_concepts/view.rb +42 -0
  121. data/manual/bounding_box/bounding_box.rb +39 -0
  122. data/manual/bounding_box/bounds.rb +49 -0
  123. data/manual/bounding_box/canvas.rb +24 -0
  124. data/manual/bounding_box/creation.rb +23 -0
  125. data/manual/bounding_box/indentation.rb +46 -0
  126. data/manual/bounding_box/nesting.rb +45 -0
  127. data/manual/bounding_box/russian_boxes.rb +40 -0
  128. data/manual/bounding_box/stretchy.rb +31 -0
  129. data/manual/contents.rb +29 -0
  130. data/manual/cover.rb +39 -0
  131. data/manual/document_and_page_options/background.rb +27 -0
  132. data/manual/document_and_page_options/document_and_page_options.rb +32 -0
  133. data/manual/document_and_page_options/metadata.rb +23 -0
  134. data/manual/document_and_page_options/page_margins.rb +38 -0
  135. data/manual/document_and_page_options/page_size.rb +34 -0
  136. data/manual/document_and_page_options/print_scaling.rb +20 -0
  137. data/manual/example_helper.rb +7 -0
  138. data/manual/graphics/circle_and_ellipse.rb +22 -0
  139. data/manual/graphics/color.rb +24 -0
  140. data/manual/graphics/common_lines.rb +30 -0
  141. data/manual/graphics/fill_and_stroke.rb +42 -0
  142. data/manual/graphics/fill_rules.rb +37 -0
  143. data/manual/graphics/gradients.rb +37 -0
  144. data/manual/graphics/graphics.rb +58 -0
  145. data/manual/graphics/helper.rb +24 -0
  146. data/manual/graphics/line_width.rb +35 -0
  147. data/manual/graphics/lines_and_curves.rb +41 -0
  148. data/manual/graphics/polygon.rb +29 -0
  149. data/manual/graphics/rectangle.rb +21 -0
  150. data/manual/graphics/rotate.rb +28 -0
  151. data/manual/graphics/scale.rb +41 -0
  152. data/manual/graphics/soft_masks.rb +46 -0
  153. data/manual/graphics/stroke_cap.rb +31 -0
  154. data/manual/graphics/stroke_dash.rb +48 -0
  155. data/manual/graphics/stroke_join.rb +30 -0
  156. data/manual/graphics/translate.rb +29 -0
  157. data/manual/graphics/transparency.rb +35 -0
  158. data/manual/how_to_read_this_manual.rb +40 -0
  159. data/manual/images/absolute_position.rb +23 -0
  160. data/manual/images/fit.rb +21 -0
  161. data/manual/images/horizontal.rb +25 -0
  162. data/manual/images/images.rb +40 -0
  163. data/manual/images/plain_image.rb +18 -0
  164. data/manual/images/scale.rb +22 -0
  165. data/manual/images/vertical.rb +28 -0
  166. data/manual/images/width_and_height.rb +25 -0
  167. data/manual/layout/boxes.rb +27 -0
  168. data/manual/layout/content.rb +25 -0
  169. data/manual/layout/layout.rb +28 -0
  170. data/manual/layout/simple_grid.rb +23 -0
  171. data/manual/outline/add_subsection_to.rb +61 -0
  172. data/manual/outline/insert_section_after.rb +47 -0
  173. data/manual/outline/outline.rb +32 -0
  174. data/manual/outline/sections_and_pages.rb +67 -0
  175. data/manual/repeatable_content/alternate_page_numbering.rb +32 -0
  176. data/manual/repeatable_content/page_numbering.rb +54 -0
  177. data/manual/repeatable_content/repeatable_content.rb +32 -0
  178. data/manual/repeatable_content/repeater.rb +55 -0
  179. data/manual/repeatable_content/stamp.rb +41 -0
  180. data/manual/security/encryption.rb +31 -0
  181. data/manual/security/permissions.rb +38 -0
  182. data/manual/security/security.rb +28 -0
  183. data/manual/table.rb +16 -0
  184. data/manual/text/alignment.rb +44 -0
  185. data/manual/text/color.rb +24 -0
  186. data/manual/text/column_box.rb +32 -0
  187. data/manual/text/fallback_fonts.rb +37 -0
  188. data/manual/text/font.rb +41 -0
  189. data/manual/text/font_size.rb +45 -0
  190. data/manual/text/font_style.rb +23 -0
  191. data/manual/text/formatted_callbacks.rb +60 -0
  192. data/manual/text/formatted_text.rb +50 -0
  193. data/manual/text/free_flowing_text.rb +51 -0
  194. data/manual/text/inline.rb +41 -0
  195. data/manual/text/kerning_and_character_spacing.rb +39 -0
  196. data/manual/text/leading.rb +25 -0
  197. data/manual/text/line_wrapping.rb +41 -0
  198. data/manual/text/paragraph_indentation.rb +34 -0
  199. data/manual/text/positioned_text.rb +38 -0
  200. data/manual/text/registering_families.rb +48 -0
  201. data/manual/text/rendering_and_color.rb +37 -0
  202. data/manual/text/right_to_left_text.rb +47 -0
  203. data/manual/text/rotation.rb +43 -0
  204. data/manual/text/single_usage.rb +37 -0
  205. data/manual/text/text.rb +73 -0
  206. data/manual/text/text_box_excess.rb +32 -0
  207. data/manual/text/text_box_extensions.rb +45 -0
  208. data/manual/text/text_box_overflow.rb +48 -0
  209. data/manual/text/utf8.rb +28 -0
  210. data/manual/text/win_ansi_charset.rb +60 -0
  211. data/prawn.gemspec +45 -0
  212. data/spec/acceptance/png.rb +25 -0
  213. data/spec/annotations_spec.rb +74 -0
  214. data/spec/bounding_box_spec.rb +510 -0
  215. data/spec/column_box_spec.rb +65 -0
  216. data/spec/data/curves.pdf +66 -0
  217. data/spec/destinations_spec.rb +15 -0
  218. data/spec/document_spec.rb +748 -0
  219. data/spec/extensions/encoding_helpers.rb +11 -0
  220. data/spec/extensions/mocha.rb +46 -0
  221. data/spec/font_metric_cache_spec.rb +52 -0
  222. data/spec/font_spec.rb +474 -0
  223. data/spec/formatted_text_arranger_spec.rb +421 -0
  224. data/spec/formatted_text_box_spec.rb +705 -0
  225. data/spec/formatted_text_fragment_spec.rb +298 -0
  226. data/spec/graphics_spec.rb +683 -0
  227. data/spec/grid_spec.rb +96 -0
  228. data/spec/image_handler_spec.rb +54 -0
  229. data/spec/images_spec.rb +153 -0
  230. data/spec/inline_formatted_text_parser_spec.rb +564 -0
  231. data/spec/jpg_spec.rb +25 -0
  232. data/spec/line_wrap_spec.rb +367 -0
  233. data/spec/measurement_units_spec.rb +25 -0
  234. data/spec/outline_spec.rb +430 -0
  235. data/spec/png_spec.rb +245 -0
  236. data/spec/reference_spec.rb +25 -0
  237. data/spec/repeater_spec.rb +160 -0
  238. data/spec/security_spec.rb +158 -0
  239. data/spec/soft_mask_spec.rb +79 -0
  240. data/spec/span_spec.rb +44 -0
  241. data/spec/spec_helper.rb +54 -0
  242. data/spec/stamp_spec.rb +160 -0
  243. data/spec/stroke_styles_spec.rb +211 -0
  244. data/spec/text_at_spec.rb +143 -0
  245. data/spec/text_box_spec.rb +1043 -0
  246. data/spec/text_rendering_mode_spec.rb +45 -0
  247. data/spec/text_spacing_spec.rb +93 -0
  248. data/spec/text_spec.rb +557 -0
  249. data/spec/text_with_inline_formatting_spec.rb +35 -0
  250. data/spec/transparency_spec.rb +91 -0
  251. data/spec/view_spec.rb +43 -0
  252. metadata +509 -0
@@ -0,0 +1,421 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "Core::Text::Formatted::Arranger#format_array" do
6
+ it "should populate unconsumed array" do
7
+ create_pdf
8
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
9
+ array = [{ :text => "hello " },
10
+ { :text => "world how ", :styles => [:bold] },
11
+ { :text => "are", :styles => [:bold, :italic] },
12
+ { :text => " you?" }]
13
+ arranger.format_array = array
14
+ arranger.unconsumed[0].should == { :text => "hello " }
15
+ arranger.unconsumed[1].should == { :text => "world how ",
16
+ :styles => [:bold] }
17
+ arranger.unconsumed[2].should == { :text => "are",
18
+ :styles => [:bold, :italic] }
19
+ arranger.unconsumed[3].should == { :text => " you?" }
20
+ end
21
+ it "should split newlines into their own elements" do
22
+ create_pdf
23
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
24
+ array = [{ :text => "\nhello\nworld" }]
25
+ arranger.format_array = array
26
+ arranger.unconsumed[0].should == { :text => "\n" }
27
+ arranger.unconsumed[1].should == { :text => "hello" }
28
+ arranger.unconsumed[2].should == { :text => "\n" }
29
+ arranger.unconsumed[3].should == { :text => "world" }
30
+ end
31
+ end
32
+ describe "Core::Text::Formatted::Arranger#preview_next_string" do
33
+ it "should not populate the consumed array" do
34
+ create_pdf
35
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
36
+ array = [{ :text => "hello" }]
37
+ arranger.format_array = array
38
+ arranger.preview_next_string
39
+ arranger.consumed.should == []
40
+ end
41
+ it "should not consumed array" do
42
+ create_pdf
43
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
44
+ array = [{ :text => "hello" }]
45
+ arranger.format_array = array
46
+ arranger.preview_next_string.should == "hello"
47
+ end
48
+ end
49
+ describe "Core::Text::Formatted::Arranger#next_string" do
50
+ before(:each) do
51
+ create_pdf
52
+ @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
53
+ array = [{ :text => "hello " },
54
+ { :text => "world how ", :styles => [:bold] },
55
+ { :text => "are", :styles => [:bold, :italic] },
56
+ { :text => " you?" }]
57
+ @arranger.format_array = array
58
+ end
59
+ it "should raise_error an error if called after a line was finalized and" +
60
+ " before a new line was initialized" do
61
+ @arranger.finalize_line
62
+ lambda do
63
+ @arranger.next_string
64
+ end.should raise_error(RuntimeError)
65
+ end
66
+ it "should populate consumed array" do
67
+ while string = @arranger.next_string
68
+ end
69
+ @arranger.consumed[0].should == { :text => "hello " }
70
+ @arranger.consumed[1].should == { :text => "world how ",
71
+ :styles => [:bold] }
72
+ @arranger.consumed[2].should == { :text => "are",
73
+ :styles => [:bold, :italic] }
74
+ @arranger.consumed[3].should == { :text => " you?" }
75
+ end
76
+ it "should populate current_format_state array" do
77
+ create_pdf
78
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
79
+ array = [{ :text => "hello " },
80
+ { :text => "world how ", :styles => [:bold] },
81
+ { :text => "are", :styles => [:bold, :italic] },
82
+ { :text => " you?" }]
83
+ arranger.format_array = array
84
+ counter = 0
85
+ while string = arranger.next_string
86
+ case counter
87
+ when 0
88
+ arranger.current_format_state.should == { }
89
+ when 1
90
+ arranger.current_format_state.should == { :styles => [:bold] }
91
+ when 2
92
+ arranger.current_format_state.should == { :styles => [:bold, :italic] }
93
+ when 3
94
+ arranger.current_format_state.should == { }
95
+ end
96
+ counter += 1
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "Core::Text::Formatted::Arranger#retrieve_fragment" do
102
+ it "should raise_error an error if called before finalize_line was called" do
103
+ create_pdf
104
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
105
+ array = [{ :text => "hello " },
106
+ { :text => "world how ", :styles => [:bold] },
107
+ { :text => "are", :styles => [:bold, :italic] },
108
+ { :text => " you?" }]
109
+ arranger.format_array = array
110
+ while string = arranger.next_string
111
+ end
112
+ lambda do
113
+ arranger.retrieve_fragment
114
+ end.should raise_error(RuntimeError)
115
+ end
116
+ it "should return the consumed fragments in order of consumption" +
117
+ " and update" do
118
+ create_pdf
119
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
120
+ array = [{ :text => "hello " },
121
+ { :text => "world how ", :styles => [:bold] },
122
+ { :text => "are", :styles => [:bold, :italic] },
123
+ { :text => " you?" }]
124
+ arranger.format_array = array
125
+ while string = arranger.next_string
126
+ end
127
+ arranger.finalize_line
128
+ arranger.retrieve_fragment.text.should == "hello "
129
+ arranger.retrieve_fragment.text.should == "world how "
130
+ arranger.retrieve_fragment.text.should == "are"
131
+ arranger.retrieve_fragment.text.should == " you?"
132
+ end
133
+ it "should never return a fragment whose text is an empty string" do
134
+ create_pdf
135
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
136
+ array = [{ :text => "hello\nworld\n\n\nhow are you?" },
137
+ { :text => "\n" },
138
+ { :text => "\n" },
139
+ { :text => "\n" },
140
+ { :text => "" },
141
+ { :text => "fine, thanks." },
142
+ { :text => "" },
143
+ { :text => "\n" },
144
+ { :text => "" }]
145
+ arranger.format_array = array
146
+ while string = arranger.next_string
147
+ end
148
+ arranger.finalize_line
149
+ while fragment = arranger.retrieve_fragment
150
+ fragment.text.should_not be_empty
151
+ end
152
+ end
153
+ it "should not alter the current font style" do
154
+ create_pdf
155
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
156
+ array = [{ :text => "hello " },
157
+ { :text => "world how ", :styles => [:bold] },
158
+ { :text => "are", :styles => [:bold, :italic] },
159
+ { :text => " you?" }]
160
+ arranger.format_array = array
161
+ while string = arranger.next_string
162
+ end
163
+ arranger.finalize_line
164
+ arranger.retrieve_fragment
165
+ arranger.current_format_state[:styles].should be_nil
166
+ end
167
+ end
168
+
169
+ describe "Core::Text::Formatted::Arranger#update_last_string" do
170
+ it "should update the last retrieved string with what actually fit on" +
171
+ "the line and the list of unconsumed with what did not" do
172
+ create_pdf
173
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
174
+ array = [{ :text => "hello " },
175
+ { :text => "world how ", :styles => [:bold] },
176
+ { :text => "are", :styles => [:bold, :italic] },
177
+ { :text => " you now?", :styles => [:bold, :italic] }]
178
+ arranger.format_array = array
179
+ while string = arranger.next_string
180
+ end
181
+ arranger.update_last_string(" you", " now?", nil)
182
+ arranger.consumed[3].should == { :text => " you",
183
+ :styles => [:bold, :italic] }
184
+ arranger.unconsumed.should == [{ :text => " now?",
185
+ :styles => [:bold, :italic] }]
186
+ end
187
+ it "should set the format state to the previously processed fragment" do
188
+ create_pdf
189
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
190
+ array = [{ :text => "hello " },
191
+ { :text => "world how ", :styles => [:bold] },
192
+ { :text => "are", :styles => [:bold, :italic] },
193
+ { :text => " you now?" }]
194
+ arranger.format_array = array
195
+ 3.times { arranger.next_string }
196
+ arranger.current_format_state.should == { :styles => [:bold, :italic] }
197
+ arranger.update_last_string("", "are", "-")
198
+ arranger.current_format_state.should == { :styles => [:bold] }
199
+ end
200
+
201
+ context "when the entire string was used" do
202
+ it "should not push empty string onto unconsumed" do
203
+ create_pdf
204
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
205
+ array = [{ :text => "hello " },
206
+ { :text => "world how ", :styles => [:bold] },
207
+ { :text => "are", :styles => [:bold, :italic] },
208
+ { :text => " you now?" }]
209
+ arranger.format_array = array
210
+ while string = arranger.next_string
211
+ end
212
+ arranger.update_last_string(" you now?", "", nil)
213
+ arranger.unconsumed.should == []
214
+ end
215
+ end
216
+ end
217
+ describe "Core::Text::Formatted::Arranger#space_count" do
218
+ before(:each) do
219
+ create_pdf
220
+ @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
221
+ array = [{ :text => "hello " },
222
+ { :text => "world how ", :styles => [:bold] },
223
+ { :text => "are", :styles => [:bold, :italic] },
224
+ { :text => " you?" }]
225
+ @arranger.format_array = array
226
+ while string = @arranger.next_string
227
+ end
228
+ end
229
+ it "should raise_error an error if called before finalize_line was called" do
230
+ lambda do
231
+ @arranger.space_count
232
+ end.should raise_error(RuntimeError)
233
+ end
234
+ it "should return the total number of spaces in all fragments" do
235
+ @arranger.finalize_line
236
+ @arranger.space_count.should == 4
237
+ end
238
+ end
239
+ describe "Core::Text::Formatted::Arranger#finalize_line" do
240
+ it "should make it so that all trailing white space fragments " +
241
+ "exclude trailing white space" do
242
+ create_pdf
243
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
244
+ array = [{ :text => "hello " },
245
+ { :text => "world how ", :styles => [:bold] },
246
+ { :text => " ", :styles => [:bold, :italic] }]
247
+ arranger.format_array = array
248
+ while string = arranger.next_string
249
+ end
250
+ arranger.finalize_line
251
+ arranger.fragments.length.should == 3
252
+
253
+ fragment = arranger.retrieve_fragment
254
+ fragment.text.should == "hello "
255
+
256
+ fragment = arranger.retrieve_fragment
257
+ fragment.text.should == "world how"
258
+
259
+ fragment = arranger.retrieve_fragment
260
+ fragment.text.should == ""
261
+ end
262
+ end
263
+
264
+ describe "Core::Text::Formatted::Arranger#line_width" do
265
+ before(:each) do
266
+ create_pdf
267
+ @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
268
+ array = [{ :text => "hello " },
269
+ { :text => "world", :styles => [:bold] }]
270
+ @arranger.format_array = array
271
+ while string = @arranger.next_string
272
+ end
273
+ end
274
+ it "should raise_error an error if called before finalize_line was called" do
275
+ lambda do
276
+ @arranger.line_width
277
+ end.should raise_error(RuntimeError)
278
+ end
279
+ it "should return the width of the complete line" do
280
+ @arranger.finalize_line
281
+ @arranger.line_width.should be > 0
282
+ end
283
+ end
284
+
285
+ describe "Core::Text::Formatted::Arranger#line_width with character_spacing > 0" do
286
+ it "should return a width greater than a line without a character_spacing" do
287
+ create_pdf
288
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
289
+
290
+ array = [{ :text => "hello " },
291
+ { :text => "world", :styles => [:bold] }]
292
+ arranger.format_array = array
293
+ while string = arranger.next_string
294
+ end
295
+ arranger.finalize_line
296
+
297
+ base_line_width = arranger.line_width
298
+
299
+
300
+ array = [{ :text => "hello " },
301
+ { :text => "world", :styles => [:bold],
302
+ :character_spacing => 7}]
303
+ arranger.format_array = array
304
+ while string = arranger.next_string
305
+ end
306
+ arranger.finalize_line
307
+ arranger.line_width.should be > base_line_width
308
+ end
309
+ end
310
+
311
+ describe "Core::Text::Formatted::Arranger#line" do
312
+ before(:each) do
313
+ create_pdf
314
+ @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
315
+ array = [{ :text => "hello " },
316
+ { :text => "world", :styles => [:bold] }]
317
+ @arranger.format_array = array
318
+ while string = @arranger.next_string
319
+ end
320
+ end
321
+ it "should raise_error an error if called before finalize_line was called" do
322
+ lambda do
323
+ @arranger.line
324
+ end.should raise_error(RuntimeError)
325
+ end
326
+ it "should return the complete line" do
327
+ @arranger.finalize_line
328
+ @arranger.line.should == "hello world"
329
+ end
330
+ end
331
+
332
+ describe "Core::Text::Formatted::Arranger#unconsumed" do
333
+ it "should return the original array if nothing was consumed" do
334
+ create_pdf
335
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
336
+ array = [{ :text => "hello " },
337
+ { :text => "world how ", :styles => [:bold] },
338
+ { :text => "are", :styles => [:bold, :italic] },
339
+ { :text => " you now?" }]
340
+ arranger.format_array = array
341
+ arranger.unconsumed.should == array
342
+ end
343
+ it "should return an empty array if everything was consumed" do
344
+ create_pdf
345
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
346
+ array = [{ :text => "hello " },
347
+ { :text => "world how ", :styles => [:bold] },
348
+ { :text => "are", :styles => [:bold, :italic] },
349
+ { :text => " you now?" }]
350
+ arranger.format_array = array
351
+ while string = arranger.next_string
352
+ end
353
+ arranger.unconsumed.should == []
354
+ end
355
+ end
356
+
357
+ describe "Core::Text::Formatted::Arranger#finished" do
358
+ it "should be_false if anything was not printed" do
359
+ create_pdf
360
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
361
+ array = [{ :text => "hello " },
362
+ { :text => "world how ", :styles => [:bold] },
363
+ { :text => "are", :styles => [:bold, :italic] },
364
+ { :text => " you now?" }]
365
+ arranger.format_array = array
366
+ while string = arranger.next_string
367
+ end
368
+ arranger.update_last_string(" you", "now?", nil)
369
+ arranger.should_not be_finished
370
+ end
371
+ it "should be_false if everything was printed" do
372
+ create_pdf
373
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
374
+ array = [{ :text => "hello " },
375
+ { :text => "world how ", :styles => [:bold] },
376
+ { :text => "are", :styles => [:bold, :italic] },
377
+ { :text => " you now?" }]
378
+ arranger.format_array = array
379
+ while string = arranger.next_string
380
+ end
381
+ arranger.should be_finished
382
+ end
383
+ end
384
+
385
+ describe "Core::Text::Formatted::Arranger.max_line_height" do
386
+ it "should be the height of the maximum consumed fragment" do
387
+ create_pdf
388
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
389
+ array = [{ :text => "hello " },
390
+ { :text => "world how ", :styles => [:bold] },
391
+ { :text => "are", :styles => [:bold, :italic],
392
+ :size => 28 },
393
+ { :text => " you now?" }]
394
+ arranger.format_array = array
395
+ while string = arranger.next_string
396
+ end
397
+ arranger.finalize_line
398
+ arranger.max_line_height.should be_within(0.0001).of(33.32)
399
+ end
400
+ end
401
+
402
+ describe "Core::Text::Formatted::Arranger#repack_unretrieved" do
403
+ it "should restore part of the original string" do
404
+ create_pdf
405
+ arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
406
+ array = [{ :text => "hello " },
407
+ { :text => "world how ", :styles => [:bold] },
408
+ { :text => "are", :styles => [:bold, :italic] },
409
+ { :text => " you now?" }]
410
+ arranger.format_array = array
411
+ while string = arranger.next_string
412
+ end
413
+ arranger.finalize_line
414
+ arranger.retrieve_fragment
415
+ arranger.retrieve_fragment
416
+ arranger.repack_unretrieved
417
+ arranger.unconsumed.should == [
418
+ { :text => "are", :styles => [:bold, :italic] },
419
+ { :text => " you now?" }]
420
+ end
421
+ end
@@ -0,0 +1,705 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ describe "Text::Formatted::Box wrapping" do
6
+ before(:each) do
7
+ create_pdf
8
+ end
9
+
10
+ it "should not wrap between two fragments" do
11
+ texts = [
12
+ {:text => "Hello "},
13
+ {:text => "World"},
14
+ {:text => "2", :styles => [:superscript]},
15
+ ]
16
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
17
+ text_box.render
18
+ text_box.text.should == "Hello\nWorld2"
19
+ end
20
+
21
+ it "should not raise an Encoding::CompatibilityError when keeping a TTF and an AFM font together" do
22
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
23
+
24
+ @pdf.font_families["Kai"] = {
25
+ :normal => { :file => file, :font => "Kai" }
26
+ }
27
+
28
+ texts = [{ :text => "Hello " },
29
+ { :text => "再见", :font => "Kai"},
30
+ { :text => "World" }]
31
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
32
+
33
+ text_box.render
34
+ end
35
+
36
+ it "should wrap between two fragments when the preceding fragment ends with white space" do
37
+ texts = [
38
+ {:text => "Hello "},
39
+ {:text => "World "},
40
+ {:text => "2", :styles => [:superscript]},
41
+ ]
42
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
43
+ text_box.render
44
+ text_box.text.should == "Hello World\n2"
45
+
46
+ texts = [
47
+ {:text => "Hello "},
48
+ {:text => "World\n"},
49
+ {:text => "2", :styles => [:superscript]},
50
+ ]
51
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
52
+ text_box.render
53
+ text_box.text.should == "Hello World\n2"
54
+ end
55
+
56
+ it "should wrap between two fragments when the final fragment begins with white space" do
57
+ texts = [
58
+ {:text => "Hello "},
59
+ {:text => "World"},
60
+ {:text => " 2", :styles => [:superscript]},
61
+ ]
62
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
63
+ text_box.render
64
+ text_box.text.should == "Hello World\n2"
65
+
66
+ texts = [
67
+ {:text => "Hello "},
68
+ {:text => "World"},
69
+ {:text => "\n2", :styles => [:superscript]},
70
+ ]
71
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
72
+ text_box.render
73
+ text_box.text.should == "Hello World\n2"
74
+ end
75
+
76
+ it "should properly handle empty slices using default encoding" do
77
+ texts = [{ :text => "Noua Delineatio Geographica generalis | Apostolicarum peregrinationum | S FRANCISCI XAUERII | Indiarum & Iaponiæ Apostoli", :font => 'Courier', :size => 10 }]
78
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Noua Delineatio Geographica gen"))
79
+ lambda {
80
+ text_box.render
81
+ }.should_not raise_error
82
+ text_box.text.should == "Noua Delineatio Geographica\ngeneralis | Apostolicarum\nperegrinationum | S FRANCISCI\nXAUERII | Indiarum & Iaponi\346\nApostoli"
83
+ end
84
+ end
85
+
86
+ describe "Text::Formatted::Box with :fallback_fonts option that includes" +
87
+ "a Chinese font and set of Chinese glyphs not in the current font" do
88
+ it "should change the font to the Chinese font for the Chinese glyphs" do
89
+ create_pdf
90
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
91
+ @pdf.font_families["Kai"] = {
92
+ :normal => { :file => file, :font => "Kai" }
93
+ }
94
+ formatted_text = [{ :text => "hello你好" },
95
+ { :text => "再见goodbye" }]
96
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Kai"])
97
+
98
+ text = PDF::Inspector::Text.analyze(@pdf.render)
99
+
100
+ fonts_used = text.font_settings.map { |e| e[:name] }
101
+ fonts_used.length.should == 4
102
+ fonts_used[0].should == :"Helvetica"
103
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
104
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
105
+ fonts_used[3].should == :"Helvetica"
106
+
107
+ text.strings[0].should == "hello"
108
+ text.strings[1].should == "你好"
109
+ text.strings[2].should == "再见"
110
+ text.strings[3].should == "goodbye"
111
+ end
112
+ end
113
+
114
+ describe "Text::Formatted::Box with :fallback_fonts option that includes" +
115
+ "an AFM font and Win-Ansi glyph not in the current Chinese font" do
116
+ it "should change the font to the AFM font for the Win-Ansi glyph" do
117
+ create_pdf
118
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
119
+ @pdf.font_families["Kai"] = {
120
+ :normal => { :file => file, :font => "Kai" }
121
+ }
122
+ @pdf.font("Kai")
123
+ formatted_text = [{ :text => "hello你好" },
124
+ { :text => "再见€" }]
125
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Helvetica"])
126
+
127
+ text = PDF::Inspector::Text.analyze(@pdf.render)
128
+
129
+ fonts_used = text.font_settings.map { |e| e[:name] }
130
+ fonts_used.length.should == 4
131
+ fonts_used[0].to_s.should =~ /GBZenKai-Medium/
132
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
133
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
134
+ fonts_used[3].should == :"Helvetica"
135
+
136
+ text.strings[0].should == "hello"
137
+ text.strings[1].should == "你好"
138
+ text.strings[2].should == "再见"
139
+ text.strings[3].should == "€"
140
+ end
141
+ end
142
+
143
+ describe "Text::Formatted::Box with :fallback_fonts option and fragment " +
144
+ "level font" do
145
+ it "should use the fragment level font except for glyphs not in that font" do
146
+ create_pdf
147
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
148
+ @pdf.font_families["Kai"] = {
149
+ :normal => { :file => file, :font => "Kai" }
150
+ }
151
+
152
+ file = "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
153
+ @pdf.font_families["DejaVu Sans"] = {
154
+ :normal => { :file => file }
155
+ }
156
+
157
+ formatted_text = [{ :text => "hello你好" },
158
+ { :text => "再见goodbye", :font => "Times-Roman" }]
159
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Kai"])
160
+
161
+ text = PDF::Inspector::Text.analyze(@pdf.render)
162
+
163
+ fonts_used = text.font_settings.map { |e| e[:name] }
164
+ fonts_used.length.should == 4
165
+ fonts_used[0].should == :"Helvetica"
166
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
167
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
168
+ fonts_used[3].should == :"Times-Roman"
169
+
170
+ text.strings[0].should == "hello"
171
+ text.strings[1].should == "你好"
172
+ text.strings[2].should == "再见"
173
+ text.strings[3].should == "goodbye"
174
+ end
175
+ end
176
+
177
+ describe "Text::Formatted::Box" do
178
+ before(:each) do
179
+ create_pdf
180
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
181
+ @pdf.font_families["Kai"] = {
182
+ :normal => { :file => file, :font => "Kai" }
183
+ }
184
+
185
+ file = "#{Prawn::DATADIR}/fonts/DejaVuSans.ttf"
186
+ @pdf.font_families["DejaVu Sans"] = {
187
+ :normal => { :file => file }
188
+ }
189
+
190
+
191
+ @formatted_text = [{ :text => "hello你好" }]
192
+ @pdf.fallback_fonts(["Kai"])
193
+ @pdf.fallback_fonts = ["Kai"]
194
+ end
195
+ it "#fallback_fonts should return the document-wide fallback fonts" do
196
+ @pdf.fallback_fonts.should == ["Kai"]
197
+ end
198
+ it "should be able to set text fallback_fonts document-wide" do
199
+ @pdf.formatted_text_box(@formatted_text)
200
+
201
+ text = PDF::Inspector::Text.analyze(@pdf.render)
202
+
203
+ fonts_used = text.font_settings.map { |e| e[:name] }
204
+ fonts_used.length.should == 2
205
+ fonts_used[0].should == :"Helvetica"
206
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
207
+ end
208
+ it "should be able to override document-wide fallback_fonts" do
209
+ @pdf.fallback_fonts = ["DejaVu Sans"]
210
+ @pdf.formatted_text_box(@formatted_text, :fallback_fonts => ["Kai"])
211
+
212
+ text = PDF::Inspector::Text.analyze(@pdf.render)
213
+
214
+ fonts_used = text.font_settings.map { |e| e[:name] }
215
+ fonts_used.length.should == 2
216
+ fonts_used[0].should == :"Helvetica"
217
+ fonts_used[1].should =~ /Kai/
218
+ end
219
+ it "should omit the fallback fonts overhead when passing an empty array " +
220
+ "as the :fallback_fonts" do
221
+ @pdf.font("Kai")
222
+
223
+ box = Prawn::Text::Formatted::Box.new(@formatted_text,
224
+ :document => @pdf,
225
+ :fallback_fonts => [])
226
+
227
+ box.expects(:process_fallback_fonts).never
228
+ box.render
229
+ end
230
+
231
+ it "should be able to clear document-wide fallback_fonts" do
232
+ @pdf.fallback_fonts([])
233
+ box = Prawn::Text::Formatted::Box.new(@formatted_text,
234
+ :document => @pdf)
235
+
236
+ @pdf.font("Kai")
237
+
238
+ box.expects(:process_fallback_fonts).never
239
+ box.render
240
+ end
241
+ end
242
+
243
+ describe "Text::Formatted::Box with :fallback_fonts option " +
244
+ "with glyphs not in the primary or the fallback fonts" do
245
+
246
+ it "should raise an exception" do
247
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
248
+ create_pdf
249
+ formatted_text = [{ :text => "hello world. 世界你好。" }]
250
+
251
+ lambda {
252
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Courier"])
253
+ }.should raise_error(Prawn::Errors::IncompatibleStringEncoding)
254
+ end
255
+ end
256
+
257
+ describe "Text::Formatted::Box#extensions" do
258
+ it "should be able to override default line wrapping" do
259
+ create_pdf
260
+ Prawn::Text::Formatted::Box.extensions << TestFormattedWrapOverride
261
+ @pdf.formatted_text_box([{ :text => "hello world" }], {})
262
+ Prawn::Text::Formatted::Box.extensions.delete(TestFormattedWrapOverride)
263
+ text = PDF::Inspector::Text.analyze(@pdf.render)
264
+ text.strings[0].should == "all your base are belong to us"
265
+ end
266
+ it "overriding Text::Formatted::Box line wrapping should not affect " +
267
+ "Text::Box wrapping" do
268
+ create_pdf
269
+ Prawn::Text::Formatted::Box.extensions << TestFormattedWrapOverride
270
+ @pdf.text_box("hello world", {})
271
+ Prawn::Text::Formatted::Box.extensions.delete(TestFormattedWrapOverride)
272
+ text = PDF::Inspector::Text.analyze(@pdf.render)
273
+ text.strings[0].should == "hello world"
274
+ end
275
+ it "overriding Text::Box line wrapping should override Text::Box wrapping" do
276
+ create_pdf
277
+ Prawn::Text::Box.extensions << TestFormattedWrapOverride
278
+ @pdf.text_box("hello world", {})
279
+ Prawn::Text::Box.extensions.delete(TestFormattedWrapOverride)
280
+ text = PDF::Inspector::Text.analyze(@pdf.render)
281
+ text.strings[0].should == "all your base are belong to us"
282
+ end
283
+ end
284
+
285
+ describe "Text::Formatted::Box#render" do
286
+ it "should handle newlines" do
287
+ create_pdf
288
+ array = [{ :text => "hello\nworld"}]
289
+ options = { :document => @pdf }
290
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
291
+ text_box.render
292
+ text_box.text.should == "hello\nworld"
293
+ end
294
+ it "should omit spaces from the beginning of the line" do
295
+ create_pdf
296
+ array = [{ :text => " hello\n world"}]
297
+ options = { :document => @pdf }
298
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
299
+ text_box.render
300
+ text_box.text.should == "hello\nworld"
301
+ end
302
+ it "should be okay printing a line of whitespace" do
303
+ create_pdf
304
+ array = [{ :text => "hello\n \nworld"}]
305
+ options = { :document => @pdf }
306
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
307
+ text_box.render
308
+ text_box.text.should == "hello\n\nworld"
309
+
310
+
311
+ array = [{ :text => "hello" + " " * 500},
312
+ { :text => " " * 500 },
313
+ { :text => " " * 500 + "\n"},
314
+ { :text => "world"}]
315
+ options = { :document => @pdf }
316
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
317
+ text_box.render
318
+ text_box.text.should == "hello\n\nworld"
319
+ end
320
+ it "should enable fragment level direction setting" do
321
+ create_pdf
322
+ number_of_hellos = 18
323
+ array = [
324
+ { :text => "hello " * number_of_hellos },
325
+ { :text => "world", :direction => :ltr },
326
+ { :text => ", how are you?" }
327
+ ]
328
+ options = { :document => @pdf, :direction => :rtl }
329
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
330
+ text_box.render
331
+ text = PDF::Inspector::Text.analyze(@pdf.render)
332
+ text.strings[0].should == "era woh ,"
333
+ text.strings[1].should == "world"
334
+ text.strings[2].should == " olleh" * number_of_hellos
335
+ text.strings[3].should == "?uoy"
336
+ end
337
+ end
338
+
339
+ describe "Text::Formatted::Box#render" do
340
+ it "should be able to perform fragment callbacks" do
341
+ create_pdf
342
+ callback_object = TestFragmentCallback.new("something", 7,
343
+ :document => @pdf)
344
+ callback_object.expects(:render_behind).with(
345
+ kind_of(Prawn::Text::Formatted::Fragment))
346
+ callback_object.expects(:render_in_front).with(
347
+ kind_of(Prawn::Text::Formatted::Fragment))
348
+ array = [{ :text => "hello world " },
349
+ { :text => "callback now",
350
+ :callback => callback_object }]
351
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
352
+ text_box.render
353
+ end
354
+ it "should be able to perform fragment callbacks on multiple objects" do
355
+ create_pdf
356
+
357
+ callback_object = TestFragmentCallback.new("something", 7,
358
+ :document => @pdf)
359
+ callback_object.expects(:render_behind).with(
360
+ kind_of(Prawn::Text::Formatted::Fragment))
361
+ callback_object.expects(:render_in_front).with(
362
+ kind_of(Prawn::Text::Formatted::Fragment))
363
+
364
+ callback_object2 = TestFragmentCallback.new("something else", 14,
365
+ :document => @pdf)
366
+ callback_object2.expects(:render_behind).with(
367
+ kind_of(Prawn::Text::Formatted::Fragment))
368
+ callback_object2.expects(:render_in_front).with(
369
+ kind_of(Prawn::Text::Formatted::Fragment))
370
+
371
+ array = [{ :text => "hello world " },
372
+ { :text => "callback now",
373
+ :callback => [callback_object, callback_object2] }]
374
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
375
+ text_box.render
376
+ end
377
+ it "fragment callbacks should be able to define only the callback they need" do
378
+ create_pdf
379
+ behind = TestFragmentCallbackBehind.new("something", 7,
380
+ :document => @pdf)
381
+ in_front = TestFragmentCallbackInFront.new("something", 7,
382
+ :document => @pdf)
383
+ array = [{ :text => "hello world " },
384
+ { :text => "callback now",
385
+ :callback => [behind, in_front] }]
386
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
387
+
388
+ text_box.render # trigger callbacks
389
+ end
390
+ it "should be able to set the font" do
391
+ create_pdf
392
+ array = [{ :text => "this contains " },
393
+ { :text => "Times-Bold",
394
+ :styles => [:bold],
395
+ :font => "Times-Roman" },
396
+ { :text => " text" }]
397
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
398
+ text_box.render
399
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
400
+ fonts = contents.font_settings.map { |e| e[:name] }
401
+ fonts.should == [:Helvetica, :"Times-Bold", :Helvetica]
402
+ contents.strings[0].should == "this contains "
403
+ contents.strings[1].should == "Times-Bold"
404
+ contents.strings[2].should == " text"
405
+ end
406
+ it "should be able to set bold" do
407
+ create_pdf
408
+ array = [{ :text => "this contains " },
409
+ { :text => "bold", :styles => [:bold] },
410
+ { :text => " text" }]
411
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
412
+ text_box.render
413
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
414
+ fonts = contents.font_settings.map { |e| e[:name] }
415
+ fonts.should == [:Helvetica, :"Helvetica-Bold", :Helvetica]
416
+ contents.strings[0].should == "this contains "
417
+ contents.strings[1].should == "bold"
418
+ contents.strings[2].should == " text"
419
+ end
420
+ it "should be able to set italics" do
421
+ create_pdf
422
+ array = [{ :text => "this contains " },
423
+ { :text => "italic", :styles => [:italic] },
424
+ { :text => " text" }]
425
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
426
+ text_box.render
427
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
428
+ fonts = contents.font_settings.map { |e| e[:name] }
429
+ fonts.should == [:Helvetica, :"Helvetica-Oblique", :Helvetica]
430
+ end
431
+ it "should be able to set subscript" do
432
+ create_pdf
433
+ array = [{ :text => "this contains " },
434
+ { :text => "subscript", :size => 18, :styles => [:subscript] },
435
+ { :text => " text" }]
436
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
437
+ text_box.render
438
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
439
+ contents.font_settings[0][:size].should == 12
440
+ contents.font_settings[1][:size].should be_within(0.0001).of(18 * 0.583)
441
+ end
442
+ it "should be able to set superscript" do
443
+ create_pdf
444
+ array = [{ :text => "this contains " },
445
+ { :text => "superscript", :size => 18, :styles => [:superscript] },
446
+ { :text => " text" }]
447
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
448
+ text_box.render
449
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
450
+ contents.font_settings[0][:size].should == 12
451
+ contents.font_settings[1][:size].should be_within(0.0001).of(18 * 0.583)
452
+ end
453
+ it "should be able to set compound bold and italic text" do
454
+ create_pdf
455
+ array = [{ :text => "this contains " },
456
+ { :text => "bold italic", :styles => [:bold, :italic] },
457
+ { :text => " text" }]
458
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
459
+ text_box.render
460
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
461
+ fonts = contents.font_settings.map { |e| e[:name] }
462
+ fonts.should == [:Helvetica, :"Helvetica-BoldOblique", :Helvetica]
463
+ end
464
+ it "should be able to underline" do
465
+ create_pdf
466
+ array = [{ :text => "this contains " },
467
+ { :text => "underlined", :styles => [:underline] },
468
+ { :text => " text" }]
469
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
470
+ text_box.render
471
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
472
+ line_drawing.points.length.should == 2
473
+ end
474
+ it "should be able to strikethrough" do
475
+ create_pdf
476
+ array = [{ :text => "this contains " },
477
+ { :text => "struckthrough", :styles => [:strikethrough] },
478
+ { :text => " text" }]
479
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
480
+ text_box.render
481
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
482
+ line_drawing.points.length.should == 2
483
+ end
484
+ it "should be able to add URL links" do
485
+ create_pdf
486
+ @pdf.expects(:link_annotation).with(kind_of(Array), :Border => [0,0,0],
487
+ :A => { :Type => :Action, :S => :URI, :URI => "http://example.com" })
488
+ array = [{ :text => "click " },
489
+ { :text => "here", :link => "http://example.com" },
490
+ { :text => " to visit" }]
491
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
492
+ text_box.render
493
+ end
494
+ it "should be able to add destination links" do
495
+ create_pdf
496
+ @pdf.expects(:link_annotation).with(kind_of(Array), :Border => [0,0,0],
497
+ :Dest => "ToC")
498
+ array = [{ :text => "Go to the " },
499
+ { :text => "Table of Contents", :anchor => "ToC" }]
500
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
501
+ text_box.render
502
+ end
503
+ it "should be able to add local actions" do
504
+ create_pdf
505
+ @pdf.expects(:link_annotation).with(kind_of(Array), :Border => [0,0,0],
506
+ :A => { :Type => :Action, :S => :Launch, :F => "../example.pdf", :NewWindow => true })
507
+ array = [{ :text => "click " },
508
+ { :text => "here", :local => "../example.pdf" },
509
+ { :text => " to open a local file" }]
510
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
511
+ text_box.render
512
+ end
513
+ it "should be able to set font size" do
514
+ create_pdf
515
+ array = [{ :text => "this contains " },
516
+ { :text => "sized", :size => 24 },
517
+ { :text => " text" }]
518
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
519
+ text_box.render
520
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
521
+ contents.font_settings[0][:size].should == 12
522
+ contents.font_settings[1][:size].should == 24
523
+ end
524
+ it "should set the baseline based on the tallest fragment on a given line" do
525
+ create_pdf
526
+ array = [{ :text => "this contains " },
527
+ { :text => "sized", :size => 24 },
528
+ { :text => " text" }]
529
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
530
+ text_box.render
531
+ @pdf.font_size(24) do
532
+ text_box.height.should be_within(0.001).of(@pdf.font.ascender + @pdf.font.descender)
533
+ end
534
+ end
535
+ it "should be able to set color via an rgb hex string" do
536
+ create_pdf
537
+ array = [{ :text => "rgb",
538
+ :color => "ff0000" }]
539
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
540
+ text_box.render
541
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
542
+ colors.fill_color_count.should == 2
543
+ colors.stroke_color_count.should == 2
544
+ end
545
+ it "should be able to set color using a cmyk array" do
546
+ create_pdf
547
+ array = [{ :text => "cmyk",
548
+ :color => [100, 0, 0, 0] }]
549
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
550
+ text_box.render
551
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
552
+ colors.fill_color_count.should == 2
553
+ colors.stroke_color_count.should == 2
554
+ end
555
+ end
556
+
557
+ describe "Text::Formatted::Box#render(:dry_run => true)" do
558
+ it "should not change the graphics state of the document" do
559
+ create_pdf
560
+
561
+ state_before = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
562
+ fill_color_count = state_before.fill_color_count
563
+ stroke_color_count = state_before.stroke_color_count
564
+ stroke_color_space_count = state_before.stroke_color_space_count
565
+
566
+ array = [{ :text => 'Foo',
567
+ :color => [0, 0, 0, 100] }]
568
+ options = { :document => @pdf }
569
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
570
+ text_box.render(:dry_run => true)
571
+
572
+ state_after = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
573
+ state_after.fill_color_count.should == fill_color_count
574
+ state_after.stroke_color_count.should == stroke_color_count
575
+ state_after.stroke_color_space_count.should == stroke_color_space_count
576
+ end
577
+ end
578
+
579
+ describe "Text::Formatted::Box#render with fragment level :character_spacing option" do
580
+ it "should draw the character spacing to the document" do
581
+ create_pdf
582
+ array = [{ :text => "hello world",
583
+ :character_spacing => 7 }]
584
+ options = { :document => @pdf }
585
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
586
+ text_box.render
587
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
588
+ contents.character_spacing[0].should == 7
589
+ end
590
+ it "should draw the character spacing to the document" do
591
+ create_pdf
592
+ array = [{ :text => "hello world",
593
+ :font => "Courier",
594
+ :character_spacing => 10 }]
595
+ options = { :document => @pdf,
596
+ :width => 100,
597
+ :overflow => :expand }
598
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
599
+ text_box.render
600
+ text_box.text.should == "hello\nworld"
601
+ end
602
+ end
603
+
604
+ describe "Text::Formatted::Box#render with :align => :justify" do
605
+ it "should not justify the last line of a paragraph" do
606
+ create_pdf
607
+ array = [{ :text => "hello world " },
608
+ { :text => "\n" },
609
+ { :text => "goodbye" }]
610
+ options = { :document => @pdf, :align => :justify }
611
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
612
+ text_box.render
613
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
614
+ contents.word_spacing.should be_empty
615
+ end
616
+ end
617
+
618
+ describe "Text::Formatted::Box#render with :valign => :center" do
619
+ it "should have a bottom gap equal to baseline and bottom of box" do
620
+ create_pdf
621
+ box_height = 100
622
+ y = 450
623
+ array = [{ :text => 'Vertical Align' }]
624
+ options = {
625
+ :document => @pdf,
626
+ :valign => :center,
627
+ :at => [0,y],
628
+ :width => 100,
629
+ :height => box_height,
630
+ :size => 16
631
+ }
632
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
633
+ text_box.render
634
+ line_padding = (box_height - text_box.height + text_box.descender) * 0.5
635
+ baseline = y - line_padding
636
+
637
+ text_box.at[1].should be_within(0.01).of(baseline)
638
+ end
639
+ end
640
+
641
+ describe "Text::Formatted::Box#render with :valign => :bottom" do
642
+ it "should not render a gap between the text and bottom of box" do
643
+ create_pdf
644
+ box_height = 100
645
+ y = 450
646
+ array = [{ :text => 'Vertical Align' }]
647
+ options = {
648
+ :document => @pdf,
649
+ :valign => :bottom,
650
+ :at => [0,y],
651
+ :width => 100,
652
+ :height => box_height,
653
+ :size => 16
654
+ }
655
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
656
+ text_box.render
657
+ top_padding = y - (box_height - text_box.height)
658
+
659
+ text_box.at[1].should be_within(0.01).of(top_padding)
660
+ end
661
+ end
662
+
663
+ class TestFragmentCallback
664
+ def initialize(string, number, options)
665
+ @document = options[:document]
666
+ end
667
+
668
+ def render_behind(fragment)
669
+ end
670
+
671
+ def render_in_front(fragment)
672
+ end
673
+ end
674
+
675
+ class TestFragmentCallbackBehind
676
+ def initialize(string, number, options)
677
+ @document = options[:document]
678
+ end
679
+
680
+ def render_behind(fragment)
681
+ end
682
+ end
683
+
684
+ class TestFragmentCallbackInFront
685
+ def initialize(string, number, options)
686
+ @document = options[:document]
687
+ end
688
+
689
+ def render_in_front(fragment)
690
+ end
691
+ end
692
+
693
+ module TestFormattedWrapOverride
694
+ def wrap(array)
695
+ initialize_wrap([{ :text => 'all your base are belong to us' }])
696
+ line_to_print = @line_wrap.wrap_line(:document => @document,
697
+ :kerning => @kerning,
698
+ :width => 10000,
699
+ :arranger => @arranger)
700
+ fragment = @arranger.retrieve_fragment
701
+ format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
702
+
703
+ []
704
+ end
705
+ end