lib/podoff.rb in podoff-1.2.4 vs lib/podoff.rb in podoff-1.3.0
- old
+ new
@@ -4,11 +4,11 @@
require 'stringio'
module Podoff
- VERSION = '1.2.4'
+ VERSION = '1.3.0'
def self.load(path, encoding)
Podoff::Document.load(path, encoding)
end
@@ -435,11 +435,11 @@
re = re.obj if re.respond_to?(:obj) # Stream
re = re.ref if re.respond_to?(:ref)
add_to_attribute(:contents, re)
end
- alias :insert_content :insert_contents
+ alias insert_content insert_contents
def to_s
source || stream.to_s
end
@@ -507,37 +507,85 @@
n = font_name[0] == '/' ? font_name[1..-1] : font_name
@font = "/#{n} #{font_size} Tf "
end
- alias :font :tf
+ alias font tf
- def rg(red, green, blue)
+ def rg(*a)
- @color = "#{red} #{green} #{blue} rg "
+ @color = to_rg(a)
end
- alias :color :rg
- alias :rgb :rg
+ alias color rg
+ alias rgb rg
- def bt(x, y, text)
+ def bt(x, y, text, opts={})
return unless text
- @content.write "\n" if @content.size > 0
- @content.write "BT "
- @content.write @font if @font
- @content.write @color if @color
- @content.write "#{x} #{y} Td (#{escape(text)}) Tj"
- @content.write " ET"
+ rgb = opts[:rgb]
+
+ @content << "\n" if @content.size > 0
+ @content << 'BT '
+ @content << @font if @font
+ if rgb
+ @content << to_rg(rgb)
+ elsif @color
+ @content << @color
+ end
+ @content << "#{x} #{y} Td (#{escape(text)}) Tj"
+ @content << ' ET'
end
- alias :text :bt
+ alias text bt
def write(text)
@content.write(text)
end
+ #def re(x, y, w, h, opts={})
+ #def re([ x, y ], [ w, h ], opts={})
+ #def re([ x, y ], opts)
+ #
+ def re(x, *a)
+
+ a = [ x, a ].flatten
+ opts = a.last.is_a?(Hash) ? a.pop : {}
+ x = a.shift; y = a.shift
+
+ rgb = opts[:rgb]
+ w = opts[:width] || opts[:w] || a[0]
+ h = opts[:height] || opts[:h] || a[1]
+
+ @content << "\n" if @content.size > 0
+ @content << to_rg(rgb) if rgb
+ @content << lineup(x, y, w, h) << ' re f'
+ end
+ alias rect re
+ alias rectangle re
+
+ #def line(x0, y0, x1, y1, x2, y2, ..., opts={})
+ #def line([ x0, y0 ], [ x1, y1 ], [ x2, y2 ], ..., opts={})
+ #
+ def line(x0, y0, *a)
+
+ a = [ x0, y0, a ].flatten
+ opts = a.last.is_a?(Hash) ? a.pop : {}
+ x0, y0, *xys = a
+
+ rgb = opts[:rgb] || opts[:rg]
+ w = opts[:width] || opts[:w]
+
+ @content << "\n" if @content.size > 0
+ @content << w.to_s << ' w ' if w
+ @content << to_rg(rgb) if rgb
+ @content << lineup(x0, y0) << ' m '
+ xys.each_slice(2) { |x, y|
+ @content << lineup(x, y, 'l ') }
+ @content << 'h S'
+ end
+
def to_s
s = @content.string
f = ''
if s.length > 98
@@ -550,12 +598,43 @@
"endobj"
end
protected
- def escape(s)
+ def escape(s); s.gsub(/\(/, '\(').gsub(/\)/, '\)'); end
+ def lineup(*a); a.flatten.collect(&:to_s).join(' '); end
- s.gsub(/\(/, '\(').gsub(/\)/, '\)')
+ COLORS = {
+ 'black' => [ 0.0, 0.0, 0.0 ],
+ 'white' => [ 1.0, 1.0, 1.0 ],
+ 'red' => [ 1.0, 0.0, 0.0 ],
+ 'green' => [ 0.0, 1.0, 0.0 ],
+ 'blue' => [ 0.0, 0.0, 1.0 ] }
+
+ def to_rg(a)
+
+ a = a[0].to_s if a.length == 1
+
+ lineup(
+ if a.is_a?(Array) && a.length == 3
+ a
+ elsif a.is_a?(String) && a.match(/^#?([0-9a-z]{3}|[0-9a-z]{6})$/i)
+ hex_to_rgb(a)
+ else
+ COLORS[a] || COLORS['red'] # else, stand out in RED
+ end,
+ 'rg ')
+ end
+
+ def hex_to_rgb(s)
+
+ s = s[1..-1] if s[0, 1] == '#'
+
+ s.chars
+ .each_slice(
+ s.length == 6 ? 2 : 1)
+ .collect { |x|
+ sprintf('%0.4f', (x.join.to_i(16) / 255.0)).gsub(/0+$/, '0') }
end
end
end