Skip to content

Commit

Permalink
Remove Ruby 1.8 specific code
Browse files Browse the repository at this point in the history
  • Loading branch information
pointlessone committed Dec 19, 2013
1 parent 2da2ebd commit ce7147d
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 238 deletions.
9 changes: 0 additions & 9 deletions bench/table_bench.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
if RUBY_VERSION =~ /1\.8/
require "rubygems"
class Array
def sample
self[rand(self.length)]
end
end
end

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require "prawn"
require "benchmark"
Expand Down
18 changes: 3 additions & 15 deletions lib/pdf/core/object_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,21 +299,9 @@ def load_object_graph(hash, object)
end
end

ruby_18 do
def is_utf8?(str)
begin
str.unpack("U*")
true
rescue
false
end
end
end
ruby_19 do
def is_utf8?(str)
str.force_encoding("utf-8")
str.valid_encoding?
end
def is_utf8?(str)
str.force_encoding("utf-8")
str.valid_encoding?
end
end
end
Expand Down
41 changes: 8 additions & 33 deletions lib/pdf/core/pdf_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,15 @@ module PDF
module Core
module_function

if "".respond_to?(:encode)
# Ruby 1.9+
def utf8_to_utf16(str)
"\xFE\xFF".force_encoding("UTF-16BE") + str.encode("UTF-16BE")
end

# encodes any string into a hex representation. The result is a string
# with only 0-9 and a-f characters. That result is valid ASCII so tag
# it as such to account for behaviour of different ruby VMs
def string_to_hex(str)
str.unpack("H*").first.force_encoding("ascii")
end
else
# Ruby 1.8
def utf8_to_utf16(str)
utf16 = "\xFE\xFF"

str.codepoints do |cp|
if cp < 0x10000 # Basic Multilingual Plane
utf16 << [cp].pack("n")
else
# pull out high/low 10 bits
hi, lo = (cp - 0x10000).divmod(2**10)
# encode a surrogate pair
utf16 << [0xD800 + hi, 0xDC00 + lo].pack("n*")
end
end

utf16
end
def utf8_to_utf16(str)
"\xFE\xFF".force_encoding("UTF-16BE") + str.encode("UTF-16BE")
end

def string_to_hex(str)
str.unpack("H*").first
end
# encodes any string into a hex representation. The result is a string
# with only 0-9 and a-f characters. That result is valid ASCII so tag
# it as such to account for behaviour of different ruby VMs
def string_to_hex(str)
str.unpack("H*").first.force_encoding("ascii")
end

# Serializes Ruby objects to their PDF equivalents. Most primitive objects
Expand Down
1 change: 0 additions & 1 deletion lib/prawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def configuration(*args)
end
end

require_relative "prawn/compatibility"
require_relative "prawn/errors"

require_relative "pdf/core"
Expand Down
91 changes: 0 additions & 91 deletions lib/prawn/compatibility.rb

This file was deleted.

3 changes: 1 addition & 2 deletions lib/prawn/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ def render(output = StringIO.new)
# pdf.render_file "foo.pdf"
#
def render_file(filename)
Kernel.const_defined?("Encoding") ? mode = "wb:ASCII-8BIT" : mode = "wb"
File.open(filename,mode) { |f| render(f) }
File.open(filename, "wb:ASCII-8BIT") { |f| render(f) }
end

# The bounds method returns the current bounding box you are currently in,
Expand Down
3 changes: 1 addition & 2 deletions lib/prawn/encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def self.mapping
private

def load_mapping
RUBY_VERSION >= "1.9" ? mode = "r:BINARY" : mode = "r"
File.open(@mapping_file, mode) do |f|
File.open(@mapping_file, "r:BINARY") do |f|
f.each do |l|
_, single_byte, unicode = *l.match(/([0-9A-Za-z]+);([0-9A-F]{4})/)
self.class.mapping["0x#{unicode}".hex] = "0x#{single_byte}".hex if single_byte
Expand Down
12 changes: 10 additions & 2 deletions lib/prawn/font/ttf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ def pdf_flags
end

def normalize_encoding(text)
text.normalize_to_utf8
begin
text.encode(::Encoding::UTF_8)
rescue => e
puts e
raise Prawn::Errors::IncompatibleStringEncoding, "Encoding " +
"#{text.encoding} can not be transparently converted to UTF-8. " +
"Please ensure the encoding of the string you are attempting " +
"to use is set correctly"
end
end

