Sha256: d7e4e795b66fc120e2a9c39526d688c8f90bdeca985ba75a850b977ddc1fa647

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 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 #:nodoc:
    module Wrapping #:nodoc:                
      ruby_18 { $KCODE="U" }
      
      # TODO: Replace with TeX optimal algorithm
      def naive_wrap(string, line_width, font_size, options = {})
        accumulated_width = 0
        scan_pattern = options[:mode] == :character ? /./ : /\S+|\s+/                                    
        
        output = ""                
        string.lines.each do |line|         
          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.rstrip}\n"
              
              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

1 entries across 1 versions & 1 rubygems

Version Path
prawn-0.2.0 lib/prawn/font/wrapping.rb