class CTioga2::Graphics::Types::AlignedPoint
A Point
, but with alignment facilities.
Constants
- AlignmentSpecification
Attributes
halign[RW]
Horizontal alignment (:left, :center, :right)
valign[RW]
Vertical alignement (:top, :center, :bottom)
Public Class Methods
from_point(pt, halign = :center, valign = :center)
click to toggle source
# 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)
click to toggle source
Creates an AlignedPoint
object from a text specification. Splits up the text at a comma and
# 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
new(x = nil, y = nil, type = :figure, halign = :center, valign = :center)
click to toggle source
Creates a AlignedPoint
Calls superclass method
CTioga2::Graphics::Types::Point::new
# 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
Public Instance Methods
to_frame_coordinates(t, width, height)
click to toggle source
Returns frame coordinates corresponding to the point, the alignment and the given size in figure coordinates
# 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)
click to toggle source
Returns a frame_margin corresponding to the point, the alignment and the given size in figure coordinates.
# 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