prawn 2.0.2 → 2.3.0

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