prawn 0.4.1 → 0.5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. data/lib/prawn.rb +2 -72
  2. metadata +33 -224
  3. data/COPYING +0 -340
  4. data/LICENSE +0 -56
  5. data/README +0 -40
  6. data/Rakefile +0 -79
  7. data/data/encodings/win_ansi.txt +0 -29
  8. data/data/fonts/Action Man.dfont +0 -0
  9. data/data/fonts/Activa.ttf +0 -0
  10. data/data/fonts/Chalkboard.ttf +0 -0
  11. data/data/fonts/Courier-Bold.afm +0 -342
  12. data/data/fonts/Courier-BoldOblique.afm +0 -342
  13. data/data/fonts/Courier-Oblique.afm +0 -342
  14. data/data/fonts/Courier.afm +0 -342
  15. data/data/fonts/DejaVuSans.ttf +0 -0
  16. data/data/fonts/Dustismo_Roman.ttf +0 -0
  17. data/data/fonts/Helvetica-Bold.afm +0 -2827
  18. data/data/fonts/Helvetica-BoldOblique.afm +0 -2827
  19. data/data/fonts/Helvetica-Oblique.afm +0 -3051
  20. data/data/fonts/Helvetica.afm +0 -3051
  21. data/data/fonts/MustRead.html +0 -19
  22. data/data/fonts/Symbol.afm +0 -213
  23. data/data/fonts/Times-Bold.afm +0 -2588
  24. data/data/fonts/Times-BoldItalic.afm +0 -2384
  25. data/data/fonts/Times-Italic.afm +0 -2667
  26. data/data/fonts/Times-Roman.afm +0 -2419
  27. data/data/fonts/ZapfDingbats.afm +0 -225
  28. data/data/fonts/comicsans.ttf +0 -0
  29. data/data/fonts/gkai00mp.ttf +0 -0
  30. data/data/images/arrow.png +0 -0
  31. data/data/images/arrow2.png +0 -0
  32. data/data/images/barcode_issue.png +0 -0
  33. data/data/images/dice.alpha +0 -0
  34. data/data/images/dice.dat +0 -0
  35. data/data/images/dice.png +0 -0
  36. data/data/images/fractal.jpg +0 -0
  37. data/data/images/letterhead.jpg +0 -0
  38. data/data/images/page_white_text.alpha +0 -0
  39. data/data/images/page_white_text.dat +0 -0
  40. data/data/images/page_white_text.png +0 -0
  41. data/data/images/pigs.jpg +0 -0
  42. data/data/images/rails.dat +0 -0
  43. data/data/images/rails.png +0 -0
  44. data/data/images/ruport.png +0 -0
  45. data/data/images/ruport_data.dat +0 -0
  46. data/data/images/ruport_transparent.png +0 -0
  47. data/data/images/ruport_type0.png +0 -0
  48. data/data/images/stef.jpg +0 -0
  49. data/data/images/web-links.dat +0 -1
  50. data/data/images/web-links.png +0 -0
  51. data/data/shift_jis_text.txt +0 -1
  52. data/examples/bounding_box/bounding_boxes.rb +0 -44
  53. data/examples/bounding_box/lazy_bounding_boxes.rb +0 -28
  54. data/examples/bounding_box/padded_box.rb +0 -24
  55. data/examples/bounding_box/russian_boxes.rb +0 -37
  56. data/examples/general/background.rb +0 -20
  57. data/examples/general/canvas.rb +0 -16
  58. data/examples/general/measurement_units.rb +0 -52
  59. data/examples/general/multi_page_layout.rb +0 -17
  60. data/examples/general/page_geometry.rb +0 -32
  61. data/examples/graphics/basic_images.rb +0 -27
  62. data/examples/graphics/cmyk.rb +0 -13
  63. data/examples/graphics/curves.rb +0 -12
  64. data/examples/graphics/hexagon.rb +0 -14
  65. data/examples/graphics/image_fit.rb +0 -16
  66. data/examples/graphics/image_flow.rb +0 -38
  67. data/examples/graphics/image_position.rb +0 -18
  68. data/examples/graphics/line.rb +0 -33
  69. data/examples/graphics/png_types.rb +0 -23
  70. data/examples/graphics/polygons.rb +0 -17
  71. data/examples/graphics/remote_images.rb +0 -12
  72. data/examples/graphics/ruport_style_helpers.rb +0 -20
  73. data/examples/graphics/stroke_bounds.rb +0 -23
  74. data/examples/m17n/chinese_text_wrapping.rb +0 -20
  75. data/examples/m17n/euro.rb +0 -16
  76. data/examples/m17n/sjis.rb +0 -29
  77. data/examples/m17n/utf8.rb +0 -14
  78. data/examples/m17n/win_ansi_charset.rb +0 -55
  79. data/examples/text/alignment.rb +0 -19
  80. data/examples/text/dfont.rb +0 -49
  81. data/examples/text/family_based_styling.rb +0 -25
  82. data/examples/text/flowing_text_with_header_and_footer.rb +0 -37
  83. data/examples/text/font_calculations.rb +0 -92
  84. data/examples/text/font_size.rb +0 -34
  85. data/examples/text/kerning.rb +0 -31
  86. data/examples/text/simple_text.rb +0 -18
  87. data/examples/text/simple_text_ttf.rb +0 -18
  88. data/examples/text/span.rb +0 -30
  89. data/examples/text/text_box.rb +0 -26
  90. data/examples/text/text_flow.rb +0 -68
  91. data/lib/prawn/compatibility.rb +0 -38
  92. data/lib/prawn/document.rb +0 -309
  93. data/lib/prawn/document/annotations.rb +0 -63
  94. data/lib/prawn/document/bounding_box.rb +0 -368
  95. data/lib/prawn/document/destinations.rb +0 -81
  96. data/lib/prawn/document/internals.rb +0 -126
  97. data/lib/prawn/document/page_geometry.rb +0 -79
  98. data/lib/prawn/document/span.rb +0 -55
  99. data/lib/prawn/document/text.rb +0 -185
  100. data/lib/prawn/document/text/box.rb +0 -76
  101. data/lib/prawn/document/text/wrapping.rb +0 -59
  102. data/lib/prawn/encoding.rb +0 -121
  103. data/lib/prawn/errors.rb +0 -40
  104. data/lib/prawn/font.rb +0 -277
  105. data/lib/prawn/font/afm.rb +0 -202
  106. data/lib/prawn/font/dfont.rb +0 -31
  107. data/lib/prawn/font/ttf.rb +0 -326
  108. data/lib/prawn/graphics.rb +0 -257
  109. data/lib/prawn/graphics/color.rb +0 -140
  110. data/lib/prawn/images.rb +0 -339
  111. data/lib/prawn/images/jpg.rb +0 -45
  112. data/lib/prawn/images/png.rb +0 -199
  113. data/lib/prawn/literal_string.rb +0 -14
  114. data/lib/prawn/measurement_extensions.rb +0 -46
  115. data/lib/prawn/measurements.rb +0 -71
  116. data/lib/prawn/name_tree.rb +0 -165
  117. data/lib/prawn/pdf_object.rb +0 -73
  118. data/lib/prawn/reference.rb +0 -59
  119. data/spec/annotations_spec.rb +0 -90
  120. data/spec/bounding_box_spec.rb +0 -141
  121. data/spec/destinations_spec.rb +0 -15
  122. data/spec/document_spec.rb +0 -193
  123. data/spec/font_spec.rb +0 -234
  124. data/spec/graphics_spec.rb +0 -209
  125. data/spec/images_spec.rb +0 -68
  126. data/spec/jpg_spec.rb +0 -25
  127. data/spec/measurement_units_spec.rb +0 -23
  128. data/spec/name_tree_spec.rb +0 -103
  129. data/spec/pdf_object_spec.rb +0 -112
  130. data/spec/png_spec.rb +0 -196
  131. data/spec/reference_spec.rb +0 -42
  132. data/spec/spec_helper.rb +0 -23
  133. data/spec/text_spec.rb +0 -178
  134. data/vendor/pdf-inspector/README +0 -18
  135. data/vendor/pdf-inspector/lib/pdf/inspector.rb +0 -25
  136. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +0 -80
  137. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +0 -16
  138. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +0 -31
  139. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +0 -19
  140. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  141. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  142. data/vendor/ttfunk/example.rb +0 -45
  143. data/vendor/ttfunk/lib/ttfunk.rb +0 -102
  144. data/vendor/ttfunk/lib/ttfunk/directory.rb +0 -17
  145. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +0 -88
  146. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +0 -69
  147. data/vendor/ttfunk/lib/ttfunk/reader.rb +0 -44
  148. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +0 -78
  149. data/vendor/ttfunk/lib/ttfunk/subset.rb +0 -18
  150. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +0 -141
  151. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +0 -46
  152. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +0 -48
  153. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +0 -63
  154. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +0 -51
  155. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +0 -72
  156. data/vendor/ttfunk/lib/ttfunk/table.rb +0 -46
  157. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +0 -34
  158. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +0 -54
  159. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +0 -126
  160. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +0 -79
  161. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +0 -64
  162. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +0 -81
  163. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +0 -37
  164. data/vendor/ttfunk/lib/ttfunk/table/head.rb +0 -44
  165. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +0 -41
  166. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +0 -47
  167. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +0 -79
  168. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +0 -62
  169. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +0 -43
  170. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +0 -40
  171. data/vendor/ttfunk/lib/ttfunk/table/name.rb +0 -119
  172. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +0 -78
  173. data/vendor/ttfunk/lib/ttfunk/table/post.rb +0 -91
  174. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +0 -43
  175. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +0 -35
  176. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +0 -23
  177. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +0 -17
  178. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +0 -17
  179. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +0 -14
