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

Kouhei Sutou commit at pdx.freedesktop.org
Thu Apr 10 19:34:25 PDT 2008


Committed by: kou

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

Modified Files:
	paper.rb 
Log Message:
* src/lib/cairo/paper.rb, test/test_paper.rb: use pt as default unit.


Index: paper.rb
===================================================================
RCS file: /cvs/cairo/rcairo/src/lib/cairo/paper.rb,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- paper.rb	9 Apr 2008 06:28:53 -0000	1.2
+++ paper.rb	11 Apr 2008 02:39:12 -0000	1.3
@@ -49,8 +49,17 @@
       end
 
       @@unit_resolvers = []
-      def register_unit_resolver(name, resolver=Proc.new)
-        @@unit_resolvers << [name, resolver]
+      def register_unit_resolver(name, *aliases, &resolver)
+        ([name] + aliases).each do |unit|
+          @@unit_resolvers << [unit, resolver]
+        end
+      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
 
       private
@@ -79,26 +88,44 @@
         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}
+    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}
 
-    attr_accessor :width, :height, :name
-    def initialize(width, height, name=nil)
-      @width = width
-      @height = height
+    attr_reader :width, :height, :unit
+    attr_accessor :name
+    def initialize(width, height, unit=nil, name=nil)
+      @unit = unit
+      self.width = width
+      self.height = height
       @name = name
     end
 
+    def width=(width)
+      @width = self.class.resolve_unit(width, @unit)
+      width
+    end
+
+    def height=(height)
+      @height = self.class.resolve_unit(height, @unit)
+      height
+    end
+
+    def unit=(unit)
+      original_unit = @unit
+      @unit = unit
+      if original_unit != @unit
+        self.width = width
+        self.height = height
+      end
+      unit
+    end
+
     def size
       [@width, @height]
     end
@@ -109,7 +136,7 @@
     end
 
     def to_s
-      string = "#{@width}x#{@height}"
+      string = "#{@width}#{@unit}x#{@height}#{@unit}"
       string << "\##{@name}" if @name
       string
     end



More information about the cairo-commit mailing list