prawn 2.1.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (278) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +2 -0
  3. data.tar.gz.sig +0 -0
  4. data/Gemfile +1 -9
  5. data/Rakefile +12 -22
  6. data/lib/prawn.rb +29 -48
  7. data/lib/prawn/document.rb +148 -123
  8. data/lib/prawn/document/bounding_box.rb +33 -26
  9. data/lib/prawn/document/column_box.rb +5 -7
  10. data/lib/prawn/document/internals.rb +6 -6
  11. data/lib/prawn/document/span.rb +20 -17
  12. data/lib/prawn/encoding.rb +65 -67
  13. data/lib/prawn/errors.rb +10 -7
  14. data/lib/prawn/font.rb +78 -62
  15. data/lib/prawn/font/afm.rb +93 -66
  16. data/lib/prawn/font/dfont.rb +2 -10
  17. data/lib/prawn/font/ttc.rb +34 -0
  18. data/lib/prawn/font/ttf.rb +73 -65
  19. data/lib/prawn/font_metric_cache.rb +9 -8
  20. data/lib/prawn/graphics.rb +110 -70
  21. data/lib/prawn/graphics/blend_mode.rb +7 -8
  22. data/lib/prawn/graphics/cap_style.rb +2 -4
  23. data/lib/prawn/graphics/color.rb +23 -26
  24. data/lib/prawn/graphics/dash.rb +22 -12
  25. data/lib/prawn/graphics/join_style.rb +8 -4
  26. data/lib/prawn/graphics/patterns.rb +185 -96
  27. data/lib/prawn/graphics/transformation.rb +11 -9
  28. data/lib/prawn/graphics/transparency.rb +15 -13
  29. data/lib/prawn/grid.rb +20 -20
  30. data/lib/prawn/image_handler.rb +4 -6
  31. data/lib/prawn/images.rb +22 -15
  32. data/lib/prawn/images/image.rb +0 -1
  33. data/lib/prawn/images/jpg.rb +26 -22
  34. data/lib/prawn/images/png.rb +60 -57
  35. data/lib/prawn/measurement_extensions.rb +8 -9
  36. data/lib/prawn/measurements.rb +14 -15
  37. data/lib/prawn/outline.rb +96 -78
  38. data/lib/prawn/repeater.rb +12 -10
  39. data/lib/prawn/security.rb +66 -48
  40. data/lib/prawn/security/arcfour.rb +1 -3
  41. data/lib/prawn/soft_mask.rb +23 -25
  42. data/lib/prawn/stamp.rb +16 -12
  43. data/lib/prawn/text.rb +59 -45
  44. data/lib/prawn/text/box.rb +9 -8
  45. data/lib/prawn/text/formatted.rb +4 -6
  46. data/lib/prawn/text/formatted/arranger.rb +51 -30
  47. data/lib/prawn/text/formatted/box.rb +112 -88
  48. data/lib/prawn/text/formatted/fragment.rb +10 -15
  49. data/lib/prawn/text/formatted/line_wrap.rb +118 -61
  50. data/lib/prawn/text/formatted/parser.rb +134 -110
  51. data/lib/prawn/text/formatted/wrap.rb +42 -32
  52. data/lib/prawn/transformation_stack.rb +3 -4
  53. data/lib/prawn/utilities.rb +6 -21
  54. data/lib/prawn/version.rb +1 -3
  55. data/lib/prawn/view.rb +4 -2
  56. data/manual/basic_concepts/adding_pages.rb +4 -7
  57. data/manual/basic_concepts/basic_concepts.rb +29 -22
  58. data/manual/basic_concepts/creation.rb +8 -11
  59. data/manual/basic_concepts/cursor.rb +2 -5
  60. data/manual/basic_concepts/measurement.rb +3 -6
  61. data/manual/basic_concepts/origin.rb +3 -6
  62. data/manual/basic_concepts/other_cursor_helpers.rb +9 -12
  63. data/manual/basic_concepts/view.rb +20 -16
  64. data/manual/bounding_box/bounding_box.rb +27 -24
  65. data/manual/bounding_box/bounds.rb +9 -12
  66. data/manual/bounding_box/canvas.rb +2 -5
  67. data/manual/bounding_box/creation.rb +4 -7
  68. data/manual/bounding_box/indentation.rb +12 -15
  69. data/manual/bounding_box/nesting.rb +22 -17
  70. data/manual/bounding_box/russian_boxes.rb +8 -9
  71. data/manual/bounding_box/stretchy.rb +10 -13
  72. data/manual/contents.rb +26 -22
  73. data/manual/cover.rb +22 -20
  74. data/manual/document_and_page_options/background.rb +9 -13
  75. data/manual/document_and_page_options/document_and_page_options.rb +23 -20
  76. data/manual/document_and_page_options/metadata.rb +16 -16
  77. data/manual/document_and_page_options/page_margins.rb +16 -20
  78. data/manual/document_and_page_options/page_size.rb +11 -12
  79. data/manual/document_and_page_options/print_scaling.rb +15 -15
  80. data/manual/example_helper.rb +2 -4
  81. data/manual/graphics/blend_mode.rb +10 -9
  82. data/manual/graphics/circle_and_ellipse.rb +2 -5
  83. data/manual/graphics/color.rb +5 -9
  84. data/manual/graphics/common_lines.rb +5 -8
  85. data/manual/graphics/fill_and_stroke.rb +2 -5
  86. data/manual/graphics/fill_rules.rb +7 -10
  87. data/manual/graphics/gradients.rb +25 -21
  88. data/manual/graphics/graphics.rb +49 -43
  89. data/manual/graphics/helper.rb +10 -9
  90. data/manual/graphics/line_width.rb +5 -7
  91. data/manual/graphics/lines_and_curves.rb +5 -8
  92. data/manual/graphics/polygon.rb +4 -8
  93. data/manual/graphics/rectangle.rb +2 -5
  94. data/manual/graphics/rotate.rb +4 -7
  95. data/manual/graphics/scale.rb +12 -15
  96. data/manual/graphics/soft_masks.rb +1 -4
  97. data/manual/graphics/stroke_cap.rb +3 -6
  98. data/manual/graphics/stroke_dash.rb +9 -12
  99. data/manual/graphics/stroke_join.rb +2 -5
  100. data/manual/graphics/translate.rb +7 -10
  101. data/manual/graphics/transparency.rb +5 -8
  102. data/manual/how_to_read_this_manual.rb +4 -6
  103. data/manual/images/absolute_position.rb +4 -7
  104. data/manual/images/fit.rb +5 -8
  105. data/manual/images/horizontal.rb +6 -9
  106. data/manual/images/images.rb +25 -23
  107. data/manual/images/plain_image.rb +3 -6
  108. data/manual/images/scale.rb +7 -10
  109. data/manual/images/vertical.rb +10 -13
  110. data/manual/images/width_and_height.rb +8 -11
  111. data/manual/layout/boxes.rb +3 -6
  112. data/manual/layout/content.rb +5 -8
  113. data/manual/layout/layout.rb +16 -16
  114. data/manual/layout/simple_grid.rb +4 -7
  115. data/manual/outline/add_subsection_to.rb +18 -21
  116. data/manual/outline/insert_section_after.rb +13 -16
  117. data/manual/outline/outline.rb +19 -17
  118. data/manual/outline/sections_and_pages.rb +15 -18
  119. data/manual/repeatable_content/alternate_page_numbering.rb +19 -17
  120. data/manual/repeatable_content/page_numbering.rb +15 -16
  121. data/manual/repeatable_content/repeatable_content.rb +23 -19
  122. data/manual/repeatable_content/repeater.rb +12 -15
  123. data/manual/repeatable_content/stamp.rb +12 -15
  124. data/manual/security/encryption.rb +7 -10
  125. data/manual/security/permissions.rb +17 -14
  126. data/manual/security/security.rb +17 -16
  127. data/manual/table.rb +2 -4
  128. data/manual/text/alignment.rb +14 -17
  129. data/manual/text/color.rb +10 -11
  130. data/manual/text/column_box.rb +5 -8
  131. data/manual/text/fallback_fonts.rb +23 -21
  132. data/manual/text/font.rb +9 -12
  133. data/manual/text/font_size.rb +11 -14
  134. data/manual/text/font_style.rb +4 -7
  135. data/manual/text/formatted_callbacks.rb +23 -21
  136. data/manual/text/formatted_text.rb +31 -25
  137. data/manual/text/free_flowing_text.rb +18 -21
  138. data/manual/text/inline.rb +16 -19
  139. data/manual/text/kerning_and_character_spacing.rb +12 -15
  140. data/manual/text/leading.rb +5 -8
  141. data/manual/text/line_wrapping.rb +33 -17
  142. data/manual/text/paragraph_indentation.rb +11 -14
  143. data/manual/text/positioned_text.rb +13 -16
  144. data/manual/text/registering_families.rb +16 -19
  145. data/manual/text/rendering_and_color.rb +7 -10
  146. data/manual/text/right_to_left_text.rb +24 -19
  147. data/manual/text/rotation.rb +26 -23
  148. data/manual/text/single_usage.rb +6 -9
  149. data/manual/text/text.rb +56 -52
  150. data/manual/text/text_box_excess.rb +18 -17
  151. data/manual/text/text_box_extensions.rb +16 -15
  152. data/manual/text/text_box_overflow.rb +15 -18
  153. data/manual/text/utf8.rb +9 -12
  154. data/manual/text/win_ansi_charset.rb +18 -19
  155. data/prawn.gemspec +37 -27
  156. data/spec/extensions/encoding_helpers.rb +0 -2
  157. data/spec/manual_spec.rb +33 -0
  158. data/spec/prawn/document/bounding_box_spec.rb +546 -0
  159. data/spec/prawn/document/column_box_spec.rb +73 -0
  160. data/spec/prawn/document/security_spec.rb +173 -0
  161. data/spec/prawn/document_annotations_spec.rb +74 -0
  162. data/spec/prawn/document_destinations_spec.rb +13 -0
  163. data/spec/prawn/document_grid_spec.rb +96 -0
  164. data/spec/prawn/document_reference_spec.rb +25 -0
  165. data/spec/prawn/document_span_spec.rb +34 -0
  166. data/spec/prawn/document_spec.rb +751 -0
  167. data/spec/prawn/font_metric_cache_spec.rb +52 -0
  168. data/spec/prawn/font_spec.rb +513 -0
  169. data/spec/prawn/graphics/blend_mode_spec.rb +61 -0
  170. data/spec/prawn/graphics/transparency_spec.rb +79 -0
  171. data/spec/prawn/graphics_spec.rb +817 -0
  172. data/spec/prawn/graphics_stroke_styles_spec.rb +227 -0
  173. data/spec/{image_handler_spec.rb → prawn/image_handler_spec.rb} +13 -15
  174. data/spec/prawn/images/jpg_spec.rb +18 -0
  175. data/spec/prawn/images/png_spec.rb +281 -0
  176. data/spec/prawn/images_spec.rb +170 -0
  177. data/spec/prawn/measurements_extensions_spec.rb +22 -0
  178. data/spec/prawn/outline_spec.rb +408 -0
  179. data/spec/prawn/repeater_spec.rb +163 -0
  180. data/spec/prawn/soft_mask_spec.rb +72 -0
  181. data/spec/prawn/stamp_spec.rb +168 -0
  182. data/spec/prawn/text/box_spec.rb +1113 -0
  183. data/spec/prawn/text/formatted/arranger_spec.rb +464 -0
  184. data/spec/prawn/text/formatted/box_spec.rb +825 -0
  185. data/spec/prawn/text/formatted/fragment_spec.rb +341 -0
  186. data/spec/prawn/text/formatted/line_wrap_spec.rb +491 -0
  187. data/spec/prawn/text/formatted/parser_spec.rb +667 -0
  188. data/spec/prawn/text_draw_text_spec.rb +147 -0
  189. data/spec/prawn/text_rendering_mode_spec.rb +42 -0
  190. data/spec/prawn/text_spacing_spec.rb +93 -0
  191. data/spec/prawn/text_spec.rb +601 -0
  192. data/spec/prawn/text_with_inline_formatting_spec.rb +33 -0
  193. data/spec/{transformation_stack_spec.rb → prawn/transformation_stack_spec.rb} +21 -20
  194. data/spec/prawn/view_spec.rb +45 -0
  195. data/spec/spec_helper.rb +16 -16
  196. metadata +96 -151
  197. metadata.gz.sig +1 -0
  198. data/data/images/16bit.alpha +0 -0
  199. data/data/images/16bit.color +0 -0
  200. data/data/images/16bit.png +0 -0
  201. data/data/images/arrow.png +0 -0
  202. data/data/images/arrow2.png +0 -0
  203. data/data/images/blend_modes_bottom_layer.jpg +0 -0
  204. data/data/images/blend_modes_top_layer.jpg +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/indexed_transparency.png +0 -0
  213. data/data/images/indexed_transparency_alpha.dat +0 -0
  214. data/data/images/indexed_transparency_color.dat +0 -0
  215. data/data/images/letterhead.jpg +0 -0
  216. data/data/images/license.md +0 -8
  217. data/data/images/page_white_text.alpha +0 -0
  218. data/data/images/page_white_text.color +0 -0
  219. data/data/images/page_white_text.png +0 -0
  220. data/data/images/pigs.jpg +0 -0
  221. data/data/images/prawn.png +0 -0
  222. data/data/images/ruport.png +0 -0
  223. data/data/images/ruport_data.dat +0 -0
  224. data/data/images/ruport_transparent.png +0 -0
  225. data/data/images/ruport_type0.png +0 -0
  226. data/data/images/stef.jpg +0 -0
  227. data/data/images/tru256.bmp +0 -0
  228. data/data/images/web-links.dat +0 -1
  229. data/data/images/web-links.png +0 -0
  230. data/data/pdfs/complex_template.pdf +0 -0
  231. data/data/pdfs/contains_ttf_font.pdf +0 -0
  232. data/data/pdfs/encrypted.pdf +0 -0
  233. data/data/pdfs/form.pdf +1 -819
  234. data/data/pdfs/hexagon.pdf +0 -61
  235. data/data/pdfs/indirect_reference.pdf +0 -86
  236. data/data/pdfs/multipage_template.pdf +0 -127
  237. data/data/pdfs/nested_pages.pdf +0 -118
  238. data/data/pdfs/page_without_mediabox.pdf +0 -193
  239. data/data/pdfs/resources_as_indirect_object.pdf +0 -83
  240. data/data/pdfs/two_hexagons.pdf +0 -90
  241. data/data/pdfs/version_1_6.pdf +0 -61
  242. data/data/shift_jis_text.txt +0 -1
  243. data/spec/acceptance/png_spec.rb +0 -35
  244. data/spec/annotations_spec.rb +0 -67
  245. data/spec/blend_mode_spec.rb +0 -71
  246. data/spec/bounding_box_spec.rb +0 -501
  247. data/spec/column_box_spec.rb +0 -59
  248. data/spec/destinations_spec.rb +0 -13
  249. data/spec/document_spec.rb +0 -738
  250. data/spec/font_metric_cache_spec.rb +0 -52
  251. data/spec/font_spec.rb +0 -475
  252. data/spec/formatted_text_arranger_spec.rb +0 -452
  253. data/spec/formatted_text_box_spec.rb +0 -716
  254. data/spec/formatted_text_fragment_spec.rb +0 -299
  255. data/spec/graphics_spec.rb +0 -705
  256. data/spec/grid_spec.rb +0 -95
  257. data/spec/images_spec.rb +0 -167
  258. data/spec/inline_formatted_text_parser_spec.rb +0 -568
  259. data/spec/jpg_spec.rb +0 -23
  260. data/spec/line_wrap_spec.rb +0 -366
  261. data/spec/measurement_units_spec.rb +0 -22
  262. data/spec/outline_spec.rb +0 -409
  263. data/spec/png_spec.rb +0 -257
  264. data/spec/reference_spec.rb +0 -25
  265. data/spec/repeater_spec.rb +0 -154
  266. data/spec/security_spec.rb +0 -151
  267. data/spec/soft_mask_spec.rb +0 -78
  268. data/spec/span_spec.rb +0 -43
  269. data/spec/stamp_spec.rb +0 -179
  270. data/spec/stroke_styles_spec.rb +0 -208
  271. data/spec/text_at_spec.rb +0 -142
  272. data/spec/text_box_spec.rb +0 -1042
  273. data/spec/text_rendering_mode_spec.rb +0 -45
  274. data/spec/text_spacing_spec.rb +0 -93
  275. data/spec/text_spec.rb +0 -543
  276. data/spec/text_with_inline_formatting_spec.rb +0 -35
  277. data/spec/transparency_spec.rb +0 -91
  278. data/spec/view_spec.rb +0 -42
