prawn 0.11.1 → 0.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +10 -0
- data/COPYING +2 -2
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +11 -0
- data/LICENSE +1 -1
- data/Rakefile +29 -38
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.color +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.color +0 -0
- data/data/images/indexed_color.dat +0 -0
- data/data/images/indexed_color.png +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.color +0 -0
- data/data/pdfs/nested_pages.pdf +13 -13
- data/lib/prawn/document/bounding_box.rb +87 -12
- data/lib/prawn/document/column_box.rb +57 -28
- data/lib/prawn/document/graphics_state.rb +11 -74
- data/lib/prawn/document/internals.rb +25 -23
- data/lib/prawn/document/snapshot.rb +11 -8
- data/lib/prawn/document/span.rb +12 -10
- data/lib/prawn/document.rb +250 -194
- data/lib/prawn/encoding.rb +9 -10
- data/lib/prawn/errors.rb +18 -29
- data/lib/prawn/font/afm.rb +52 -41
- data/lib/prawn/font/dfont.rb +4 -3
- data/lib/prawn/font/ttf.rb +44 -48
- data/lib/prawn/font.rb +138 -88
- data/lib/prawn/font_metric_cache.rb +47 -0
- data/lib/prawn/graphics/cap_style.rb +4 -3
- data/lib/prawn/graphics/color.rb +13 -5
- data/lib/prawn/graphics/dash.rb +53 -31
- data/lib/prawn/graphics/join_style.rb +9 -7
- data/lib/prawn/graphics/patterns.rb +138 -0
- data/lib/prawn/graphics/transformation.rb +10 -9
- data/lib/prawn/graphics/transparency.rb +3 -1
- data/lib/prawn/graphics.rb +316 -61
- data/lib/prawn/image_handler.rb +36 -0
- data/lib/prawn/images/image.rb +49 -0
- data/lib/prawn/images/jpg.rb +21 -15
- data/lib/prawn/images/png.rb +62 -119
- data/lib/prawn/images.rb +89 -108
- data/lib/prawn/layout/grid.rb +66 -54
- data/lib/prawn/layout.rb +10 -15
- data/lib/prawn/measurement_extensions.rb +10 -6
- data/lib/prawn/measurements.rb +27 -21
- data/lib/prawn/outline.rb +6 -308
- data/lib/prawn/repeater.rb +11 -9
- data/lib/prawn/security/arcfour.rb +1 -0
- data/lib/prawn/security.rb +55 -33
- data/lib/prawn/soft_mask.rb +96 -0
- data/lib/prawn/stamp.rb +5 -3
- data/lib/prawn/table/cell/image.rb +69 -0
- data/lib/prawn/table/cell/in_table.rb +4 -2
- data/lib/prawn/table/cell/span_dummy.rb +93 -0
- data/lib/prawn/table/cell/subtable.rb +2 -2
- data/lib/prawn/table/cell/text.rb +44 -26
- data/lib/prawn/table/cell.rb +302 -50
- data/lib/prawn/table/cells.rb +147 -49
- data/lib/prawn/table/column_width_calculator.rb +61 -0
- data/lib/prawn/table.rb +297 -118
- data/lib/prawn/text/box.rb +21 -5
- data/lib/prawn/text/formatted/arranger.rb +290 -0
- data/lib/prawn/text/formatted/box.rb +103 -59
- data/lib/prawn/text/formatted/fragment.rb +34 -23
- data/lib/prawn/text/formatted/line_wrap.rb +266 -0
- data/lib/prawn/text/formatted/parser.rb +15 -5
- data/lib/prawn/text/formatted/wrap.rb +150 -0
- data/lib/prawn/text/formatted.rb +5 -4
- data/lib/prawn/text.rb +38 -24
- data/lib/prawn/utilities.rb +46 -0
- data/lib/prawn.rb +85 -20
- 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 +32 -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/document_and_page_options/print_scaling.rb +20 -0
- data/manual/example_file.rb +111 -0
- data/manual/example_helper.rb +411 -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 +30 -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 +24 -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 +48 -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 +36 -0
- data/manual/manual/foreword.rb +85 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +34 -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/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 +54 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/group.rb +31 -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/{examples/m17n → manual/text}/win_ansi_charset.rb +14 -10
- data/prawn.gemspec +27 -17
- data/spec/acceptance/png.rb +23 -0
- data/spec/annotations_spec.rb +16 -32
- data/spec/bounding_box_spec.rb +284 -2
- data/spec/cell_spec.rb +169 -38
- data/spec/column_box_spec.rb +65 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +5 -5
- data/spec/document_spec.rb +212 -113
- data/spec/extensions/encoding_helpers.rb +9 -0
- data/spec/extensions/mocha.rb +2 -3
- data/spec/font_metric_cache_spec.rb +52 -0
- data/spec/font_spec.rb +205 -95
- data/spec/formatted_text_arranger_spec.rb +43 -43
- data/spec/formatted_text_box_spec.rb +63 -24
- data/spec/formatted_text_fragment_spec.rb +8 -8
- data/spec/graphics_spec.rb +175 -68
- data/spec/grid_spec.rb +26 -15
- data/spec/image_handler_spec.rb +54 -0
- data/spec/images_spec.rb +58 -30
- data/spec/inline_formatted_text_parser_spec.rb +73 -19
- data/spec/jpg_spec.rb +4 -4
- data/spec/line_wrap_spec.rb +28 -28
- data/spec/measurement_units_spec.rb +6 -6
- data/spec/object_store_spec.rb +17 -106
- data/spec/outline_spec.rb +103 -63
- data/spec/png_spec.rb +25 -25
- data/spec/reference_spec.rb +8 -65
- data/spec/repeater_spec.rb +25 -11
- data/spec/security_spec.rb +44 -12
- data/spec/snapshot_spec.rb +38 -6
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +10 -15
- data/spec/spec_helper.rb +32 -8
- data/spec/stamp_spec.rb +29 -30
- data/spec/stroke_styles_spec.rb +36 -18
- data/spec/table/span_dummy_spec.rb +17 -0
- data/spec/table_spec.rb +850 -104
- data/spec/text_at_spec.rb +19 -33
- data/spec/text_box_spec.rb +117 -64
- data/spec/text_rendering_mode_spec.rb +5 -5
- data/spec/text_spacing_spec.rb +20 -2
- data/spec/text_spec.rb +111 -59
- data/spec/transparency_spec.rb +5 -5
- metadata +477 -328
- data/HACKING +0 -50
- data/README +0 -141
- 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/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/examples/bounding_box/bounding_boxes.rb +0 -44
- data/examples/bounding_box/indentation.rb +0 -35
- data/examples/bounding_box/russian_boxes.rb +0 -37
- data/examples/bounding_box/stretched_nesting.rb +0 -68
- data/examples/example_helper.rb +0 -8
- data/examples/general/background.rb +0 -24
- data/examples/general/canvas.rb +0 -16
- data/examples/general/context_sensitive_headers.rb +0 -38
- data/examples/general/float.rb +0 -12
- data/examples/general/margin.rb +0 -37
- data/examples/general/measurement_units.rb +0 -52
- data/examples/general/metadata-info.rb +0 -17
- data/examples/general/multi_page_layout.rb +0 -19
- data/examples/general/outlines.rb +0 -67
- data/examples/general/page_geometry.rb +0 -32
- data/examples/general/page_numbering.rb +0 -40
- data/examples/general/page_templates.rb +0 -20
- data/examples/general/repeaters.rb +0 -48
- data/examples/general/stamp.rb +0 -42
- data/examples/general/templates.rb +0 -14
- data/examples/graphics/basic_images.rb +0 -24
- data/examples/graphics/cmyk.rb +0 -13
- data/examples/graphics/curves.rb +0 -12
- data/examples/graphics/gradient.rb +0 -23
- data/examples/graphics/hexagon.rb +0 -14
- data/examples/graphics/image_fit.rb +0 -16
- data/examples/graphics/image_flow.rb +0 -38
- data/examples/graphics/image_position.rb +0 -18
- data/examples/graphics/line.rb +0 -33
- data/examples/graphics/png_types.rb +0 -23
- data/examples/graphics/polygons.rb +0 -17
- data/examples/graphics/remote_images.rb +0 -13
- data/examples/graphics/rounded_polygons.rb +0 -20
- data/examples/graphics/rounded_rectangle.rb +0 -21
- data/examples/graphics/ruport_style_helpers.rb +0 -20
- data/examples/graphics/stroke_bounds.rb +0 -21
- data/examples/graphics/stroke_cap_and_join.rb +0 -46
- data/examples/graphics/stroke_dash.rb +0 -43
- data/examples/graphics/transformations.rb +0 -53
- data/examples/graphics/transparency.rb +0 -27
- data/examples/grid/bounding_boxes.rb +0 -22
- data/examples/grid/column_gutter_grid.rb +0 -21
- data/examples/grid/multi_boxes.rb +0 -52
- data/examples/grid/show_grid.rb +0 -14
- data/examples/grid/simple_grid.rb +0 -21
- data/examples/m17n/chinese_text_wrapping.rb +0 -18
- data/examples/m17n/euro.rb +0 -16
- data/examples/m17n/full_win_ansi_character_list.rb +0 -20
- data/examples/m17n/sjis.rb +0 -29
- data/examples/m17n/utf8.rb +0 -14
- data/examples/security/hello_foo.rb +0 -9
- data/examples/table/bill.rb +0 -54
- data/examples/table/borders.rb +0 -25
- data/examples/table/cell.rb +0 -13
- data/examples/table/checkerboard.rb +0 -23
- data/examples/table/header.rb +0 -15
- data/examples/table/inline_format_table.rb +0 -13
- data/examples/table/multi_page_table.rb +0 -10
- data/examples/table/simple_table.rb +0 -25
- data/examples/table/subtable.rb +0 -13
- data/examples/table/widths.rb +0 -21
- data/examples/text/alignment.rb +0 -19
- data/examples/text/character_spacing.rb +0 -13
- data/examples/text/dfont.rb +0 -49
- data/examples/text/family_based_styling.rb +0 -25
- data/examples/text/font_calculations.rb +0 -92
- data/examples/text/font_size.rb +0 -34
- data/examples/text/hyphenation.rb +0 -45
- data/examples/text/indent_paragraphs.rb +0 -24
- data/examples/text/inline_format.rb +0 -104
- data/examples/text/kerning.rb +0 -31
- data/examples/text/rendering_mode.rb +0 -21
- data/examples/text/rotated.rb +0 -99
- data/examples/text/shaped_text_box.rb +0 -32
- data/examples/text/simple_text.rb +0 -18
- data/examples/text/simple_text_ttf.rb +0 -18
- data/examples/text/span.rb +0 -30
- data/examples/text/text_box.rb +0 -90
- data/examples/text/text_box_returning_excess.rb +0 -52
- data/examples/text/text_flow.rb +0 -68
- data/lib/prawn/compatibility.rb +0 -51
- data/lib/prawn/core/annotations.rb +0 -61
- data/lib/prawn/core/byte_string.rb +0 -9
- data/lib/prawn/core/destinations.rb +0 -90
- data/lib/prawn/core/document_state.rb +0 -78
- data/lib/prawn/core/literal_string.rb +0 -16
- data/lib/prawn/core/name_tree.rb +0 -165
- data/lib/prawn/core/object_store.rb +0 -264
- data/lib/prawn/core/page.rb +0 -213
- data/lib/prawn/core/pdf_object.rb +0 -108
- data/lib/prawn/core/reference.rb +0 -112
- data/lib/prawn/core/text/formatted/arranger.rb +0 -293
- data/lib/prawn/core/text/formatted/line_wrap.rb +0 -272
- data/lib/prawn/core/text/formatted/wrap.rb +0 -149
- data/lib/prawn/core/text.rb +0 -268
- data/lib/prawn/core.rb +0 -85
- data/lib/prawn/document/page_geometry.rb +0 -136
- data/lib/prawn/graphics/gradient.rb +0 -84
- data/spec/name_tree_spec.rb +0 -112
- data/spec/pdf_object_spec.rb +0 -170
- data/spec/template_spec.rb +0 -291
@@ -0,0 +1,266 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# core/text/formatted/line_wrap.rb : Implements individual line wrapping of
|
4
|
+
# formatted text
|
5
|
+
#
|
6
|
+
# Copyright February 2010, Daniel Nelson. All Rights Reserved.
|
7
|
+
#
|
8
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
+
#
|
10
|
+
|
11
|
+
module Prawn
|
12
|
+
module Text
|
13
|
+
module Formatted #:nodoc:
|
14
|
+
|
15
|
+
# @private
|
16
|
+
class LineWrap #:nodoc:
|
17
|
+
|
18
|
+
# The width of the last wrapped line
|
19
|
+
#
|
20
|
+
def width
|
21
|
+
@accumulated_width || 0
|
22
|
+
end
|
23
|
+
|
24
|
+
# The number of spaces in the last wrapped line
|
25
|
+
attr_reader :space_count
|
26
|
+
|
27
|
+
# Whether this line is the last line in the paragraph
|
28
|
+
def paragraph_finished?
|
29
|
+
@newline_encountered || is_next_string_newline? || @arranger.finished?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Work in conjunction with the PDF::Formatted::Arranger
|
33
|
+
# defined in the :arranger option to determine what formatted text
|
34
|
+
# will fit within the width defined by the :width option
|
35
|
+
#
|
36
|
+
def wrap_line(options)
|
37
|
+
initialize_line(options)
|
38
|
+
|
39
|
+
while fragment = @arranger.next_string
|
40
|
+
@fragment_output = ""
|
41
|
+
|
42
|
+
fragment.lstrip! if first_fragment_on_this_line?(fragment)
|
43
|
+
next if empty_line?(fragment)
|
44
|
+
|
45
|
+
unless apply_font_settings_and_add_fragment_to_line(fragment)
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@arranger.finalize_line
|
50
|
+
@accumulated_width = @arranger.line_width
|
51
|
+
@space_count = @arranger.space_count
|
52
|
+
@arranger.line
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def first_fragment_on_this_line?(fragment)
|
58
|
+
line_empty? && fragment != "\n"
|
59
|
+
end
|
60
|
+
|
61
|
+
def empty_line?(fragment)
|
62
|
+
empty = line_empty? && fragment.empty? && is_next_string_newline?
|
63
|
+
@arranger.update_last_string("", "", soft_hyphen) if empty
|
64
|
+
empty
|
65
|
+
end
|
66
|
+
|
67
|
+
def is_next_string_newline?
|
68
|
+
@arranger.preview_next_string == "\n"
|
69
|
+
end
|
70
|
+
|
71
|
+
def apply_font_settings_and_add_fragment_to_line(fragment)
|
72
|
+
result = nil
|
73
|
+
@arranger.apply_font_settings do
|
74
|
+
# if font has changed from Unicode to non-Unicode, or vice versa, the characters used for soft hyphens
|
75
|
+
# and zero-width spaces will be different
|
76
|
+
set_soft_hyphen_and_zero_width_space
|
77
|
+
result = add_fragment_to_line(fragment)
|
78
|
+
end
|
79
|
+
result
|
80
|
+
end
|
81
|
+
|
82
|
+
# returns true iff all text was printed without running into the end of
|
83
|
+
# the line
|
84
|
+
#
|
85
|
+
def add_fragment_to_line(fragment)
|
86
|
+
if fragment == ""
|
87
|
+
true
|
88
|
+
elsif fragment == "\n"
|
89
|
+
@newline_encountered = true
|
90
|
+
false
|
91
|
+
else
|
92
|
+
fragment.scan(scan_pattern).each do |segment|
|
93
|
+
if segment == zero_width_space
|
94
|
+
segment_width = 0
|
95
|
+
else
|
96
|
+
segment_width = @document.width_of(segment, :kerning => @kerning)
|
97
|
+
end
|
98
|
+
|
99
|
+
if @accumulated_width + segment_width <= @width
|
100
|
+
@accumulated_width += segment_width
|
101
|
+
@fragment_output += segment
|
102
|
+
else
|
103
|
+
end_of_the_line_reached(segment)
|
104
|
+
fragment_finished(fragment)
|
105
|
+
return false
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
fragment_finished(fragment)
|
110
|
+
true
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# The pattern used to determine chunks of text to place on a given line
|
115
|
+
#
|
116
|
+
def scan_pattern
|
117
|
+
pattern = "[^#{break_chars}]+#{soft_hyphen}|" +
|
118
|
+
"[^#{break_chars}]+#{hyphen}+|" +
|
119
|
+
"[^#{break_chars}]+|" +
|
120
|
+
"[#{whitespace}]+|" +
|
121
|
+
"#{hyphen}+[^#{break_chars}]*|" +
|
122
|
+
"#{soft_hyphen}"
|
123
|
+
Regexp.new(pattern)
|
124
|
+
end
|
125
|
+
|
126
|
+
# The pattern used to determine whether any word breaks exist on a
|
127
|
+
# current line, which in turn determines whether character level
|
128
|
+
# word breaking is needed
|
129
|
+
#
|
130
|
+
def word_division_scan_pattern
|
131
|
+
Regexp.new("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
|
132
|
+
end
|
133
|
+
|
134
|
+
def break_chars
|
135
|
+
"#{whitespace}#{soft_hyphen}#{hyphen}"
|
136
|
+
end
|
137
|
+
|
138
|
+
def whitespace
|
139
|
+
" \\t#{zero_width_space}"
|
140
|
+
end
|
141
|
+
|
142
|
+
def hyphen
|
143
|
+
"-"
|
144
|
+
end
|
145
|
+
|
146
|
+
def soft_hyphen
|
147
|
+
@soft_hyphen
|
148
|
+
end
|
149
|
+
|
150
|
+
def zero_width_space
|
151
|
+
@zero_width_space
|
152
|
+
end
|
153
|
+
|
154
|
+
def line_empty?
|
155
|
+
@line_empty && @accumulated_width == 0
|
156
|
+
end
|
157
|
+
|
158
|
+
def initialize_line(options)
|
159
|
+
@document = options[:document]
|
160
|
+
@kerning = options[:kerning]
|
161
|
+
@width = options[:width]
|
162
|
+
|
163
|
+
@accumulated_width = 0
|
164
|
+
@line_empty = true
|
165
|
+
@line_contains_more_than_one_word = false
|
166
|
+
|
167
|
+
@arranger = options[:arranger]
|
168
|
+
@arranger.initialize_line
|
169
|
+
|
170
|
+
@newline_encountered = false
|
171
|
+
@line_full = false
|
172
|
+
end
|
173
|
+
|
174
|
+
def set_soft_hyphen_and_zero_width_space
|
175
|
+
# this is done once per fragment, after the font settings for the fragment are applied --
|
176
|
+
# it could actually be skipped if the font hasn't changed
|
177
|
+
font = @document.font
|
178
|
+
@soft_hyphen = font.normalize_encoding(Prawn::Text::SHY)
|
179
|
+
@zero_width_space = font.unicode? ? Prawn::Text::ZWSP : ""
|
180
|
+
end
|
181
|
+
|
182
|
+
def fragment_finished(fragment)
|
183
|
+
if fragment == "\n"
|
184
|
+
@newline_encountered = true
|
185
|
+
@line_empty = false
|
186
|
+
else
|
187
|
+
update_output_based_on_last_fragment(fragment, soft_hyphen)
|
188
|
+
update_line_status_based_on_last_output
|
189
|
+
determine_whether_to_pull_preceding_fragment_to_join_this_one(fragment)
|
190
|
+
end
|
191
|
+
remember_this_fragment_for_backward_looking_ops
|
192
|
+
end
|
193
|
+
|
194
|
+
def update_output_based_on_last_fragment(fragment, normalized_soft_hyphen=nil)
|
195
|
+
remaining_text = fragment.slice(@fragment_output.length..fragment.length)
|
196
|
+
raise Prawn::Errors::CannotFit if line_finished? && line_empty? &&
|
197
|
+
@fragment_output.empty? && !fragment.strip.empty?
|
198
|
+
@arranger.update_last_string(@fragment_output, remaining_text, normalized_soft_hyphen)
|
199
|
+
end
|
200
|
+
|
201
|
+
def determine_whether_to_pull_preceding_fragment_to_join_this_one(current_fragment)
|
202
|
+
if @fragment_output.empty? &&
|
203
|
+
!current_fragment.empty? &&
|
204
|
+
@line_contains_more_than_one_word
|
205
|
+
unless previous_fragment_ended_with_breakable? ||
|
206
|
+
fragment_begins_with_breakable?(current_fragment)
|
207
|
+
@fragment_output = @previous_fragment_output_without_last_word
|
208
|
+
update_output_based_on_last_fragment(@previous_fragment)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def remember_this_fragment_for_backward_looking_ops
|
214
|
+
@previous_fragment = @fragment_output.dup
|
215
|
+
pf = @previous_fragment
|
216
|
+
@previous_fragment_ended_with_breakable = pf =~ /[#{break_chars}]$/
|
217
|
+
last_word = pf.slice(/[^#{break_chars}]*$/)
|
218
|
+
last_word_length = last_word.nil? ? 0 : last_word.length
|
219
|
+
@previous_fragment_output_without_last_word = pf.slice(0, pf.length - last_word_length)
|
220
|
+
end
|
221
|
+
|
222
|
+
def previous_fragment_ended_with_breakable?
|
223
|
+
@previous_fragment_ended_with_breakable
|
224
|
+
end
|
225
|
+
|
226
|
+
def fragment_begins_with_breakable?(fragment)
|
227
|
+
fragment =~ /^[#{break_chars}]/
|
228
|
+
end
|
229
|
+
|
230
|
+
def line_finished?
|
231
|
+
@line_full || paragraph_finished?
|
232
|
+
end
|
233
|
+
|
234
|
+
def update_line_status_based_on_last_output
|
235
|
+
@line_contains_more_than_one_word = true if @fragment_output =~ word_division_scan_pattern
|
236
|
+
end
|
237
|
+
|
238
|
+
def end_of_the_line_reached(segment)
|
239
|
+
update_line_status_based_on_last_output
|
240
|
+
wrap_by_char(segment) unless @line_contains_more_than_one_word
|
241
|
+
@line_full = true
|
242
|
+
end
|
243
|
+
|
244
|
+
def wrap_by_char(segment)
|
245
|
+
font = @document.font
|
246
|
+
segment.each_char do |char|
|
247
|
+
break unless append_char(char,font)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def append_char(char,font)
|
252
|
+
# kerning doesn't make sense in the context of a single character
|
253
|
+
char_width = font.compute_width_of(char)
|
254
|
+
|
255
|
+
if @accumulated_width + char_width <= @width
|
256
|
+
@accumulated_width += char_width
|
257
|
+
@fragment_output << char
|
258
|
+
true
|
259
|
+
else
|
260
|
+
false
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
@@ -13,8 +13,9 @@ module Prawn
|
|
13
13
|
module Formatted
|
14
14
|
|
15
15
|
class Parser
|
16
|
+
# @group Extension API
|
16
17
|
|
17
|
-
|
18
|
+
PARSER_REGEX = begin
|
18
19
|
regex_string = "\n|" +
|
19
20
|
"<b>|</b>|" +
|
20
21
|
"<i>|</i>|" +
|
@@ -29,8 +30,11 @@ module Prawn
|
|
29
30
|
"<em>|</em>|" +
|
30
31
|
"<a[^>]*>|</a>|" +
|
31
32
|
"[^<\n]+"
|
32
|
-
|
33
|
-
|
33
|
+
Regexp.new(regex_string, Regexp::MULTILINE)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.format(string, *args)
|
37
|
+
tokens = string.gsub(/<br\s*\/?>/, "\n").scan(PARSER_REGEX)
|
34
38
|
self.array_from_tokens(tokens)
|
35
39
|
end
|
36
40
|
|
@@ -122,10 +126,11 @@ module Prawn
|
|
122
126
|
colors = []
|
123
127
|
link = nil
|
124
128
|
anchor = nil
|
129
|
+
local = nil
|
125
130
|
fonts = []
|
126
131
|
sizes = []
|
127
132
|
character_spacings = []
|
128
|
-
|
133
|
+
|
129
134
|
while token = tokens.shift
|
130
135
|
case token
|
131
136
|
when "<b>", "<strong>"
|
@@ -155,6 +160,7 @@ module Prawn
|
|
155
160
|
when "</link>", "</a>"
|
156
161
|
link = nil
|
157
162
|
anchor = nil
|
163
|
+
local = nil
|
158
164
|
when "</color>"
|
159
165
|
colors.pop
|
160
166
|
when "</font>"
|
@@ -168,6 +174,9 @@ module Prawn
|
|
168
174
|
|
169
175
|
matches = /anchor="([^"]*)"/.match(token) || /anchor='([^']*)'/.match(token)
|
170
176
|
anchor = matches[1] unless matches.nil?
|
177
|
+
|
178
|
+
matches = /local="([^"]*)"/.match(token) || /local='([^']*)'/.match(token)
|
179
|
+
local = matches[1] unless matches.nil?
|
171
180
|
elsif token =~ /^<color[^>]*>$/
|
172
181
|
matches = /rgb="#?([^"]*)"/.match(token) || /rgb='#?([^']*)'/.match(token)
|
173
182
|
colors << matches[1] if matches
|
@@ -179,7 +188,7 @@ module Prawn
|
|
179
188
|
# intend to support rgb="#ffffff" or rgb='#ffffff',
|
180
189
|
# r="255" g="255" b="255" or r='255' g='255' b='255',
|
181
190
|
# and c="100" m="100" y="100" k="100" or
|
182
|
-
# c='100' m='100' y='100' k='100'
|
191
|
+
# c='100' m='100' y='100' k='100'
|
183
192
|
# color = { :rgb => "#ffffff" }
|
184
193
|
# color = { :r => 255, :g => 255, :b => 255 }
|
185
194
|
# color = { :c => 100, :m => 100, :y => 100, :k => 100 }
|
@@ -197,6 +206,7 @@ module Prawn
|
|
197
206
|
array << { :text => string,
|
198
207
|
:styles => styles.dup,
|
199
208
|
:color => colors.last,
|
209
|
+
:local => local,
|
200
210
|
:link => link,
|
201
211
|
:anchor => anchor,
|
202
212
|
:font => fonts.last,
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require_relative "line_wrap"
|
2
|
+
require_relative "arranger"
|
3
|
+
|
4
|
+
module Prawn
|
5
|
+
module Text
|
6
|
+
module Formatted #:nodoc:
|
7
|
+
# @private
|
8
|
+
|
9
|
+
module Wrap #:nodoc:
|
10
|
+
|
11
|
+
def initialize(array, options)
|
12
|
+
@line_wrap = Prawn::Text::Formatted::LineWrap.new
|
13
|
+
@arranger = Prawn::Text::Formatted::Arranger.new(@document,
|
14
|
+
:kerning => options[:kerning])
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
# See the developer documentation for PDF::Core::Text#wrap
|
19
|
+
#
|
20
|
+
# Formatted#wrap should set the following variables:
|
21
|
+
# <tt>@line_height</tt>::
|
22
|
+
# the height of the tallest fragment in the last printed line
|
23
|
+
# <tt>@descender</tt>::
|
24
|
+
# the descender height of the tallest fragment in the last
|
25
|
+
# printed line
|
26
|
+
# <tt>@ascender</tt>::
|
27
|
+
# the ascender heigth of the tallest fragment in the last
|
28
|
+
# printed line
|
29
|
+
# <tt>@baseline_y</tt>::
|
30
|
+
# the baseline of the current line
|
31
|
+
# <tt>@nothing_printed</tt>::
|
32
|
+
# set to true until something is printed, then false
|
33
|
+
# <tt>@everything_printed</tt>::
|
34
|
+
# set to false until everything printed, then true
|
35
|
+
#
|
36
|
+
# Returns any formatted text that was not printed
|
37
|
+
#
|
38
|
+
def wrap(array) #:nodoc:
|
39
|
+
initialize_wrap(array)
|
40
|
+
|
41
|
+
stop = false
|
42
|
+
while !stop
|
43
|
+
# wrap before testing if enough height for this line because the
|
44
|
+
# height of the highest fragment on this line will be used to
|
45
|
+
# determine the line height
|
46
|
+
@line_wrap.wrap_line(:document => @document,
|
47
|
+
:kerning => @kerning,
|
48
|
+
:width => available_width,
|
49
|
+
:arranger => @arranger)
|
50
|
+
|
51
|
+
if enough_height_for_this_line?
|
52
|
+
move_baseline_down
|
53
|
+
print_line
|
54
|
+
else
|
55
|
+
stop = true
|
56
|
+
end
|
57
|
+
|
58
|
+
stop ||= @single_line || @arranger.finished?
|
59
|
+
end
|
60
|
+
@text = @printed_lines.join("\n")
|
61
|
+
@everything_printed = @arranger.finished?
|
62
|
+
@arranger.unconsumed
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def print_line
|
68
|
+
@nothing_printed = false
|
69
|
+
printed_fragments = []
|
70
|
+
fragments_this_line = []
|
71
|
+
|
72
|
+
word_spacing = word_spacing_for_this_line
|
73
|
+
while fragment = @arranger.retrieve_fragment
|
74
|
+
fragment.word_spacing = word_spacing
|
75
|
+
if fragment.text == "\n"
|
76
|
+
printed_fragments << "\n" if @printed_lines.last == ""
|
77
|
+
break
|
78
|
+
end
|
79
|
+
printed_fragments << fragment.text
|
80
|
+
fragments_this_line << fragment
|
81
|
+
end
|
82
|
+
|
83
|
+
accumulated_width = 0
|
84
|
+
fragments_this_line.reverse! if @direction == :rtl
|
85
|
+
fragments_this_line.each do |fragment_this_line|
|
86
|
+
fragment_this_line.default_direction = @direction
|
87
|
+
format_and_draw_fragment(fragment_this_line, accumulated_width,
|
88
|
+
@line_wrap.width, word_spacing)
|
89
|
+
accumulated_width += fragment_this_line.width
|
90
|
+
end
|
91
|
+
|
92
|
+
@printed_lines << printed_fragments.map { |s| s.force_encoding(::Encoding::UTF_8) }.join
|
93
|
+
end
|
94
|
+
|
95
|
+
def word_spacing_for_this_line
|
96
|
+
if @align == :justify &&
|
97
|
+
@line_wrap.space_count > 0 &&
|
98
|
+
!@line_wrap.paragraph_finished?
|
99
|
+
(available_width - @line_wrap.width) / @line_wrap.space_count
|
100
|
+
else
|
101
|
+
0
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def enough_height_for_this_line?
|
106
|
+
@line_height = @arranger.max_line_height
|
107
|
+
@descender = @arranger.max_descender
|
108
|
+
@ascender = @arranger.max_ascender
|
109
|
+
if @baseline_y == 0
|
110
|
+
diff = @ascender + @descender
|
111
|
+
else
|
112
|
+
diff = @descender + @line_height + @leading
|
113
|
+
end
|
114
|
+
require_relatived_total_height = @baseline_y.abs + diff
|
115
|
+
if require_relatived_total_height > @height + 0.0001
|
116
|
+
# no room for the full height of this line
|
117
|
+
@arranger.repack_unretrieved
|
118
|
+
false
|
119
|
+
else
|
120
|
+
true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def initialize_wrap(array)
|
125
|
+
@text = nil
|
126
|
+
@arranger.format_array = array
|
127
|
+
|
128
|
+
# these values will depend on the maximum value within a given line
|
129
|
+
@line_height = 0
|
130
|
+
@descender = 0
|
131
|
+
@ascender = 0
|
132
|
+
@baseline_y = 0
|
133
|
+
|
134
|
+
@printed_lines = []
|
135
|
+
@nothing_printed = true
|
136
|
+
@everything_printed = false
|
137
|
+
end
|
138
|
+
|
139
|
+
def format_and_draw_fragment(fragment, accumulated_width,
|
140
|
+
line_width, word_spacing)
|
141
|
+
@arranger.apply_color_and_font_settings(fragment) do
|
142
|
+
draw_fragment(fragment, accumulated_width,
|
143
|
+
line_width, word_spacing)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
data/lib/prawn/text/formatted.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
require_relative "formatted/wrap"
|
2
|
+
|
3
|
+
require_relative "formatted/box"
|
4
|
+
require_relative "formatted/parser"
|
5
|
+
require_relative "formatted/fragment"
|