prawn 0.15.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. data/COPYING +2 -2
  2. data/LICENSE +1 -1
  3. data/README.md +96 -0
  4. data/Rakefile +27 -30
  5. data/data/fonts/Action Man.dfont +0 -0
  6. data/data/fonts/Activa.ttf +0 -0
  7. data/data/fonts/Chalkboard.ttf +0 -0
  8. data/data/fonts/DejaVuSans.ttf +0 -0
  9. data/data/fonts/Dustismo_Roman.ttf +0 -0
  10. data/data/fonts/comicsans.ttf +0 -0
  11. data/data/fonts/gkai00mp.ttf +0 -0
  12. data/data/images/16bit.alpha +0 -0
  13. data/data/images/16bit.dat +0 -0
  14. data/data/images/dice.alpha +0 -0
  15. data/data/images/dice.dat +0 -0
  16. data/data/images/page_white_text.alpha +0 -0
  17. data/data/images/page_white_text.dat +0 -0
  18. data/data/images/rails.dat +0 -0
  19. data/data/images/rails.png +0 -0
  20. data/data/pdfs/nested_pages.pdf +13 -13
  21. data/lib/prawn.rb +21 -85
  22. data/lib/prawn/compatibility.rb +51 -0
  23. data/lib/prawn/core.rb +85 -0
  24. data/lib/prawn/core/annotations.rb +61 -0
  25. data/lib/prawn/core/byte_string.rb +9 -0
  26. data/lib/prawn/core/destinations.rb +90 -0
  27. data/lib/prawn/core/document_state.rb +78 -0
  28. data/lib/prawn/core/literal_string.rb +16 -0
  29. data/lib/prawn/core/name_tree.rb +177 -0
  30. data/lib/prawn/core/object_store.rb +264 -0
  31. data/lib/prawn/core/page.rb +215 -0
  32. data/lib/prawn/core/pdf_object.rb +108 -0
  33. data/lib/prawn/core/reference.rb +115 -0
  34. data/lib/prawn/core/text.rb +268 -0
  35. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  36. data/lib/prawn/core/text/formatted/line_wrap.rb +273 -0
  37. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  38. data/lib/prawn/document.rb +122 -155
  39. data/lib/prawn/document/bounding_box.rb +7 -36
  40. data/lib/prawn/document/column_box.rb +10 -38
  41. data/lib/prawn/document/graphics_state.rb +74 -11
  42. data/lib/prawn/document/internals.rb +23 -24
  43. data/lib/prawn/document/page_geometry.rb +136 -0
  44. data/lib/prawn/document/snapshot.rb +6 -7
  45. data/lib/prawn/document/span.rb +10 -12
  46. data/lib/prawn/encoding.rb +10 -9
  47. data/lib/prawn/errors.rb +30 -15
  48. data/lib/prawn/font.rb +104 -136
  49. data/lib/prawn/font/afm.rb +44 -46
  50. data/lib/prawn/font/dfont.rb +3 -4
  51. data/lib/prawn/font/ttf.rb +50 -31
  52. data/lib/prawn/graphics.rb +57 -302
  53. data/lib/prawn/graphics/cap_style.rb +3 -4
  54. data/lib/prawn/graphics/color.rb +5 -13
  55. data/lib/prawn/graphics/dash.rb +31 -53
  56. data/lib/prawn/graphics/gradient.rb +84 -0
  57. data/lib/prawn/graphics/join_style.rb +7 -9
  58. data/lib/prawn/graphics/transformation.rb +9 -10
  59. data/lib/prawn/graphics/transparency.rb +1 -3
  60. data/lib/prawn/images.rb +59 -69
  61. data/lib/prawn/images/image.rb +22 -6
  62. data/lib/prawn/images/jpg.rb +14 -20
  63. data/lib/prawn/images/png.rb +118 -61
  64. data/lib/prawn/layout.rb +15 -10
  65. data/lib/prawn/layout/grid.rb +54 -66
  66. data/lib/prawn/measurement_extensions.rb +6 -10
  67. data/lib/prawn/measurements.rb +21 -27
  68. data/lib/prawn/outline.rb +308 -6
  69. data/lib/prawn/repeater.rb +8 -10
  70. data/lib/prawn/security.rb +33 -55
  71. data/lib/prawn/security/arcfour.rb +0 -1
  72. data/lib/prawn/stamp.rb +3 -5
  73. data/lib/prawn/table.rb +60 -188
  74. data/lib/prawn/table/cell.rb +44 -272
  75. data/lib/prawn/table/cell/image.rb +3 -2
  76. data/lib/prawn/table/cell/in_table.rb +2 -4
  77. data/lib/prawn/table/cell/subtable.rb +2 -2
  78. data/lib/prawn/table/cell/text.rb +18 -41
  79. data/lib/prawn/table/cells.rb +48 -142
  80. data/lib/prawn/text.rb +25 -32
  81. data/lib/prawn/text/box.rb +6 -12
  82. data/lib/prawn/text/formatted.rb +4 -5
  83. data/lib/prawn/text/formatted/box.rb +59 -96
  84. data/lib/prawn/text/formatted/fragment.rb +23 -34
  85. data/lib/prawn/text/formatted/parser.rb +5 -15
  86. data/prawn.gemspec +13 -24
  87. data/spec/annotations_spec.rb +32 -16
  88. data/spec/bounding_box_spec.rb +17 -119
  89. data/spec/cell_spec.rb +42 -112
  90. data/spec/destinations_spec.rb +5 -5
  91. data/spec/document_spec.rb +111 -155
  92. data/spec/extensions/mocha.rb +0 -1
  93. data/spec/font_spec.rb +99 -149
  94. data/spec/formatted_text_arranger_spec.rb +43 -43
  95. data/spec/formatted_text_box_spec.rb +44 -43
  96. data/spec/formatted_text_fragment_spec.rb +8 -8
  97. data/spec/graphics_spec.rb +68 -151
  98. data/spec/grid_spec.rb +15 -26
  99. data/spec/images_spec.rb +30 -51
  100. data/spec/inline_formatted_text_parser_spec.rb +20 -69
  101. data/spec/jpg_spec.rb +4 -4
  102. data/spec/line_wrap_spec.rb +28 -28
  103. data/spec/measurement_units_spec.rb +6 -6
  104. data/spec/name_tree_spec.rb +112 -0
  105. data/spec/object_store_spec.rb +106 -17
  106. data/spec/outline_spec.rb +63 -103
  107. data/spec/pdf_object_spec.rb +170 -0
  108. data/spec/png_spec.rb +25 -25
  109. data/spec/reference_spec.rb +65 -8
  110. data/spec/repeater_spec.rb +10 -10
  111. data/spec/security_spec.rb +12 -44
  112. data/spec/snapshot_spec.rb +7 -7
  113. data/spec/span_spec.rb +15 -10
  114. data/spec/spec_helper.rb +8 -32
  115. data/spec/stamp_spec.rb +30 -29
  116. data/spec/stroke_styles_spec.rb +18 -36
  117. data/spec/table_spec.rb +111 -706
  118. data/spec/template_spec.rb +297 -0
  119. data/spec/text_at_spec.rb +33 -19
  120. data/spec/text_box_spec.rb +64 -100
  121. data/spec/text_rendering_mode_spec.rb +5 -5
  122. data/spec/text_spacing_spec.rb +4 -4
  123. data/spec/text_spec.rb +64 -84
  124. data/spec/transparency_spec.rb +5 -5
  125. metadata +290 -463
  126. checksums.yaml +0 -7
  127. data/.yardopts +0 -10
  128. data/Gemfile +0 -11
  129. data/data/images/16bit.color +0 -0
  130. data/data/images/dice.color +0 -0
  131. data/data/images/indexed_color.dat +0 -0
  132. data/data/images/indexed_color.png +0 -0
  133. data/data/images/page_white_text.color +0 -0
  134. data/lib/prawn/font_metric_cache.rb +0 -47
  135. data/lib/prawn/graphics/patterns.rb +0 -138
  136. data/lib/prawn/image_handler.rb +0 -36
  137. data/lib/prawn/soft_mask.rb +0 -96
  138. data/lib/prawn/table/cell/span_dummy.rb +0 -93
  139. data/lib/prawn/table/column_width_calculator.rb +0 -61
  140. data/lib/prawn/text/formatted/arranger.rb +0 -290
  141. data/lib/prawn/text/formatted/line_wrap.rb +0 -266
  142. data/lib/prawn/text/formatted/wrap.rb +0 -150
  143. data/lib/prawn/utilities.rb +0 -46
  144. data/manual/basic_concepts/adding_pages.rb +0 -27
  145. data/manual/basic_concepts/basic_concepts.rb +0 -34
  146. data/manual/basic_concepts/creation.rb +0 -39
  147. data/manual/basic_concepts/cursor.rb +0 -33
  148. data/manual/basic_concepts/measurement.rb +0 -25
  149. data/manual/basic_concepts/origin.rb +0 -38
  150. data/manual/basic_concepts/other_cursor_helpers.rb +0 -40
  151. data/manual/bounding_box/bounding_box.rb +0 -39
  152. data/manual/bounding_box/bounds.rb +0 -49
  153. data/manual/bounding_box/canvas.rb +0 -24
  154. data/manual/bounding_box/creation.rb +0 -23
  155. data/manual/bounding_box/indentation.rb +0 -46
  156. data/manual/bounding_box/nesting.rb +0 -45
  157. data/manual/bounding_box/russian_boxes.rb +0 -40
  158. data/manual/bounding_box/stretchy.rb +0 -31
  159. data/manual/document_and_page_options/background.rb +0 -27
  160. data/manual/document_and_page_options/document_and_page_options.rb +0 -32
  161. data/manual/document_and_page_options/metadata.rb +0 -23
  162. data/manual/document_and_page_options/page_margins.rb +0 -38
  163. data/manual/document_and_page_options/page_size.rb +0 -34
  164. data/manual/document_and_page_options/print_scaling.rb +0 -20
  165. data/manual/example_file.rb +0 -111
  166. data/manual/example_helper.rb +0 -411
  167. data/manual/example_package.rb +0 -53
  168. data/manual/example_section.rb +0 -46
  169. data/manual/graphics/circle_and_ellipse.rb +0 -22
  170. data/manual/graphics/color.rb +0 -24
  171. data/manual/graphics/common_lines.rb +0 -30
  172. data/manual/graphics/fill_and_stroke.rb +0 -42
  173. data/manual/graphics/fill_rules.rb +0 -37
  174. data/manual/graphics/gradients.rb +0 -37
  175. data/manual/graphics/graphics.rb +0 -58
  176. data/manual/graphics/helper.rb +0 -24
  177. data/manual/graphics/line_width.rb +0 -35
  178. data/manual/graphics/lines_and_curves.rb +0 -41
  179. data/manual/graphics/polygon.rb +0 -29
  180. data/manual/graphics/rectangle.rb +0 -21
  181. data/manual/graphics/rotate.rb +0 -28
  182. data/manual/graphics/scale.rb +0 -41
  183. data/manual/graphics/soft_masks.rb +0 -46
  184. data/manual/graphics/stroke_cap.rb +0 -31
  185. data/manual/graphics/stroke_dash.rb +0 -48
  186. data/manual/graphics/stroke_join.rb +0 -30
  187. data/manual/graphics/translate.rb +0 -29
  188. data/manual/graphics/transparency.rb +0 -35
  189. data/manual/images/absolute_position.rb +0 -23
  190. data/manual/images/fit.rb +0 -21
  191. data/manual/images/horizontal.rb +0 -25
  192. data/manual/images/images.rb +0 -40
  193. data/manual/images/plain_image.rb +0 -18
  194. data/manual/images/scale.rb +0 -22
  195. data/manual/images/vertical.rb +0 -28
  196. data/manual/images/width_and_height.rb +0 -25
  197. data/manual/layout/boxes.rb +0 -27
  198. data/manual/layout/content.rb +0 -25
  199. data/manual/layout/layout.rb +0 -28
  200. data/manual/layout/simple_grid.rb +0 -23
  201. data/manual/manual/cover.rb +0 -36
  202. data/manual/manual/foreword.rb +0 -85
  203. data/manual/manual/how_to_read_this_manual.rb +0 -41
  204. data/manual/manual/manual.rb +0 -34
  205. data/manual/outline/add_subsection_to.rb +0 -61
  206. data/manual/outline/insert_section_after.rb +0 -47
  207. data/manual/outline/outline.rb +0 -32
  208. data/manual/outline/sections_and_pages.rb +0 -67
  209. data/manual/repeatable_content/page_numbering.rb +0 -54
  210. data/manual/repeatable_content/repeatable_content.rb +0 -31
  211. data/manual/repeatable_content/repeater.rb +0 -55
  212. data/manual/repeatable_content/stamp.rb +0 -41
  213. data/manual/security/encryption.rb +0 -31
  214. data/manual/security/permissions.rb +0 -38
  215. data/manual/security/security.rb +0 -28
  216. data/manual/syntax_highlight.rb +0 -52
  217. data/manual/table/basic_block.rb +0 -53
  218. data/manual/table/before_rendering_page.rb +0 -26
  219. data/manual/table/cell_border_lines.rb +0 -24
  220. data/manual/table/cell_borders_and_bg.rb +0 -31
  221. data/manual/table/cell_dimensions.rb +0 -30
  222. data/manual/table/cell_text.rb +0 -38
  223. data/manual/table/column_widths.rb +0 -30
  224. data/manual/table/content_and_subtables.rb +0 -39
  225. data/manual/table/creation.rb +0 -27
  226. data/manual/table/filtering.rb +0 -36
  227. data/manual/table/flow_and_header.rb +0 -17
  228. data/manual/table/image_cells.rb +0 -33
  229. data/manual/table/position.rb +0 -29
  230. data/manual/table/row_colors.rb +0 -20
  231. data/manual/table/span.rb +0 -30
  232. data/manual/table/style.rb +0 -22
  233. data/manual/table/table.rb +0 -52
  234. data/manual/table/width.rb +0 -27
  235. data/manual/text/alignment.rb +0 -44
  236. data/manual/text/color.rb +0 -24
  237. data/manual/text/column_box.rb +0 -32
  238. data/manual/text/fallback_fonts.rb +0 -37
  239. data/manual/text/font.rb +0 -41
  240. data/manual/text/font_size.rb +0 -45
  241. data/manual/text/font_style.rb +0 -23
  242. data/manual/text/formatted_callbacks.rb +0 -60
  243. data/manual/text/formatted_text.rb +0 -54
  244. data/manual/text/free_flowing_text.rb +0 -51
  245. data/manual/text/group.rb +0 -31
  246. data/manual/text/inline.rb +0 -43
  247. data/manual/text/kerning_and_character_spacing.rb +0 -39
  248. data/manual/text/leading.rb +0 -25
  249. data/manual/text/line_wrapping.rb +0 -41
  250. data/manual/text/paragraph_indentation.rb +0 -26
  251. data/manual/text/positioned_text.rb +0 -38
  252. data/manual/text/registering_families.rb +0 -48
  253. data/manual/text/rendering_and_color.rb +0 -37
  254. data/manual/text/right_to_left_text.rb +0 -43
  255. data/manual/text/rotation.rb +0 -43
  256. data/manual/text/single_usage.rb +0 -37
  257. data/manual/text/text.rb +0 -75
  258. data/manual/text/text_box_excess.rb +0 -32
  259. data/manual/text/text_box_extensions.rb +0 -45
  260. data/manual/text/text_box_overflow.rb +0 -44
  261. data/manual/text/utf8.rb +0 -28
  262. data/manual/text/win_ansi_charset.rb +0 -59
  263. data/spec/acceptance/png.rb +0 -23
  264. data/spec/column_box_spec.rb +0 -65
  265. data/spec/extensions/encoding_helpers.rb +0 -9
  266. data/spec/font_metric_cache_spec.rb +0 -52
  267. data/spec/image_handler_spec.rb +0 -54
  268. data/spec/soft_mask_spec.rb +0 -117
  269. data/spec/table/span_dummy_spec.rb +0 -17
