piglop-prawn 0.10.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (276) hide show
  1. data/COPYING +340 -0
  2. data/HACKING +49 -0
  3. data/LICENSE +56 -0
  4. data/README +140 -0
  5. data/Rakefile +52 -0
  6. data/data/encodings/win_ansi.txt +29 -0
  7. data/data/fonts/Action Man.dfont +0 -0
  8. data/data/fonts/Activa.ttf +0 -0
  9. data/data/fonts/Chalkboard.ttf +0 -0
  10. data/data/fonts/Courier-Bold.afm +342 -0
  11. data/data/fonts/Courier-BoldOblique.afm +342 -0
  12. data/data/fonts/Courier-Oblique.afm +342 -0
  13. data/data/fonts/Courier.afm +342 -0
  14. data/data/fonts/DejaVuSans.ttf +0 -0
  15. data/data/fonts/Dustismo_Roman.ttf +0 -0
  16. data/data/fonts/Helvetica-Bold.afm +2827 -0
  17. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  18. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  19. data/data/fonts/Helvetica.afm +3051 -0
  20. data/data/fonts/MustRead.html +19 -0
  21. data/data/fonts/Symbol.afm +213 -0
  22. data/data/fonts/Times-Bold.afm +2588 -0
  23. data/data/fonts/Times-BoldItalic.afm +2384 -0
  24. data/data/fonts/Times-Italic.afm +2667 -0
  25. data/data/fonts/Times-Roman.afm +2419 -0
  26. data/data/fonts/ZapfDingbats.afm +225 -0
  27. data/data/fonts/comicsans.ttf +0 -0
  28. data/data/fonts/gkai00mp.ttf +0 -0
  29. data/data/images/16bit.alpha +0 -0
  30. data/data/images/16bit.dat +0 -0
  31. data/data/images/16bit.png +0 -0
  32. data/data/images/arrow.png +0 -0
  33. data/data/images/arrow2.png +0 -0
  34. data/data/images/barcode_issue.png +0 -0
  35. data/data/images/dice.alpha +0 -0
  36. data/data/images/dice.dat +0 -0
  37. data/data/images/dice.png +0 -0
  38. data/data/images/dice_interlaced.png +0 -0
  39. data/data/images/fractal.jpg +0 -0
  40. data/data/images/letterhead.jpg +0 -0
  41. data/data/images/page_white_text.alpha +0 -0
  42. data/data/images/page_white_text.dat +0 -0
  43. data/data/images/page_white_text.png +0 -0
  44. data/data/images/pigs.jpg +0 -0
  45. data/data/images/rails.dat +0 -0
  46. data/data/images/rails.png +0 -0
  47. data/data/images/ruport.png +0 -0
  48. data/data/images/ruport_data.dat +0 -0
  49. data/data/images/ruport_transparent.png +0 -0
  50. data/data/images/ruport_type0.png +0 -0
  51. data/data/images/stef.jpg +0 -0
  52. data/data/images/tru256.bmp +0 -0
  53. data/data/images/web-links.dat +1 -0
  54. data/data/images/web-links.png +0 -0
  55. data/data/pdfs/complex_template.pdf +0 -0
  56. data/data/pdfs/contains_ttf_font.pdf +0 -0
  57. data/data/pdfs/encrypted.pdf +0 -0
  58. data/data/pdfs/hexagon.pdf +61 -0
  59. data/data/pdfs/indirect_reference.pdf +86 -0
  60. data/data/pdfs/nested_pages.pdf +118 -0
  61. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  62. data/data/pdfs/two_hexagons.pdf +90 -0
  63. data/data/pdfs/version_1_6.pdf +61 -0
  64. data/data/shift_jis_text.txt +1 -0
  65. data/examples/bounding_box/bounding_boxes.rb +43 -0
  66. data/examples/bounding_box/indentation.rb +34 -0
  67. data/examples/bounding_box/russian_boxes.rb +36 -0
  68. data/examples/bounding_box/stretched_nesting.rb +67 -0
  69. data/examples/example_helper.rb +4 -0
  70. data/examples/general/background.rb +23 -0
  71. data/examples/general/canvas.rb +15 -0
  72. data/examples/general/context_sensitive_headers.rb +37 -0
  73. data/examples/general/float.rb +11 -0
  74. data/examples/general/margin.rb +36 -0
  75. data/examples/general/measurement_units.rb +51 -0
  76. data/examples/general/metadata-info.rb +16 -0
  77. data/examples/general/multi_page_layout.rb +18 -0
  78. data/examples/general/outlines.rb +50 -0
  79. data/examples/general/page_geometry.rb +31 -0
  80. data/examples/general/page_numbering.rb +15 -0
  81. data/examples/general/repeaters.rb +47 -0
  82. data/examples/general/stamp.rb +41 -0
  83. data/examples/general/templates.rb +13 -0
  84. data/examples/graphics/basic_images.rb +23 -0
  85. data/examples/graphics/cmyk.rb +12 -0
  86. data/examples/graphics/curves.rb +11 -0
  87. data/examples/graphics/hexagon.rb +13 -0
  88. data/examples/graphics/image_fit.rb +15 -0
  89. data/examples/graphics/image_flow.rb +37 -0
  90. data/examples/graphics/image_position.rb +17 -0
  91. data/examples/graphics/line.rb +32 -0
  92. data/examples/graphics/png_types.rb +22 -0
  93. data/examples/graphics/polygons.rb +16 -0
  94. data/examples/graphics/remote_images.rb +12 -0
  95. data/examples/graphics/rounded_polygons.rb +19 -0
  96. data/examples/graphics/rounded_rectangle.rb +20 -0
  97. data/examples/graphics/ruport_style_helpers.rb +19 -0
  98. data/examples/graphics/stroke_bounds.rb +20 -0
  99. data/examples/graphics/stroke_cap_and_join.rb +45 -0
  100. data/examples/graphics/stroke_dash.rb +42 -0
  101. data/examples/graphics/transformations.rb +52 -0
  102. data/examples/graphics/transparency.rb +26 -0
  103. data/examples/m17n/chinese_text_wrapping.rb +17 -0
  104. data/examples/m17n/euro.rb +15 -0
  105. data/examples/m17n/sjis.rb +28 -0
  106. data/examples/m17n/utf8.rb +13 -0
  107. data/examples/m17n/win_ansi_charset.rb +54 -0
  108. data/examples/security/hello_foo.rb +8 -0
  109. data/examples/table/bill.rb +53 -0
  110. data/examples/table/cell.rb +12 -0
  111. data/examples/table/checkerboard.rb +22 -0
  112. data/examples/table/header.rb +14 -0
  113. data/examples/table/inline_format_table.rb +12 -0
  114. data/examples/table/multi_page_table.rb +9 -0
  115. data/examples/table/simple_table.rb +24 -0
  116. data/examples/table/subtable.rb +12 -0
  117. data/examples/table/widths.rb +20 -0
  118. data/examples/text/alignment.rb +18 -0
  119. data/examples/text/dfont.rb +48 -0
  120. data/examples/text/family_based_styling.rb +24 -0
  121. data/examples/text/font_calculations.rb +91 -0
  122. data/examples/text/font_size.rb +33 -0
  123. data/examples/text/hyphenation.rb +45 -0
  124. data/examples/text/indent_paragraphs.rb +18 -0
  125. data/examples/text/inline_format.rb +72 -0
  126. data/examples/text/kerning.rb +30 -0
  127. data/examples/text/rotated.rb +98 -0
  128. data/examples/text/shaped_text_box.rb +31 -0
  129. data/examples/text/simple_text.rb +17 -0
  130. data/examples/text/simple_text_ttf.rb +17 -0
  131. data/examples/text/text_box.rb +88 -0
  132. data/examples/text/text_box_returning_excess.rb +51 -0
  133. data/examples/text/text_flow.rb +67 -0
  134. data/lib/prawn/compatibility.rb +51 -0
  135. data/lib/prawn/core/annotations.rb +61 -0
  136. data/lib/prawn/core/byte_string.rb +9 -0
  137. data/lib/prawn/core/destinations.rb +90 -0
  138. data/lib/prawn/core/document_state.rb +84 -0
  139. data/lib/prawn/core/literal_string.rb +16 -0
  140. data/lib/prawn/core/name_tree.rb +165 -0
  141. data/lib/prawn/core/object_store.rb +219 -0
  142. data/lib/prawn/core/page.rb +179 -0
  143. data/lib/prawn/core/pdf_object.rb +83 -0
  144. data/lib/prawn/core/reference.rb +112 -0
  145. data/lib/prawn/core/text/formatted/arranger.rb +260 -0
  146. data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
  147. data/lib/prawn/core/text/formatted/wrap.rb +113 -0
  148. data/lib/prawn/core/text/line_wrap.rb +198 -0
  149. data/lib/prawn/core/text/wrap.rb +80 -0
  150. data/lib/prawn/core/text.rb +115 -0
  151. data/lib/prawn/core.rb +89 -0
  152. data/lib/prawn/document/bounding_box.rb +425 -0
  153. data/lib/prawn/document/graphics_state.rb +48 -0
  154. data/lib/prawn/document/internals.rb +176 -0
  155. data/lib/prawn/document/page_geometry.rb +136 -0
  156. data/lib/prawn/document/snapshot.rb +87 -0
  157. data/lib/prawn/document.rb +573 -0
  158. data/lib/prawn/encoding.rb +121 -0
  159. data/lib/prawn/errors.rb +94 -0
  160. data/lib/prawn/font/afm.rb +225 -0
  161. data/lib/prawn/font/dfont.rb +42 -0
  162. data/lib/prawn/font/ttf.rb +350 -0
  163. data/lib/prawn/font.rb +342 -0
  164. data/lib/prawn/graphics/cap_style.rb +38 -0
  165. data/lib/prawn/graphics/color.rb +205 -0
  166. data/lib/prawn/graphics/dash.rb +71 -0
  167. data/lib/prawn/graphics/join_style.rb +38 -0
  168. data/lib/prawn/graphics/transformation.rb +156 -0
  169. data/lib/prawn/graphics/transparency.rb +99 -0
  170. data/lib/prawn/graphics.rb +321 -0
  171. data/lib/prawn/images/jpg.rb +46 -0
  172. data/lib/prawn/images/png.rb +226 -0
  173. data/lib/prawn/images.rb +348 -0
  174. data/lib/prawn/measurement_extensions.rb +46 -0
  175. data/lib/prawn/measurements.rb +71 -0
  176. data/lib/prawn/outline.rb +278 -0
  177. data/lib/prawn/repeater.rb +129 -0
  178. data/lib/prawn/security/arcfour.rb +51 -0
  179. data/lib/prawn/security.rb +262 -0
  180. data/lib/prawn/stamp.rb +126 -0
  181. data/lib/prawn/table/accessors.rb +180 -0
  182. data/lib/prawn/table/cell/in_table.rb +27 -0
  183. data/lib/prawn/table/cell/subtable.rb +65 -0
  184. data/lib/prawn/table/cell/text.rb +128 -0
  185. data/lib/prawn/table/cell.rb +330 -0
  186. data/lib/prawn/table.rb +421 -0
  187. data/lib/prawn/text/box.rb +380 -0
  188. data/lib/prawn/text/formatted/box.rb +215 -0
  189. data/lib/prawn/text/formatted/fragment.rb +127 -0
  190. data/lib/prawn/text/formatted/parser.rb +202 -0
  191. data/lib/prawn/text/formatted.rb +4 -0
  192. data/lib/prawn/text.rb +442 -0
  193. data/lib/prawn.rb +25 -0
  194. data/spec/annotations_spec.rb +90 -0
  195. data/spec/bounding_box_spec.rb +190 -0
  196. data/spec/cell_spec.rb +337 -0
  197. data/spec/destinations_spec.rb +15 -0
  198. data/spec/document_spec.rb +461 -0
  199. data/spec/font_spec.rb +314 -0
  200. data/spec/formatted_text_arranger_spec.rb +400 -0
  201. data/spec/formatted_text_box_spec.rb +659 -0
  202. data/spec/formatted_text_fragment_spec.rb +211 -0
  203. data/spec/graphics_spec.rb +446 -0
  204. data/spec/images_spec.rb +96 -0
  205. data/spec/inline_formatted_text_parser_spec.rb +446 -0
  206. data/spec/jpg_spec.rb +25 -0
  207. data/spec/line_wrap_spec.rb +341 -0
  208. data/spec/measurement_units_spec.rb +23 -0
  209. data/spec/name_tree_spec.rb +112 -0
  210. data/spec/object_store_spec.rb +160 -0
  211. data/spec/outline_spec.rb +229 -0
  212. data/spec/pdf_object_spec.rb +127 -0
  213. data/spec/png_spec.rb +237 -0
  214. data/spec/reference_spec.rb +82 -0
  215. data/spec/repeater_spec.rb +96 -0
  216. data/spec/security_spec.rb +120 -0
  217. data/spec/snapshot_spec.rb +138 -0
  218. data/spec/spec_helper.rb +26 -0
  219. data/spec/stamp_spec.rb +108 -0
  220. data/spec/stroke_styles_spec.rb +163 -0
  221. data/spec/table_spec.rb +583 -0
  222. data/spec/template_spec.rb +158 -0
  223. data/spec/text_at_spec.rb +119 -0
  224. data/spec/text_box_spec.rb +675 -0
  225. data/spec/text_spacing_spec.rb +57 -0
  226. data/spec/text_spec.rb +328 -0
  227. data/spec/text_with_inline_formatting_spec.rb +193 -0
  228. data/spec/transparency_spec.rb +75 -0
  229. data/vendor/pdf-inspector/README +18 -0
  230. data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
  231. data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
  232. data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
  233. data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
  234. data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
  235. data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
  236. data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
  237. data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
  238. data/vendor/ttfunk/example.rb +45 -0
  239. data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
  240. data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
  241. data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
  242. data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
  243. data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
  244. data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
  245. data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
  246. data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
  247. data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
  248. data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
  249. data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
  250. data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
  251. data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
  252. data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
  253. data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
  254. data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
  255. data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
  256. data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
  257. data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
  258. data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
  259. data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
  260. data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
  261. data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
  262. data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
  263. data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
  264. data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
  265. data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
  266. data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
  267. data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
  268. data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
  269. data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
  270. data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
  271. data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
  272. data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
  273. data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
  274. data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
  275. data/vendor/ttfunk/lib/ttfunk.rb +102 -0
  276. metadata +364 -0
