File: units.rb

package info (click to toggle)
ruby-rghost 0.9.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,188 kB
  • sloc: ruby: 3,374; makefile: 6; sh: 1
file content (70 lines) | stat: -rw-r--r-- 1,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require "rghost/function"
require "rghost/ps_object"
# The postscript default unit is the 1/72th inch. RGhost uses the centimeters (cm) as it's default, mainly for positioning coordinates and numeric object sizes (if the size is a String, it won't be parsed to the default unit). This setting can be changed setting the value of RGhost::Config::GS[:unit] before the document is created using any of the Units child classes.
#===Example:
#
#====Setting to inches.
# RGhost::Config::GS[:unit]=Units::Inch
# doc.moveto :x => 1, :y => 2  #=> 1 inch x 2 inches
#====Explicitly setting to Cm.
# doc.moveto :x => '1 cm' , :y => '2 cm'
#====Using the Postscript unit
# doc.moveto :x => '100' , :y => '200'
module RGhost::Units
  class Unit < RGhost::PsObject
    attr_accessor :value

    def initialize(value = 0)
      @value = value
    end

    def ps
      "#{@value} #{self.class.to_s.gsub("RGhost::Units::", "").downcase} "
    end

    def self.define
      ""
    end
  end

  # n/72 * 2.545
  class Cm < Unit
    def self.define
      Function.new("cm", "72 div 2.545 mul")
    end
  end

  # n*72
  class Inch < Unit
    def self.define
      RGhost::Function.new("inch", "72 mul")
    end
  end

  class PSUnit < Unit
    def ps
      "#{@value} "
    end
  end

  # Parses units
  # Units::DEFAULT=Utits::Cm
  # Units.parse(2)                #=> "2 cm"
  # Units.parse(:current_row)     #=> "current_row"
  # Units.parse("3 inch")         #=> "3 inch"
  # Units.parse("2")              #=> "2"
  #
  # Using US metric
  #  Units::DEFAULT=Utits::Inch
  #  Units.parse(2)               #=> "2 inch"
  #  Units.parse(:current_row)    #=> "current_row"
  #  Units.parse("3 cm")          #=> "3 cm"
  #  Units.parse("5")             #=> "5"
  def self.parse(value)
    if value.is_a? Numeric
      RGhost::Config::GS[:unit].new(value)
    else
      RGhost::PsObject.new(value)
    end
  end
end