Class: CTioga2::Graphics::Types::AlignedPoint

Inherits:
Point
  • Object
show all
Defined in:
lib/ctioga2/graphics/types/point.rb

Overview

A Point, but with alignment facilities.

Constant Summary collapse

AlignmentSpecification =
{
  'r' => :right,
  'c' => :center,
  'l' => :left,
  't' => :top,
  'b' => :bottom
}

Instance Attribute Summary collapse

Attributes inherited from Point

#x, #y

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Point

#to_figure_xy, #to_frame_xy

Constructor Details

#initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) ⇒ AlignedPoint

Creates a AlignedPoint



127
128
129
130
131
132
# File 'lib/ctioga2/graphics/types/point.rb', line 127

def initialize(x = nil, y = nil, type = :figure, 
               halign = :center, valign = :center)
  super(x,y,type)
  @halign = halign
  @valign = valign
end

Instance Attribute Details

#halignObject

Horizontal alignment (:left, :center, :right)



124
125
126
# File 'lib/ctioga2/graphics/types/point.rb', line 124

def halign
  @halign
end

#valignObject

Vertical alignement (:top, :center, :bottom)



121
122
123
# File 'lib/ctioga2/graphics/types/point.rb', line 121

def valign
  @valign
end

Class Method Details

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

Creates an AlignedPoint object from a text specification. Splits up the text at a comma and



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ctioga2/graphics/types/point.rb', line 191

def self.from_text(text, default = :figure)
  if not text =~ /^\s*([btlrc]{2})(?::([^,]+),\s*(.*))?\s*$/
    raise "Invalid format for aligned point: #{text}"
  end

  specs = $1
  specs = specs.chars.map {|x| 
    AlignmentSpecification.fetch(x.downcase)
  }
  coord = AlignedPoint.new
  if specs[0] == :center
    specs.reverse!
  end
  case specs[0]
  when :center
    coord.halign = :center
    coord.valign = :center
  when :left, :right
    coord.halign = specs[0]
    coord.valign = specs[1]
  when :top, :bottom
    coord.valign = specs[0]
    coord.halign = specs[1]
  end

  if $2
    x_spec,y_spec = $2,$3
    coord.x = BaseCoordinate.from_text(x_spec, :x, default)
    coord.y = BaseCoordinate.from_text(y_spec, :y, default)
  else
    case coord.halign
    when :center
      coord.x = BaseCoordinate.new(:frame, 0.5, :x)
    when :left
      coord.x = BaseCoordinate.new(:frame, 0.05, :x)
    when :right
      coord.x = BaseCoordinate.new(:frame, 0.95, :x)
    end

    case coord.valign
    when :center
      coord.y = BaseCoordinate.new(:frame, 0.5, :y)
    when :bottom
      coord.y = BaseCoordinate.new(:frame, 0.05, :y)
    when :top
      coord.y = BaseCoordinate.new(:frame, 0.95, :y)
    end
  end
  return coord
end

Instance Method Details

#to_frame_coordinates(t, width, height) ⇒ Object

Returns frame coordinates corresponding to the point, the alignment and the given size in figure coordinates



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ctioga2/graphics/types/point.rb', line 136

def to_frame_coordinates(t, width, height)
  dx = t.convert_figure_to_frame_dx(width).abs
  dy = t.convert_figure_to_frame_dy(height).abs
  x,y = self.to_frame_xy(t)

  case @valign
  when :top
    yt = y
    yb = y - dy
  when :bottom
    yt = y + dy
    yb = y
  when :center
    yt = y + dy/2
    yb = y - dy/2
  else 
    raise "Unknown vertical alignment: #{@valign}"
  end

  case @halign
  when :right
    xl = x - dx
    xr = x
  when :left
    xl = x
    xr = x + dx
  when :center
    xl = x - dx/2
    xr = x + dx/2
  else 
    raise "Unknown horizontal alignment: #{@halign}"
  end
  return [xl, yt, xr, yb]
end

#to_frame_margins(t, width, height) ⇒ Object

Returns a frame_margin corresponding to the point, the alignment and the given size in figure coordinates.

See #to_frame_coordinates



175
176
177
178
# File 'lib/ctioga2/graphics/types/point.rb', line 175

def to_frame_margins(t, width, height)
  xl, yt, xr, yb = to_frame_coordinates(t, width, height)
  return [xl,1 - xr, 1 - yt, yb]
end