Class: CTioga::Dimension
- Inherits:
-
Object
- Object
- CTioga::Dimension
- Includes:
- Tioga::Utils
- Defined in:
- lib/CTioga/dimension.rb
Overview
A class which represents a dimension, either absolute or relative.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#type ⇒ Object
The type of the dimension : * :abs means absolute * :rel is relative.
-
#value ⇒ Object
The actual dimension.
Class Method Summary collapse
-
.absolute_to_relative(dimensions, frames) ⇒ Object
Converts an absolute dimension in terms of a relative one, using the given frames.
- .update_extensions(extensions, new_ext) ⇒ Object
Instance Method Summary collapse
-
#initialize(spec, type = nil) ⇒ Dimension
constructor
Creates a dimension.
-
#scale!(fact) ⇒ Object
Scales the dimension by the given factor.
-
#to_absolute(frames, side = :horizontal) ⇒ Object
Converts self to an absolute dimension.
-
#to_relative(frames, side = :horizontal) ⇒ Object
Converts self to a relative dimension.
Constructor Details
#initialize(spec, type = nil) ⇒ Dimension
Creates a dimension. If the dimension contains a letter, it will be interpreted in terms of a TeX size. If it contains a percent - or nothing, it will be interpreted as a relative measure. If it is a float, it will be interpreted as a relative measure unless type is not nil.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/CTioga/dimension.rb', line 48 def initialize(spec, type = nil) case spec when /^\s*[\d.]+[a-zA-Z]+\s*$/ @type = :abs @value = tex_dimension_to_bp(spec) when /^\s*([\d.]+)\s*%\s*$/ @type = :rel @value = $1.to_f * 0.01 # Value in percents when Float @value = spec @type = type || :rel when /^\s*([\d.]+)/ @value = spec.to_f @type = type || :rel end end |
Instance Attribute Details
#type ⇒ Object
The type of the dimension :
-
:abs means absolute
-
:rel is relative
34 35 36 |
# File 'lib/CTioga/dimension.rb', line 34 def type @type end |
#value ⇒ Object
The actual dimension. Its meaning depends on the value of type:
-
:abs : the value is in bp points
-
:rel : the value is a fraction of the corresponding size in the given plot.
41 42 43 |
# File 'lib/CTioga/dimension.rb', line 41 def value @value end |
Class Method Details
.absolute_to_relative(dimensions, frames) ⇒ Object
Converts an absolute dimension in terms of a relative one, using the given frames.
98 99 100 101 102 103 104 105 |
# File 'lib/CTioga/dimension.rb', line 98 def Dimension.absolute_to_relative(dimensions, frames) ret_val = [] ret_val[0] = dimensions[0]/((frames[1] - frames[0]).abs) ret_val[1] = dimensions[1]/((frames[1] - frames[0]).abs) ret_val[2] = dimensions[2]/((frames[3] - frames[2]).abs) ret_val[3] = dimensions[3]/((frames[3] - frames[2]).abs) return ret_val end |
.update_extensions(extensions, new_ext) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/CTioga/dimension.rb', line 107 def self.update_extensions(extensions, new_ext) 4.times do |i| extensions[i] = new_ext[i] unless new_ext[i] < extensions[i] end return extensions end |
Instance Method Details
#scale!(fact) ⇒ Object
Scales the dimension by the given factor
90 91 92 93 |
# File 'lib/CTioga/dimension.rb', line 90 def scale!(fact) @value *= fact return self end |
#to_absolute(frames, side = :horizontal) ⇒ Object
Converts self to an absolute dimension. frames is the frames of the current object (outer_frames) expressed in big points.
79 80 81 82 83 84 85 86 87 |
# File 'lib/CTioga/dimension.rb', line 79 def to_absolute(frames, side = :horizontal) # The easy thing first: return @value if @type == :abs if side == :horizontal return @value * ((frames[1] - frames[0]).abs) else return @value * ((frames[3] - frames[2]).abs) end end |
#to_relative(frames, side = :horizontal) ⇒ Object
Converts self to a relative dimension. frames is the frames of the current object (outer_frames) expressed in big points.
67 68 69 70 71 72 73 74 75 |
# File 'lib/CTioga/dimension.rb', line 67 def to_relative(frames, side = :horizontal) # The easy thing first: return @value if @type == :rel if side == :horizontal return @value/((frames[1] - frames[0]).abs) else return @value/((frames[3] - frames[2]).abs) end end |