[cairo-commit] rcairo/src/lib/cairo paper.rb, NONE, 1.1 papers.rb, NONE, 1.1
Kouhei Sutou
commit at pdx.freedesktop.org
Tue Apr 8 23:11:11 PDT 2008
Committed by: kou
Update of /cvs/cairo/rcairo/src/lib/cairo
In directory kemper:/tmp/cvs-serv31469/src/lib/cairo
Added Files:
paper.rb papers.rb
Log Message:
* src/lib/cairo/paper.rb, src/lib/cairo/papers.rb, test/test_paper.rb: add.
--- NEW FILE: paper.rb ---
module Cairo
class Paper
class ParseError < StandardError
end
class UnknownPaperName < ParseError
attr_reader :name
def initialize(name)
@name = name
super("unknown paper name: #{name}")
end
end
class UnknownUnit < ParseError
attr_reader :unit
def initialize(unit)
@unit = unit
super("unknown unit: #{unit}")
end
end
class << self
def parse(paper_description)
case paper_description
when Symbol
paper = resolve_constant(paper_description)
raise UnknownPaperName.new(paper_description) if paper.nil?
paper
when String
paper = resolve_constant(paper_description)
paper ||= parse_size(paper_description.gsub(/#.*\z/, ''))
end
end
@@unit_resolvers = []
def register_unit_resolver(name, resolver=Proc.new)
@@unit_resolvers << [name, resolver]
end
private
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
def parse_size(size)
size_re = /(\d+(\.\d*)?)/
unit_re = /([a-zA-Z]+?)/
return nil if /\A#{size_re}#{unit_re}?x#{size_re}#{unit_re}?\z/ !~ size
width = $1
width_fractional = $2
width_unit = $3
height = $4
height_fractional = $5
height_unit = $6
width = width_fractional ? Float(width) : Integer(width)
height = height_fractional ? Float(height) : Integer(height)
new(resolve_unit(width, width_unit),
resolve_unit(height, height_unit))
end
def resolve_unit(size, target_unit)
@@unit_resolvers.each do |unit, resolver|
return resolver.call(size) if target_unit == unit
end
raise UnknownUnit.new(target_unit)
end
end
register_unit_resolver(nil) {|size| size}
register_unit_resolver("mm") {|size| size}
register_unit_resolver("inch") {|size| size * 25.4}
attr_accessor :width, :height, :name
def initialize(width, height, name=nil)
@width = width
@height = height
@name = name
end
def size
[@width, @height]
end
def ==(other)
other.is_a?(self.class) and
[@width, @height, @name] == [other.width, other.height, other.name]
end
def to_s
string = "#{@width}x#{@height}"
string << "\##{@name}" if @name
string
end
end
end
--- NEW FILE: papers.rb ---
require 'cairo/paper'
module Cairo
class Paper
A0 = new(841, 1189, "A0")
A1 = new(594, 841, "A1")
A2 = new(420, 594, "A2")
A3 = new(297, 420, "A3")
A4 = new(210, 297, "A4")
A5 = new(148, 210, "A5")
A6 = new(105, 148, "A6")
A7 = new(74, 105, "A7")
A8 = new(52, 74, "A8")
A9 = new(37, 52, "A9")
A10 = new(26, 37, "A10")
B0 = new(1030, 1456, "B0")
B1 = new(728, 1030, "B1")
B2 = new(515, 728, "B2")
B3 = new(364, 515, "B3")
B4 = new(257, 364, "B4")
B5 = new(182, 257, "B5")
B6 = new(128, 182, "B6")
B7 = new(91, 128, "B7")
B8 = new(64, 91, "B8")
B9 = new(45, 64, "B9")
B10 = new(32, 45, "B10")
LETTER = new(215.9, 279.4, "letter")
LEGAL = new(215.9, 355.6, "legal")
TABLOID = new(279.9, 431.8, "tabloid")
JAPANESE_POSTCARD = new(100, 148, "Japanese postcard")
end
end
More information about the cairo-commit
mailing list