[cairo-commit] rcairo/src/lib/cairo paper.rb, 1.3, 1.4 papers.rb, 1.2, 1.3

Kouhei Sutou commit at pdx.freedesktop.org
Thu Apr 10 21:41:22 PDT 2008


Committed by: kou

Update of /cvs/cairo/rcairo/src/lib/cairo
In directory kemper:/tmp/cvs-serv29559/src/lib/cairo

Modified Files:
	paper.rb papers.rb 
Log Message:
* src/lib/cairo/papers.rb: add landscape papers.
* test/test_paper.rb, src/lib/cairo/paper.rb: support name parsing.
* src/lib/cairo/paper.rb: improve unit handling.


Index: paper.rb
===================================================================
RCS file: /cvs/cairo/rcairo/src/lib/cairo/paper.rb,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- paper.rb	11 Apr 2008 02:39:12 -0000	1.3
+++ paper.rb	11 Apr 2008 04:46:08 -0000	1.4
@@ -38,7 +38,7 @@
           raise UnknownPaperName.new(paper_description)
         when String
           paper = resolve_constant(paper_description)
-          paper ||= parse_size(paper_description.gsub(/#.*\z/, ''))
+          paper ||= parse_size(paper_description)
           return paper.dup if paper
         when Array
           return new(*paper_description)
@@ -48,18 +48,36 @@
         nil
       end
 
-      @@unit_resolvers = []
-      def register_unit_resolver(name, *aliases, &resolver)
-        ([name] + aliases).each do |unit|
-          @@unit_resolvers << [unit, resolver]
+      @@default_unit = nil
+      def default_unit
+        @@default_unit
+      end
+
+      def default_unit=(unit)
+        @@default_unit = unit
+      end
+
+      @@unit_resolvers = {}
+      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
 
-      def resolve_unit(size, target_unit)
-        @@unit_resolvers.each do |unit, resolver|
-          return resolver.call(size) if target_unit == unit
+      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(target_unit)
+        raise UnknownUnit.new(to_unit)
       end
 
       private
@@ -75,7 +93,9 @@
       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
+        if /\A#{size_re}#{unit_re}?x#{size_re}#{unit_re}?(?:#(.*))?\z/ !~ size
+          return nil
+        end
 
         width = $1
         width_fractional = $2
@@ -83,56 +103,61 @@
         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),
-            resolve_unit(height, height_unit))
+        new(resolve_unit(width, width_unit, "pt"),
+            resolve_unit(height, height_unit, "pt"),
+            nil, name)
       end
     end
 
-    register_unit_resolver(nil) {|size| size}
-    register_unit_resolver("pt") {|size| size}
-    register_unit_resolver("in", "inch") {|size| size * 72}
-    register_unit_resolver("mm") {|size| size / 25.4 * 72}
-    register_unit_resolver("cm") {|size| size / 2.54 * 72}
-    register_unit_resolver("m") {|size| size / 0.0254 * 72}
+    self.default_unit = "pt"
+    register_unit_resolver("pt", ["in", "inch"]) {|size| size / 72.0}
+    register_unit_resolver(["in", "inch"], "pt") {|size| size * 72}
+    register_unit_resolver("pt", "mm") {|size| size * 25.4 / 72.0}
+    register_unit_resolver("mm", "pt") {|size| size / 25.4 * 72}
+    register_unit_resolver("pt", "cm") {|size| size * 2.54 / 72.0}
+    register_unit_resolver("cm", "pt") {|size| size / 2.54 * 72}
+    register_unit_resolver("pt", "m") {|size| size * 0.0254 / 72.0}
+    register_unit_resolver("m", "pt") {|size| size / 0.0254 * 72}
 
-    attr_reader :width, :height, :unit
+    attr_reader :unit
+    attr_writer :width, :height
     attr_accessor :name
     def initialize(width, height, unit=nil, name=nil)
+      @width = width
+      @height = height
       @unit = unit
-      self.width = width
-      self.height = height
       @name = name
     end
 
-    def width=(width)
-      @width = self.class.resolve_unit(width, @unit)
-      width
+    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
 
-    def height=(height)
-      @height = self.class.resolve_unit(height, @unit)
-      height
+    def width(unit=nil)
+      return @width if unit.nil?
+      self.class.resolve_unit(@width, @unit, unit)
     end
 
