Skip to content

Commit

Permalink
Ruby 1.9 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed May 14, 2009
1 parent 6535e05 commit ef9787c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/pdf/techbook.rb
Expand Up @@ -534,7 +534,7 @@ def techbook_parse(document, progress = nil)

__build_xref_table(document)

document.each do |line|
document.each_line do |line|
begin
progress.inc if progress
@techbook_line__ += 1
Expand Down Expand Up @@ -854,7 +854,7 @@ def self.run(args)
# class program, then regenerate.
if (_tm_doc < _tm_cch) and (_tm_prg < _tm_cch)
$stderr.puts PDF::Writer::Lang[:techbook_using_cached_doc] % File.basename(files[:cache])
pdf = File.open(files[:cache], "rb") { |cf| Marshal.load(cf.read) }
pdf = File.open(files[:cache], "rb:binary") { |cf| Marshal.load(cf.read) }
pdf.save_as(files[:pdf])
File.open(files[:pdf], "wb") { |pf| pf.write pdf.render }
exit(0)
Expand Down
8 changes: 4 additions & 4 deletions lib/pdf/writer.rb
Expand Up @@ -709,7 +709,7 @@ def render(debug = false)

xref = []

content = "%PDF-#{@version}\n%âãÏÓ\n"
content = "%PDF-#{@version}\n%\303\242\303\243\303\217\303\223\n"
pos = content.size

objects.each do |oo|
Expand Down Expand Up @@ -1466,7 +1466,7 @@ def add_text(x, y, text, size = nil, angle = 0, word_space_adjust = 0)
end

def char_width(font, char)
char = char[0] unless @fonts[font].c[char]
char = char.bytes.to_a.first unless @fonts[font].c[char]

if @fonts[font].differences and @fonts[font].c[char].nil?
name = @fonts[font].differences[char] || 'M'
Expand Down Expand Up @@ -1563,7 +1563,7 @@ def text_width(text, size = nil)

max = 0

text.to_s.each do |line|
text.to_s.each_line do |line|
width = text_line_width(line, size)
max = width if width > max
end
Expand Down Expand Up @@ -2372,7 +2372,7 @@ def text(text, options = {})
height = font_height(size)
end

text.each do |line|
text.each_line do |line|
start = true
loop do # while not line.empty? or start
break if (line.nil? or line.empty?) and not start
Expand Down
2 changes: 1 addition & 1 deletion lib/pdf/writer/fontmetrics.rb
Expand Up @@ -78,7 +78,7 @@ def self.open(font_name)
font = PDF::Writer::FontMetrics.new

# An AFM file contains key names followed by valuees.
file.each do |line|
file.each_line do |line|
line.chomp!
line.strip!
key, *values = line.split
Expand Down
2 changes: 1 addition & 1 deletion lib/pdf/writer/graphics.rb
Expand Up @@ -543,7 +543,7 @@ def add_image_from_file(image, x, y, width = nil, height = nil, link = nil)
if image.respond_to?(:read)
data = image.read
else
open(image, 'rb') { |ff| data = ff.read }
open(image,'rb:binary') { |ff| data = ff.read }
end

add_image(data, x, y, width, height, nil, link)
Expand Down
7 changes: 6 additions & 1 deletion lib/pdf/writer/object/contents.rb
Expand Up @@ -13,7 +13,8 @@ class PDF::Writer::Object::Contents < PDF::Writer::Object
def initialize(parent, page = nil)
super(parent)

@data = ""
@data = RUBY_VERSION < '1.9' ? "" : "".force_encoding("BINARY")

@info = {}
@raw = false
@on_page = nil
Expand All @@ -38,6 +39,10 @@ def each

def <<(v)
raise TypeError unless v.kind_of?(PDF::Writer::Object) or v.kind_of?(String)
if RUBY_VERSION >= '1.9'
@data.force_encoding 'BINARY'
v.force_encoding 'BINARY'
end
@data << v
end

Expand Down

0 comments on commit ef9787c

Please sign in to comment.