Class: CTioga2::Graphics::Types::Dimension

Inherits:
Object
  • Object
show all
Includes:
Tioga::Utils
Defined in:
lib/ctioga2/graphics/types/dimensions.rb

Overview

A Dimension is an object that represents a dimension in the different ways understood by Tioga:

  • an “absolute” dimension, ie, in real units (postscript points)

  • a “text” dimension, in units of the height of the current text object

  • a frame/page/figure dimension, in units of the current frame/page/figure coordinates

Direct Known Subclasses

BaseCoordinate

Constant Summary collapse

DimensionConversion =

Dimension conversion constants taken straight from the TeXbook

{
  "pt" => (72.0/72.27),
  "bp" => 1.0,
  "in" => 72.0,
  "cm" => (72.0/2.54),
  "mm" => (72.0/25.4),
}
DimensionRegexp =

A regular expression that matches all dimensions.

/^\s*([+-]?\s*[\d.eE+-]+)\s*([a-zA-Z]+)?\s*$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value, orientation = :x) ⇒ Dimension

Creates a Dimension object of the given type, the given value oriented along the given orientation



54
55
56
57
58
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 54

def initialize(type, value, orientation = :x)
  @type = type
  @value = value
  @orientation = orientation
end

Instance Attribute Details

#orientationObject

The orientation of the dimension: vertical (:y) or horizontal (:x) ?



46
47
48
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 46

def orientation
  @orientation
end

#typeObject

What is the underlying representation of the dimension ?

  • :bp in postscript points

  • :dy in text height units

  • :frame in frame coordinates

  • :page in page coordinates

  • :figure in figure coordinates



42
43
44
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 42

def type
  @type
end

#valueObject

The actual dimension. The interpretation depends on the value of #type.



50
51
52
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 50

def value
  @value
end

Class Method Details

.from_text(text, orientation, default = :figure) ⇒ Object

Creates a Dimension object from a text specification. The text should be in the forms

value unit

where unit is one of bp, pt, in, mm, cm, dy (the latter being one unit of height) f|figure, F|Frame|frame, p|page. It can be ommitted, in which case it defaults to the default parameter.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 127

def self.from_text(text, orientation, default = :figure)
  # Absolute or :dy dimension
  if text =~ DimensionRegexp
    value = Float($1)
    unit = $2
    if ! unit
      unit = default
    elsif DimensionConversion.key?(unit.downcase)
      value *= DimensionConversion[unit.downcase]
      unit = :bp
    else
      case unit
      when /^dy$/i
        unit = :dy
      when /^F|(?i:frame)$/
        unit = :frame
      when /^f|(?i:figure)$/
        unit = :figure
      when /^p|(?i:page)$/
        unit = :page
      else
        raise "Unkown dimension unit: #{unit}"
      end
    end
    return Dimension.new(unit, value, orientation)
  else
    raise "Unkown Dimension specification: '#{text}'"
  end
end

Instance Method Details

#replace_if_bigger(t, dimension) ⇒ Object

Replace this Dimension by dimension if the latter is bigger. Conserves the current orientation.



97
98
99
100
101
102
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 97

def replace_if_bigger(t, dimension)
  if self.to_figure(t) < dimension.to_figure(t, @orientation)
    @type = dimension.type
    @value = dimension.value
  end
end

#to_figure(t, orientation = nil) ⇒ Object

Converts the Dimension to the figure coordinates of the current figure in t.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 62

def to_figure(t, orientation = nil)
  orientation ||= @orientation
  case @type
  when :bp
    return t.send("convert_output_to_figure_d#{orientation}", @value) * 10
  when :dy
    return t.send("default_text_height_d#{orientation}") * @value
  when :frame
    return t.send("convert_frame_to_figure_d#{orientation}", @value)
  when :page
    return t.send("convert_page_to_figure_d#{orientation}", @value)
  when :figure
    return @value
  else
    raise "Invalid type for Dimension: #{@type}"
  end
end

#to_frame(t, orientation = nil) ⇒ Object

Converts the Dimension to the frame coordinates of the current frame in t.



82
83
84
85
86
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 82

def to_frame(t, orientation = nil)
  orientation ||= @orientation
  return t.send("convert_figure_to_frame_d#{orientation}", 
                to_figure(t, orientation))
end

#to_text_height(t, orientation = nil) ⇒ Object

Express the Dimension in units of text height (dy)



89
90
91
92
93
# File 'lib/ctioga2/graphics/types/dimensions.rb', line 89

def to_text_height(t, orientation = nil)
  orientation ||= @orientation
  return self.to_figure(t, orientation)/
    t.send("default_text_height_d#{orientation}")
end