class Podoff::Stream

Constants

COLORS

Attributes

deflate[RW]
obj[RW]

Public Class Methods

new(obj=nil, opts={}) click to toggle source
# File lib/podoff.rb, line 499
def initialize(obj=nil, opts={})

  @obj = obj
  @font = nil
  @color = nil
  @content = StringIO.new
  @deflate = opts.has_key?(:deflate) ? opts[:deflate] : true
end

Public Instance Methods

bt(x, y, text, opts={}) click to toggle source
# File lib/podoff.rb, line 523
def bt(x, y, text, opts={})

  return unless text

  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
Also aliased as: text
color(*a)
Alias for: rg
font(font_name, font_size)
Alias for: tf
line(x0, y0, *a) click to toggle source

def line(x0, y0, x1, y1, x2, y2, …, opts={}) def line([ x0, y0 ], [ x1, y1 ], [ x2, y2 ], …, opts={})

# File lib/podoff.rb, line 571
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
re(x, *a) click to toggle source

def re(x, y, w, h, opts={}) def re([ x, y ], [ w, h ], opts={}) def re([ x, y ], opts)

# File lib/podoff.rb, line 551
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
Also aliased as: rect, rectangle
rect(x, *a)
Alias for: re
rectangle(x, *a)
Alias for: re
rg(*a) click to toggle source
# File lib/podoff.rb, line 516
def rg(*a)

  @color = to_rg(a)
end
Also aliased as: color, rgb
rgb(*a)
Alias for: rg
text(x, y, text, opts={})
Alias for: bt
tf(font_name, font_size) click to toggle source
# File lib/podoff.rb, line 508
def tf(font_name, font_size)

  n = font_name[0] == '/' ? font_name[1..-1] : font_name

  @font = "/#{n} #{font_size} Tf "
end
Also aliased as: font
to_s() click to toggle source
# File lib/podoff.rb, line 589
def to_s

  s = @content.string
  f = ''
  if @deflate && s.length > 98
    f = ' /Filter /FlateDecode'
    s = Zlib::Deflate.deflate(s)
  end

  "#{obj.ref} obj\n" +
  "<</Length #{s.size}#{f}>>\nstream\n#{s}\nendstream\n" +
  "endobj"
end
write(text) click to toggle source
# File lib/podoff.rb, line 542
def write(text)

  @content.write(text)
end

Protected Instance Methods

escape(s) click to toggle source
# File lib/podoff.rb, line 605
def escape(s); s.gsub(/\(/, '\(').gsub(/\)/, '\)'); end
hex_to_rgb(s) click to toggle source
# File lib/podoff.rb, line 630
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
lineup(*a) click to toggle source
# File lib/podoff.rb, line 606
def lineup(*a); a.flatten.collect(&:to_s).join(' '); end
to_rg(a) click to toggle source
# File lib/podoff.rb, line 615
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