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

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