@@ -1,452 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
-
5
- describe Prawn::Text::Formatted::Arranger do
6
- let(:document) { create_pdf }
7
- subject { Prawn::Text::Formatted::Arranger.new document }
8
-
9
- describe '#format_array' do
10
- it 'populates the unconsumed array' do
11
- array = [
12
- { text: 'hello ' },
13
- { text: 'world how ', styles: [:bold] },
14
- { text: 'are', styles: [:bold, :italic] },
15
- { text: ' you?' }
16
- ]
17
-
18
- subject.format_array = array
19
-
20
- expect(subject.unconsumed[0]).to eq(text: 'hello ')
21
- expect(subject.unconsumed[1]).to eq(text: 'world how ', styles: [:bold])
22
- expect(subject.unconsumed[2]).to eq(text: 'are', styles: [:bold, :italic])
23
- expect(subject.unconsumed[3]).to eq(text: ' you?')
24
- end
25
-
26
- it 'splits newlins into their own elements' do
27
- array = [
28
- { text: "\nhello\nworld" }
29
- ]
30
-
31
- subject.format_array = array
32
-
33
- expect(subject.unconsumed[0]).to eq(text: "\n")
34
- expect(subject.unconsumed[1]).to eq(text: "hello")
35
- expect(subject.unconsumed[2]).to eq(text: "\n")
36
- expect(subject.unconsumed[3]).to eq(text: "world")
37
- end
38
- end
39
-
40
- describe '#preview_next_string' do
41
- context 'with a formatted array' do
42
- let(:array) { [{ text: 'hello' }] }
43
-
44
- before do
45
- subject.format_array = array
46
- end
47
-
48
- it 'does not populate the consumed array' do
49
- subject.preview_next_string
50
- expect(subject.consumed).to eq([])
51
- end
52
-
53
- it 'returns the text of the next unconsumed hash' do
54
- expect(subject.preview_next_string).to eq("hello")
55
- end
56
-
57
- it 'returns nil if there is no more unconsumed text' do
58
- subject.next_string
59
- expect(subject.preview_next_string).to be_nil
60
- end
61
- end
62
- end
63
-
64
- describe '#next_string' do
65
- let(:array) {
66
- [
67
- { text: 'hello ' },
68
- { text: 'world how ', styles: [:bold] },
69
- { text: 'are', styles: [:bold, :italic] },
70
- { text: ' you?' }
71
- ]
72
- }
73
-
74
- before do
75
- subject.format_array = array
76
- end
77
-
78
- it 'raises RuntimeError if called after a line was finalized' do
79
- subject.finalize_line
80
- expect { subject.next_string }.to raise_error(RuntimeError)
81
- end
82
-
83
- it 'populates the conumed array' do
84
- while string = subject.next_string
85
- end
86
-
87
- expect(subject.consumed[0]).to eq(text: 'hello ')
88
- expect(subject.consumed[1]).to eq(text: 'world how ', styles: [:bold])
89
- expect(subject.consumed[2]).to eq(text: 'are', styles: [:bold, :italic])
90
- expect(subject.consumed[3]).to eq(text: ' you?')
91
- end
92
-
93
- it 'populates the current_format_state array' do
94
- string = subject.next_string
95
- expect(subject.current_format_state).to eq({})
96
-
97
- string = subject.next_string
98
- expect(subject.current_format_state).to eq(:styles => [:bold])
99
-
100
- string = subject.next_string
101
- expect(subject.current_format_state).to eq(:styles => [:bold, :italic])
102
-
103
- string = subject.next_string
104
- expect(subject.current_format_state).to eq({})
105
- end
106
-
107
- it 'returns the text of the newly consumed hash' do
108
- expect(subject.next_string).to eq('hello ')
109
- end
110
-
111
- it 'returns nil when there are no more unconsumed hashes' do
112
- 4.times do
113
- subject.next_string
114
- end
115
-
116
- expect(subject.next_string).to be_nil
117
- end
118
- end
119
-
120
- describe '#retrieve_fragment' do
121
- context 'with a formatted array whos text is an empty string' do
122
- let(:array) {
123
- [
124
- { text: "hello\nworld\n\n\nhow are you?" },
125
- { text: "\n" },
126
- { text: "\n" },
127
- { text: "\n" },
128
- { text: "" },
129
- { text: "fine, thanks." },
130
- { text: "" },
131
- { text: "\n" },
132
- { text: "" }
133
- ]
134
- }
135
-
136
- before do
137
- subject.format_array = array
138
-
139
- while string = subject.next_string
140
- end
141
-
142
- subject.finalize_line
143
- end
144
-
145
- it 'never returns a fragment whose text is an empty string' do
146
- while fragment = subject.retrieve_fragment
147
- expect(fragment.text).not_to be_empty
148
- end
149
- end
150
- end
151
-
152
- context 'with formatted array' do
153
- let(:array) {
154
- [
155
- { text: 'hello ' },
156
- { text: 'world how ', styles: [:bold] },
157
- { text: 'are', styles: [:bold, :italic] },
158
- { text: ' you?' }
159
- ]
160
- }
161
-
162
- before do
163
- subject.format_array = array
164
- end
165
-
166
- context 'after all strings have been consumed' do
167
- before do
168
- while string = subject.next_string
169
- end
170
- end
171
-
172
- it 'should raise RuntimeError an error if not finalized' do
173
- expect { subject.retrieve_fragment }.to raise_error(RuntimeError)
174
- end
175
-
176
- context 'and finalized' do
177
- before do
178
- subject.finalize_line
179
- end
180
-
181
- it 'returns the consumed fragments in order of consumption' do
182
- expect(subject.retrieve_fragment.text).to eq("hello ")
183
- expect(subject.retrieve_fragment.text).to eq("world how ")
184
- expect(subject.retrieve_fragment.text).to eq("are")
185
- expect(subject.retrieve_fragment.text).to eq(" you?")
186
- end
187
-
188
- it 'does not alter the current font style' do
189
- subject.retrieve_fragment
190
- expect(subject.current_format_state[:styles]).to be_nil
191
- end
192
- end
193
- end
194
- end
195
- end
196
- end
197
-
198
- describe "Core::Text::Formatted::Arranger#update_last_string" do
199
- it "should update the last retrieved string with what actually fit on" \
200
- "the line and the list of unconsumed with what did not" do
201
- create_pdf
202
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
203
- array = [{ :text => "hello " },
204
- { :text => "world how ", :styles => [:bold] },
205
- { :text => "are", :styles => [:bold, :italic] },
206
- { :text => " you now?", :styles => [:bold, :italic] }]
207
- arranger.format_array = array
208
- while string = arranger.next_string
209
- end
210
- arranger.update_last_string(" you", " now?", nil)
211
- expect(arranger.consumed[3]).to eq(:text => " you",
212
- :styles => [:bold, :italic])
213
- expect(arranger.unconsumed).to eq([{ :text => " now?",
214
- :styles => [:bold, :italic] }])
215
- end
216
- it "should set the format state to the previously processed fragment" do
217
- create_pdf
218
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
219
- array = [{ :text => "hello " },
220
- { :text => "world how ", :styles => [:bold] },
221
- { :text => "are", :styles => [:bold, :italic] },
222
- { :text => " you now?" }]
223
- arranger.format_array = array
224
- 3.times { arranger.next_string }
225
- expect(arranger.current_format_state).to eq(:styles => [:bold, :italic])
226
- arranger.update_last_string("", "are", "-")
227
- expect(arranger.current_format_state).to eq(:styles => [:bold])
228
- end
229
-
230
- context "when the entire string was used" do
231
- it "should not push empty string onto unconsumed" do
232
- create_pdf
233
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
234
- array = [
235
- { :text => "hello " },
236
- { :text => "world how ", :styles => [:bold] },
237
- { :text => "are", :styles => [:bold, :italic] },
238
- { :text => " you now?" }
239
- ]
240
- arranger.format_array = array
241
- while string = arranger.next_string
242
- end
243
- arranger.update_last_string(" you now?", "", nil)
244
- expect(arranger.unconsumed).to eq([])
245
- end
246
- end
247
- end
248
- describe "Core::Text::Formatted::Arranger#space_count" do
249
- before(:each) do
250
- create_pdf
251
- @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
252
- array = [{ :text => "hello " },
253
- { :text => "world how ", :styles => [:bold] },
254
- { :text => "are", :styles => [:bold, :italic] },
255
- { :text => " you?" }]
256
- @arranger.format_array = array
257
- while string = @arranger.next_string
258
- end
259
- end
260
- it "should raise_error an error if called before finalize_line was called" do
261
- expect do
262
- @arranger.space_count
263
- end.to raise_error(RuntimeError)
264
- end
265
- it "should return the total number of spaces in all fragments" do
266
- @arranger.finalize_line
267
- expect(@arranger.space_count).to eq(4)
268
- end
269
- end
270
- describe "Core::Text::Formatted::Arranger#finalize_line" do
271
- it "should make it so that all trailing white space fragments " \
272
- "exclude trailing white space" do
273
- create_pdf
274
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
275
- array = [{ :text => "hello " },
276
- { :text => "world how ", :styles => [:bold] },
277
- { :text => " ", :styles => [:bold, :italic] }]
278
- arranger.format_array = array
279
- while string = arranger.next_string
280
- end
281
- arranger.finalize_line
282
- expect(arranger.fragments.length).to eq(3)
283
-
284
- fragment = arranger.retrieve_fragment
285
- expect(fragment.text).to eq("hello ")
286
-
287
- fragment = arranger.retrieve_fragment
288
- expect(fragment.text).to eq("world how")
289
-
290
- fragment = arranger.retrieve_fragment
291
- expect(fragment.text).to eq("")
292
- end
293
- end
294
-
295
- describe "Core::Text::Formatted::Arranger#line_width" do
296
- before(:each) do
297
- create_pdf
298
- @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
299
- array = [{ :text => "hello " },
300
- { :text => "world", :styles => [:bold] }]
301
- @arranger.format_array = array
302
- while string = @arranger.next_string
303
- end
304
- end
305
- it "should raise_error an error if called before finalize_line was called" do
306
- expect do
307
- @arranger.line_width
308
- end.to raise_error(RuntimeError)
309
- end
310
- it "should return the width of the complete line" do
311
- @arranger.finalize_line
312
- expect(@arranger.line_width).to be > 0
313
- end
314
- end
315
-
316
- describe "Core::Text::Formatted::Arranger#line_width with character_spacing > 0" do
317
- it "should return a width greater than a line without a character_spacing" do
318
- create_pdf
319
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
320
-
321
- array = [{ :text => "hello " },
322
- { :text => "world", :styles => [:bold] }]
323
- arranger.format_array = array
324
- while string = arranger.next_string
325
- end
326
- arranger.finalize_line
327
-
328
- base_line_width = arranger.line_width
329
-
330
- array = [{ :text => "hello " },
331
- { :text => "world", :styles => [:bold],
332
- :character_spacing => 7 }]
333
- arranger.format_array = array
334
- while string = arranger.next_string
335
- end
336
- arranger.finalize_line
337
- expect(arranger.line_width).to be > base_line_width
338
- end
339
- end
340
-
341
- describe "Core::Text::Formatted::Arranger#line" do
342
- before(:each) do
343
- create_pdf
344
- @arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
345
- array = [{ :text => "hello " },
346
- { :text => "world", :styles => [:bold] }]
347
- @arranger.format_array = array
348
- while string = @arranger.next_string
349
- end
350
- end
351
- it "should raise_error an error if called before finalize_line was called" do
352
- expect do
353
- @arranger.line
354
- end.to raise_error(RuntimeError)
355
- end
356
- it "should return the complete line" do
357
- @arranger.finalize_line
358
- expect(@arranger.line).to eq("hello world")
359
- end
360
- end
361
-
362
- describe "Core::Text::Formatted::Arranger#unconsumed" do
363
- it "should return the original array if nothing was consumed" do
364
- create_pdf
365
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
366
- array = [{ :text => "hello " },
367
- { :text => "world how ", :styles => [:bold] },
368
- { :text => "are", :styles => [:bold, :italic] },
369
- { :text => " you now?" }]
370
- arranger.format_array = array
371
- expect(arranger.unconsumed).to eq(array)
372
- end
373
- it "should return an empty array if everything was consumed" do
374
- create_pdf
375
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
376
- array = [{ :text => "hello " },
377
- { :text => "world how ", :styles => [:bold] },
378
- { :text => "are", :styles => [:bold, :italic] },
379
- { :text => " you now?" }]
380
- arranger.format_array = array
381
- while string = arranger.next_string
382
- end
383
- expect(arranger.unconsumed).to eq([])
384
- end
385
- end
386
-
387
- describe "Core::Text::Formatted::Arranger#finished" do
388
- it "should be_false if anything was not printed" do
389
- create_pdf
390
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
391
- array = [{ :text => "hello " },
392
- { :text => "world how ", :styles => [:bold] },
393
- { :text => "are", :styles => [:bold, :italic] },
394
- { :text => " you now?" }]
395
- arranger.format_array = array
396
- while string = arranger.next_string
397
- end
398
- arranger.update_last_string(" you", "now?", nil)
399
- expect(arranger).not_to be_finished
400
- end
401
- it "should be_false if everything was printed" do
402
- create_pdf
403
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
404
- array = [{ :text => "hello " },
405
- { :text => "world how ", :styles => [:bold] },
406
- { :text => "are", :styles => [:bold, :italic] },
407
- { :text => " you now?" }]
408
- arranger.format_array = array
409
- while string = arranger.next_string
410
- end
411
- expect(arranger).to be_finished
412
- end
413
- end
414
-
415
- describe "Core::Text::Formatted::Arranger.max_line_height" do
416
- it "should be the height of the maximum consumed fragment" do
417
- create_pdf
418
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
419
- array = [{ :text => "hello " },
420
- { :text => "world how ", :styles => [:bold] },
421
- { :text => "are", :styles => [:bold, :italic],
422
- :size => 28 },
423
- { :text => " you now?" }]
424
- arranger.format_array = array
425
- while string = arranger.next_string
426
- end
427
- arranger.finalize_line
428
- expect(arranger.max_line_height).to be_within(0.0001).of(33.32)
429
- end
430
- end
431
-
432
- describe "Core::Text::Formatted::Arranger#repack_unretrieved" do
433
- it "should restore part of the original string" do
434
- create_pdf
435
- arranger = Prawn::Text::Formatted::Arranger.new(@pdf)
436
- array = [{ :text => "hello " },
437
- { :text => "world how ", :styles => [:bold] },
438
- { :text => "are", :styles => [:bold, :italic] },
439
- { :text => " you now?" }]
440
- arranger.format_array = array
441
- while string = arranger.next_string
442
- end
443
- arranger.finalize_line
444
- arranger.retrieve_fragment
445
- arranger.retrieve_fragment
446
- arranger.repack_unretrieved
447
- expect(arranger.unconsumed).to eq([
448
- { :text => "are", :styles => [:bold, :italic] },
449
- { :text => " you now?" }
450
- ])
451
- end
452
- 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
- expect(box).to_not receive(:process_fallback_fonts)
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
- expect(box).to_not receive(:process_fallback_fonts)
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
- expect(callback_object).to receive(:render_behind).with(
343
- kind_of(Prawn::Text::Formatted::Fragment)
344
- )
345
- expect(callback_object).to receive(: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
- expect(callback_object).to receive(:render_behind).with(
359
- kind_of(Prawn::Text::Formatted::Fragment)
360
- )
361
- expect(callback_object).to receive(:render_in_front).with(
362
- kind_of(Prawn::Text::Formatted::Fragment)
363
- )
364
-
365
- callback_object2 = TestFragmentCallback.new("something else", 14, :document => @pdf)
366
- expect(callback_object2).to receive(:render_behind).with(
367
- kind_of(Prawn::Text::Formatted::Fragment)
368
- )
369
- expect(callback_object2).to receive(: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
- expect(@pdf).to receive(: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
- expect(@pdf).to receive(: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
- expect(@pdf).to receive(: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