Class: HexaPDF::Content::GraphicObject::Geom2D

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/content/graphic_object/geom2d.rb

Overview

This class provides support for drawing Geom2D objects like line segments and polygons.

By default, the paths for the objects are not only added to the canvas but are also stroked or filled (depending on the specific Geom2D object).

Supported Geom2D objects are:

  • Geom2D::Point

  • Geom2D::Segment

  • Geom2D::Polygon

  • Geom2D::PolygonSet

Examples:

#>pdf-center
canvas.draw(:geom2d, object: ::Geom2D::Point(-10, 10))
canvas.draw(:geom2d, object: ::Geom2D::Polygon([10, 10], [30, 20], [0, 50]))

See: Geom2D - github.com/gettalong/geom2d

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGeom2D

Creates a Geom2D drawing support object.

A call to #configure is mandatory afterwards to set the #object to be drawn.



99
100
101
102
103
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 99

def initialize
  @object = nil
  @point_radius = 1
  @path_only = false
end

Instance Attribute Details

#objectObject

The Geom2D object that should be drawn.

This attribute must be set before drawing.



75
76
77
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 75

def object
  @object
end

#path_onlyObject

Specifies whether only paths should be drawn or if they should be stroked/filled too (the default).

Examples:

#>pdf-center
canvas.draw(:geom2d, object: ::Geom2D::Segment([0, 0], [0, 50]))
canvas.draw(:geom2d, object: ::Geom2D::Segment([0, 0], [50, 0]), path_only: true)


94
95
96
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 94

def path_only
  @path_only
end

#point_radiusObject

The radius to use when drawing Geom2D::Point objects, defaults to 1.

Examples:

#>pdf-center
canvas.draw(:geom2d, object: ::Geom2D::Point(0, 0))
canvas.draw(:geom2d, object: ::Geom2D::Point(50, 0), point_radius: 5)


84
85
86
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 84

def point_radius
  @point_radius
end

Class Method Details

.configure(**kwargs) ⇒ Object

Creates and configures a new Geom2D drawing support object.

See #configure for the allowed keyword arguments.



68
69
70
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 68

def self.configure(**kwargs)
  new.configure(**kwargs)
end

Instance Method Details

#configure(object: nil, point_radius: nil, path_only: nil) ⇒ Object

Configures the Geom2D drawing support object. The following arguments are allowed:

:object

The object that should be drawn. If this argument has not been set before and is also not given, an error will be raised when calling #draw.

:point_radius

The radius of the points when drawing points.

:path_only

Whether only the path should be drawn.

Any arguments not specified are not modified and retain their old value, see the getter methods for the inital values.

Returns self.

Examples:

#>pdf-center
obj = canvas.graphic_object(:geom2d, object: ::Geom2D::Point(0, 0))
canvas.draw(obj)
canvas.opacity(fill_alpha: 0.5).fill_color("hp-blue").
  draw(obj, point_radius: 10)


124
125
126
127
128
129
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 124

def configure(object: nil, point_radius: nil, path_only: nil)
  @object = object if object
  @point_radius = point_radius if point_radius
  @path_only = path_only if path_only
  self
end

#draw(canvas) ⇒ Object

Draws the Geom2D object onto the given Canvas.

Examples:

#>pdf-center
obj = canvas.graphic_object(:geom2d, object: ::Geom2D::Point(0, 0))
obj.draw(canvas)


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/hexapdf/content/graphic_object/geom2d.rb', line 138

def draw(canvas)
  case @object
  when ::Geom2D::Point then draw_point(canvas)
  when ::Geom2D::Segment then draw_segment(canvas)
  when ::Geom2D::Rectangle then draw_rectangle(canvas)
  when ::Geom2D::Polygon then draw_polygon(canvas)
  when ::Geom2D::PolygonSet then draw_polygon_set(canvas)
  else
    raise HexaPDF::Error, "Object of type #{@object.class} unusable"
  end
end