Class: Inkmake::InkscapeUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/inkmake.rb

Constant Summary collapse

Units =

90dpi as reference

{
  "pt" => 1.25,
  "pc" => 15,
  "mm" => 3.543307,
  "cm" => 35.43307,
  "dm" => 354.3307,
  "m"  => 3543.307,
  "in" => 90,
  "ft" => 1080,
  "uu" => 1 # user unit, 90 dpi
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, unit = "uu") ⇒ InkscapeUnit

Returns a new instance of InkscapeUnit.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/inkmake.rb', line 70

def initialize(value, unit="uu")
  case value
  when /^(\d+(?:\.\d+)?)(\w+)?$/ then
    @value = $1.to_f
    @unit = $2
    @unit ||= unit
    @unit = (@unit == "px" or Units.has_key?(@unit)) ? @unit : "uu"
  else
    @value = value.kind_of?(String) ? value.to_f: value
    @unit = unit
  end
end

Instance Attribute Details

#unitObject (readonly)

Returns the value of attribute unit.



68
69
70
# File 'lib/inkmake.rb', line 68

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



68
69
70
# File 'lib/inkmake.rb', line 68

def value
  @value
end

Instance Method Details

#scale(f) ⇒ Object



92
93
94
95
# File 'lib/inkmake.rb', line 92

def scale(f)
  return self if @unit == "px"
  InkscapeUnit.new(@value * f, @unit)
end

#to_pixels(dpi = 90.0) ⇒ Object



83
84
85
86
# File 'lib/inkmake.rb', line 83

def to_pixels(dpi=90.0)
  return @value.round if @unit == "px"
  ((dpi / 90.0) * Units[@unit] * @value).round
end

#to_sObject



88
89
90
# File 'lib/inkmake.rb', line 88

def to_s
  "%g#{@unit}" % @value
end