class Cairo::Paper

Attributes

height[W]
name[RW]
unit[R]
width[W]

Public Class Methods

default_unit() click to toggle source
# File lib/cairo/paper.rb, line 52
def default_unit
  @@default_unit
end
default_unit=(unit) click to toggle source
# File lib/cairo/paper.rb, line 56
def default_unit=(unit)
  @@default_unit = unit
end
new(width, height, unit=nil, name=nil) click to toggle source
# File lib/cairo/paper.rb, line 128
def initialize(width, height, unit=nil, name=nil)
  @width = width
  @height = height
  @unit = unit
  @name = name
end
parse(paper_description, robust=false) click to toggle source
# File lib/cairo/paper.rb, line 31
def parse(paper_description, robust=false)
  case paper_description
  when Paper
    return paper_description.dup
  when Symbol
    paper = resolve_constant(paper_description)
    return paper.dup if paper
    raise UnknownPaperName.new(paper_description)
  when String
    paper = resolve_constant(paper_description)
    paper ||= parse_size(paper_description)
    return paper.dup if paper
  when Array
    return new(*paper_description)
  end

  raise UnrecognizedPaperDescription.new(paper_description) if robust
  nil
end
register_unit_resolver(from_units, to_units, &resolver) click to toggle source
# File lib/cairo/paper.rb, line 61
def register_unit_resolver(from_units, to_units, &resolver)
  from_units = [from_units] unless from_units.is_a?(Array)
  to_units = [to_units] unless to_units.is_a?(Array)
  from_units.each do |from_unit|
    @@unit_resolvers[from_unit] ||= []
    to_units.each do |unit|
      @@unit_resolvers[from_unit] << [unit, resolver]
    end
  end
end
resolve_unit(size, from_unit, to_unit) click to toggle source
# File lib/cairo/paper.rb, line 72
def resolve_unit(size, from_unit, to_unit)
  from_unit ||= default_unit
  return size if from_unit == to_unit
  from_units = @@unit_resolvers[from_unit] || []
  raise UnknownUnit.new(from_unit) if from_units.empty?
  from_units.each do |unit, resolver|
    return resolver.call(size) if to_unit == unit
  end
  raise UnknownUnit.new(to_unit)
end

Private Class Methods

parse_size(size) click to toggle source
# File lib/cairo/paper.rb, line 93
def parse_size(size)
  size_re = /(\d+(\.\d*)?)/
  unit_re = /([a-zA-Z]+?)/
  if /\A#{size_re}#{unit_re}?x#{size_re}#{unit_re}?(?:#(.*))?\z/ !~ size
    return nil
  end

  width = $1
  width_fractional = $2
  width_unit = $3
  height = $4
  height_fractional = $5
  height_unit = $6
  name = $7
  width = width_fractional ? Float(width) : Integer(width)
  height = height_fractional ?  Float(height) : Integer(height)
  new(resolve_unit(width, width_unit, "pt"),
      resolve_unit(height, height_unit, "pt"),
      nil, name)
end
resolve_constant(name) click to toggle source
# File lib/cairo/paper.rb, line 84
def resolve_constant(name)
  name = name.to_s.upcase.gsub(/\s+|-/, '_')
  if /\A[A-Z]/ =~ name and const_defined?(name)
    const_get(name)
  else
    nil
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/cairo/paper.rb, line 157
def ==(other)
  other.is_a?(self.class) and @name == other.name and
    width_in_delta?(other.width(@unit)) and
    height_in_delta?(other.height(@unit))
end
height(unit=nil) click to toggle source
# File lib/cairo/paper.rb, line 148
def height(unit=nil)
  return @height if unit.nil?
  self.class.resolve_unit(@height, @unit, unit)
end
size(unit=nil) click to toggle source
# File lib/cairo/paper.rb, line 153
def size(unit=nil)
  [width(unit), height(unit)]
end
to_s() click to toggle source
# File lib/cairo/paper.rb, line 163
def to_s
  string = "#{@width}#{@unit}x#{@height}#{@unit}"
  string << "\##{@name}" if @name
  string
end
unit=(unit) click to toggle source
# File lib/cairo/paper.rb, line 135
def unit=(unit)
  if @unit != unit
    @width = self.class.resolve_unit(width, @unit, unit)
    @height = self.class.resolve_unit(height, @unit, unit)
  end
  @unit = unit
end
width(unit=nil) click to toggle source
# File lib/cairo/paper.rb, line 143
def width(unit=nil)
  return @width if unit.nil?
  self.class.resolve_unit(@width, @unit, unit)
end

Private Instance Methods

height_in_delta?(value, delta=nil) click to toggle source
# File lib/cairo/paper.rb, line 174
def height_in_delta?(value, delta=nil)
  in_delta?(@height, delta, value)
end
in_delta?(value, delta, other) click to toggle source
# File lib/cairo/paper.rb, line 178
def in_delta?(value, delta, other)
  delta ||= 0.001
  value - delta < other and other < value + delta
end
width_in_delta?(value, delta=nil) click to toggle source
# File lib/cairo/paper.rb, line 170
def width_in_delta?(value, delta=nil)
  in_delta?(@width, delta, value)
end