@@ -1,79 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # page_geometry.rb : Describes PDF page geometries
4
- #
5
- # Copyright April 2008, Gregory Brown. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
- module Prawn
10
- class Document
11
- module PageGeometry
12
-
13
- # Dimensions pulled from PDF::Writer, rubyforge.org/projects/ruby-pdf
14
- SIZES = { "4A0" => [4767.87, 6740.79],
15
- "2A0" => [3370.39, 4767.87],
16
- "A0" => [2383.94, 3370.39],
17
- "A1" => [1683.78, 2383.94],
18
- "A2" => [1190.55, 1683.78],
19
- "A3" => [841.89, 1190.55],
20
- "A4" => [595.28, 841.89],
21
- "A5" => [419.53, 595.28],
22
- "A6" => [297.64, 419.53],
23
- "A7" => [209.76, 297.64],
24
- "A8" => [147.40, 209.76],
25
- "A9" => [104.88, 147.40],
26
- "A10" => [73.70, 104.88],
27
- "B0" => [2834.65, 4008.19],
28
- "B1" => [2004.09, 2834.65],
29
- "B2" => [1417.32, 2004.09],
30
- "B3" => [1000.63, 1417.32],
31
- "B4" => [708.66, 1000.63],
32
- "B5" => [498.90, 708.66],
33
- "B6" => [354.33, 498.90],
34
- "B7" => [249.45, 354.33],
35
- "B8" => [175.75, 249.45],
36
- "B9" => [124.72, 175.75],
37
- "B10" => [87.87, 124.72],
38
- "C0" => [2599.37, 3676.54],
39
- "C1" => [1836.85, 2599.37],
40
- "C2" => [1298.27, 1836.85],
41
- "C3" => [918.43, 1298.27],
42
- "C4" => [649.13, 918.43],
43
- "C5" => [459.21, 649.13],
44
- "C6" => [323.15, 459.21],
45
- "C7" => [229.61, 323.15],
46
- "C8" => [161.57, 229.61],
47
- "C9" => [113.39, 161.57],
48
- "C10" => [79.37, 113.39],
49
- "RA0" => [2437.80, 3458.27],
50
- "RA1" => [1729.13, 2437.80],
51
- "RA2" => [1218.90, 1729.13],
52
- "RA3" => [864.57, 1218.90],
53
- "RA4" => [609.45, 864.57],
54
- "SRA0" => [2551.18, 3628.35],
55
- "SRA1" => [1814.17, 2551.18],
56
- "SRA2" => [1275.59, 1814.17],
57
- "SRA3" => [907.09, 1275.59],
58
- "SRA4" => [637.80, 907.09],
59
- "EXECUTIVE" => [521.86, 756.00],
60
- "FOLIO" => [612.00, 936.00],
61
- "LEGAL" => [612.00, 1008.00],
62
- "LETTER" => [612.00, 792.00],
63
- "TABLOID" => [792.00, 1224.00] }
64
-
65
- def page_dimensions #:nodoc:
66
- coords = SIZES[page_size] || page_size
67
- [0,0] + case(page_layout)
68
- when :portrait
69
- coords
70
- when :landscape
71
- coords.reverse
72
- else
73
- raise Prawn::Errors::InvalidPageLayout,
74
- "Layout must be either :portrait or :landscape"
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,55 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # span.rb : Implements text columns
4
- #
5
- # Copyright September 2008, Gregory Brown. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
- module Prawn
10
- class Document
11
- # A span is a special purpose bounding box that allows a column of
12
- # elements to be positioned relative to the margin_box.
13
- #
14
- # Arguments:
15
- # +width+:: The width of the column in PDF points
16
- #
17
- # Options:
18
- # <tt>:position</tt>:: One of :left, :center, :right or an x offset
19
- #
20
- # This method is typically used for flowing a column of text from one
21
- # page to the next.
22
- #
23
- # span(350, :position => :center) do
24
- # text "Here's some centered text in a 350 point column. " * 100
25
- # end
26
- #
27
- def span(width, options={})
28
- Prawn.verify_options [:position], options
29
- original_position = self.y
30
-
31
- # FIXME: How many effing times do I want to write this same code?
32
- left_boundary = case(options[:position] || :left)
33
- when :left
34
- margin_box.absolute_left
35
- when :center
36
- margin_box.absolute_left + margin_box.width / 2.0 - width /2.0
37
- when :right
38
- margin_box.absolute_right - width
39
- when Numeric
40
- margin_box.absolute_left + options[:position]
41
- else
42
- raise ArgumentError, "Invalid option for :position"
43
- end
44
-
45
- # we need to bust out of whatever nested bounding boxes we're in.
46
- canvas do
47
- bounding_box([left_boundary,
48
- margin_box.absolute_top], :width => width) do
49
- self.y = original_position
50
- yield
51
- end
52
- end
53
- end
54
- end
55
- end
@@ -1,185 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # text.rb : Implements PDF text primitives
4
- #
5
- # Copyright May 2008, Gregory Brown. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
- require "zlib"
9
- require "prawn/document/text/box"
10
- require "prawn/document/text/wrapping"
11
-
12
- module Prawn
13
- class Document
14
- module Text
15
- include Wrapping
16
-
17
- # Draws text on the page. If a point is specified via the +:at+
18
- # option the text will begin exactly at that point, and the string is
19
- # assumed to be pre-formatted to properly fit the page.
20
- #
21
- # pdf.text "Hello World", :at => [100,100]
22
- # pdf.text "Goodbye World", :at => [50,50], :size => 16
23
- #
24
- # When +:at+ is not specified, Prawn attempts to wrap the text to
25
- # fit within your current bounding box (or margin_box if no bounding box
26
- # is being used ). Text will flow onto the next page when it reaches
27
- # the bottom of the bounding box. Text wrap in Prawn does not re-flow
28
- # linebreaks, so if you want fully automated text wrapping, be sure to
29
- # remove newlines before attempting to draw your string.
30
- #
31
- # pdf.text "Will be wrapped when it hits the edge of your bounding box"
32
- # pdf.text "This will be centered", :align => :center
33
- # pdf.text "This will be right aligned", :align => :right
34
- #
35
- # Wrapping is done by splitting words by spaces by default. If your text
36
- # does not contain spaces, you can wrap based on characters instead:
37
- #
38
- # pdf.text "This will be wrapped by character", :wrap => :character
39
- #
40
- # If your font contains kerning pairs data that Prawn can parse, the
41
- # text will be kerned by default. You can disable this feature by passing
42
- # <tt>:kerning => false</tt>.
43
- #
44
- # === Text Positioning Details:
45
- #
46
- # When using the :at parameter, Prawn will position your text by its
47
- # baseline, and flow along a single line.
48
- #
49
- # Otherwise, the text is positioned at font.ascender below the baseline,
50
- # making it easy to use this method within bounding boxes and spans.
51
- #
52
- # == Rotation
53
- #
54
- # Text can be rotated before it is placed on the canvas by specifying the
55
- # :rotate option. Rotation occurs counter-clockwise.
56
- #
57
- # == Encoding
58
- #
59
- # Note that strings passed to this function should be encoded as UTF-8.
60
- # If you get unexpected characters appearing in your rendered document,
61
- # check this.
62
- #
63
- # If the current font is a built-in one, although the string must be
64
- # encoded as UTF-8, only characters that are available in WinAnsi
65
- # are allowed.
66
- #
67
- # If an empty box is rendered to your PDF instead of the character you
68
- # wanted it usually means the current font doesn't include that character.
69
- #
70
- def text(text,options={})
71
- # we'll be messing with the strings encoding, don't change the users
72
- # original string
73
- text = text.to_s.dup
74
-
75
- save_font do
76
- options = text_options.merge(options)
77
- process_text_options(options)
78
-
79
- font.normalize_encoding(text) unless @skip_encoding
80
-
81
- if options[:at]
82
- x,y = translate(options[:at])
83
- font_size(options[:size]) { add_text_content(text,x,y,options) }
84
- else
85
- if options[:rotate]
86
- raise ArgumentError, "Rotated text may only be used with :at"
87
- end
88
- wrapped_text(text,options)
89
- end
90
- end
91
- end
92
-
93
- # A hash of configuration options, to be used globally by text().
94
- #
95
- # pdf.text_options.update(:size => 16, :align => :right)
96
- # pdf.text "Hello World" #=> Size 16 w. right alignment
97
- #
98
- def text_options
99
- @text_options ||= {}
100
- end
101
-
102
- private
103
-
104
- def process_text_options(options)
105
- Prawn.verify_options [:style, :kerning, :size, :at, :wrap,
106
- :spacing, :align, :rotate, :final_gap ], options
107
-
108
- if options[:style]
109
- raise "Bad font family" unless font.family
110
- font(font.family,:style => options[:style])
111
- end
112
-
113
- unless options.key?(:kerning)
114
- options[:kerning] = font.has_kerning_data?
115
- end
116
-
117
- options[:size] ||= font_size
118
- end
119
-
120
- def move_text_position(dy)
121
- bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
122
- @bounding_box.absolute_bottom
123
- start_new_page if (y - dy) < bottom
124
-
125
- self.y -= dy
126
- end
127
-
128
- def wrapped_text(text,options)
129
- options[:align] ||= :left
130
-
131
- font_size(options[:size]) do
132
- text = naive_wrap(text, bounds.right, font_size,
133
- :kerning => options[:kerning], :mode => options[:wrap])
134
-
135
- lines = text.lines.to_a
136
- last_gap_before = options.fetch(:final_gap, true) ? lines.length : lines.length-1
137
-
138
- lines.each_with_index do |e,i|
139
- move_text_position(font.ascender)
140
-
141
- line_width = font.width_of(e, :kerning => options[:kerning])
142
- case(options[:align])
143
- when :left
144
- x = @bounding_box.absolute_left
145
- when :center
146
- x = @bounding_box.absolute_left +
147
- (@bounding_box.width - line_width) / 2.0
148
- when :right
149
- x = @bounding_box.absolute_right - line_width
150
- end
151
-
152
- add_text_content(e,x,y,options)
153
-
154
- if i < last_gap_before
155
- move_text_position(font.line_gap - font.descender)
156
- move_text_position(options[:spacing]) if options[:spacing]
157
- end
158
- end
159
- end
160
- end
161
-
162
- def add_text_content(text, x, y, options)
163
- chunks = font.encode_text(text,options)
164
-
165
- add_content "\nBT"
166
- if options[:rotate]
167
- rad = options[:rotate].to_i * Math::PI / 180
168
- arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
169
- add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
170
- else
171
- add_content "#{x} #{y} Td"
172
- end
173
-
174
- chunks.each do |(subset, string)|
175
- font.add_to_current_page(subset)
176
- add_content "/#{font.identifier_for(subset)} #{font_size} Tf"
177
-
178
- operation = options[:kerning] && string.is_a?(Array) ? "TJ" : "Tj"
179
- add_content Prawn::PdfObject(string, true) << " " << operation
180
- end
181
- add_content "ET\n"
182
- end
183
- end
184
- end
185
- end
@@ -1,76 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # text/box.rb : Implements simple text boxes
4
- #
5
- # Copyright September 2008, Gregory Brown. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
-
9
- module Prawn
10
- class Document
11
-
12
- # Defines an invisible rectangle which you can flow text in. When the
13
- # text overflows the box, you can either display :ellipses, :truncate
14
- # the text, or allow it to :overflow the bottom boundary.
15
- #
16
- # text_box "Oh hai text box. " * 200,
17
- # :width => 300, :height => font.height * 5,
18
- # :overflow => :ellipses,
19
- # :at => [100,bounds.top]
20
- #
21
- def text_box(text,options)
22
- Text::Box.new(text, options.merge(:for => self)).render
23
- end
24
-
25
- module Text
26
- class Box #:nodoc:
27
- def initialize(text,options={})
28
- @document = options[:for]
29
- @text = text
30
- @at = options[:at] || [0, @document.y - @document.bounds.absolute_bottom]
31
- @width = options[:width] || @document.bounds.width
32
- @height = options[:height]
33
- @overflow = options[:overflow] || :truncate
34
- end
35
-
36
- def render
37
- x,y = @at
38
-
39
- unless @overflow == :expand
40
- original_y = @document.y
41
- fit_text_to_box
42
- end
43
-
44
- @document.bounding_box([x,@document.bounds.top],
45
- :width => @width, :height => @document.bounds.height) do
46
- @document.y = @document.bounds.absolute_bottom + y
47
- @document.text @text
48
- end
49
-
50
- unless @overflow == :expand
51
- @document.y = y + @document.bounds.absolute_bottom - @height
52
- end
53
- end
54
-
55
- private
56
-
57
- def fit_text_to_box
58
- text = @document.naive_wrap(@text, @width, @document.font_size)
59
-
60
- max_lines = (@height / @document.font.height).floor
61
-
62
- lines = text.lines.to_a
63
-
64
- if lines.length > max_lines
65
- @text = lines[0...max_lines].join
66
- case(@overflow)
67
- when :ellipses
68
- @text[-3..-1] = "..."
69
- end
70
- end
71
- end
72
-
73
- end
74
- end
75
- end
76
- end
@@ -1,59 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # wrapping.rb : Implementation of naive text wrap
4
- #
5
- # Copyright May 2008, Michael Daines. All Rights Reserved.
6
- #
7
- # This is free software. Please see the LICENSE and COPYING files for details.
8
- module Prawn
9
- class Document
10
- module Text
11
- module Wrapping #:nodoc:
12
- ruby_18 { $KCODE="U" }
13
-
14
- # Gets height of text in PDF points at current font size.
15
- # Text +:line_width+ must be specified in PDF points.
16
- #
17
- # If using an AFM, string *must* be encoded as WinAnsi
18
- # (Use normalize_encoding to convert)
19
- #
20
- def height_of(string, line_width, size=font_size)
21
- string = naive_wrap(string, line_width, size)
22
- string.lines.to_a.length * font.height_at(size)
23
- end
24
-
25
- # TODO: Replace with TeX optimal algorithm
26
- def naive_wrap(string, line_width, font_size, options = {})
27
- scan_pattern = options[:mode] == :character ? /./ : /\S+|\s+/
28
-
29
- output = ""
30
- string.lines.each do |line|
31
- accumulated_width = 0
32
- segments = line.scan(scan_pattern)
33
-
34
- segments.each do |segment|
35
- segment_width = font.width_of(segment, :size => font_size, :kerning => options[:kerning])
36
-
37
- if (accumulated_width + segment_width).round > line_width.round
38
- output = "#{output.sub(/[ \t]*\n?(\n*)\z/, "\n\\1")}"
39
-
40
- if segment =~ /\s/
41
- accumulated_width = 0
42
- else
43
- output << segment
44
- accumulated_width = segment_width
45
- end
46
- else
47
- output << segment
48
- accumulated_width += segment_width
49
- end
50
- end
51
- end
52
-
53
- output
54
- end
55
-
56
- end
57
- end
58
- end
59
- end