Class: CTioga2::Graphics::Types::AlignedPoint
- 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
-
#halign ⇒ Object
Horizontal alignment (:left, :center, :right).
-
#valign ⇒ Object
Vertical alignement (:top, :center, :bottom).
Attributes inherited from Point
Class Method Summary collapse
-
.from_text(text, default = :figure) ⇒ Object
Creates an AlignedPoint object from a text specification.
Instance Method Summary collapse
-
#initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) ⇒ AlignedPoint
constructor
Creates a AlignedPoint.
-
#to_frame_coordinates(t, width, height) ⇒ Object
Returns frame coordinates corresponding to the point, the alignment and the given size in figure coordinates.
-
#to_frame_margins(t, width, height) ⇒ Object
Returns a frame_margin corresponding to the point, the alignment and the given size in figure coordinates.
Methods inherited from Point
Constructor Details
#initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) ⇒ AlignedPoint
Creates a AlignedPoint
211 212 213 214 215 216 |
# File 'lib/ctioga2/graphics/types/point.rb', line 211 def initialize(x = nil, y = nil, type = :figure, halign = :center, valign = :center) super(x,y,type) @halign = halign @valign = valign end |
Instance Attribute Details
#halign ⇒ Object
Horizontal alignment (:left, :center, :right)
208 209 210 |
# File 'lib/ctioga2/graphics/types/point.rb', line 208 def halign @halign end |
#valign ⇒ Object
Vertical alignement (:top, :center, :bottom)
205 206 207 |
# File 'lib/ctioga2/graphics/types/point.rb', line 205 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
275 276 277 278 279 280 281 282 283 284 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 |
# File 'lib/ctioga2/graphics/types/point.rb', line 275 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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/ctioga2/graphics/types/point.rb', line 220 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
259 260 261 262 |
# File 'lib/ctioga2/graphics/types/point.rb', line 259 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 |