File: wrap.rb

package info (click to toggle)
ruby-prawn 1.0.0~rc1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,248 kB
  • sloc: ruby: 17,499; sh: 44; makefile: 17
file content (153 lines) | stat: -rw-r--r-- 5,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require "prawn/core/text/formatted/line_wrap"
require "prawn/core/text/formatted/arranger"

module Prawn
  module Core
    module Text
      module Formatted #:nodoc:
        module Wrap #:nodoc:

          def initialize(array, options)
            @line_wrap = Prawn::Core::Text::Formatted::LineWrap.new
            @arranger = Prawn::Core::Text::Formatted::Arranger.new(@document,
              :kerning => options[:kerning])
          end
          

          # See the developer documentation for Prawn::Core::Text#wrap
          #
          # Formatted#wrap should set the following variables:
          #   <tt>@line_height</tt>::
          #        the height of the tallest fragment in the last printed line
          #   <tt>@descender</tt>::
          #        the descender height of the tallest fragment in the last
          #        printed line
          #   <tt>@ascender</tt>::
          #        the ascender heigth of the tallest fragment in the last
          #        printed line
          #   <tt>@baseline_y</tt>::
          #       the baseline of the current line
          #   <tt>@nothing_printed</tt>::
          #       set to true until something is printed, then false
          #   <tt>@everything_printed</tt>::
          #       set to false until everything printed, then true
          #
          # Returns any formatted text that was not printed
          #
          def wrap(array) #:nodoc:
            initialize_wrap(array)

            stop = false
            while !stop
              # wrap before testing if enough height for this line because the
              # height of the highest fragment on this line will be used to
              # determine the line height
              @line_wrap.wrap_line(:document => @document,
                                   :kerning => @kerning,
                                   :width => available_width,
                                   :arranger => @arranger)

              if enough_height_for_this_line?
                move_baseline_down
                print_line
              else
                stop = true
              end

              stop ||= @single_line || @arranger.finished?
            end
            @text = @printed_lines.join("\n")
            @everything_printed = @arranger.finished?
            @arranger.unconsumed
          end

          private

          def print_line
            @nothing_printed = false
            printed_fragments = []
            fragments_this_line = []

            word_spacing = word_spacing_for_this_line
            while fragment = @arranger.retrieve_fragment
              fragment.word_spacing = word_spacing
              if fragment.text == "\n"
                printed_fragments << "\n" if @printed_lines.last == ""
                break
              end
              printed_fragments << fragment.text
              fragments_this_line << fragment
            end

            accumulated_width = 0
            fragments_this_line.reverse! if @direction == :rtl
            fragments_this_line.each do |fragment|
              fragment.default_direction = @direction
              format_and_draw_fragment(fragment, accumulated_width,
                                       @line_wrap.width, word_spacing)
              accumulated_width += fragment.width
            end

            if "".respond_to?(:force_encoding)
              printed_fragments.map! { |s| s.force_encoding("utf-8") }
            end
            @printed_lines << printed_fragments.join
          end

          def word_spacing_for_this_line
            if @align == :justify &&
                @line_wrap.space_count > 0 &&
                !@line_wrap.paragraph_finished?
              (available_width - @line_wrap.width) / @line_wrap.space_count
            else
              0
            end
          end

          def enough_height_for_this_line?
            @line_height = @arranger.max_line_height
            @descender   = @arranger.max_descender
            @ascender    = @arranger.max_ascender
            if @baseline_y == 0
              diff = @ascender + @descender
            else
              diff = @descender + @line_height + @leading
            end
            required_total_height = @baseline_y.abs + diff
            if required_total_height > @height + 0.0001
              # no room for the full height of this line
              @arranger.repack_unretrieved
              false
            else
              true
            end
          end

          def initialize_wrap(array)
            @text = nil
            @arranger.format_array = array

            # these values will depend on the maximum value within a given line
            @line_height = 0
            @descender   = 0
            @ascender    = 0
            @baseline_y  = 0

            @printed_lines = []
            @nothing_printed = true
            @everything_printed = false
          end

          def format_and_draw_fragment(fragment, accumulated_width,
                                       line_width, word_spacing)
            @arranger.apply_color_and_font_settings(fragment) do
              draw_fragment(fragment, accumulated_width,
                            line_width, word_spacing)
            end
          end

        end
      end
    end
  end
end