nurettin-prawn 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +18 -0
- data/LICENSE +56 -0
- data/README.md +98 -0
- data/Rakefile +45 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/prawn.png +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/form.pdf +820 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/multipage_template.pdf +127 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/lib/prawn.rb +29 -0
- data/lib/prawn/compatibility.rb +87 -0
- data/lib/prawn/core.rb +87 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +79 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +177 -0
- data/lib/prawn/core/object_store.rb +320 -0
- data/lib/prawn/core/page.rb +212 -0
- data/lib/prawn/core/pdf_object.rb +125 -0
- data/lib/prawn/core/reference.rb +119 -0
- data/lib/prawn/core/text.rb +268 -0
- data/lib/prawn/core/text/formatted/arranger.rb +294 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
- data/lib/prawn/core/text/formatted/wrap.rb +153 -0
- data/lib/prawn/document.rb +707 -0
- data/lib/prawn/document/bounding_box.rb +510 -0
- data/lib/prawn/document/column_box.rb +132 -0
- data/lib/prawn/document/graphics_state.rb +136 -0
- data/lib/prawn/document/internals.rb +173 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +89 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +99 -0
- data/lib/prawn/font.rb +386 -0
- data/lib/prawn/font/afm.rb +203 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +343 -0
- data/lib/prawn/graphics.rb +523 -0
- data/lib/prawn/graphics/cap_style.rb +46 -0
- data/lib/prawn/graphics/color.rb +231 -0
- data/lib/prawn/graphics/dash.rb +82 -0
- data/lib/prawn/graphics/join_style.rb +47 -0
- data/lib/prawn/graphics/patterns.rb +137 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +196 -0
- data/lib/prawn/images/image.rb +65 -0
- data/lib/prawn/images/jpg.rb +85 -0
- data/lib/prawn/images/png.rb +362 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +122 -0
- data/lib/prawn/security.rb +269 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +134 -0
- data/lib/prawn/table.rb +609 -0
- data/lib/prawn/table/cell.rb +776 -0
- data/lib/prawn/table/cell/image.rb +70 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/span_dummy.rb +88 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +152 -0
- data/lib/prawn/table/cells.rb +260 -0
- data/lib/prawn/text.rb +420 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +563 -0
- data/lib/prawn/text/formatted/fragment.rb +253 -0
- data/lib/prawn/text/formatted/parser.rb +217 -0
- data/lib/prawn/utilities.rb +44 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +34 -0
- data/manual/basic_concepts/creation.rb +39 -0
- data/manual/basic_concepts/cursor.rb +33 -0
- data/manual/basic_concepts/measurement.rb +25 -0
- data/manual/basic_concepts/origin.rb +38 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
- data/manual/bounding_box/bounding_box.rb +39 -0
- data/manual/bounding_box/bounds.rb +49 -0
- data/manual/bounding_box/canvas.rb +24 -0
- data/manual/bounding_box/creation.rb +23 -0
- data/manual/bounding_box/indentation.rb +46 -0
- data/manual/bounding_box/nesting.rb +45 -0
- data/manual/bounding_box/russian_boxes.rb +40 -0
- data/manual/bounding_box/stretchy.rb +31 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +31 -0
- data/manual/document_and_page_options/metadata.rb +23 -0
- data/manual/document_and_page_options/page_margins.rb +38 -0
- data/manual/document_and_page_options/page_size.rb +34 -0
- data/manual/example_file.rb +116 -0
- data/manual/example_helper.rb +430 -0
- data/manual/example_package.rb +53 -0
- data/manual/example_section.rb +46 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +28 -0
- data/manual/graphics/fill_and_stroke.rb +42 -0
- data/manual/graphics/fill_rules.rb +37 -0
- data/manual/graphics/gradients.rb +37 -0
- data/manual/graphics/graphics.rb +58 -0
- data/manual/graphics/helper.rb +17 -0
- data/manual/graphics/line_width.rb +35 -0
- data/manual/graphics/lines_and_curves.rb +41 -0
- data/manual/graphics/polygon.rb +29 -0
- data/manual/graphics/rectangle.rb +21 -0
- data/manual/graphics/rotate.rb +28 -0
- data/manual/graphics/scale.rb +41 -0
- data/manual/graphics/soft_masks.rb +46 -0
- data/manual/graphics/stroke_cap.rb +31 -0
- data/manual/graphics/stroke_dash.rb +43 -0
- data/manual/graphics/stroke_join.rb +30 -0
- data/manual/graphics/translate.rb +29 -0
- data/manual/graphics/transparency.rb +35 -0
- data/manual/images/absolute_position.rb +23 -0
- data/manual/images/fit.rb +21 -0
- data/manual/images/horizontal.rb +25 -0
- data/manual/images/images.rb +40 -0
- data/manual/images/plain_image.rb +18 -0
- data/manual/images/scale.rb +22 -0
- data/manual/images/vertical.rb +28 -0
- data/manual/images/width_and_height.rb +25 -0
- data/manual/layout/boxes.rb +27 -0
- data/manual/layout/content.rb +25 -0
- data/manual/layout/layout.rb +28 -0
- data/manual/layout/simple_grid.rb +23 -0
- data/manual/manual/cover.rb +26 -0
- data/manual/manual/foreword.rb +13 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +36 -0
- data/manual/outline/add_subsection_to.rb +61 -0
- data/manual/outline/insert_section_after.rb +47 -0
- data/manual/outline/outline.rb +32 -0
- data/manual/outline/sections_and_pages.rb +67 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +31 -0
- data/manual/repeatable_content/repeater.rb +55 -0
- data/manual/repeatable_content/stamp.rb +41 -0
- data/manual/security/encryption.rb +31 -0
- data/manual/security/permissions.rb +38 -0
- data/manual/security/security.rb +28 -0
- data/manual/syntax_highlight.rb +52 -0
- data/manual/table/basic_block.rb +53 -0
- data/manual/table/before_rendering_page.rb +26 -0
- data/manual/table/cell_border_lines.rb +24 -0
- data/manual/table/cell_borders_and_bg.rb +31 -0
- data/manual/table/cell_dimensions.rb +30 -0
- data/manual/table/cell_text.rb +38 -0
- data/manual/table/column_widths.rb +30 -0
- data/manual/table/content_and_subtables.rb +39 -0
- data/manual/table/creation.rb +27 -0
- data/manual/table/filtering.rb +36 -0
- data/manual/table/flow_and_header.rb +17 -0
- data/manual/table/image_cells.rb +33 -0
- data/manual/table/position.rb +29 -0
- data/manual/table/row_colors.rb +20 -0
- data/manual/table/span.rb +30 -0
- data/manual/table/style.rb +22 -0
- data/manual/table/table.rb +52 -0
- data/manual/table/width.rb +27 -0
- data/manual/templates/full_template.rb +23 -0
- data/manual/templates/page_template.rb +47 -0
- data/manual/templates/templates.rb +26 -0
- data/manual/text/alignment.rb +44 -0
- data/manual/text/color.rb +24 -0
- data/manual/text/column_box.rb +32 -0
- data/manual/text/fallback_fonts.rb +37 -0
- data/manual/text/font.rb +41 -0
- data/manual/text/font_size.rb +45 -0
- data/manual/text/font_style.rb +23 -0
- data/manual/text/formatted_callbacks.rb +60 -0
- data/manual/text/formatted_text.rb +50 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/group.rb +29 -0
- data/manual/text/inline.rb +43 -0
- data/manual/text/kerning_and_character_spacing.rb +39 -0
- data/manual/text/leading.rb +25 -0
- data/manual/text/line_wrapping.rb +41 -0
- data/manual/text/paragraph_indentation.rb +26 -0
- data/manual/text/positioned_text.rb +38 -0
- data/manual/text/registering_families.rb +48 -0
- data/manual/text/rendering_and_color.rb +37 -0
- data/manual/text/right_to_left_text.rb +43 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +75 -0
- data/manual/text/text_box_excess.rb +32 -0
- data/manual/text/text_box_extensions.rb +45 -0
- data/manual/text/text_box_overflow.rb +44 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +59 -0
- data/prawn.gemspec +46 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +493 -0
- data/spec/cell_spec.rb +584 -0
- data/spec/column_box_spec.rb +33 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +736 -0
- data/spec/extensions/encoding_helpers.rb +6 -0
- data/spec/extensions/mocha.rb +44 -0
- data/spec/font_spec.rb +433 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +640 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +651 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +140 -0
- data/spec/inline_formatted_text_parser_spec.rb +515 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +333 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +170 -0
- data/spec/outline_spec.rb +426 -0
- data/spec/pdf_object_spec.rb +172 -0
- data/spec/png_spec.rb +240 -0
- data/spec/reference_spec.rb +105 -0
- data/spec/repeater_spec.rb +158 -0
- data/spec/security_spec.rb +126 -0
- data/spec/snapshot_spec.rb +186 -0
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/stamp_spec.rb +159 -0
- data/spec/stroke_styles_spec.rb +193 -0
- data/spec/table_spec.rb +1209 -0
- data/spec/template_spec.rb +351 -0
- data/spec/text_at_spec.rb +129 -0
- data/spec/text_box_spec.rb +1029 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +421 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +89 -0
- metadata +544 -0
@@ -0,0 +1,268 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# prawn/core/text.rb : Implements low level text helpers for Prawn
|
4
|
+
#
|
5
|
+
# Copyright January 2010, Daniel Nelson. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
module Prawn
|
10
|
+
module Core
|
11
|
+
module Text #:nodoc:
|
12
|
+
|
13
|
+
# These should be used as a base. Extensions may build on this list
|
14
|
+
#
|
15
|
+
VALID_OPTIONS = [:kerning, :size, :style]
|
16
|
+
MODES = { :fill => 0, :stroke => 1, :fill_stroke => 2, :invisible => 3,
|
17
|
+
:fill_clip => 4, :stroke_clip => 5, :fill_stroke_clip => 6,
|
18
|
+
:clip => 7 }
|
19
|
+
|
20
|
+
attr_reader :skip_encoding
|
21
|
+
|
22
|
+
# Low level text placement method. All font and size alterations
|
23
|
+
# should already be set
|
24
|
+
#
|
25
|
+
def draw_text!(text, options)
|
26
|
+
x,y = map_to_absolute(options[:at])
|
27
|
+
add_text_content(text,x,y,options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Low level call to set the current font style and extract text options from
|
31
|
+
# an options hash. Should be called from within a save_font block
|
32
|
+
#
|
33
|
+
def process_text_options(options)
|
34
|
+
if options[:style]
|
35
|
+
raise "Bad font family" unless font.family
|
36
|
+
font(font.family, :style => options[:style])
|
37
|
+
end
|
38
|
+
|
39
|
+
# must compare against false to keep kerning on as default
|
40
|
+
unless options[:kerning] == false
|
41
|
+
options[:kerning] = font.has_kerning_data?
|
42
|
+
end
|
43
|
+
|
44
|
+
options[:size] ||= font_size
|
45
|
+
end
|
46
|
+
|
47
|
+
# Retrieve the current default kerning setting.
|
48
|
+
#
|
49
|
+
# Defaults to true
|
50
|
+
#
|
51
|
+
def default_kerning?
|
52
|
+
return true if @default_kerning.nil?
|
53
|
+
@default_kerning
|
54
|
+
end
|
55
|
+
|
56
|
+
# Call with a boolean to set the document-wide kerning setting. This can be
|
57
|
+
# overridden using the :kerning text option when drawing text or a text
|
58
|
+
# box.
|
59
|
+
#
|
60
|
+
# pdf.default_kerning = false
|
61
|
+
# pdf.text("hello world") # text is not kerned
|
62
|
+
# pdf.text("hello world", :kerning => true) # text is kerned
|
63
|
+
#
|
64
|
+
def default_kerning(boolean)
|
65
|
+
@default_kerning = boolean
|
66
|
+
end
|
67
|
+
|
68
|
+
alias_method :default_kerning=, :default_kerning
|
69
|
+
|
70
|
+
# Call with no argument to retrieve the current default leading.
|
71
|
+
#
|
72
|
+
# Call with a number to set the document-wide text leading. This can be
|
73
|
+
# overridden using the :leading text option when drawing text or a text
|
74
|
+
# box.
|
75
|
+
#
|
76
|
+
# pdf.default_leading = 7
|
77
|
+
# pdf.text("hello world") # a leading of 7 is used
|
78
|
+
# pdf.text("hello world", :leading => 0) # a leading of 0 is used
|
79
|
+
#
|
80
|
+
# Defaults to 0
|
81
|
+
#
|
82
|
+
def default_leading(number=nil)
|
83
|
+
if number.nil?
|
84
|
+
return 0 if @default_leading.nil?
|
85
|
+
@default_leading
|
86
|
+
else
|
87
|
+
@default_leading = number
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
alias_method :default_leading=, :default_leading
|
92
|
+
|
93
|
+
# Call with no argument to retrieve the current text direction.
|
94
|
+
#
|
95
|
+
# Call with a symbol to set the document-wide text direction. This can be
|
96
|
+
# overridden using the :direction text option when drawing text or a text
|
97
|
+
# box.
|
98
|
+
#
|
99
|
+
# pdf.text_direction = :rtl
|
100
|
+
# pdf.text("hello world") # prints "dlrow olleh"
|
101
|
+
# pdf.text("hello world", :direction => :ltr) # prints "hello world"
|
102
|
+
#
|
103
|
+
# Valid directions are:
|
104
|
+
#
|
105
|
+
# * :ltr - left-to-right (default)
|
106
|
+
# * :rtl - right-to-left
|
107
|
+
#
|
108
|
+
# Side effects:
|
109
|
+
#
|
110
|
+
# * When printing left-to-right, the default text alignment is :left
|
111
|
+
# * When printing right-to-left, the default text alignment is :right
|
112
|
+
#
|
113
|
+
def text_direction(direction=nil)
|
114
|
+
if direction.nil?
|
115
|
+
return :ltr if @text_direction.nil?
|
116
|
+
@text_direction
|
117
|
+
else
|
118
|
+
@text_direction = direction
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
alias_method :text_direction=, :text_direction
|
123
|
+
|
124
|
+
# Call with no argument to retrieve the current fallback fonts.
|
125
|
+
#
|
126
|
+
# Call with an array of font names. Each name must be the name of an AFM
|
127
|
+
# font or the name that was used to register a family of TTF fonts (see
|
128
|
+
# Prawn::Document#font_families). If present, then each glyph will be
|
129
|
+
# rendered using the first font that includes the glyph, starting with the
|
130
|
+
# current font and then moving through :fallback_fonts from left to right.
|
131
|
+
#
|
132
|
+
# Call with an empty array to turn off fallback fonts
|
133
|
+
#
|
134
|
+
# file = "#{Prawn::DATADIR}/fonts/gkai00mp.ttf"
|
135
|
+
# font_families["Kai"] = {
|
136
|
+
# :normal => { :file => file, :font => "Kai" }
|
137
|
+
# }
|
138
|
+
# file = "#{Prawn::DATADIR}/fonts/Action Man.dfont"
|
139
|
+
# font_families["Action Man"] = {
|
140
|
+
# :normal => { :file => file, :font => "ActionMan" },
|
141
|
+
# }
|
142
|
+
# fallback_fonts ["Times-Roman", "Kai"]
|
143
|
+
# font "Action Man"
|
144
|
+
# text "hello ƒ 你好"
|
145
|
+
# > hello prints in Action Man
|
146
|
+
# > ƒ prints in Times-Roman
|
147
|
+
# > 你好 prints in Kai
|
148
|
+
#
|
149
|
+
# fallback_fonts [] # clears document-wide fallback fonts
|
150
|
+
#
|
151
|
+
# Side effects:
|
152
|
+
#
|
153
|
+
# * Increased overhead when fallback fonts are declared as each glyph is
|
154
|
+
# checked to see whether it exists in the current font
|
155
|
+
#
|
156
|
+
def fallback_fonts(fallback_fonts=nil)
|
157
|
+
if fallback_fonts.nil?
|
158
|
+
return [] if @fallback_fonts.nil?
|
159
|
+
@fallback_fonts
|
160
|
+
else
|
161
|
+
@fallback_fonts = fallback_fonts
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
alias_method :fallback_fonts=, :fallback_fonts
|
166
|
+
|
167
|
+
# Call with no argument to retrieve the current text rendering mode.
|
168
|
+
#
|
169
|
+
# Call with a symbol and block to temporarily change the current
|
170
|
+
# text rendering mode.
|
171
|
+
#
|
172
|
+
# pdf.text_rendering_mode(:stroke) do
|
173
|
+
# pdf.text("Outlined Text")
|
174
|
+
# end
|
175
|
+
#
|
176
|
+
# Valid modes are:
|
177
|
+
#
|
178
|
+
# * :fill - fill text (default)
|
179
|
+
# * :stroke - stroke text
|
180
|
+
# * :fill_stroke - fill, then stroke text
|
181
|
+
# * :invisible - invisible text
|
182
|
+
# * :fill_clip - fill text then add to path for clipping
|
183
|
+
# * :stroke_clip - stroke text then add to path for clipping
|
184
|
+
# * :fill_stroke_clip - fill then stroke text, then add to path for clipping
|
185
|
+
# * :clip - add text to path for clipping
|
186
|
+
#
|
187
|
+
def text_rendering_mode(mode=nil)
|
188
|
+
return @text_rendering_mode || :fill if mode.nil?
|
189
|
+
unless MODES.key?(mode)
|
190
|
+
raise ArgumentError, "mode must be between one of #{MODES.keys.join(', ')} (#{mode})"
|
191
|
+
end
|
192
|
+
original_mode = @text_rendering_mode || :fill
|
193
|
+
if original_mode == mode
|
194
|
+
yield
|
195
|
+
else
|
196
|
+
@text_rendering_mode = mode
|
197
|
+
add_content "\n#{MODES[mode]} Tr"
|
198
|
+
yield
|
199
|
+
add_content "\n#{MODES[original_mode]} Tr"
|
200
|
+
@text_rendering_mode = original_mode
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# Increases or decreases the space between characters.
|
205
|
+
# For horizontal text, a positive value will increase the space.
|
206
|
+
# For veritical text, a positive value will decrease the space.
|
207
|
+
#
|
208
|
+
def character_spacing(amount=nil)
|
209
|
+
return @character_spacing || 0 if amount.nil?
|
210
|
+
original_character_spacing = character_spacing
|
211
|
+
if original_character_spacing == amount
|
212
|
+
yield
|
213
|
+
else
|
214
|
+
@character_spacing = amount
|
215
|
+
add_content "\n%.3f Tc" % amount
|
216
|
+
yield
|
217
|
+
add_content "\n%.3f Tc" % original_character_spacing
|
218
|
+
@character_spacing = original_character_spacing
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Increases or decreases the space between words.
|
223
|
+
# For horizontal text, a positive value will increase the space.
|
224
|
+
# For veritical text, a positive value will decrease the space.
|
225
|
+
#
|
226
|
+
def word_spacing(amount=nil)
|
227
|
+
return @word_spacing || 0 if amount.nil?
|
228
|
+
original_word_spacing = word_spacing
|
229
|
+
if original_word_spacing == amount
|
230
|
+
yield
|
231
|
+
else
|
232
|
+
@word_spacing = amount
|
233
|
+
add_content "\n%.3f Tw" % amount
|
234
|
+
yield
|
235
|
+
add_content "\n%.3f Tw" % original_word_spacing
|
236
|
+
@word_spacing = original_word_spacing
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
private
|
241
|
+
|
242
|
+
def add_text_content(text, x, y, options)
|
243
|
+
chunks = font.encode_text(text,options)
|
244
|
+
|
245
|
+
add_content "\nBT"
|
246
|
+
|
247
|
+
if options[:rotate]
|
248
|
+
rad = options[:rotate].to_f * Math::PI / 180
|
249
|
+
arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
|
250
|
+
add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
|
251
|
+
else
|
252
|
+
add_content "#{x} #{y} Td"
|
253
|
+
end
|
254
|
+
|
255
|
+
chunks.each do |(subset, string)|
|
256
|
+
font.add_to_current_page(subset)
|
257
|
+
add_content "/#{font.identifier_for(subset)} #{font_size} Tf"
|
258
|
+
|
259
|
+
operation = options[:kerning] && string.is_a?(Array) ? "TJ" : "Tj"
|
260
|
+
add_content Prawn::Core::PdfObject(string, true) << " " << operation
|
261
|
+
end
|
262
|
+
|
263
|
+
add_content "ET\n"
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
end
|
@@ -0,0 +1,294 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# core/text/formatted/arranger.rb : Implements a data structure for 2-stage
|
4
|
+
# processing of lines of formatted text
|
5
|
+
#
|
6
|
+
# Copyright February 2010, Daniel Nelson. All Rights Reserved.
|
7
|
+
#
|
8
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
+
|
10
|
+
module Prawn
|
11
|
+
module Core
|
12
|
+
module Text
|
13
|
+
module Formatted #:nodoc:
|
14
|
+
|
15
|
+
class Arranger #:nodoc:
|
16
|
+
attr_reader :max_line_height
|
17
|
+
attr_reader :max_descender
|
18
|
+
attr_reader :max_ascender
|
19
|
+
attr_accessor :consumed
|
20
|
+
|
21
|
+
# The following present only for testing purposes
|
22
|
+
attr_reader :unconsumed
|
23
|
+
attr_reader :fragments
|
24
|
+
attr_reader :current_format_state
|
25
|
+
|
26
|
+
def initialize(document, options={})
|
27
|
+
@document = document
|
28
|
+
@fragments = []
|
29
|
+
@unconsumed = []
|
30
|
+
@kerning = options[:kerning]
|
31
|
+
end
|
32
|
+
|
33
|
+
def space_count
|
34
|
+
if @unfinalized_line
|
35
|
+
raise "Lines must be finalized before calling #space_count"
|
36
|
+
end
|
37
|
+
@fragments.inject(0) do |sum, fragment|
|
38
|
+
sum + fragment.space_count
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def line_width
|
43
|
+
if @unfinalized_line
|
44
|
+
raise "Lines must be finalized before calling #line_width"
|
45
|
+
end
|
46
|
+
@fragments.inject(0) do |sum, fragment|
|
47
|
+
sum + fragment.width
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def line
|
52
|
+
if @unfinalized_line
|
53
|
+
raise "Lines must be finalized before calling #line"
|
54
|
+
end
|
55
|
+
@fragments.collect do |fragment|
|
56
|
+
if ruby_18 { true }
|
57
|
+
fragment.text
|
58
|
+
else
|
59
|
+
fragment.text.dup.force_encoding("utf-8")
|
60
|
+
end
|
61
|
+
end.join
|
62
|
+
end
|
63
|
+
|
64
|
+
def finalize_line
|
65
|
+
@unfinalized_line = false
|
66
|
+
omit_trailing_whitespace_from_line_width
|
67
|
+
@fragments = []
|
68
|
+
@consumed.each do |hash|
|
69
|
+
text = hash[:text]
|
70
|
+
format_state = hash.dup
|
71
|
+
format_state.delete(:text)
|
72
|
+
fragment = Prawn::Text::Formatted::Fragment.new(text,
|
73
|
+
format_state,
|
74
|
+
@document)
|
75
|
+
@fragments << fragment
|
76
|
+
set_fragment_measurements(fragment)
|
77
|
+
set_line_measurement_maximums(fragment)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def format_array=(array)
|
82
|
+
initialize_line
|
83
|
+
@unconsumed = []
|
84
|
+
array.each do |hash|
|
85
|
+
hash[:text].scan(/[^\n]+|\n/) do |line|
|
86
|
+
@unconsumed << hash.merge(:text => line)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def initialize_line
|
92
|
+
@unfinalized_line = true
|
93
|
+
@max_line_height = 0
|
94
|
+
@max_descender = 0
|
95
|
+
@max_ascender = 0
|
96
|
+
|
97
|
+
@consumed = []
|
98
|
+
@fragments = []
|
99
|
+
end
|
100
|
+
|
101
|
+
def finished?
|
102
|
+
@unconsumed.length == 0
|
103
|
+
end
|
104
|
+
|
105
|
+
def next_string
|
106
|
+
unless @unfinalized_line
|
107
|
+
raise "Lines must not be finalized when calling #next_string"
|
108
|
+
end
|
109
|
+
hash = @unconsumed.shift
|
110
|
+
if hash.nil?
|
111
|
+
nil
|
112
|
+
else
|
113
|
+
@consumed << hash.dup
|
114
|
+
@current_format_state = hash.dup
|
115
|
+
@current_format_state.delete(:text)
|
116
|
+
hash[:text]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def preview_next_string
|
121
|
+
hash = @unconsumed.first
|
122
|
+
if hash.nil? then nil
|
123
|
+
else hash[:text]
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def apply_color_and_font_settings(fragment, &block)
|
128
|
+
if fragment.color
|
129
|
+
original_fill_color = @document.fill_color
|
130
|
+
original_stroke_color = @document.stroke_color
|
131
|
+
@document.fill_color(*fragment.color)
|
132
|
+
@document.stroke_color(*fragment.color)
|
133
|
+
apply_font_settings(fragment, &block)
|
134
|
+
@document.stroke_color = original_stroke_color
|
135
|
+
@document.fill_color = original_fill_color
|
136
|
+
else
|
137
|
+
apply_font_settings(fragment, &block)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def apply_font_settings(fragment=nil, &block)
|
142
|
+
if fragment.nil?
|
143
|
+
font = current_format_state[:font]
|
144
|
+
size = current_format_state[:size]
|
145
|
+
character_spacing = current_format_state[:character_spacing] ||
|
146
|
+
@document.character_spacing
|
147
|
+
styles = current_format_state[:styles]
|
148
|
+
font_style = font_style(styles)
|
149
|
+
else
|
150
|
+
font = fragment.font
|
151
|
+
size = fragment.size
|
152
|
+
character_spacing = fragment.character_spacing
|
153
|
+
styles = fragment.styles
|
154
|
+
font_style = font_style(styles)
|
155
|
+
end
|
156
|
+
|
157
|
+
@document.character_spacing(character_spacing) do
|
158
|
+
if font || font_style != :normal
|
159
|
+
raise "Bad font family" unless @document.font.family
|
160
|
+
@document.font(font || @document.font.family, :style => font_style) do
|
161
|
+
apply_font_size(size, styles, &block)
|
162
|
+
end
|
163
|
+
else
|
164
|
+
apply_font_size(size, styles, &block)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def update_last_string(printed, unprinted, normalized_soft_hyphen=nil)
|
170
|
+
return if printed.nil?
|
171
|
+
if printed.empty?
|
172
|
+
@consumed.pop
|
173
|
+
else
|
174
|
+
@consumed.last[:text] = printed
|
175
|
+
if normalized_soft_hyphen
|
176
|
+
@consumed.last[:normalized_soft_hyphen] = normalized_soft_hyphen
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
unless unprinted.empty?
|
181
|
+
@unconsumed.unshift(@current_format_state.merge(:text => unprinted))
|
182
|
+
end
|
183
|
+
|
184
|
+
load_previous_format_state if printed.empty?
|
185
|
+
end
|
186
|
+
|
187
|
+
def retrieve_fragment
|
188
|
+
if @unfinalized_line
|
189
|
+
raise "Lines must be finalized before fragments can be retrieved"
|
190
|
+
end
|
191
|
+
@fragments.shift
|
192
|
+
end
|
193
|
+
|
194
|
+
def repack_unretrieved
|
195
|
+
new_unconsumed = []
|
196
|
+
while fragment = retrieve_fragment
|
197
|
+
fragment.include_trailing_white_space!
|
198
|
+
new_unconsumed << fragment.format_state.merge(:text => fragment.text)
|
199
|
+
end
|
200
|
+
@unconsumed = new_unconsumed.concat(@unconsumed)
|
201
|
+
end
|
202
|
+
|
203
|
+
def font_style(styles)
|
204
|
+
if styles.nil?
|
205
|
+
:normal
|
206
|
+
elsif styles.include?(:bold) && styles.include?(:italic)
|
207
|
+
:bold_italic
|
208
|
+
elsif styles.include?(:bold)
|
209
|
+
:bold
|
210
|
+
elsif styles.include?(:italic)
|
211
|
+
:italic
|
212
|
+
else
|
213
|
+
:normal
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
private
|
218
|
+
|
219
|
+
def load_previous_format_state
|
220
|
+
if @consumed.empty?
|
221
|
+
@current_format_state = {}
|
222
|
+
else
|
223
|
+
hash = @consumed.last
|
224
|
+
@current_format_state = hash.dup
|
225
|
+
@current_format_state.delete(:text)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
def apply_font_size(size, styles)
|
230
|
+
if subscript?(styles) || superscript?(styles)
|
231
|
+
relative_size = 0.583
|
232
|
+
if size.nil?
|
233
|
+
size = @document.font_size * relative_size
|
234
|
+
else
|
235
|
+
size = size * relative_size
|
236
|
+
end
|
237
|
+
end
|
238
|
+
if size.nil?
|
239
|
+
yield
|
240
|
+
else
|
241
|
+
@document.font_size(size) { yield }
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def subscript?(styles)
|
246
|
+
if styles.nil? then false
|
247
|
+
else styles.include?(:subscript)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def superscript?(styles)
|
252
|
+
if styles.nil? then false
|
253
|
+
else styles.include?(:superscript)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
def omit_trailing_whitespace_from_line_width
|
258
|
+
@consumed.reverse_each do |hash|
|
259
|
+
if hash[:text] == "\n"
|
260
|
+
break
|
261
|
+
elsif hash[:text].strip.empty? && @consumed.length > 1
|
262
|
+
# this entire fragment is trailing white space
|
263
|
+
hash[:exclude_trailing_white_space] = true
|
264
|
+
else
|
265
|
+
# this fragment contains the first non-white space we have
|
266
|
+
# encountered since the end of the line
|
267
|
+
hash[:exclude_trailing_white_space] = true
|
268
|
+
break
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
def set_fragment_measurements(fragment)
|
274
|
+
apply_font_settings(fragment) do
|
275
|
+
fragment.width = @document.width_of(fragment.text,
|
276
|
+
:kerning => @kerning)
|
277
|
+
fragment.line_height = @document.font.height
|
278
|
+
fragment.descender = @document.font.descender
|
279
|
+
fragment.ascender = @document.font.ascender
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
def set_line_measurement_maximums(fragment)
|
284
|
+
@max_line_height = [@max_line_height, fragment.line_height].compact.max
|
285
|
+
@max_descender = [@max_descender, fragment.descender].compact.max
|
286
|
+
@max_ascender = [@max_ascender, fragment.ascender].compact.max
|
287
|
+
end
|
288
|
+
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|