nurettin-prawn 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. data/COPYING +2 -0
  2. data/GPLv2 +340 -0
  3. data/GPLv3 +674 -0
  4. data/Gemfile +18 -0
  5. data/LICENSE +56 -0
  6. data/README.md +98 -0
  7. data/Rakefile +45 -0
  8. data/data/encodings/win_ansi.txt +29 -0
  9. data/data/fonts/Action Man.dfont +0 -0
  10. data/data/fonts/Activa.ttf +0 -0
  11. data/data/fonts/Chalkboard.ttf +0 -0
  12. data/data/fonts/Courier-Bold.afm +342 -0
  13. data/data/fonts/Courier-BoldOblique.afm +342 -0
  14. data/data/fonts/Courier-Oblique.afm +342 -0
  15. data/data/fonts/Courier.afm +342 -0
  16. data/data/fonts/DejaVuSans.ttf +0 -0
  17. data/data/fonts/Dustismo_Roman.ttf +0 -0
  18. data/data/fonts/Helvetica-Bold.afm +2827 -0
  19. data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
  20. data/data/fonts/Helvetica-Oblique.afm +3051 -0
  21. data/data/fonts/Helvetica.afm +3051 -0
  22. data/data/fonts/MustRead.html +19 -0
  23. data/data/fonts/Symbol.afm +213 -0
  24. data/data/fonts/Times-Bold.afm +2588 -0
  25. data/data/fonts/Times-BoldItalic.afm +2384 -0
  26. data/data/fonts/Times-Italic.afm +2667 -0
  27. data/data/fonts/Times-Roman.afm +2419 -0
  28. data/data/fonts/ZapfDingbats.afm +225 -0
  29. data/data/fonts/comicsans.ttf +0 -0
  30. data/data/fonts/gkai00mp.ttf +0 -0
  31. data/data/images/16bit.alpha +0 -0
  32. data/data/images/16bit.dat +0 -0
  33. data/data/images/16bit.png +0 -0
  34. data/data/images/arrow.png +0 -0
  35. data/data/images/arrow2.png +0 -0
  36. data/data/images/barcode_issue.png +0 -0
  37. data/data/images/dice.alpha +0 -0
  38. data/data/images/dice.dat +0 -0
  39. data/data/images/dice.png +0 -0
  40. data/data/images/dice_interlaced.png +0 -0
  41. data/data/images/fractal.jpg +0 -0
  42. data/data/images/letterhead.jpg +0 -0
  43. data/data/images/page_white_text.alpha +0 -0
  44. data/data/images/page_white_text.dat +0 -0
  45. data/data/images/page_white_text.png +0 -0
  46. data/data/images/pigs.jpg +0 -0
  47. data/data/images/prawn.png +0 -0
  48. data/data/images/rails.dat +0 -0
  49. data/data/images/rails.png +0 -0
  50. data/data/images/ruport.png +0 -0
  51. data/data/images/ruport_data.dat +0 -0
  52. data/data/images/ruport_transparent.png +0 -0
  53. data/data/images/ruport_type0.png +0 -0
  54. data/data/images/stef.jpg +0 -0
  55. data/data/images/tru256.bmp +0 -0
  56. data/data/images/web-links.dat +1 -0
  57. data/data/images/web-links.png +0 -0
  58. data/data/pdfs/complex_template.pdf +0 -0
  59. data/data/pdfs/contains_ttf_font.pdf +0 -0
  60. data/data/pdfs/encrypted.pdf +0 -0
  61. data/data/pdfs/form.pdf +820 -0
  62. data/data/pdfs/hexagon.pdf +61 -0
  63. data/data/pdfs/indirect_reference.pdf +86 -0
  64. data/data/pdfs/multipage_template.pdf +127 -0
  65. data/data/pdfs/nested_pages.pdf +118 -0
  66. data/data/pdfs/page_without_mediabox.pdf +193 -0
  67. data/data/pdfs/resources_as_indirect_object.pdf +83 -0
  68. data/data/pdfs/two_hexagons.pdf +90 -0
  69. data/data/pdfs/version_1_6.pdf +61 -0
  70. data/data/shift_jis_text.txt +1 -0
  71. data/lib/prawn.rb +29 -0
  72. data/lib/prawn/compatibility.rb +87 -0
  73. data/lib/prawn/core.rb +87 -0
  74. data/lib/prawn/core/annotations.rb +61 -0
  75. data/lib/prawn/core/byte_string.rb +9 -0
  76. data/lib/prawn/core/destinations.rb +90 -0
  77. data/lib/prawn/core/document_state.rb +79 -0
  78. data/lib/prawn/core/literal_string.rb +16 -0
  79. data/lib/prawn/core/name_tree.rb +177 -0
  80. data/lib/prawn/core/object_store.rb +320 -0
  81. data/lib/prawn/core/page.rb +212 -0
  82. data/lib/prawn/core/pdf_object.rb +125 -0
  83. data/lib/prawn/core/reference.rb +119 -0
  84. data/lib/prawn/core/text.rb +268 -0
  85. data/lib/prawn/core/text/formatted/arranger.rb +294 -0
  86. data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
  87. data/lib/prawn/core/text/formatted/wrap.rb +153 -0
  88. data/lib/prawn/document.rb +707 -0
  89. data/lib/prawn/document/bounding_box.rb +510 -0
  90. data/lib/prawn/document/column_box.rb +132 -0
  91. data/lib/prawn/document/graphics_state.rb +136 -0
  92. data/lib/prawn/document/internals.rb +173 -0
  93. data/lib/prawn/document/page_geometry.rb +136 -0
  94. data/lib/prawn/document/snapshot.rb +89 -0
  95. data/lib/prawn/document/span.rb +55 -0
  96. data/lib/prawn/encoding.rb +121 -0
  97. data/lib/prawn/errors.rb +99 -0
  98. data/lib/prawn/font.rb +386 -0
  99. data/lib/prawn/font/afm.rb +203 -0
  100. data/lib/prawn/font/dfont.rb +42 -0
  101. data/lib/prawn/font/ttf.rb +343 -0
  102. data/lib/prawn/graphics.rb +523 -0
  103. data/lib/prawn/graphics/cap_style.rb +46 -0
  104. data/lib/prawn/graphics/color.rb +231 -0
  105. data/lib/prawn/graphics/dash.rb +82 -0
  106. data/lib/prawn/graphics/join_style.rb +47 -0
  107. data/lib/prawn/graphics/patterns.rb +137 -0
  108. data/lib/prawn/graphics/transformation.rb +156 -0
  109. data/lib/prawn/graphics/transparency.rb +99 -0
  110. data/lib/prawn/images.rb +196 -0
  111. data/lib/prawn/images/image.rb +65 -0
  112. data/lib/prawn/images/jpg.rb +85 -0
  113. data/lib/prawn/images/png.rb +362 -0
  114. data/lib/prawn/layout.rb +20 -0
  115. data/lib/prawn/layout/grid.rb +259 -0
  116. data/lib/prawn/measurement_extensions.rb +46 -0
  117. data/lib/prawn/measurements.rb +71 -0
  118. data/lib/prawn/outline.rb +326 -0
  119. data/lib/prawn/repeater.rb +122 -0
  120. data/lib/prawn/security.rb +269 -0
  121. data/lib/prawn/soft_mask.rb +94 -0
  122. data/lib/prawn/stamp.rb +134 -0
  123. data/lib/prawn/table.rb +609 -0
  124. data/lib/prawn/table/cell.rb +776 -0
  125. data/lib/prawn/table/cell/image.rb +70 -0
  126. data/lib/prawn/table/cell/in_table.rb +27 -0
  127. data/lib/prawn/table/cell/span_dummy.rb +88 -0
  128. data/lib/prawn/table/cell/subtable.rb +65 -0
  129. data/lib/prawn/table/cell/text.rb +152 -0
  130. data/lib/prawn/table/cells.rb +260 -0
  131. data/lib/prawn/text.rb +420 -0
  132. data/lib/prawn/text/box.rb +141 -0
  133. data/lib/prawn/text/formatted.rb +4 -0
  134. data/lib/prawn/text/formatted/box.rb +563 -0
  135. data/lib/prawn/text/formatted/fragment.rb +253 -0
  136. data/lib/prawn/text/formatted/parser.rb +217 -0
  137. data/lib/prawn/utilities.rb +44 -0
  138. data/manual/basic_concepts/adding_pages.rb +27 -0
  139. data/manual/basic_concepts/basic_concepts.rb +34 -0
  140. data/manual/basic_concepts/creation.rb +39 -0
  141. data/manual/basic_concepts/cursor.rb +33 -0
  142. data/manual/basic_concepts/measurement.rb +25 -0
  143. data/manual/basic_concepts/origin.rb +38 -0
  144. data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
  145. data/manual/bounding_box/bounding_box.rb +39 -0
  146. data/manual/bounding_box/bounds.rb +49 -0
  147. data/manual/bounding_box/canvas.rb +24 -0
  148. data/manual/bounding_box/creation.rb +23 -0
  149. data/manual/bounding_box/indentation.rb +46 -0
  150. data/manual/bounding_box/nesting.rb +45 -0
  151. data/manual/bounding_box/russian_boxes.rb +40 -0
  152. data/manual/bounding_box/stretchy.rb +31 -0
  153. data/manual/document_and_page_options/background.rb +27 -0
  154. data/manual/document_and_page_options/document_and_page_options.rb +31 -0
  155. data/manual/document_and_page_options/metadata.rb +23 -0
  156. data/manual/document_and_page_options/page_margins.rb +38 -0
  157. data/manual/document_and_page_options/page_size.rb +34 -0
  158. data/manual/example_file.rb +116 -0
  159. data/manual/example_helper.rb +430 -0
  160. data/manual/example_package.rb +53 -0
  161. data/manual/example_section.rb +46 -0
  162. data/manual/graphics/circle_and_ellipse.rb +22 -0
  163. data/manual/graphics/color.rb +24 -0
  164. data/manual/graphics/common_lines.rb +28 -0
  165. data/manual/graphics/fill_and_stroke.rb +42 -0
  166. data/manual/graphics/fill_rules.rb +37 -0
  167. data/manual/graphics/gradients.rb +37 -0
  168. data/manual/graphics/graphics.rb +58 -0
  169. data/manual/graphics/helper.rb +17 -0
  170. data/manual/graphics/line_width.rb +35 -0
  171. data/manual/graphics/lines_and_curves.rb +41 -0
  172. data/manual/graphics/polygon.rb +29 -0
  173. data/manual/graphics/rectangle.rb +21 -0
  174. data/manual/graphics/rotate.rb +28 -0
  175. data/manual/graphics/scale.rb +41 -0
  176. data/manual/graphics/soft_masks.rb +46 -0
  177. data/manual/graphics/stroke_cap.rb +31 -0
  178. data/manual/graphics/stroke_dash.rb +43 -0
  179. data/manual/graphics/stroke_join.rb +30 -0
  180. data/manual/graphics/translate.rb +29 -0
  181. data/manual/graphics/transparency.rb +35 -0
  182. data/manual/images/absolute_position.rb +23 -0
  183. data/manual/images/fit.rb +21 -0
  184. data/manual/images/horizontal.rb +25 -0
  185. data/manual/images/images.rb +40 -0
  186. data/manual/images/plain_image.rb +18 -0
  187. data/manual/images/scale.rb +22 -0
  188. data/manual/images/vertical.rb +28 -0
  189. data/manual/images/width_and_height.rb +25 -0
  190. data/manual/layout/boxes.rb +27 -0
  191. data/manual/layout/content.rb +25 -0
  192. data/manual/layout/layout.rb +28 -0
  193. data/manual/layout/simple_grid.rb +23 -0
  194. data/manual/manual/cover.rb +26 -0
  195. data/manual/manual/foreword.rb +13 -0
  196. data/manual/manual/how_to_read_this_manual.rb +41 -0
  197. data/manual/manual/manual.rb +36 -0
  198. data/manual/outline/add_subsection_to.rb +61 -0
  199. data/manual/outline/insert_section_after.rb +47 -0
  200. data/manual/outline/outline.rb +32 -0
  201. data/manual/outline/sections_and_pages.rb +67 -0
  202. data/manual/repeatable_content/page_numbering.rb +54 -0
  203. data/manual/repeatable_content/repeatable_content.rb +31 -0
  204. data/manual/repeatable_content/repeater.rb +55 -0
  205. data/manual/repeatable_content/stamp.rb +41 -0
  206. data/manual/security/encryption.rb +31 -0
  207. data/manual/security/permissions.rb +38 -0
  208. data/manual/security/security.rb +28 -0
  209. data/manual/syntax_highlight.rb +52 -0
  210. data/manual/table/basic_block.rb +53 -0
  211. data/manual/table/before_rendering_page.rb +26 -0
  212. data/manual/table/cell_border_lines.rb +24 -0
  213. data/manual/table/cell_borders_and_bg.rb +31 -0
  214. data/manual/table/cell_dimensions.rb +30 -0
  215. data/manual/table/cell_text.rb +38 -0
  216. data/manual/table/column_widths.rb +30 -0
  217. data/manual/table/content_and_subtables.rb +39 -0
  218. data/manual/table/creation.rb +27 -0
  219. data/manual/table/filtering.rb +36 -0
  220. data/manual/table/flow_and_header.rb +17 -0
  221. data/manual/table/image_cells.rb +33 -0
  222. data/manual/table/position.rb +29 -0
  223. data/manual/table/row_colors.rb +20 -0
  224. data/manual/table/span.rb +30 -0
  225. data/manual/table/style.rb +22 -0
  226. data/manual/table/table.rb +52 -0
  227. data/manual/table/width.rb +27 -0
  228. data/manual/templates/full_template.rb +23 -0
  229. data/manual/templates/page_template.rb +47 -0
  230. data/manual/templates/templates.rb +26 -0
  231. data/manual/text/alignment.rb +44 -0
  232. data/manual/text/color.rb +24 -0
  233. data/manual/text/column_box.rb +32 -0
  234. data/manual/text/fallback_fonts.rb +37 -0
  235. data/manual/text/font.rb +41 -0
  236. data/manual/text/font_size.rb +45 -0
  237. data/manual/text/font_style.rb +23 -0
  238. data/manual/text/formatted_callbacks.rb +60 -0
  239. data/manual/text/formatted_text.rb +50 -0
  240. data/manual/text/free_flowing_text.rb +51 -0
  241. data/manual/text/group.rb +29 -0
  242. data/manual/text/inline.rb +43 -0
  243. data/manual/text/kerning_and_character_spacing.rb +39 -0
  244. data/manual/text/leading.rb +25 -0
  245. data/manual/text/line_wrapping.rb +41 -0
  246. data/manual/text/paragraph_indentation.rb +26 -0
  247. data/manual/text/positioned_text.rb +38 -0
  248. data/manual/text/registering_families.rb +48 -0
  249. data/manual/text/rendering_and_color.rb +37 -0
  250. data/manual/text/right_to_left_text.rb +43 -0
  251. data/manual/text/rotation.rb +43 -0
  252. data/manual/text/single_usage.rb +37 -0
  253. data/manual/text/text.rb +75 -0
  254. data/manual/text/text_box_excess.rb +32 -0
  255. data/manual/text/text_box_extensions.rb +45 -0
  256. data/manual/text/text_box_overflow.rb +44 -0
  257. data/manual/text/utf8.rb +28 -0
  258. data/manual/text/win_ansi_charset.rb +59 -0
  259. data/prawn.gemspec +46 -0
  260. data/spec/annotations_spec.rb +90 -0
  261. data/spec/bounding_box_spec.rb +493 -0
  262. data/spec/cell_spec.rb +584 -0
  263. data/spec/column_box_spec.rb +33 -0
  264. data/spec/data/curves.pdf +66 -0
  265. data/spec/destinations_spec.rb +15 -0
  266. data/spec/document_spec.rb +736 -0
  267. data/spec/extensions/encoding_helpers.rb +6 -0
  268. data/spec/extensions/mocha.rb +44 -0
  269. data/spec/font_spec.rb +433 -0
  270. data/spec/formatted_text_arranger_spec.rb +421 -0
  271. data/spec/formatted_text_box_spec.rb +640 -0
  272. data/spec/formatted_text_fragment_spec.rb +298 -0
  273. data/spec/graphics_spec.rb +651 -0
  274. data/spec/grid_spec.rb +85 -0
  275. data/spec/images_spec.rb +140 -0
  276. data/spec/inline_formatted_text_parser_spec.rb +515 -0
  277. data/spec/jpg_spec.rb +25 -0
  278. data/spec/line_wrap_spec.rb +333 -0
  279. data/spec/measurement_units_spec.rb +23 -0
  280. data/spec/name_tree_spec.rb +112 -0
  281. data/spec/object_store_spec.rb +170 -0
  282. data/spec/outline_spec.rb +426 -0
  283. data/spec/pdf_object_spec.rb +172 -0
  284. data/spec/png_spec.rb +240 -0
  285. data/spec/reference_spec.rb +105 -0
  286. data/spec/repeater_spec.rb +158 -0
  287. data/spec/security_spec.rb +126 -0
  288. data/spec/snapshot_spec.rb +186 -0
  289. data/spec/soft_mask_spec.rb +117 -0
  290. data/spec/span_spec.rb +49 -0
  291. data/spec/spec_helper.rb +36 -0
  292. data/spec/stamp_spec.rb +159 -0
  293. data/spec/stroke_styles_spec.rb +193 -0
  294. data/spec/table_spec.rb +1209 -0
  295. data/spec/template_spec.rb +351 -0
  296. data/spec/text_at_spec.rb +129 -0
  297. data/spec/text_box_spec.rb +1029 -0
  298. data/spec/text_rendering_mode_spec.rb +45 -0
  299. data/spec/text_spacing_spec.rb +93 -0
  300. data/spec/text_spec.rb +421 -0
  301. data/spec/text_with_inline_formatting_spec.rb +35 -0
  302. data/spec/transparency_spec.rb +89 -0
  303. metadata +544 -0
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ # snapshot.rb : Implements transactional rendering for Prawn
4
+ #
5
+ # Copyright August 2009, Brad Ediger. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+
9
+ require 'delegate'
10
+
11
+ module Prawn
12
+ class Document
13
+ module Snapshot
14
+
15
+ RollbackTransaction = Class.new(StandardError)
16
+
17
+ # Call this within a +transaction+ block to roll back the transaction and
18
+ # prevent any of its data from being rendered. You must reset the
19
+ # y-position yourself if you have performed any drawing operations that
20
+ # modify it.
21
+ #
22
+ def rollback
23
+ raise RollbackTransaction
24
+ end
25
+
26
+ # Run a block of drawing operations, to be completed atomically. If
27
+ # +rollback+ is called or a RollbackTransaction exception is raised
28
+ # inside the block, all actions taken inside the block will be rolled
29
+ # back (with the exception of y-position, which you must restore
30
+ # yourself).
31
+ #
32
+ # Returns true on success, or false if the transaction was rolled back.
33
+ #
34
+ def transaction
35
+ snap = take_snapshot
36
+ yield
37
+ true
38
+ rescue RollbackTransaction
39
+ restore_snapshot(snap)
40
+ false
41
+ end
42
+
43
+ private
44
+
45
+ # Takes a current snapshot of the document's state, sufficient to
46
+ # reconstruct it after it was amended.
47
+ #
48
+ def take_snapshot
49
+ # current_page holds a ref to the Pages dictionary which grows
50
+ # monotonically as data is added to the document, so we share that
51
+ # between the old and new copies.
52
+ {:page_content => state.page.content.deep_copy,
53
+ :current_page => state.page.dictionary.deep_copy(share=[:Parent]),
54
+ :bounds => bounds.deep_copy,
55
+ :page_number => page_number,
56
+ :page_kids => state.store.pages.data[:Kids].compact.map{|kid| kid.identifier},
57
+ :dests => names? && names.data[:Dests].deep_copy}
58
+ end
59
+
60
+ # Rolls the page state back to the state of the given snapshot.
61
+ #
62
+ def restore_snapshot(shot)
63
+ page = state.page
64
+ # Because these objects are referenced by identifier from the Pages
65
+ # dictionary, we can't just restore them over the current refs in
66
+ # page_content and current_page. We have to restore them over the old
67
+ # ones.
68
+ page.content = shot[:page_content].identifier
69
+ page.content.replace shot[:page_content]
70
+
71
+ page.dictionary = shot[:current_page].identifier
72
+ page.dictionary.replace shot[:current_page]
73
+ page.dictionary.data[:Contents] = page.content
74
+
75
+ self.page_number = shot[:page_number]
76
+
77
+ state.store.pages.data[:Kids] = shot[:page_kids].map{|id| state.store[id]}
78
+ state.store.pages.data[:Count] = shot[:page_kids].size
79
+
80
+ self.bounds = BoundingBox.restore_deep_copy(shot[:bounds], self)
81
+
82
+ if shot[:dests]
83
+ names.data[:Dests] = shot[:dests]
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,55 @@
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: Any way to move this upstream?
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
@@ -0,0 +1,121 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Copyright September 2008, Gregory Brown, James Healy All Rights Reserved.
4
+ #
5
+ # This is free software. Please see the LICENSE and COPYING files for details.
6
+ #
7
+ module Prawn
8
+ module Encoding
9
+ # Map between unicode and WinAnsiEnoding
10
+ #
11
+ class WinAnsi #:nodoc:
12
+ CHARACTERS = %w[
13
+ .notdef .notdef .notdef .notdef
14
+ .notdef .notdef .notdef .notdef
15
+ .notdef .notdef .notdef .notdef
16
+ .notdef .notdef .notdef .notdef
17
+ .notdef .notdef .notdef .notdef
18
+ .notdef .notdef .notdef .notdef
19
+ .notdef .notdef .notdef .notdef
20
+ .notdef .notdef .notdef .notdef
21
+
22
+ space exclam quotedbl numbersign
23
+ dollar percent ampersand quotesingle
24
+ parenleft parenright asterisk plus
25
+ comma hyphen period slash
26
+ zero one two three
27
+ four five six seven
28
+ eight nine colon semicolon
29
+ less equal greater question
30
+
31
+ at A B C
32
+ D E F G
33
+ H I J K
34
+ L M N O
35
+ P Q R S
36
+ T U V W
37
+ X Y Z bracketleft
38
+ backslash bracketright asciicircum underscore
39
+
40
+ grave a b c
41
+ d e f g
42
+ h i j k
43
+ l m n o
44
+ p q r s
45
+ t u v w
46
+ x y z braceleft
47
+ bar braceright asciitilde .notdef
48
+
49
+ Euro .notdef quotesinglbase florin
50
+ quotedblbase ellipsis dagger daggerdbl
51
+ circumflex perthousand Scaron guilsinglleft
52
+ OE .notdef Zcaron .notdef
53
+ .notdef quoteleft quoteright quotedblleft
54
+ quotedblright bullet endash emdash
55
+ tilde trademark scaron guilsinglright
56
+ oe .notdef zcaron ydieresis
57
+
58
+ space exclamdown cent sterling
59
+ currency yen brokenbar section
60
+ dieresis copyright ordfeminine guillemotleft
61
+ logicalnot hyphen registered macron
62
+ degree plusminus twosuperior threesuperior
63
+ acute mu paragraph periodcentered
64
+ cedilla onesuperior ordmasculine guillemotright
65
+ onequarter onehalf threequarters questiondown
66
+
67
+ Agrave Aacute Acircumflex Atilde
68
+ Adieresis Aring AE Ccedilla
69
+ Egrave Eacute Ecircumflex Edieresis
70
+ Igrave Iacute Icircumflex Idieresis
71
+ Eth Ntilde Ograve Oacute
72
+ Ocircumflex Otilde Odieresis multiply
73
+ Oslash Ugrave Uacute Ucircumflex
74
+ Udieresis Yacute Thorn germandbls
75
+
76
+ agrave aacute acircumflex atilde
77
+ adieresis aring ae ccedilla
78
+ egrave eacute ecircumflex edieresis
79
+ igrave iacute icircumflex idieresis
80
+ eth ntilde ograve oacute
81
+ ocircumflex otilde odieresis divide
82
+ oslash ugrave uacute ucircumflex
83
+ udieresis yacute thorn ydieresis
84
+ ]
85
+
86
+ def initialize
87
+ @mapping_file = "#{Prawn::DATADIR}/encodings/win_ansi.txt"
88
+ load_mapping if self.class.mapping.empty?
89
+ end
90
+
91
+ # Converts a Unicode codepoint into a valid WinAnsi single byte character.
92
+ #
93
+ # If there is no WinAnsi equivlant for a character, a _ will be substituted.
94
+ #
95
+ def [](codepoint)
96
+ # unicode codepoints < 255 map directly to the single byte value in WinAnsi
97
+ return codepoint if codepoint <= 255
98
+
99
+ # There are a handful of codepoints > 255 that have equivilants in WinAnsi.
100
+ # Replace anything else with an underscore
101
+ self.class.mapping[codepoint] || 95
102
+ end
103
+
104
+ def self.mapping
105
+ @mapping ||= {}
106
+ end
107
+
108
+ private
109
+
110
+ def load_mapping
111
+ RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
112
+ File.open(@mapping_file, mode) do |f|
113
+ f.each do |l|
114
+ m, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
115
+ self.class.mapping["0x#{unicode}".hex] = "0x#{single_byte}".hex if single_byte
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+ #
3
+ # errors.rb : Implements custom error classes for Prawn
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
+ module Errors
11
+
12
+ # This error is raised when Prawn::PdfObject() encounters a Ruby object it
13
+ # cannot convert to PDF
14
+ #
15
+ FailedObjectConversion = Class.new(StandardError)
16
+
17
+ # This error is raised when Document#page_layout is set to anything
18
+ # other than :portrait or :landscape
19
+ #
20
+ InvalidPageLayout = Class.new(StandardError)
21
+
22
+ # Raised when a table is spanned in an impossible way.
23
+ #
24
+ InvalidTableSpan = Class.new(StandardError)
25
+
26
+ # This error is raised when a method requiring a current page is called
27
+ # without being on a page.
28
+ #
29
+ NotOnPage = Class.new(StandardError)
30
+
31
+ # This error is raised when Prawn cannot find a specified font
32
+ #
33
+ UnknownFont = Class.new(StandardError)
34
+
35
+ # Raised when Prawn is asked to draw something into a too-small box
36
+ #
37
+ CannotFit = Class.new(StandardError)
38
+
39
+ # Raised if group() is called with a block that is too big to be
40
+ # rendered in the current context.
41
+ #
42
+ CannotGroup = Class.new(StandardError)
43
+
44
+ # This error is raised when Prawn is being used on a M17N aware VM,
45
+ # and the user attempts to add text that isn't compatible with UTF-8
46
+ # to their document
47
+ #
48
+ IncompatibleStringEncoding = Class.new(StandardError)
49
+
50
+ # This error is raised when Prawn encounters an unknown key in functions
51
+ # that accept an options hash. This usually means there is a typo in your
52
+ # code or that the option you are trying to use has a different name than
53
+ # what you have specified.
54
+ #
55
+ UnknownOption = Class.new(StandardError)
56
+
57
+ # this error is raised when a user attempts to embed an image of an unsupported
58
+ # type. This can either a completely unsupported format, or a dialect of a
59
+ # supported format (ie. some types of PNG)
60
+ UnsupportedImageType = Class.new(StandardError)
61
+
62
+ # This error is raised when a named element has alredy been
63
+ # created. For example, in the stamp module, stamps must have
64
+ # unique names within a document
65
+ NameTaken = Class.new(StandardError)
66
+
67
+ # This error is raised when a name is not a valid format
68
+ InvalidName = Class.new(StandardError)
69
+
70
+ # This error is raised when an object is attempted to be
71
+ # referenced by name, but no such name is associated with an object
72
+ UndefinedObjectName = Class.new(StandardError)
73
+
74
+ # This error is raised when a required option has not been set
75
+ RequiredOption = Class.new(StandardError)
76
+
77
+ # This error is raised when a requested outline item with a given title does not exist
78
+ UnknownOutlineTitle = Class.new(StandardError)
79
+
80
+ # This error is raised when a block is required, but not provided
81
+ BlockRequired = Class.new(StandardError)
82
+
83
+ # This error is rased when a graphics method is called with improper arguments
84
+ InvalidGraphicsPath = Class.new(StandardError)
85
+
86
+ # This error is raised when Prawn fails to load a template file
87
+ #
88
+ TemplateError = Class.new(StandardError)
89
+
90
+ # This error is raise when trying to restore a graphic state that
91
+ #
92
+ EmptyGraphicStateStack = Class.new(StandardError)
93
+
94
+ # Raised when unrecognized content is provided for a table cell.
95
+ #
96
+ UnrecognizedTableContent = Class.new(StandardError)
97
+
98
+ end
99
+ end
@@ -0,0 +1,386 @@
1
+ # encoding: utf-8
2
+ #
3
+ # font.rb : The Prawn font class
4
+ #
5
+ # Copyright May 2008, Gregory Brown / James Healy. All Rights Reserved.
6
+ #
7
+ # This is free software. Please see the LICENSE and COPYING files for details.
8
+ #
9
+ require "prawn/font/afm"
10
+ require "prawn/font/ttf"
11
+ require "prawn/font/dfont"
12
+
13
+ module Prawn
14
+
15
+ class Document
16
+ # Without arguments, this returns the currently selected font. Otherwise,
17
+ # it sets the current font. When a block is used, the font is applied
18
+ # transactionally and is rolled back when the block exits.
19
+ #
20
+ # Prawn::Document.generate("font.pdf") do
21
+ # text "Default font is Helvetica"
22
+ #
23
+ # font "Times-Roman"
24
+ # text "Now using Times-Roman"
25
+ #
26
+ # font("Chalkboard.ttf") do
27
+ # text "Using TTF font from file Chalkboard.ttf"
28
+ # font "Courier", :style => :bold
29
+ # text "You see this in bold Courier"
30
+ # end
31
+ #
32
+ # text "Times-Roman, again"
33
+ # end
34
+ #
35
+ # The :name parameter must be a string. It can be one of the 14 built-in
36
+ # fonts supported by PDF, or the location of a TTF file. The Font::AFM::BUILT_INS
37
+ # array specifies the valid built in font values.
38
+ #
39
+ # If a ttf font is specified, the glyphs necessary to render your document
40
+ # will be embedded in the rendered PDF. This should be your preferred option
41
+ # in most cases. It will increase the size of the resulting file, but also
42
+ # make it more portable.
43
+ #
44
+ # The options parameter is an optional hash providing size and style. To use
45
+ # the :style option you need to map those font styles to their respective font files.
46
+ # See font_families for more information.
47
+ #
48
+ def font(name=nil, options={})
49
+ return((defined?(@font) && @font) || font("Helvetica")) if name.nil?
50
+
51
+ if state.pages.empty? && !state.page.in_stamp_stream?
52
+ raise Prawn::Errors::NotOnPage
53
+ end
54
+
55
+ new_font = find_font(name.to_s, options)
56
+
57
+ if block_given?
58
+ save_font do
59
+ set_font(new_font, options[:size])
60
+ yield
61
+ end
62
+ else
63
+ set_font(new_font, options[:size])
64
+ end
65
+
66
+ @font
67
+ end
68
+
69
+ # When called with no argument, returns the current font size.
70
+ # When called with a single argument but no block, sets the current font
71
+ # size. When a block is used, the font size is applied transactionally and
72
+ # is rolled back when the block exits. You may still change the font size
73
+ # within a transactional block for individual text segments, or nested calls
74
+ # to font_size.
75
+ #
76
+ # Prawn::Document.generate("font_size.pdf") do
77
+ # font_size 16
78
+ # text "At size 16"
79
+ #
80
+ # font_size(10) do
81
+ # text "At size 10"
82
+ # text "At size 6", :size => 6
83
+ # text "At size 10"
84
+ # end
85
+ #
86
+ # text "At size 16"
87
+ # end
88
+ #
89
+ # When called without an argument, this method returns the current font
90
+ # size.
91
+ #
92
+ def font_size(points=nil)
93
+ return @font_size unless points
94
+ size_before_yield = @font_size
95
+ @font_size = points
96
+ block_given? ? yield : return
97
+ @font_size = size_before_yield
98
+ end
99
+
100
+ # Sets the font directly, given an actual Font object
101
+ # and size.
102
+ #
103
+ def set_font(font, size=nil) # :nodoc:
104
+ @font = font
105
+ @font_size = size if size
106
+ end
107
+
108
+ # Saves the current font, and then yields. When the block
109
+ # finishes, the original font is restored.
110
+ #
111
+ def save_font
112
+ @font ||= find_font("Helvetica")
113
+ original_font = @font
114
+ original_size = @font_size
115
+
116
+ yield
117
+ ensure
118
+ set_font(original_font, original_size) if original_font
119
+ end
120
+
121
+ # Looks up the given font using the given criteria. Once a font has been
122
+ # found by that matches the criteria, it will be cached to subsequent lookups
123
+ # for that font will return the same object.
124
+ #--
125
+ # Challenges involved: the name alone is not sufficient to uniquely identify
126
+ # a font (think dfont suitcases that can hold multiple different fonts in a
127
+ # single file). Thus, the :name key is included in the cache key.
128
+ #
129
+ # It is further complicated, however, since fonts in some formats (like the
130
+ # dfont suitcases) can be identified either by numeric index, OR by their
131
+ # name within the suitcase, and both should hash to the same font object
132
+ # (to avoid the font being embedded multiple times). This is not yet implemented,
133
+ # which means if someone selects a font both by name, and by index, the
134
+ # font will be embedded twice. Since we do font subsetting, this double
135
+ # embedding won't be catastrophic, just annoying.
136
+ # ++
137
+ def find_font(name, options={}) #:nodoc:
138
+ if font_families.key?(name)
139
+ family, name = name, font_families[name][options[:style] || :normal]
140
+ if name.is_a?(Hash)
141
+ options = options.merge(name)
142
+ name = options[:file]
143
+ end
144
+ end
145
+ key = "#{name}:#{options[:font] || 0}"
146
+ font_registry[key] ||= Font.load(self, name, options.merge(:family => family))
147
+ end
148
+
149
+ # Hash of Font objects keyed by names
150
+ #
151
+ def font_registry #:nodoc:
152
+ @font_registry ||= {}
153
+ end
154
+
155
+ # Hash that maps font family names to their styled individual font names.
156
+ #
157
+ # To add support for another font family, append to this hash, e.g:
158
+ #
159
+ # pdf.font_families.update(
160
+ # "MyTrueTypeFamily" => { :bold => "foo-bold.ttf",
161
+ # :italic => "foo-italic.ttf",
162
+ # :bold_italic => "foo-bold-italic.ttf",
163
+ # :normal => "foo.ttf" })
164
+ #
165
+ # This will then allow you to use the fonts like so:
166
+ #
167
+ # pdf.font("MyTrueTypeFamily", :style => :bold)
168
+ # pdf.text "Some bold text"
169
+ # pdf.font("MyTrueTypeFamily")
170
+ # pdf.text "Some normal text"
171
+ #
172
+ # This assumes that you have appropriate TTF fonts for each style you
173
+ # wish to support.
174
+ #
175
+ # By default the styles :bold, :italic, :bold_italic, and :normal are
176
+ # defined for fonts "Courier", "Times-Roman" and "Helvetica".
177
+ #
178
+ # You probably want to provide those four styles, but are free to define
179
+ # custom ones, like :thin, and use them in font calls.
180
+ #
181
+ def font_families
182
+ @font_families ||= Hash.new.merge!(
183
+ { "Courier" => { :bold => "Courier-Bold",
184
+ :italic => "Courier-Oblique",
185
+ :bold_italic => "Courier-BoldOblique",
186
+ :normal => "Courier" },
187
+
188
+ "Times-Roman" => { :bold => "Times-Bold",
189
+ :italic => "Times-Italic",
190
+ :bold_italic => "Times-BoldItalic",
191
+ :normal => "Times-Roman" },
192
+
193
+ "Helvetica" => { :bold => "Helvetica-Bold",
194
+ :italic => "Helvetica-Oblique",
195
+ :bold_italic => "Helvetica-BoldOblique",
196
+ :normal => "Helvetica" }
197
+ })
198
+ end
199
+
200
+ # Returns the width of the given string using the given font. If :size is not
201
+ # specified as one of the options, the string is measured using the current
202
+ # font size. You can also pass :kerning as an option to indicate whether
203
+ # kerning should be used when measuring the width (defaults to +false+).
204
+ #
205
+ # Note that the string _must_ be encoded properly for the font being used.
206
+ # For AFM fonts, this is WinAnsi. For TTF, make sure the font is encoded as
207
+ # UTF-8. You can use the Font#normalize_encoding method to make sure strings
208
+ # are in an encoding appropriate for the current font.
209
+ #--
210
+ # For the record, this method used to be a method of Font (and still delegates
211
+ # to width computations on Font). However, having the primary interface for
212
+ # calculating string widths exist on Font made it tricky to write extensions
213
+ # for Prawn in which widths are computed differently (e.g., taking formatting
214
+ # tags into account, or the like).
215
+ #
216
+ # By putting width_of here, on Document itself, extensions may easily override
217
+ # it and redefine the width calculation behavior.
218
+ #++
219
+ def width_of(string, options={})
220
+ if options[:inline_format]
221
+ # Build up an Arranger with the entire string on one line, finalize it,
222
+ # and find its width.
223
+ arranger = Core::Text::Formatted::Arranger.new(self, options)
224
+ arranger.consumed = Text::Formatted::Parser.to_array(string)
225
+ arranger.finalize_line
226
+
227
+ arranger.line_width
228
+ else
229
+ f = if options[:style]
230
+ # override style with :style => :bold
231
+ find_font(@font ? @font.name : 'Helvetica',
232
+ :style => options[:style])
233
+ else
234
+ font
235
+ end
236
+ f.compute_width_of(string, options) +
237
+ (character_spacing * font.character_count(string))
238
+ end
239
+ end
240
+ end
241
+
242
+ # Provides font information and helper functions.
243
+ #
244
+ class Font
245
+
246
+ # The current font name
247
+ attr_reader :name
248
+
249
+ # The current font family
250
+ attr_reader :family
251
+
252
+ # The options hash used to initialize the font
253
+ attr_reader :options
254
+
255
+ # Shortcut interface for constructing a font object. Filenames of the form
256
+ # *.ttf will call Font::TTF.new, *.dfont Font::DFont.new, and anything else
257
+ # will be passed through to Font::AFM.new()
258
+ #
259
+ def self.load(document,name,options={})
260
+ case name.to_s
261
+ when /\.ttf$/i then TTF.new(document, name, options)
262
+ when /\.dfont$/i then DFont.new(document, name, options)
263
+ when /\.afm$/i then AFM.new(document, name, options)
264
+ else AFM.new(document, name, options)
265
+ end
266
+ end
267
+
268
+ def initialize(document,name,options={}) #:nodoc:
269
+ @document = document
270
+ @name = name
271
+ @options = options
272
+
273
+ @family = options[:family]
274
+
275
+ @identifier = generate_unique_id
276
+
277
+ @references = {}
278
+ end
279
+
280
+ # The size of the font ascender in PDF points
281
+ #
282
+ def ascender
283
+ @ascender / 1000.0 * size
284
+ end
285
+
286
+ # The size of the font descender in PDF points
287
+ #
288
+ def descender
289
+ -@descender / 1000.0 * size
290
+ end
291
+
292
+ # The size of the recommended gap between lines of text in PDF points
293
+ #
294
+ def line_gap
295
+ @line_gap / 1000.0 * size
296
+ end
297
+
298
+ def identifier_for(subset)
299
+ "#{@identifier}.#{subset}".to_sym
300
+ end
301
+
302
+ def inspect
303
+ "#{self.class.name}< #{name}: #{size} >"
304
+ end
305
+
306
+ # Normalizes the encoding of the string to an encoding supported by the
307
+ # font. The string is expected to be UTF-8 going in. It will be re-encoded
308
+ # and the new string will be returned. For an in-place (destructive)
309
+ # version, see normalize_encoding!.
310
+ def normalize_encoding(string)
311
+ raise NotImplementedError, "subclasses of Prawn::Font must implement #normalize_encoding"
312
+ end
313
+
314
+ # Destructive version of normalize_encoding; normalizes the encoding of a
315
+ # string in place.
316
+ #
317
+ def normalize_encoding!(str)
318
+ str.replace(normalize_encoding(str))
319
+ end
320
+
321
+ # Gets height of current font in PDF points at the given font size
322
+ #
323
+ def height_at(size)
324
+ @normalized_height ||= (@ascender - @descender + @line_gap) / 1000.0
325
+ @normalized_height * size
326
+ end
327
+
328
+ # Gets height of current font in PDF points at current font size
329
+ #
330
+ def height
331
+ height_at(size)
332
+ end
333
+
334
+ # Registers the given subset of the current font with the current PDF
335
+ # page. This is safe to call multiple times for a given font and subset,
336
+ # as it will only add the font the first time it is called.
337
+ #
338
+ def add_to_current_page(subset)
339
+ @references[subset] ||= register(subset)
340
+ @document.state.page.fonts.merge!(identifier_for(subset) => @references[subset])
341
+ end
342
+
343
+ def identifier_for(subset) #:nodoc:
344
+ "#{@identifier}.#{subset}"
345
+ end
346
+
347
+ def inspect #:nodoc:
348
+ "#{self.class.name}< #{name}: #{size} >"
349
+ end
350
+
351
+ private
352
+
353
+ # generate a font identifier that hasn't been used on the curretn page yet
354
+ #
355
+ def generate_unique_id
356
+ offset, id = 0, nil
357
+
358
+ while id.nil? || page_contains_font_id?(id)
359
+ offset += 1
360
+ id = :"F#{@document.font_registry.size + offset}"
361
+ end
362
+
363
+ id
364
+ end
365
+
366
+ # Returns true if the provided font identifier already exists in the document.
367
+ # This is used when adding new fonts to a document to ensure we don't step
368
+ # on fonts imported from a template.
369
+ #
370
+ # page_contains_font_id?("F1")
371
+ # => true
372
+ #
373
+ def page_contains_font_id?(id)
374
+ id = id.to_s
375
+ @document.state.page.fonts.keys.any? { |exist_id|
376
+ exist_id.to_s[0,id.size] == id
377
+ }
378
+ end
379
+
380
+ def size
381
+ @document.font_size
382
+ end
383
+
384
+ end
385
+
386
+ end