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



212
213
214
215
216
217
# File 'lib/ctioga2/graphics/types/point.rb', line 212

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)



209
210
211
# File 'lib/ctioga2/graphics/types/point.rb', line 209

def halign
  @halign
end

#valignObject

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



206
207
208
# File 'lib/ctioga2/graphics/types/point.rb', line 206

def valign
  @valign
end

Class Method Details

.from_point(pt, halign = :center, valign = :center) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/ctioga2/graphics/types/point.rb', line 219

def self.from_point(pt, halign = :center, valign = :center)
  a = AlignedPoint.new
  a.x = pt.x
  a.y = pt.y
  a.halign = halign
  a.valign = valign
  return a
end

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

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



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ctioga2/graphics/types/point.rb', line 285

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/ctioga2/graphics/types/point.rb', line 230

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



269
270
271
272
# File 'lib/ctioga2/graphics/types/point.rb', line 269

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