-    def unit=(unit)
-      original_unit = @unit
-      @unit = unit
-      if original_unit != @unit
-        self.width = width
-        self.height = height
-      end
-      unit
+    def height(unit=nil)
+      return @height if unit.nil?
+      self.class.resolve_unit(@height, @unit, unit)
     end
 
-    def size
-      [@width, @height]
+    def size(unit=nil)
+      [width(unit), height(unit)]
     end
 
     def ==(other)
-      other.is_a?(self.class) and
-        [@width, @height, @name] == [other.width, other.height, other.name]
+      other.is_a?(self.class) and @name == other.name and
+        width_in_delta?(other.width(@unit)) and
+        height_in_delta?(other.height(@unit))
     end
 
     def to_s
@@ -140,5 +165,19 @@
       string << "\##{@name}" if @name
       string
     end
+
+    private
+    def width_in_delta?(value, delta=nil)
+      in_delta?(@width, delta, value)
+    end
+
+    def height_in_delta?(value, delta=nil)
+      in_delta?(@height, delta, value)
+    end
+
+    def in_delta?(value, delta, other)
+      delta ||= 0.001
+      value - delta < other and other < value + delta
+    end
   end
 end

Index: papers.rb
===================================================================
RCS file: /cvs/cairo/rcairo/src/lib/cairo/papers.rb,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- papers.rb	11 Apr 2008 03:02:01 -0000	1.2
+++ papers.rb	11 Apr 2008 04:46:08 -0000	1.3
@@ -2,33 +2,40 @@
 
 module Cairo
   class Paper
-    A0 = new(841, 1189, "mm", "A0")
-    A1 = new(594, 841, "mm", "A1")
-    A2 = new(420, 594, "mm", "A2")
-    A3 = new(297, 420, "mm", "A3")
-    A4 = new(210, 297, "mm", "A4")
-    A5 = new(148, 210, "mm", "A5")
-    A6 = new(105, 148, "mm", "A6")
-    A7 = new(74, 105, "mm", "A7")
-    A8 = new(52, 74, "mm", "A8")
-    A9 = new(37, 52, "mm", "A9")
-    A10 = new(26, 37, "mm", "A10")
+    [
+     [841, 1189, "A0"],
+     [594, 841, "A1"],
+     [420, 594, "A2"],
+     [297, 420, "A3"],
+     [210, 297, "A4"],
+     [148, 210, "A5"],
+     [105, 148, "A6"],
+     [74, 105, "A7"],
+     [52, 74, "A8"],
+     [37, 52, "A9"],
+     [26, 37, "A10"],
 
-    B0 = new(1030, 1456, "mm", "B0")
-    B1 = new(728, 1030, "mm", "B1")
-    B2 = new(515, 728, "mm", "B2")
-    B3 = new(364, 515, "mm", "B3")
-    B4 = new(257, 364, "mm", "B4")
-    B5 = new(182, 257, "mm", "B5")
-    B6 = new(128, 182, "mm", "B6")
-    B7 = new(91, 128, "mm", "B7")
-    B8 = new(64, 91, "mm", "B8")
-    B9 = new(45, 64, "mm", "B9")
-    B10 = new(32, 45, "mm", "B10")
+     [1030, 1456, "B0"],
+     [728, 1030, "B1"],
+     [515, 728, "B2"],
+     [364, 515, "B3"],
+     [257, 364, "B4"],
+     [182, 257, "B5"],
+     [128, 182, "B6"],
+     [91, 128, "B7"],
+     [64, 91, "B8"],
+     [45, 64, "B9"],
+     [32, 45, "B10"],
 
-    LETTER = new(215.9, 279.4, "mm", "letter")
-    LEGAL = new(215.9, 355.6, "mm", "legal")
-    TABLOID = new(279.9, 431.8, "mm", "tabloid")
-    JAPANESE_POSTCARD = new(100, 148, "mm", "Japanese postcard")
+     [215.9, 279.4, "letter"],
+     [215.9, 355.6, "legal"],
+     [279.9, 431.8, "tabloid"],
+     [100, 148, "Japanese postcard"],
+    ].each do |width, height, name|
+      constant_name = name.upcase.gsub(/ +/, '_')
+      const_set(constant_name, new(width, height, "mm", name))
+      const_set("#{constant_name}_LANDSCAPE",
+                new(height, width, "mm", "#{name} (landscape)"))
+    end
   end
 end



More information about the cairo-commit mailing list