nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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::Core::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,640 @@
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_error Encoding::CompatibilityError when keeping a TTF and an " +
22
+ "AFM font together" do
23
+ ruby_19 do
24
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
25
+ @pdf.font_families["Kai"] = {
26
+ :normal => { :file => file, :font => "Kai" }
27
+ }
28
+
29
+ texts = [{ :text => "Hello " },
30
+ { :text => "再见", :font => "Kai"},
31
+ { :text => "World" }]
32
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
33
+ lambda {
34
+ text_box.render
35
+ }.should_not raise_error(Encoding::CompatibilityError)
36
+ end
37
+ end
38
+
39
+ it "should wrap between two fragments when the preceding fragment ends with white space" do
40
+ texts = [
41
+ {:text => "Hello "},
42
+ {:text => "World "},
43
+ {:text => "2", :styles => [:superscript]},
44
+ ]
45
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
46
+ text_box.render
47
+ text_box.text.should == "Hello World\n2"
48
+
49
+ texts = [
50
+ {:text => "Hello "},
51
+ {:text => "World\n"},
52
+ {:text => "2", :styles => [:superscript]},
53
+ ]
54
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
55
+ text_box.render
56
+ text_box.text.should == "Hello World\n2"
57
+ end
58
+
59
+ it "should wrap between two fragments when the final fragment begins with white space" do
60
+ texts = [
61
+ {:text => "Hello "},
62
+ {:text => "World"},
63
+ {:text => " 2", :styles => [:superscript]},
64
+ ]
65
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
66
+ text_box.render
67
+ text_box.text.should == "Hello World\n2"
68
+
69
+ texts = [
70
+ {:text => "Hello "},
71
+ {:text => "World"},
72
+ {:text => "\n2", :styles => [:superscript]},
73
+ ]
74
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Hello World"))
75
+ text_box.render
76
+ text_box.text.should == "Hello World\n2"
77
+ end
78
+
79
+ it "should properly handle empty slices using default encoding" do
80
+ texts = [{ :text => "Noua Delineatio Geographica generalis | Apostolicarum peregrinationum | S FRANCISCI XAUERII | Indiarum & Iaponiæ Apostoli", :font => 'Courier', :size => 10 }]
81
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Noua Delineatio Geographica gen"))
82
+ lambda {
83
+ text_box.render
84
+ }.should_not raise_error
85
+ text_box.text.should == "Noua Delineatio Geographica\ngeneralis | Apostolicarum\nperegrinationum | S FRANCISCI\nXAUERII | Indiarum & Iaponi\346\nApostoli"
86
+ end
87
+
88
+ describe "Unicode" do
89
+ before do
90
+ if RUBY_VERSION < '1.9'
91
+ @reset_value = $KCODE
92
+ $KCODE='u'
93
+ else
94
+ @reset_value = [Encoding.default_external, Encoding.default_internal]
95
+ Encoding.default_external = Encoding::UTF_8
96
+ Encoding.default_internal = Encoding::UTF_8
97
+ end
98
+ end
99
+
100
+ after do
101
+ if RUBY_VERSION < '1.9'
102
+ $KCODE=@reset_value
103
+ else
104
+ Encoding.default_external = @reset_value[0]
105
+ Encoding.default_internal = @reset_value[1]
106
+ end
107
+ end
108
+
109
+ it "should properly handle empty slices using Unicode encoding" do
110
+ texts = [{ :text => "Noua Delineatio Geographica generalis | Apostolicarum peregrinationum | S FRANCISCI XAUERII | Indiarum & Iaponiæ Apostoli", :font => 'Courier', :size => 10 }]
111
+ text_box = Prawn::Text::Formatted::Box.new(texts, :document => @pdf, :width => @pdf.width_of("Noua Delineatio Geographica gen"))
112
+ lambda {
113
+ text_box.render
114
+ }.should_not raise_error
115
+ text_box.text.should == "Noua Delineatio Geographica\ngeneralis | Apostolicarum\nperegrinationum | S FRANCISCI\nXAUERII | Indiarum & Iaponi\346\nApostoli"
116
+ end
117
+ end
118
+ end
119
+
120
+ describe "Text::Formatted::Box with :fallback_fonts option that includes" +
121
+ "a Chinese font and set of Chinese glyphs not in the current font" do
122
+ it "should change the font to the Chinese font for the Chinese glyphs" do
123
+ create_pdf
124
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
125
+ @pdf.font_families["Kai"] = {
126
+ :normal => { :file => file, :font => "Kai" }
127
+ }
128
+ formatted_text = [{ :text => "hello你好" },
129
+ { :text => "再见goodbye" }]
130
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Kai"])
131
+
132
+ text = PDF::Inspector::Text.analyze(@pdf.render)
133
+
134
+ fonts_used = text.font_settings.map { |e| e[:name] }
135
+ fonts_used.length.should == 4
136
+ fonts_used[0].should == :"Helvetica"
137
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
138
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
139
+ fonts_used[3].should == :"Helvetica"
140
+
141
+ text.strings[0].should == "hello"
142
+ text.strings[1].should == "你好"
143
+ text.strings[2].should == "再见"
144
+ text.strings[3].should == "goodbye"
145
+ end
146
+ end
147
+
148
+ describe "Text::Formatted::Box with :fallback_fonts option that includes" +
149
+ "an AFM font and Win-Ansi glyph not in the current Chinese font" do
150
+ it "should change the font to the AFM font for the Win-Ansi glyph" do
151
+ create_pdf
152
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
153
+ @pdf.font_families["Kai"] = {
154
+ :normal => { :file => file, :font => "Kai" }
155
+ }
156
+ @pdf.font("Kai")
157
+ formatted_text = [{ :text => "hello你好" },
158
+ { :text => "再见€" }]
159
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Helvetica"])
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].to_s.should =~ /GBZenKai-Medium/
166
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
167
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
168
+ fonts_used[3].should == :"Helvetica"
169
+
170
+ text.strings[0].should == "hello"
171
+ text.strings[1].should == "你好"
172
+ text.strings[2].should == "再见"
173
+ text.strings[3].should == "€"
174
+ end
175
+ end
176
+
177
+ describe "Text::Formatted::Box with :fallback_fonts option and fragment " +
178
+ "level font" do
179
+ it "should use the fragment level font except for glyphs not in that font" do
180
+ create_pdf
181
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
182
+ @pdf.font_families["Kai"] = {
183
+ :normal => { :file => file, :font => "Kai" }
184
+ }
185
+ formatted_text = [{ :text => "hello你好" },
186
+ { :text => "再见goodbye", :font => "Times-Roman" }]
187
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Kai"])
188
+
189
+ text = PDF::Inspector::Text.analyze(@pdf.render)
190
+
191
+ fonts_used = text.font_settings.map { |e| e[:name] }
192
+ fonts_used.length.should == 4
193
+ fonts_used[0].should == :"Helvetica"
194
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
195
+ fonts_used[2].to_s.should =~ /GBZenKai-Medium/
196
+ fonts_used[3].should == :"Times-Roman"
197
+
198
+ text.strings[0].should == "hello"
199
+ text.strings[1].should == "你好"
200
+ text.strings[2].should == "再见"
201
+ text.strings[3].should == "goodbye"
202
+ end
203
+ end
204
+
205
+ describe "Text::Formatted::Box" do
206
+ before(:each) do
207
+ create_pdf
208
+ file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
209
+ @pdf.font_families["Kai"] = {
210
+ :normal => { :file => file, :font => "Kai" }
211
+ }
212
+ @formatted_text = [{ :text => "hello你好" }]
213
+ @pdf.fallback_fonts(["Kai"])
214
+ @pdf.fallback_fonts = ["Kai"]
215
+ end
216
+ it "#fallback_fonts should return the document-wide fallback fonts" do
217
+ @pdf.fallback_fonts.should == ["Kai"]
218
+ end
219
+ it "should be able to set text fallback_fonts document-wide" do
220
+ @pdf.formatted_text_box(@formatted_text)
221
+
222
+ text = PDF::Inspector::Text.analyze(@pdf.render)
223
+
224
+ fonts_used = text.font_settings.map { |e| e[:name] }
225
+ fonts_used.length.should == 2
226
+ fonts_used[0].should == :"Helvetica"
227
+ fonts_used[1].to_s.should =~ /GBZenKai-Medium/
228
+ end
229
+ it "should be able to override document-wide fallback_fonts" do
230
+ @pdf.formatted_text_box(@formatted_text, :fallback_fonts => ["Courier"])
231
+
232
+ text = PDF::Inspector::Text.analyze(@pdf.render)
233
+
234
+ fonts_used = text.font_settings.map { |e| e[:name] }
235
+ fonts_used.length.should == 1
236
+ fonts_used[0].should == :"Helvetica"
237
+ end
238
+ it "should omit the fallback fonts overhead when passing an empty array " +
239
+ "as the :fallback_fonts" do
240
+ box = Prawn::Text::Formatted::Box.new(@formatted_text,
241
+ :document => @pdf,
242
+ :fallback_fonts => [])
243
+ box.expects(:process_fallback_fonts).never
244
+ box.render
245
+ end
246
+ it "should be able to clear document-wide fallback_fonts" do
247
+ @pdf.fallback_fonts([])
248
+ box = Prawn::Text::Formatted::Box.new(@formatted_text,
249
+ :document => @pdf)
250
+ box.expects(:process_fallback_fonts).never
251
+ box.render
252
+ end
253
+ end
254
+
255
+ describe "Text::Formatted::Box with :fallback_fonts option " +
256
+ "with glyphs not in the primary or the fallback fonts" do
257
+ it "should use the primary font" do
258
+ create_pdf
259
+ formatted_text = [{ :text => "hello world. 世界你好。" }]
260
+ @pdf.formatted_text_box(formatted_text, :fallback_fonts => ["Helvetica"])
261
+
262
+ text = PDF::Inspector::Text.analyze(@pdf.render)
263
+
264
+ fonts_used = text.font_settings.map { |e| e[:name] }
265
+ fonts_used.length.should == 1
266
+ fonts_used[0].should == :"Helvetica"
267
+ end
268
+ end
269
+
270
+ describe "Text::Formatted::Box#extensions" do
271
+ it "should be able to override default line wrapping" do
272
+ create_pdf
273
+ Prawn::Text::Formatted::Box.extensions << TestFormattedWrapOverride
274
+ @pdf.formatted_text_box([{ :text => "hello world" }], {})
275
+ Prawn::Text::Formatted::Box.extensions.delete(TestFormattedWrapOverride)
276
+ text = PDF::Inspector::Text.analyze(@pdf.render)
277
+ text.strings[0].should == "all your base are belong to us"
278
+ end
279
+ it "overriding Text::Formatted::Box line wrapping should not affect " +
280
+ "Text::Box wrapping" do
281
+ create_pdf
282
+ Prawn::Text::Formatted::Box.extensions << TestFormattedWrapOverride
283
+ @pdf.text_box("hello world", {})
284
+ Prawn::Text::Formatted::Box.extensions.delete(TestFormattedWrapOverride)
285
+ text = PDF::Inspector::Text.analyze(@pdf.render)
286
+ text.strings[0].should == "hello world"
287
+ end
288
+ it "overriding Text::Box line wrapping should override Text::Box wrapping" do
289
+ create_pdf
290
+ Prawn::Text::Box.extensions << TestFormattedWrapOverride
291
+ @pdf.text_box("hello world", {})
292
+ Prawn::Text::Box.extensions.delete(TestFormattedWrapOverride)
293
+ text = PDF::Inspector::Text.analyze(@pdf.render)
294
+ text.strings[0].should == "all your base are belong to us"
295
+ end
296
+ end
297
+
298
+ describe "Text::Formatted::Box#render" do
299
+ it "should handle newlines" do
300
+ create_pdf
301
+ array = [{ :text => "hello\nworld"}]
302
+ options = { :document => @pdf }
303
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
304
+ text_box.render
305
+ text_box.text.should == "hello\nworld"
306
+ end
307
+ it "should omit spaces from the beginning of the line" do
308
+ create_pdf
309
+ array = [{ :text => " hello\n world"}]
310
+ options = { :document => @pdf }
311
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
312
+ text_box.render
313
+ text_box.text.should == "hello\nworld"
314
+ end
315
+ it "should be okay printing a line of whitespace" do
316
+ create_pdf
317
+ array = [{ :text => "hello\n \nworld"}]
318
+ options = { :document => @pdf }
319
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
320
+ text_box.render
321
+ text_box.text.should == "hello\n\nworld"
322
+
323
+
324
+ array = [{ :text => "hello" + " " * 500},
325
+ { :text => " " * 500 },
326
+ { :text => " " * 500 + "\n"},
327
+ { :text => "world"}]
328
+ options = { :document => @pdf }
329
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
330
+ text_box.render
331
+ text_box.text.should == "hello\n\nworld"
332
+ end
333
+ it "should enable fragment level direction setting" do
334
+ create_pdf
335
+ number_of_hellos = 18
336
+ array = [
337
+ { :text => "hello " * number_of_hellos },
338
+ { :text => "world", :direction => :ltr },
339
+ { :text => ", how are you?" }
340
+ ]
341
+ options = { :document => @pdf, :direction => :rtl }
342
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
343
+ text_box.render
344
+ text = PDF::Inspector::Text.analyze(@pdf.render)
345
+ text.strings[0].should == "era woh ,"
346
+ text.strings[1].should == "world"
347
+ text.strings[2].should == " olleh" * number_of_hellos
348
+ text.strings[3].should == "?uoy"
349
+ end
350
+ end
351
+
352
+ describe "Text::Formatted::Box#render" do
353
+ it "should be able to perform fragment callbacks" do
354
+ create_pdf
355
+ callback_object = TestFragmentCallback.new("something", 7,
356
+ :document => @pdf)
357
+ callback_object.expects(:render_behind).with(
358
+ kind_of(Prawn::Text::Formatted::Fragment))
359
+ callback_object.expects(:render_in_front).with(
360
+ kind_of(Prawn::Text::Formatted::Fragment))
361
+ array = [{ :text => "hello world " },
362
+ { :text => "callback now",
363
+ :callback => callback_object }]
364
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
365
+ text_box.render
366
+ end
367
+ it "should be able to perform fragment callbacks on multiple objects" do
368
+ create_pdf
369
+
370
+ callback_object = TestFragmentCallback.new("something", 7,
371
+ :document => @pdf)
372
+ callback_object.expects(:render_behind).with(
373
+ kind_of(Prawn::Text::Formatted::Fragment))
374
+ callback_object.expects(:render_in_front).with(
375
+ kind_of(Prawn::Text::Formatted::Fragment))
376
+
377
+ callback_object2 = TestFragmentCallback.new("something else", 14,
378
+ :document => @pdf)
379
+ callback_object2.expects(:render_behind).with(
380
+ kind_of(Prawn::Text::Formatted::Fragment))
381
+ callback_object2.expects(:render_in_front).with(
382
+ kind_of(Prawn::Text::Formatted::Fragment))
383
+
384
+ array = [{ :text => "hello world " },
385
+ { :text => "callback now",
386
+ :callback => [callback_object, callback_object2] }]
387
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
388
+ text_box.render
389
+ end
390
+ it "fragment callbacks should be able to define only the callback they need" do
391
+ create_pdf
392
+ behind = TestFragmentCallbackBehind.new("something", 7,
393
+ :document => @pdf)
394
+ in_front = TestFragmentCallbackInFront.new("something", 7,
395
+ :document => @pdf)
396
+ array = [{ :text => "hello world " },
397
+ { :text => "callback now",
398
+ :callback => [behind, in_front] }]
399
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
400
+ lambda { text_box.render }.should_not raise_error(NoMethodError)
401
+ end
402
+ it "should be able to set the font" do
403
+ create_pdf
404
+ array = [{ :text => "this contains " },
405
+ { :text => "Times-Bold",
406
+ :styles => [:bold],
407
+ :font => "Times-Roman" },
408
+ { :text => " text" }]
409
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
410
+ text_box.render
411
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
412
+ fonts = contents.font_settings.map { |e| e[:name] }
413
+ fonts.should == [:Helvetica, :"Times-Bold", :Helvetica]
414
+ contents.strings[0].should == "this contains "
415
+ contents.strings[1].should == "Times-Bold"
416
+ contents.strings[2].should == " text"
417
+ end
418
+ it "should be able to set bold" do
419
+ create_pdf
420
+ array = [{ :text => "this contains " },
421
+ { :text => "bold", :styles => [:bold] },
422
+ { :text => " text" }]
423
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
424
+ text_box.render
425
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
426
+ fonts = contents.font_settings.map { |e| e[:name] }
427
+ fonts.should == [:Helvetica, :"Helvetica-Bold", :Helvetica]
428
+ contents.strings[0].should == "this contains "
429
+ contents.strings[1].should == "bold"
430
+ contents.strings[2].should == " text"
431
+ end
432
+ it "should be able to set italics" do
433
+ create_pdf
434
+ array = [{ :text => "this contains " },
435
+ { :text => "italic", :styles => [:italic] },
436
+ { :text => " text" }]
437
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
438
+ text_box.render
439
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
440
+ fonts = contents.font_settings.map { |e| e[:name] }
441
+ fonts.should == [:Helvetica, :"Helvetica-Oblique", :Helvetica]
442
+ end
443
+ it "should be able to set subscript" do
444
+ create_pdf
445
+ array = [{ :text => "this contains " },
446
+ { :text => "subscript", :size => 18, :styles => [:subscript] },
447
+ { :text => " text" }]
448
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
449
+ text_box.render
450
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
451
+ contents.font_settings[0][:size].should == 12
452
+ contents.font_settings[1][:size].should be_within(0.0001).of(18 * 0.583)
453
+ end
454
+ it "should be able to set superscript" do
455
+ create_pdf
456
+ array = [{ :text => "this contains " },
457
+ { :text => "superscript", :size => 18, :styles => [:superscript] },
458
+ { :text => " text" }]
459
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
460
+ text_box.render
461
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
462
+ contents.font_settings[0][:size].should == 12
463
+ contents.font_settings[1][:size].should be_within(0.0001).of(18 * 0.583)
464
+ end
465
+ it "should be able to set compound bold and italic text" do
466
+ create_pdf
467
+ array = [{ :text => "this contains " },
468
+ { :text => "bold italic", :styles => [:bold, :italic] },
469
+ { :text => " text" }]
470
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
471
+ text_box.render
472
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
473
+ fonts = contents.font_settings.map { |e| e[:name] }
474
+ fonts.should == [:Helvetica, :"Helvetica-BoldOblique", :Helvetica]
475
+ end
476
+ it "should be able to underline" do
477
+ create_pdf
478
+ array = [{ :text => "this contains " },
479
+ { :text => "underlined", :styles => [:underline] },
480
+ { :text => " text" }]
481
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
482
+ text_box.render
483
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
484
+ line_drawing.points.length.should == 2
485
+ end
486
+ it "should be able to strikethrough" do
487
+ create_pdf
488
+ array = [{ :text => "this contains " },
489
+ { :text => "struckthrough", :styles => [:strikethrough] },
490
+ { :text => " text" }]
491
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
492
+ text_box.render
493
+ line_drawing = PDF::Inspector::Graphics::Line.analyze(@pdf.render)
494
+ line_drawing.points.length.should == 2
495
+ end
496
+ it "should be able to add URL links" do
497
+ create_pdf
498
+ @pdf.expects(:link_annotation).with(kind_of(Array), :Border => [0,0,0],
499
+ :A => { :Type => :Action, :S => :URI, :URI => "http://example.com" })
500
+ array = [{ :text => "click " },
501
+ { :text => "here", :link => "http://example.com" },
502
+ { :text => " to visit" }]
503
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
504
+ text_box.render
505
+ end
506
+ it "should be able to add destination links" do
507
+ create_pdf
508
+ @pdf.expects(:link_annotation).with(kind_of(Array), :Border => [0,0,0],
509
+ :Dest => "ToC")
510
+ array = [{ :text => "Go to the " },
511
+ { :text => "Table of Contents", :anchor => "ToC" }]
512
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
513
+ text_box.render
514
+ end
515
+ it "should be able to set font size" do
516
+ create_pdf
517
+ array = [{ :text => "this contains " },
518
+ { :text => "sized", :size => 24 },
519
+ { :text => " text" }]
520
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
521
+ text_box.render
522
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
523
+ contents.font_settings[0][:size].should == 12
524
+ contents.font_settings[1][:size].should == 24
525
+ end
526
+ it "should set the baseline based on the tallest fragment on a given line" do
527
+ create_pdf
528
+ array = [{ :text => "this contains " },
529
+ { :text => "sized", :size => 24 },
530
+ { :text => " text" }]
531
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
532
+ text_box.render
533
+ @pdf.font_size(24) do
534
+ text_box.height.should be_within(0.001).of(@pdf.font.ascender + @pdf.font.descender)
535
+ end
536
+ end
537
+ it "should be able to set color via an rgb hex string" do
538
+ create_pdf
539
+ array = [{ :text => "rgb",
540
+ :color => "ff0000" }]
541
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
542
+ text_box.render
543
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
544
+ colors.fill_color_count.should == 2
545
+ colors.stroke_color_count.should == 2
546
+ end
547
+ it "should be able to set color using a cmyk array" do
548
+ create_pdf
549
+ array = [{ :text => "cmyk",
550
+ :color => [100, 0, 0, 0] }]
551
+ text_box = Prawn::Text::Formatted::Box.new(array, :document => @pdf)
552
+ text_box.render
553
+ colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
554
+ colors.fill_color_count.should == 2
555
+ colors.stroke_color_count.should == 2
556
+ end
557
+ end
558
+
559
+ describe "Text::Formatted::Box#render with fragment level :character_spacing option" do
560
+ it "should draw the character spacing to the document" do
561
+ create_pdf
562
+ array = [{ :text => "hello world",
563
+ :character_spacing => 7 }]
564
+ options = { :document => @pdf }
565
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
566
+ text_box.render
567
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
568
+ contents.character_spacing[0].should == 7
569
+ end
570
+ it "should draw the character spacing to the document" do
571
+ create_pdf
572
+ array = [{ :text => "hello world",
573
+ :font => "Courier",
574
+ :character_spacing => 10 }]
575
+ options = { :document => @pdf,
576
+ :width => 100,
577
+ :overflow => :expand }
578
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
579
+ text_box.render
580
+ text_box.text.should == "hello\nworld"
581
+ end
582
+ end
583
+
584
+ describe "Text::Formatted::Box#render with :align => :justify" do
585
+ it "should not justify the last line of a paragraph" do
586
+ create_pdf
587
+ array = [{ :text => "hello world " },
588
+ { :text => "\n" },
589
+ { :text => "goodbye" }]
590
+ options = { :document => @pdf, :align => :justify }
591
+ text_box = Prawn::Text::Formatted::Box.new(array, options)
592
+ text_box.render
593
+ contents = PDF::Inspector::Text.analyze(@pdf.render)
594
+ contents.word_spacing.should be_empty
595
+ end
596
+ end
597
+
598
+ class TestFragmentCallback
599
+ def initialize(string, number, options)
600
+ @document = options[:document]
601
+ end
602
+
603
+ def render_behind(fragment)
604
+ end
605
+
606
+ def render_in_front(fragment)
607
+ end
608
+ end
609
+
610
+ class TestFragmentCallbackBehind
611
+ def initialize(string, number, options)
612
+ @document = options[:document]
613
+ end
614
+
615
+ def render_behind(fragment)
616
+ end
617
+ end
618
+
619
+ class TestFragmentCallbackInFront
620
+ def initialize(string, number, options)
621
+ @document = options[:document]
622
+ end
623
+
624
+ def render_in_front(fragment)
625
+ end
626
+ end
627
+
628
+ module TestFormattedWrapOverride
629
+ def wrap(array)
630
+ initialize_wrap([{ :text => 'all your base are belong to us' }])
631
+ line_to_print = @line_wrap.wrap_line(:document => @document,
632
+ :kerning => @kerning,
633
+ :width => 10000,
634
+ :arranger => @arranger)
635
+ fragment = @arranger.retrieve_fragment
636
+ format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
637
+
638
+ []
639
+ end
640
+ end