data/lib/prawn/text.rb ADDED
@@ -0,0 +1,442 @@
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 "prawn/core/text"
9
+ require "prawn/core/text/wrap"
10
+ require "prawn/text/box"
11
+ require "prawn/text/formatted"
12
+ require "zlib"
13
+
14
+ module Prawn
15
+ module Text
16
+
17
+ include Prawn::Core::Text
18
+ include Prawn::Text::Formatted
19
+
20
+ # If you want text to flow onto a new page or between columns, this is the
21
+ # method to use. If, instead, if you want to place bounded text outside of
22
+ # the flow of a document (for captions, labels, charts, etc.), use Text::Box
23
+ # or its convenience method text_box.
24
+ #
25
+ # Draws text on the page. Prawn attempts to wrap the text to fit within your
26
+ # current bounding box (or margin_box if no bounding box is being used).
27
+ # Text will flow onto the next page when it reaches the bottom of the
28
+ # bounding box. Text wrap in Prawn does not re-flow linebreaks, so if you
29
+ # want fully automated text wrapping, be sure to remove newlines before
30
+ # attempting to draw your string.
31
+ #
32
+ # == Examples
33
+ #
34
+ # pdf.text "Will be wrapped when it hits the edge of your bounding box"
35
+ # pdf.text "This will be centered", :align => :center
36
+ # pdf.text "This will be right aligned", :align => :right
37
+ # pdf.text "This <i>includes <b>inline</b></i> <font size='24'>" +
38
+ # "formatting</font>", :inline_format => true
39
+ #
40
+ # If your font contains kerning pair data that Prawn can parse, the
41
+ # text will be kerned by default. You can disable kerning by including
42
+ # a false <tt>:kerning</tt> option. If you want to disable kerning on an
43
+ # entire document, set default_kerning = false for that document
44
+ #
45
+ # === Text Positioning Details
46
+ #
47
+ # The text is positioned at font.ascender below the baseline,
48
+ # making it easy to use this method within bounding boxes and spans.
49
+ #
50
+ # == Encoding
51
+ #
52
+ # Note that strings passed to this function should be encoded as UTF-8.
53
+ # If you get unexpected characters appearing in your rendered document,
54
+ # check this.
55
+ #
56
+ # If the current font is a built-in one, although the string must be
57
+ # encoded as UTF-8, only characters that are available in WinAnsi
58
+ # are allowed.
59
+ #
60
+ # If an empty box is rendered to your PDF instead of the character you
61
+ # wanted it usually means the current font doesn't include that character.
62
+ #
63
+ # == Options (default values marked in [])
64
+ #
65
+ # <tt>:inline_format</tt>::
66
+ # <tt>boolean</tt>. If true, then the string parameter is interpreted
67
+ # as a HTML-esque string that recognizes the following tags:
68
+ # <tt>\<b></b></tt>:: bold
69
+ # <tt>\<i></i></tt>:: italic
70
+ # <tt>\<u></u></tt>:: underline
71
+ # <tt>\<strikethrough></strikethrough></tt>:: strikethrough
72
+ # <tt>\<sub></sub></tt>:: subscript
73
+ # <tt>\<sup></sup></tt>:: superscript
74
+ # <tt>\<font></font></tt>::
75
+ # with the following attributes (using double or single quotes)
76
+ # <tt>size="24"</tt>::
77
+ # attribute for setting size
78
+ # <tt>name="Helvetica"</tt>::
79
+ # attribute for setting the font. The font name must be an
80
+ # AFM font with the desired faces or must be a font that is
81
+ # already registered using Prawn::Document#font_families
82
+ # <tt>\<color></color></tt>::
83
+ # with the following attributes
84
+ # <tt>rgb="ffffff" or rgb="#ffffff"</tt>::
85
+ # <tt>c="100" m="100" y="100" k="100"</tt>::
86
+ # <tt>\<link></link></tt>::
87
+ # with the following attributes
88
+ # <tt>href="http://example.com"</tt>:: an external link
89
+ # <tt>anchor="ToC"</tt>::
90
+ # where the value of the anchor attribute is the name of a
91
+ # destination that has already been or will be registered
92
+ # using Prawn::Core::Destinations#add_dest. A clickable link
93
+ # will be created to that destination.
94
+ # Note that you must explicitly underline and color using the
95
+ # appropriate tags if you which to draw attention to the link
96
+ #
97
+ # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
98
+ # is available with the current font)
99
+ # [value of default_kerning?]
100
+ # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
101
+ # size]
102
+ # <tt>:style</tt>:: The style to use. The requested style must be part of
103
+ # the current font familly. [current style]
104
+ # <tt>:indent_paragraphs</tt>:: <tt>number</tt>. The amount to indent the
105
+ # first line of each paragraph. Omit this
106
+ # option if you do not want indenting
107
+ # <tt>:align</tt>:: <tt>:left</tt>, <tt>:center</tt>, <tt>:right</tt>, or
108
+ # <tt>:justify</tt> Alignment within the bounding box [:left]
109
+ # <tt>:valign</tt>:: <tt>:top</tt>, <tt>:center</tt>, or <tt>:bottom</tt>.
110
+ # Vertical alignment within the bounding box [:top]
111
+ # <tt>:leading</tt>:: <tt>number</tt>. Additional space between lines [0]
112
+ # <tt>:final_gap</tt>:: <tt>boolean</tt>. If true, then the space between
113
+ # each line is included below the last line;
114
+ # otherwise, document.y is placed just below the
115
+ # descender of the last line printed [true]
116
+ #
117
+ # == Exceptions
118
+ #
119
+ # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option included
120
+ #
121
+ # Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
122
+ # any text
123
+ #
124
+ def text(string, options={})
125
+ # we modify the options. don't change the user's hash
126
+ options = options.dup
127
+
128
+ if options[:inline_format]
129
+ options.delete(:inline_format)
130
+ array = Text::Formatted::Parser.to_array(string)
131
+ formatted_text(array, options)
132
+ return
133
+ end
134
+
135
+ inspect_options_for_text(options)
136
+
137
+ if @indent_paragraphs
138
+ string.split("\n").each do |paragraph|
139
+ options[:skip_encoding] = false
140
+ remaining_text = draw_indented_line(paragraph, options)
141
+ options[:skip_encoding] = true
142
+ if remaining_text == paragraph
143
+ # we were too close to the bottom of the page to print even one line
144
+ @bounding_box.move_past_bottom
145
+ remaining_text = draw_indented_line(paragraph, options)
146
+ end
147
+ remaining_text = fill_text_box(remaining_text, options)
148
+ draw_remaining_text_on_new_pages(remaining_text, options)
149
+ end
150
+ else
151
+ remaining_text = fill_text_box(string, options)
152
+ options[:skip_encoding] = true
153
+ draw_remaining_text_on_new_pages(remaining_text, options)
154
+ end
155
+ end
156
+
157
+
158
+ # Draws formatted text to the page.
159
+ # Formatted text is comprised of an array of hashes, where each hash defines
160
+ # text and format information. See Text::Formatted#formatted_text_box for
161
+ # more information on the structure of this array
162
+ #
163
+ # == Example
164
+ #
165
+ # text([{ :text => "hello" },
166
+ # { :text => "world",
167
+ # :size => 24,
168
+ # :style => [:bold, :italic] }])
169
+ #
170
+ # == Options
171
+ #
172
+ # Accepts the same options as #text
173
+ #
174
+ # == Exceptions
175
+ #
176
+ # Same as for #text
177
+ #
178
+ def formatted_text(array, options={})
179
+ # we modify the options. don't change the user's hash
180
+ options = options.dup
181
+
182
+ inspect_options_for_text(options)
183
+
184
+ if @indent_paragraphs
185
+ Text::Formatted::Parser.array_paragraphs(array).each do |paragraph|
186
+ options[:skip_encoding] = false
187
+ remaining_text = draw_indented_formatted_line(paragraph, options)
188
+ options[:skip_encoding] = true
189
+ if remaining_text == paragraph
190
+ # we were too close to the bottom of the page to print even one line
191
+ @bounding_box.move_past_bottom
192
+ remaining_text = draw_indented_formatted_line(paragraph, options)
193
+ end
194
+ remaining_text = fill_formatted_text_box(remaining_text, options)
195
+ draw_remaining_formatted_text_on_new_pages(remaining_text, options)
196
+ end
197
+ else
198
+ remaining_text = fill_formatted_text_box(array, options)
199
+ options[:skip_encoding] = true
200
+ draw_remaining_formatted_text_on_new_pages(remaining_text, options)
201
+ end
202
+ end
203
+
204
+ # Draws text on the page, beginning at the point specified by the :at option
205
+ # the string is assumed to be pre-formatted to properly fit the page.
206
+ #
207
+ # pdf.draw_text "Hello World", :at => [100,100]
208
+ # pdf.draw_text "Goodbye World", :at => [50,50], :size => 16
209
+ #
210
+ # If your font contains kerning pair data that Prawn can parse, the
211
+ # text will be kerned by default. You can disable kerning by including
212
+ # a false <tt>:kerning</tt> option. If you want to disable kerning on an
213
+ # entire document, set default_kerning = false for that document
214
+ #
215
+ # === Text Positioning Details:
216
+ #
217
+ # Prawn will position your text by the left-most edge of its baseline, and
218
+ # flow along a single line. (This means that :align will not work)
219
+ #
220
+ # == Rotation
221
+ #
222
+ # Text can be rotated before it is placed on the canvas by specifying the
223
+ # <tt>:rotate</tt> option with a given angle. Rotation occurs counter-clockwise.
224
+ #
225
+ # == Encoding
226
+ #
227
+ # Note that strings passed to this function should be encoded as UTF-8.
228
+ # If you get unexpected characters appearing in your rendered document,
229
+ # check this.
230
+ #
231
+ # If the current font is a built-in one, although the string must be
232
+ # encoded as UTF-8, only characters that are available in WinAnsi
233
+ # are allowed.
234
+ #
235
+ # If an empty box is rendered to your PDF instead of the character you
236
+ # wanted it usually means the current font doesn't include that character.
237
+ #
238
+ # == Options (default values marked in [])
239
+ #
240
+ # <tt>:at</tt>:: <tt>[x, y]</tt>(required). The position at which to start the text
241
+ # <tt>:kerning</tt>:: <tt>boolean</tt>. Whether or not to use kerning (if it
242
+ # is available with the current font)
243
+ # [value of default_kerning?]
244
+ # <tt>:size</tt>:: <tt>number</tt>. The font size to use. [current font
245
+ # size]
246
+ # <tt>:style</tt>:: The style to use. The requested style must be part of
247
+ # the current font familly. [current style]
248
+ #
249
+ # <tt>:rotate</tt>:: <tt>number</tt>. The angle to which to rotate text
250
+ #
251
+ # == Exceptions
252
+ #
253
+ # Raises <tt>ArgumentError</tt> if <tt>:at</tt> option omitted
254
+ #
255
+ # Raises <tt>ArgumentError</tt> if <tt>:align</tt> option included
256
+ #
257
+ def draw_text(text, options)
258
+ # we modify the options. don't change the user's hash
259
+ options = options.dup
260
+ inspect_options_for_draw_text(options)
261
+ # dup because normalize_encoding changes the string
262
+ text = text.to_s.dup
263
+ save_font do
264
+ process_text_options(options)
265
+ font.normalize_encoding!(text) unless @skip_encoding
266
+ font_size(options[:size]) { draw_text!(text, options) }
267
+ end
268
+ end
269
+
270
+ # Gets height of text in PDF points.
271
+ # Same options as #text, except as noted.
272
+ # Not compatible with :indent_paragraphs option
273
+ #
274
+ # ==Example
275
+ #
276
+ # height_of("hello\nworld")
277
+ #
278
+ # == Exceptions
279
+ #
280
+ # Raises <tt>NotImplementedError</tt> if <tt>:indent_paragraphs</tt>
281
+ # option included
282
+ #
283
+ # Raises <tt>Prawn::Errrors::CannotFit</tt> if not wide enough to print
284
+ # any text
285
+ #
286
+ def height_of(string, options={})
287
+ if options[:indent_paragraphs]
288
+ raise NotImplementedError, ":indent_paragraphs option not available" +
289
+ "with height_of"
290
+ end
291
+ process_final_gap_option(options)
292
+ box = Text::Box.new(string,
293
+ options.merge(:height => 100000000,
294
+ :document => self))
295
+ printed = box.render(:dry_run => true)
296
+
297
+ height = box.height - (box.line_height - box.ascender)
298
+ height += box.line_height + box.leading - box.ascender if @final_gap
299
+ height
300
+ end
301
+
302
+ # Gets height of formatted text in PDF points.
303
+ # See documentation for #height_of.
304
+ #
305
+ # ==Example
306
+ #
307
+ # height_of_formatted([{ :text => "hello" },
308
+ # { :text => "world",
309
+ # :size => 24,
310
+ # :style => [:bold, :italic] }])
311
+ #
312
+ def height_of_formatted(array, options={})
313
+ if options[:indent_paragraphs]
314
+ raise NotImplementedError, ":indent_paragraphs option not available" +
315
+ "with height_of"
316
+ end
317
+ process_final_gap_option(options)
318
+ box = Text::Formatted::Box.new(array,
319
+ options.merge(:height => 100000000,
320
+ :document => self))
321
+ printed = box.render(:dry_run => true)
322
+
323
+ height = box.height - (box.line_height - box.ascender)
324
+ height += box.line_height + box.leading - box.ascender if @final_gap
325
+ height
326
+ end
327
+
328
+ private
329
+
330
+ def draw_remaining_text_on_new_pages(remaining_text, options)
331
+ while remaining_text.length > 0
332
+ @bounding_box.move_past_bottom
333
+ previous_remaining_text = remaining_text
334
+ remaining_text = fill_text_box(remaining_text, options)
335
+ break if remaining_text == previous_remaining_text
336
+ end
337
+ end
338
+
339
+ def draw_indented_line(string, options)
340
+ indent(@indent_paragraphs) do
341
+ fill_text_box(string, options.dup.merge(:single_line => true))
342
+ end
343
+ end
344
+
345
+ def fill_text_box(text, options)
346
+ merge_text_box_positioning_options(options)
347
+
348
+ box = Text::Box.new(text, options)
349
+ remaining_text = box.render
350
+
351
+ self.y -= box.height - (box.line_height - box.ascender)
352
+ if @final_gap
353
+ self.y -= box.line_height + box.leading - box.ascender
354
+ end
355
+ remaining_text
356
+ end
357
+
358
+
359
+
360
+
361
+ def draw_remaining_formatted_text_on_new_pages(remaining_text, options)
362
+ while remaining_text.length > 0
363
+ @bounding_box.move_past_bottom
364
+ previous_remaining_text = remaining_text
365
+ remaining_text = fill_formatted_text_box(remaining_text, options)
366
+ break if remaining_text == previous_remaining_text
367
+ end
368
+ end
369
+
370
+ def draw_indented_formatted_line(string, options)
371
+ indent(@indent_paragraphs) do
372
+ fill_formatted_text_box(string, options.dup.merge(:single_line => true))
373
+ end
374
+ end
375
+
376
+ def fill_formatted_text_box(text, options)
377
+ merge_text_box_positioning_options(options)
378
+ box = Text::Formatted::Box.new(text, options)
379
+ remaining_text = box.render
380
+
381
+ self.y -= box.height - (box.line_height - box.ascender)
382
+ if @final_gap
383
+ self.y -= box.line_height + box.leading - box.ascender
384
+ end
385
+ remaining_text
386
+ end
387
+
388
+
389
+
390
+ def merge_text_box_positioning_options(options)
391
+ bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
392
+ @bounding_box.absolute_bottom
393
+
394
+ options[:height] = y - bottom
395
+ options[:width] = bounds.width
396
+ options[:at] = [@bounding_box.left_side - @bounding_box.absolute_left,
397
+ y - @bounding_box.absolute_bottom]
398
+ end
399
+
400
+ def inspect_options_for_draw_text(options)
401
+ if options[:at].nil?
402
+ raise ArgumentError, "The :at option is required for draw_text"
403
+ elsif options[:align]
404
+ raise ArgumentError, "The :align option does not work with draw_text"
405
+ end
406
+ if options[:kerning].nil? then
407
+ options[:kerning] = default_kerning?
408
+ end
409
+ valid_options = Prawn::Core::Text::VALID_OPTIONS + [:at, :rotate]
410
+ Prawn.verify_options(valid_options, options)
411
+ end
412
+
413
+ def inspect_options_for_text(options)
414
+ if options[:at]
415
+ raise ArgumentError, ":at is no longer a valid option with text." +
416
+ "use draw_text or text_box instead"
417
+ end
418
+ process_final_gap_option(options)
419
+ process_indent_paragraphs_option(options)
420
+ options[:document] = self
421
+ end
422
+
423
+ def process_final_gap_option(options)
424
+ @final_gap = options[:final_gap].nil? || options[:final_gap]
425
+ options.delete(:final_gap)
426
+ end
427
+
428
+ def process_indent_paragraphs_option(options)
429
+ @indent_paragraphs = options[:indent_paragraphs]
430
+ options.delete(:indent_paragraphs)
431
+ end
432
+
433
+ def move_text_position(dy)
434
+ bottom = @bounding_box.stretchy? ? @margin_box.absolute_bottom :
435
+ @bounding_box.absolute_bottom
436
+
437
+ @bounding_box.move_past_bottom if (y - dy) < bottom
438
+
439
+ self.y -= dy
440
+ end
441
+ end
442
+ end
data/lib/prawn.rb ADDED
@@ -0,0 +1,25 @@
1
+ # Welcome to Prawn, the best PDF Generation library ever.
2
+ # This documentation covers user level functionality.
3
+ #
4
+ # Those looking to contribute code or write extensions should look
5
+ # into the lib/prawn/core/* source tree.
6
+ #
7
+ module Prawn #:nodoc:
8
+ end
9
+
10
+ require "prawn/core"
11
+ require "prawn/text"
12
+ require "prawn/graphics"
13
+ require "prawn/images"
14
+ require "prawn/images/jpg"
15
+ require "prawn/images/png"
16
+ require "prawn/stamp"
17
+ require "prawn/security"
18
+ require "prawn/document"
19
+ require "prawn/font"
20
+ require "prawn/encoding"
21
+ require "prawn/measurements"
22
+ require "prawn/repeater"
23
+ require "prawn/outline"
24
+ require "prawn/table"
25
+
@@ -0,0 +1,90 @@
1
+ # encoding: utf-8
2
+
3
+ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
4
+
5
+ class PageAnnotations
6
+ attr_reader :pages
7
+
8
+ def self.parse(document)
9
+ receiver = new
10
+ PDF::Reader.string(document.render, receiver)
11
+ return receiver
12
+ end
13
+
14
+ def initialize
15
+ @pages = []
16
+ end
17
+
18
+ def begin_page(params)
19
+ @pages << params
20
+ end
21
+ end
22
+
23
+ describe "When creating annotations" do
24
+
25
+ before(:each) { create_pdf }
26
+
27
+ it "should append annotation to current page" do
28
+ @pdf.start_new_page
29
+ @pdf.annotate(:Rect => [0,0,10,10], :Subtype => :Text, :Contents => "Hello world!")
30
+ obj = PageAnnotations.parse(@pdf)
31
+ obj.pages[0][:Annots].nil?.should == true
32
+ obj.pages[1][:Annots].length.should == 1
33
+ end
34
+
35
+ it "should force :Type to be :Annot" do
36
+ opts = @pdf.annotate(:Rect => [0,0,10,10], :Subtype => :Text, :Contents => "Hello world!")
37
+ opts[:Type].should == :Annot
38
+ opts = @pdf.annotate(:Type => :Bogus, :Rect => [0,0,10,10], :Subtype => :Text, :Contents => "Hello world!")
39
+ opts[:Type].should == :Annot
40
+ end
41
+
42
+ end
43
+
44
+ describe "When creating text annotations" do
45
+
46
+ before(:each) do
47
+ @rect = [0,0,10,10]
48
+ @content = "Hello, world!"
49
+ create_pdf
50
+ end
51
+
52
+ it "should build appropriate annotation" do
53
+ opts = @pdf.text_annotation(@rect, @content)
54
+ opts[:Type].should == :Annot
55
+ opts[:Subtype].should == :Text
56
+ opts[:Rect].should == @rect
57
+ opts[:Contents].should == @content
58
+ end
59
+
60
+ it "should merge extra options" do
61
+ opts = @pdf.text_annotation(@rect, @content, :Open => true, :Subtype => :Bogus)
62
+ opts[:Subtype].should == :Text
63
+ opts[:Open].should == true
64
+ end
65
+
66
+ end
67
+
68
+ describe "When creating link annotations" do
69
+
70
+ before(:each) do
71
+ @rect = [0,0,10,10]
72
+ @dest = "home"
73
+ create_pdf
74
+ end
75
+
76
+ it "should build appropriate annotation" do
77
+ opts = @pdf.link_annotation(@rect, :Dest => @dest)
78
+ opts[:Type].should == :Annot
79
+ opts[:Subtype].should == :Link
80
+ opts[:Rect].should == @rect
81
+ opts[:Dest].should == @dest
82
+ end
83
+
84
+ it "should merge extra options" do
85
+ opts = @pdf.link_annotation(@rect, :Dest => @dest, :Subtype => :Bogus)
86
+ opts[:Subtype].should == :Link
87
+ opts[:Dest].should == @dest
88
+ end
89
+
90
+ end