@@ -1,61 +0,0 @@
1
- module Prawn
2
- class Table
3
- # @private
4
- class ColumnWidthCalculator
5
- def initialize(cells)
6
- @cells = cells
7
-
8
- @widths_by_column = Hash.new(0)
9
- @rows_with_a_span_dummy = Hash.new(false)
10
- end
11
-
12
- def natural_widths
13
- @cells.each do |cell|
14
- @rows_with_a_span_dummy[cell.row] = true if cell.is_a?(Cell::SpanDummy)
15
- end
16
-
17
- #calculate natural column width for all rows that do not include a span dummy
18
- @cells.each do |cell|
19
- unless @rows_with_a_span_dummy[cell.row]
20
- @widths_by_column[cell.column] =
21
- [@widths_by_column[cell.column], cell.width.to_f].max
22
- end
23
- end
24
-
25
- #integrate natural column widths for all rows that do include a span dummy
26
- @cells.each do |cell|
27
- next unless @rows_with_a_span_dummy[cell.row]
28
- #the width of a SpanDummy cell will be calculated by the "mother" cell
29
- next if cell.is_a?(Cell::SpanDummy)
30
-
31
- if cell.colspan == 1
32
- @widths_by_column[cell.column] =
33
- [@widths_by_column[cell.column], cell.width.to_f].max
34
- else
35
- #calculate the current with of all cells that will be spanned by the current cell
36
- current_width_of_spanned_cells =
37
- @widths_by_column.to_a[cell.column..(cell.column + cell.colspan - 1)]
38
- .collect{|key, value| value}.inject(0, :+)
39
-
40
- #update the Hash only if the new with is at least equal to the old one
41
- #due to arithmetic errors we need to ignore a small difference in the new and the old sum
42
- #the same had to be done in the column_widht_calculator#natural_width
43
- update_hash = ((cell.width.to_f - current_width_of_spanned_cells) >
44
- Prawn::FLOAT_PRECISION)
45
-
46
- if update_hash
47
- # Split the width of colspanned cells evenly by columns
48
- width_per_column = cell.width.to_f / cell.colspan
49
- # Update the Hash
50
- cell.colspan.times do |i|
51
- @widths_by_column[cell.column + i] = width_per_column
52
- end
53
- end
54
- end
55
- end
56
-
57
- @widths_by_column.sort_by { |col, _| col }.map { |_, w| w }
58
- end
59
- end
60
- end
61
- end
@@ -1,290 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # core/text/formatted/arranger.rb : Implements a data structure for 2-stage
4
- # processing of lines of formatted text
5
- #
6
- # Copyright February 2010, Daniel Nelson. All Rights Reserved.
7
- #
8
- # This is free software. Please see the LICENSE and COPYING files for details.
9
-
10
- module Prawn
11
- module Text
12
- module Formatted #:nodoc:
13
-
14
- # @private
15
-
16
- class Arranger #:nodoc:
17
- attr_reader :max_line_height
18
- attr_reader :max_descender
19
- attr_reader :max_ascender
20
- attr_accessor :consumed
21
-
22
- # The following present only for testing purposes
23
- attr_reader :unconsumed
24
- attr_reader :fragments
25
- attr_reader :current_format_state
26
-
27
- def initialize(document, options={})
28
- @document = document
29
- @fragments = []
30
- @unconsumed = []
31
- @kerning = options[:kerning]
32
- end
33
-
34
- def space_count
35
- if @unfinalized_line
36
- raise "Lines must be finalized before calling #space_count"
37
- end
38
- @fragments.inject(0) do |sum, fragment|
39
- sum + fragment.space_count
40
- end
41
- end
42
-
43
- def line_width
44
- if @unfinalized_line
45
- raise "Lines must be finalized before calling #line_width"
46
- end
47
- @fragments.inject(0) do |sum, fragment|
48
- sum + fragment.width
49
- end
50
- end
51
-
52
- def line
53
- if @unfinalized_line
54
- raise "Lines must be finalized before calling #line"
55
- end
56
- @fragments.collect do |fragment|
57
- fragment.text.dup.force_encoding(::Encoding::UTF_8)
58
- end.join
59
- end
60
-
61
- def finalize_line
62
- @unfinalized_line = false
63
- omit_trailing_whitespace_from_line_width
64
- @fragments = []
65
- @consumed.each do |hash|
66
- text = hash[:text]
67
- format_state = hash.dup
68
- format_state.delete(:text)
69
- fragment = Prawn::Text::Formatted::Fragment.new(text,
70
- format_state,
71
- @document)
72
- @fragments << fragment
73
- set_fragment_measurements(fragment)
74
- set_line_measurement_maximums(fragment)
75
- end
76
- end
77
-
78
- def format_array=(array)
79
- initialize_line
80
- @unconsumed = []
81
- array.each do |hash|
82
- hash[:text].scan(/[^\n]+|\n/) do |line|
83
- @unconsumed << hash.merge(:text => line)
84
- end
85
- end
86
- end
87
-
88
- def initialize_line
89
- @unfinalized_line = true
90
- @max_line_height = 0
91
- @max_descender = 0
92
- @max_ascender = 0
93
-
94
- @consumed = []
95
- @fragments = []
96
- end
97
-
98
- def finished?
99
- @unconsumed.length == 0
100
- end
101
-
102
- def next_string
103
- unless @unfinalized_line
104
- raise "Lines must not be finalized when calling #next_string"
105
- end
106
- hash = @unconsumed.shift
107
- if hash.nil?
108
- nil
109
- else
110
- @consumed << hash.dup
111
- @current_format_state = hash.dup
112
- @current_format_state.delete(:text)
113
- hash[:text]
114
- end
115
- end
116
-
117
- def preview_next_string
118
- hash = @unconsumed.first
119
- if hash.nil? then nil
120
- else hash[:text]
121
- end
122
- end
123
-
124
- def apply_color_and_font_settings(fragment, &block)
125
- if fragment.color
126
- original_fill_color = @document.fill_color
127
- original_stroke_color = @document.stroke_color
128
- @document.fill_color(*fragment.color)
129
- @document.stroke_color(*fragment.color)
130
- apply_font_settings(fragment, &block)
131
- @document.stroke_color = original_stroke_color
132
- @document.fill_color = original_fill_color
133
- else
134
- apply_font_settings(fragment, &block)
135
- end
136
- end
137
-
138
- def apply_font_settings(fragment=nil, &block)
139
- if fragment.nil?
140
- font = current_format_state[:font]
141
- size = current_format_state[:size]
142
- character_spacing = current_format_state[:character_spacing] ||
143
- @document.character_spacing
144
- styles = current_format_state[:styles]
145
- font_style = font_style(styles)
146
- else
147
- font = fragment.font
148
- size = fragment.size
149
- character_spacing = fragment.character_spacing
150
- styles = fragment.styles
151
- font_style = font_style(styles)
152
- end
153
-
154
- @document.character_spacing(character_spacing) do
155
- if font || font_style != :normal
156
- raise "Bad font family" unless @document.font.family
157
- @document.font(font || @document.font.family, :style => font_style) do
158
- apply_font_size(size, styles, &block)
159
- end
160
- else
161
- apply_font_size(size, styles, &block)
162
- end
163
- end
164
- end
165
-
166
- def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
167
- return if printed.nil?
168
- if printed.empty?
169
- @consumed.pop
170
- else
171
- @consumed.last[:text] = printed
172
- if normalized_soft_hyphen
173
- @consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
174
- end
175
- end
176
-
177
- unless unprinted.empty?
178
- @unconsumed.unshift(@current_format_state.merge(:text => unprinted))
179
- end
180
-
181
- load_previous_format_state if printed.empty?
182
- end
183
-
184
- def retrieve_fragment
185
- if @unfinalized_line
186
- raise "Lines must be finalized before fragments can be retrieved"
187
- end
188
- @fragments.shift
189
- end
190
-
191
- def repack_unretrieved
192
- new_unconsumed = []
193
- while fragment = retrieve_fragment
194
- fragment.include_trailing_white_space!
195
- new_unconsumed << fragment.format_state.merge(:text => fragment.text)
196
- end
197
- @unconsumed = new_unconsumed.concat(@unconsumed)
198
- end
199
-
200
- def font_style(styles)
201
- if styles.nil?
202
- :normal
203
- elsif styles.include?(:bold) && styles.include?(:italic)
204
- :bold_italic
205
- elsif styles.include?(:bold)
206
- :bold
207
- elsif styles.include?(:italic)
208
- :italic
209
- else
210
- :normal
211
- end
212
- end
213
-
214
- private
215
-
216
- def load_previous_format_state
217
- if @consumed.empty?
218
- @current_format_state = {}
219
- else
220
- hash = @consumed.last
221
- @current_format_state = hash.dup
222
- @current_format_state.delete(:text)
223
- end
224
- end
225
-
226
- def apply_font_size(size, styles)
227
- if subscript?(styles) || superscript?(styles)
228
- relative_size = 0.583
229
- if size.nil?
230
- size = @document.font_size * relative_size
231
- else
232
- size = size * relative_size
233
- end
234
- end
235
- if size.nil?
236
- yield
237
- else
238
- @document.font_size(size) { yield }
239
- end
240
- end
241
-
242
- def subscript?(styles)
243
- if styles.nil? then false
244
- else styles.include?(:subscript)
245
- end
246
- end
247
-
248
- def superscript?(styles)
249
- if styles.nil? then false
250
- else styles.include?(:superscript)
251
- end
252
- end
253
-
254
- def omit_trailing_whitespace_from_line_width
255
- @consumed.reverse_each do |hash|
256
- if hash[:text] == "\n"
257
- break
258
- elsif hash[:text].strip.empty? && @consumed.length > 1
259
- # this entire fragment is trailing white space
260
- hash[:exclude_trailing_white_space] = true
261
- else
262
- # this fragment contains the first non-white space we have
263
- # encountered since the end of the line
264
- hash[:exclude_trailing_white_space] = true
265
- break
266
- end
267
- end
268
- end
269
-
270
- def set_fragment_measurements(fragment)
271
- apply_font_settings(fragment) do
272
- fragment.width = @document.width_of(fragment.text,
273
- :kerning => @kerning)
274
- fragment.line_height = @document.font.height
275
- fragment.descender = @document.font.descender
276
- fragment.ascender = @document.font.ascender
277
- end
278
- end
279
-
280
- def set_line_measurement_maximums(fragment)
281
- @max_line_height = [defined?(@max_line_height) && @max_line_height, fragment.line_height].compact.max
282
- @max_descender = [defined?(@max_descender) && @max_descender, fragment.descender].compact.max
283
- @max_ascender = [defined?(@max_ascender) && @max_ascender, fragment.ascender].compact.max
284
- end
285
-
286
- end
287
-
288
- end
289
- end
290
- end
@@ -1,266 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # core/text/formatted/line_wrap.rb : Implements individual line wrapping of
4
- # formatted text
5
- #
6
- # Copyright February 2010, Daniel Nelson. All Rights Reserved.
7
- #
8
- # This is free software. Please see the LICENSE and COPYING files for details.
9
- #
10
-
11
- module Prawn
12
- module Text
13
- module Formatted #:nodoc:
14
-
15
- # @private
16
- class LineWrap #:nodoc:
17
-
18
- # The width of the last wrapped line
19
- #
20
- def width
21
- @accumulated_width || 0
22
- end
23
-
24
- # The number of spaces in the last wrapped line
25
- attr_reader :space_count
26
-
27
- # Whether this line is the last line in the paragraph
28
- def paragraph_finished?
29
- @newline_encountered || is_next_string_newline? || @arranger.finished?
30
- end
31
-
32
- # Work in conjunction with the PDF::Formatted::Arranger
33
- # defined in the :arranger option to determine what formatted text
34
- # will fit within the width defined by the :width option
35
- #
36
- def wrap_line(options)
37
- initialize_line(options)
38
-
39
- while fragment = @arranger.next_string
40
- @fragment_output = ""
41
-
42
- fragment.lstrip! if first_fragment_on_this_line?(fragment)
43
- next if empty_line?(fragment)
44
-
45
- unless apply_font_settings_and_add_fragment_to_line(fragment)
46
- break
47
- end
48
- end
49
- @arranger.finalize_line
50
- @accumulated_width = @arranger.line_width
51
- @space_count = @arranger.space_count
52
- @arranger.line
53
- end
54
-
55
- private
56
-
57
- def first_fragment_on_this_line?(fragment)
58
- line_empty? && fragment != "\n"
59
- end
60
-
61
- def empty_line?(fragment)
62
- empty = line_empty? && fragment.empty? && is_next_string_newline?
63
- @arranger.update_last_string("", "", soft_hyphen) if empty
64
- empty
65
- end
66
-
67
- def is_next_string_newline?
68
- @arranger.preview_next_string == "\n"
69
- end
70
-
71
- def apply_font_settings_and_add_fragment_to_line(fragment)
72
- result = nil
73
- @arranger.apply_font_settings do
74
- # if font has changed from Unicode to non-Unicode, or vice versa, the characters used for soft hyphens
75
- # and zero-width spaces will be different
76
- set_soft_hyphen_and_zero_width_space
77
- result = add_fragment_to_line(fragment)
78
- end
79
- result
80
- end
81
-
82
- # returns true iff all text was printed without running into the end of
83
- # the line
84
- #
85
- def add_fragment_to_line(fragment)
86
- if fragment == ""
87
- true
88
- elsif fragment == "\n"
89
- @newline_encountered = true
90
- false
91
- else
92
- fragment.scan(scan_pattern).each do |segment|
93
- if segment == zero_width_space
94
- segment_width = 0
95
- else
96
- segment_width = @document.width_of(segment, :kerning => @kerning)
97
- end
98
-
99
- if @accumulated_width + segment_width <= @width
100
- @accumulated_width += segment_width
101
- @fragment_output += segment
102
- else
103
- end_of_the_line_reached(segment)
104
- fragment_finished(fragment)
105
- return false
106
- end
107
- end
108
-
109
- fragment_finished(fragment)
110
- true
111
- end
112
- end
113
-
114
- # The pattern used to determine chunks of text to place on a given line
115
- #
116
- def scan_pattern
117
- pattern = "[^#{break_chars}]+#{soft_hyphen}|" +
118
- "[^#{break_chars}]+#{hyphen}+|" +
119
- "[^#{break_chars}]+|" +
120
- "[#{whitespace}]+|" +
121
- "#{hyphen}+[^#{break_chars}]*|" +
122
- "#{soft_hyphen}"
123
- Regexp.new(pattern)
124
- end
125
-
126
- # The pattern used to determine whether any word breaks exist on a
127
- # current line, which in turn determines whether character level
128
- # word breaking is needed
129
- #
130
- def word_division_scan_pattern
131
- Regexp.new("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
132
- end
133
-
134
- def break_chars
135
- "#{whitespace}#{soft_hyphen}#{hyphen}"
136
- end
137
-
138
- def whitespace
139
- " \\t#{zero_width_space}"
140
- end
141
-
142
- def hyphen
143
- "-"
144
- end
145
-
146
- def soft_hyphen
147
- @soft_hyphen
148
- end
149
-
150
- def zero_width_space
151
- @zero_width_space
152
- end
153
-
154
- def line_empty?
155
- @line_empty && @accumulated_width == 0
156
- end
157
-
158
- def initialize_line(options)
159
- @document = options[:document]
160
- @kerning = options[:kerning]
161
- @width = options[:width]
162
-
163
- @accumulated_width = 0
164
- @line_empty = true
165
- @line_contains_more_than_one_word = false
166
-
167
- @arranger = options[:arranger]
168
- @arranger.initialize_line
169
-
170
- @newline_encountered = false
171
- @line_full = false
172
- end
173
-
174
- def set_soft_hyphen_and_zero_width_space
175
- # this is done once per fragment, after the font settings for the fragment are applied --
176
- # it could actually be skipped if the font hasn't changed
177
- font = @document.font
178
- @soft_hyphen = font.normalize_encoding(Prawn::Text::SHY)
179
- @zero_width_space = font.unicode? ? Prawn::Text::ZWSP : ""
180
- end
181
-
182
- def fragment_finished(fragment)
183
- if fragment == "\n"
184
- @newline_encountered = true
185
- @line_empty = false
186
- else
187
- update_output_based_on_last_fragment(fragment, soft_hyphen)
188
- update_line_status_based_on_last_output
189
- determine_whether_to_pull_preceding_fragment_to_join_this_one(fragment)
190
- end
191
- remember_this_fragment_for_backward_looking_ops
192
- end
193
-
194
- def update_output_based_on_last_fragment(fragment, normalized_soft_hyphen=nil)
195
- remaining_text = fragment.slice(@fragment_output.length..fragment.length)
196
- raise Prawn::Errors::CannotFit if line_finished? && line_empty? &&
197
- @fragment_output.empty? && !fragment.strip.empty?
198
- @arranger.update_last_string(@fragment_output, remaining_text, normalized_soft_hyphen)
199
- end
200
-
201
- def determine_whether_to_pull_preceding_fragment_to_join_this_one(current_fragment)
202
- if @fragment_output.empty? &&
203
- !current_fragment.empty? &&
204
- @line_contains_more_than_one_word
205
- unless previous_fragment_ended_with_breakable? ||
206
- fragment_begins_with_breakable?(current_fragment)
207
- @fragment_output = @previous_fragment_output_without_last_word
208
- update_output_based_on_last_fragment(@previous_fragment)
209
- end
210
- end
211
- end
212
-
213
- def remember_this_fragment_for_backward_looking_ops
214
- @previous_fragment = @fragment_output.dup
215
- pf = @previous_fragment
216
- @previous_fragment_ended_with_breakable = pf =~ /[#{break_chars}]$/
217
- last_word = pf.slice(/[^#{break_chars}]*$/)
218
- last_word_length = last_word.nil? ? 0 : last_word.length
219
- @previous_fragment_output_without_last_word = pf.slice(0, pf.length - last_word_length)
220
- end
221
-
222
- def previous_fragment_ended_with_breakable?
223
- @previous_fragment_ended_with_breakable
224
- end
225
-
226
- def fragment_begins_with_breakable?(fragment)
227
- fragment =~ /^[#{break_chars}]/
228
- end
229
-
230
- def line_finished?
231
- @line_full || paragraph_finished?
232
- end
233
-
234
- def update_line_status_based_on_last_output
235
- @line_contains_more_than_one_word = true if @fragment_output =~ word_division_scan_pattern
236
- end
237
-
238
- def end_of_the_line_reached(segment)
239
- update_line_status_based_on_last_output
240
- wrap_by_char(segment) unless @line_contains_more_than_one_word
241
- @line_full = true
242
- end
243
-
244
- def wrap_by_char(segment)
245
- font = @document.font
246
- segment.each_char do |char|
247
- break unless append_char(char,font)
248
- end
249
- end
250
-
251
- def append_char(char,font)
252
- # kerning doesn't make sense in the context of a single character
253
- char_width = font.compute_width_of(char)
254
-
255
- if @accumulated_width + char_width <= @width
256
- @accumulated_width += char_width
257
- @fragment_output << char
258
- true
259
- else
260
- false
261
- end
262
- end
263
- end
264
- end
265
- end
266
- end