def glyph_present?(char)
Expand All @@ -171,7 +179,7 @@ def glyph_present?(char)
# Returns the number of characters in +str+ (a UTF-8-encoded string).
#
def character_count(str)
str.unicode_length
str.length
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/images/jpg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.can_render?(image_blob)
#
def initialize(data)
@data = data.dup
ruby_19 { data.force_encoding("binary") }
data.force_encoding("binary")
data = StringIO.new(data)

c_marker = "\xff" # Section marker.
Expand Down
6 changes: 1 addition & 5 deletions lib/prawn/text/formatted/arranger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ def line
raise "Lines must be finalized before calling #line"
end
@fragments.collect do |fragment|
if ruby_18 { true }
fragment.text
else
fragment.text.dup.force_encoding("utf-8")
end
fragment.text.dup.force_encoding("utf-8")
end.join
end

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn/text/formatted/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def analyze_glyphs_for_fallback_font_support(hash)
# all fonts
fallback_fonts << fragment_font

hash[:text].unicode_characters do |char|
hash[:text].each_char do |char|
@document.font(fragment_font)
font_glyph_pairs << [find_font_for_this_glyph(char,
@document.font.family,
Expand Down
14 changes: 4 additions & 10 deletions lib/prawn/text/formatted/fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,7 @@ def process_text(text)
end
case direction
when :rtl
if ruby_18 { true }
string.scan(/./mu).reverse.join
else
string.reverse
end
string.reverse
else
string
end
Expand All @@ -232,11 +228,9 @@ def normalized_soft_hyphen

def process_soft_hyphens(string)
if string.length > 0 && normalized_soft_hyphen
ruby_19 {
if string.encoding != normalized_soft_hyphen.encoding
string.force_encoding(normalized_soft_hyphen.encoding)
end
}
if string.encoding != normalized_soft_hyphen.encoding
string.force_encoding(normalized_soft_hyphen.encoding)
end
string[0..-2].gsub(normalized_soft_hyphen, "") + string[-1..-1]
else
string
Expand Down
29 changes: 4 additions & 25 deletions lib/prawn/text/formatted/line_wrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ def scan_pattern
"[#{whitespace}]+|" +
"#{hyphen}+[^#{break_chars}]*|" +
"#{soft_hyphen}"
new_regexp(pattern)
Regexp.new(pattern)
end

# The pattern used to determine whether any word breaks exist on a
# current line, which in turn determines whether character level
# word breaking is needed
#
def word_division_scan_pattern
new_regexp("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
Regexp.new("\\s|[#{zero_width_space}#{soft_hyphen}#{hyphen}]")
end

def break_chars
Expand Down Expand Up @@ -241,18 +241,9 @@ def end_of_the_line_reached(segment)
end

def wrap_by_char(segment)
# this conditional is only necessary for Ruby 1.8 compatibility
# String#unicode_characters is a helper which iterates over UTF-8 characters
# under Ruby 1.9, it is implemented simply by aliasing #each_char
font = @document.font
if font.unicode?
segment.unicode_characters do |char|
break unless append_char(char,font)
end
else
segment.each_char do |char|
break unless append_char(char,font)
end
segment.each_char do |char|
break unless append_char(char,font)
end
end

Expand All @@ -268,18 +259,6 @@ def append_char(char,font)
false
end
end

def new_regexp(pattern)
regexp = ruby_19 {
Regexp.new(pattern)
}
regexp = regexp || ruby_18 {
lang = @document.font.unicode? ? 'U' : 'N'
Regexp.new(pattern, 0, lang)
}
regexp
end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manual/manual/manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Generates the Prawn by example manual.
#

Encoding.default_external = "UTF-8" if defined? Encoding
Encoding.default_external = "UTF-8"

require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))
Expand Down
5 changes: 2 additions & 3 deletions spec/extensions/encoding_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module EncodingHelpers
def win1252_string(str)
ruby_19 { str.force_encoding("Windows-1252") }
str
str.force_encoding("Windows-1252")
end

def bin_string(str)
ruby_19 { str.force_encoding("ASCII-8BIT") } || ruby_18 { str }
str.force_encoding("ASCII-8BIT")
end
end
2 changes: 1 addition & 1 deletion spec/filters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")

FILTERS = {
:FlateDecode => {'test' => ruby_19 { "x\x9C+I-.\x01\x00\x04]\x01\xC1".force_encoding(Encoding::ASCII_8BIT) } || "x\x9C+I-.\x01\x00\x04]\x01\xC1" },
:FlateDecode => {'test' => "x\x9C+I-.\x01\x00\x04]\x01\xC1".force_encoding(Encoding::ASCII_8BIT) },
:DCTDecode => {'test' => "test"}
}

Expand Down
Loading

0 comments on commit ce7147d

Please sign in to comment.