Sha256: 0756ed5de4217217d8f15539e7473a1b346b1c9485cc2522a5865830bb5d697b

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

# encoding: utf-8

# wrapping.rb : Implementation of naive text wrap
#
# Copyright May 2008, Michael Daines. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
module Prawn
  class Font
    module Wrapping #:nodoc:                
      ruby_18 { $KCODE="U" }
      
      # TODO: Replace with TeX optimal algorithm
      def naive_wrap(string, line_width, font_size, options = {})
        scan_pattern = options[:mode] == :character ? /./ : /\S+|\s+/                                    
        
        output = ""                
        string.lines.each do |line| 
          accumulated_width = 0        
          segments = line.scan(scan_pattern)
                                        
          segments.each do |segment|    
            segment_width = string_width(segment, font_size, 
              :kerning => options[:kerning]) 
      
            if (accumulated_width + segment_width).round > line_width.round
              output = "#{output.sub(/[ \t]*\n?(\n*)\z/, "\n\\1")}"
              
              if segment =~ /\s/           
                accumulated_width = 0
              else
                output << segment
                accumulated_width = segment_width
              end
            else                           
              output << segment
              accumulated_width += segment_width
            end
          end    
        end
  
        output
      end
      
    end  
  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
fullcirclegroup-fullcirclegroup-prawn-0.2.99.2 lib/prawn/font/wrapping.rb
fullcirclegroup-prawn-0.2.99.3 lib/prawn/font/wrapping.rb
prawn-0.2.1 lib/prawn/font/wrapping.rb
prawn-0.2.2 lib/prawn/font/wrapping.rb
prawn-0.3.0 lib/prawn/font/wrapping.rb
prawn-0.2.3 lib/prawn/font/wrapping.rb