Class: Cairo::Paper

Inherits:
Object
  • Object
show all
Defined in:
lib/cairo/paper.rb,
lib/cairo/papers.rb

Defined Under Namespace

Classes: ParseError, UnknownPaperName, UnknownUnit, UnrecognizedPaperDescription

Constant Summary collapse

@@default_unit =
nil
@@unit_resolvers =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, unit = nil, name = nil) ⇒ Paper

Returns a new instance of Paper.



128
129
130
131
132
133
# File 'lib/cairo/paper.rb', line 128

def initialize(width, height, unit=nil, name=nil)
  @width = width
  @height = height
  @unit = unit
  @name = name
end

Instance Attribute Details

#height(unit = nil) ⇒ Object



148
149
150
151
# File 'lib/cairo/paper.rb', line 148

def height(unit=nil)
  return @height if unit.nil?
  self.class.resolve_unit(@height, @unit, unit)
end

#nameObject

Returns the value of attribute name.



127
128
129
# File 'lib/cairo/paper.rb', line 127

def name
  @name
end

#unitObject

Returns the value of attribute unit.



125
126
127
# File 'lib/cairo/paper.rb', line 125

def unit
  @unit
end

#width(unit = nil) ⇒ Object



143
144
145
146
# File 'lib/cairo/paper.rb', line 143

def width(unit=nil)
  return @width if unit.nil?
  self.class.resolve_unit(@width, @unit, unit)
end

Class Method Details

.default_unitObject



52
53
54
# File 'lib/cairo/paper.rb', line 52

def default_unit
  @@default_unit
end

.default_unit=(unit) ⇒ Object



56
57
58
# File 'lib/cairo/paper.rb', line 56

def default_unit=(unit)
  @@default_unit = unit
end

.parse(paper_description, robust = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object

Raises:



72
73
74
75
76
77
78
79
80
81
# 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

Instance Method Details

#==(other) ⇒ Object



157
158
159
160
161
# 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

#size(unit = nil) ⇒ Object



153
154
155
# File 'lib/cairo/paper.rb', line 153

def size(unit=nil)
  [width(unit), height(unit)]
end

#to_sObject



163
164
165
166
167
# File 'lib/cairo/paper.rb', line 163

def to_s
  string = "#{@width}#{@unit}x#{@height}#{@unit}"
  string << "\##{@name}" if @name
  string
end