class HexaPDF::Content::GraphicObject::Geom2D

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

See: Geom2D - github.com/gettalong/geom2d

Attributes

object[RW]

The Geom2D object that should be drawn

path_only[RW]

Specifies whether only paths should be drawn or if they should be stroked/filled too

point_radius[RW]

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

Public Class Methods

configure(**kwargs) click to toggle source

Creates and configures a new Geom2D drawing support object.

See configure for the allowed keyword arguments.

# File lib/hexapdf/content/graphic_object/geom2d.rb, line 51
def self.configure(**kwargs)
  new.configure(**kwargs)
end
new() click to toggle source

Creates a Geom2D drawing support object.

# File lib/hexapdf/content/graphic_object/geom2d.rb, line 65
def initialize
  @object = nil
  @point_radius = 1
  @path_only = false
end

Public Instance Methods

configure(object:, point_radius: nil, path_only: nil) click to toggle source

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

:object

The object that should be drawn.

: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.

# File lib/hexapdf/content/graphic_object/geom2d.rb, line 81
def configure(object:, point_radius: nil, path_only: nil)
  @object = object
  @point_radius = point_radius if point_radius
  @path_only = path_only if path_only
  self
end
draw(canvas) click to toggle source

Draws the Geom2D object onto the given Canvas.

# File lib/hexapdf/content/graphic_object/geom2d.rb, line 89
def draw(canvas)
  case @object
  when ::Geom2D::Point then draw_point(canvas)
  when ::Geom2D::Segment then draw_segment(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

Private Instance Methods

draw_point(canvas) click to toggle source
# File lib/hexapdf/content/graphic_object/geom2d.rb, line 102
def draw_point(canvas)
  canvas.circle(@object.x, @object.y, @point_radius)
  canvas.fill unless @path_only
end
draw_polygon(canvas) click to toggle source
# File lib/hexapdf/content/graphic_object/geom2d.rb, line 113
def draw_polygon(canvas)
  return unless @object.nr_of_vertices > 1
  canvas.move_to(@object[0].x, @object[0].y)
  1.upto(@object.nr_of_vertices - 1) {|i| canvas.line_to(@object[i].x, @object[i].y) }
  canvas.stroke unless @path_only
end
draw_polygon_set(canvas) click to toggle source
# File lib/hexapdf/content/graphic_object/geom2d.rb, line 120
def draw_polygon_set(canvas)
  return if @object.nr_of_contours == 0
  @object.polygons.each do |poly|
    canvas.move_to(poly[0].x, poly[0].y)
    1.upto(poly.nr_of_vertices - 1) {|i| canvas.line_to(poly[i].x, poly[i].y) }
    canvas.close_subpath
  end
  canvas.stroke unless @path_only
end
draw_segment(canvas) click to toggle source
# File lib/hexapdf/content/graphic_object/geom2d.rb, line 107
def draw_segment(canvas)
  canvas.line(@object.start_point.x, @object.start_point.y,
              @object.end_point.x, @object.end_point.y)
  canvas.stroke unless @path_only
end