class HexaPDF::Content::Canvas

Parent
Included Modules

This class provides the basic drawing operations supported by PDF.

General Information

A canvas object is used for modifying content streams on a level higher than text. It would be possible to write a content stream by hand since PDF uses a simplified reversed polish notation for specifying operators: First come the operands, then comes the operator and no operator returns any result. However, it is easy to make mistakes this way and one has to know all operators and their operands.

This is rather tedious and therefore this class exists. It allows one to modify a content stream by invoking methods that should be familiar to anyone that has ever used a graphic API. There are methods for moving the current point, drawing lines and curves, setting the color, line width and so on.

The PDF operators themselves are implemented as classes, see Operator. The canvas class uses the BaseOperator#invoke and BaseOperator#serialize methods for applying changes and serialization, with one exception: color setters don't invoke the corresponding operator implementation but directly work on the graphics state.

PDF Graphics

Graphics Operators and Objects

There are about 60 PDF content stream operators. Some are used for changing the graphics state, some for drawing paths and others for showing text. This is all abstracted through the Canvas class.

PDF knows about five different graphics objects: path objects, text objects, external objects, inline image objects and shading objects. If none of the five graphics objects is current, the content stream is at the so called page description level (in between graphics objects).

Additionally the PDF operators are divided into several groups, like path painting or text showing operators, and such groups of operators are allowed to be used only in certain graphics objects or the page description level.

Have a look at the PDF specification (PDF1.7 s8.2) for more details.

HexaPDF tries to ensure the proper use of the operators and graphics objects and if it cannot do it, an error is raised. So if you don't modify a content stream directly but via the Canvas methods, you generally don't have to worry about the low-level inner workings.

Graphics State

Some operators modify the so called graphics state (see Content::GraphicsState). The graphics state is a collection of settings that is used during processing or creating a content stream. For example, the path painting operators don't have operands to specify the line width or the stroke color but take this information from the graphics state.

One important thing about the graphics state is that it is only possible to restore a prior state using the save and restore methods. It is not possible to reset the graphics state while creating the content stream!

Paths

A PDF path object consists of one or more subpaths. Each subpath can be a rectangle or can consist of lines and cubic bezier curves. No other types of subpaths are known to PDF. However, the Canvas class contains additional methods that use the basic path construction methods for drawing other paths like circles.

When a subpath is started, the current graphics object is changed to :path. After all path constructions are finished, a path painting method needs to be invoked to change back to the page description level. Optionally, the path painting method may be preceeded by a clipping path method to change the current clipping path (see clip_path).

There are four kinds of path painting methods:

  • Those that stroke the path,

  • those that fill the path,

  • those that stroke and fill the path and

  • one to neither stroke or fill the path (used, for example, to just set the clipping path).

In addition filling may be done using either the nonzero winding number rule or the even-odd rule.

Special Graphics State Methods

These methods are only allowed when the current graphics object is :none, i.e. operations are done on the page description level.

See: PDF1.7 s8, s9

Attributes

contents[R]

The serialized contents produced by the various canvas operations up to this point.

Note that the returned string may not be a completely valid PDF content stream since a graphic object may be open or the graphics state not completely restored.

See: stream_data

context[R]

The context for which the canvas was created (a HexaPDF::Type::Page or HexaPDF::Type::Form object).

current_point[R]

The current point [x, y] of the path.

This attribute holds the current point which is only valid if the current graphics objects is :path.

When the current point changes, the array is modified in place instead of creating a new array!

graphics_object[RW]

The current graphics object.

The graphics object should not be changed directly. It is automatically updated according to the invoked methods.

This attribute can have the following values:

:none

No current graphics object, i.e. the page description level.

:path

The current graphics object is a path.

:clipping_path

The current graphics object is a clipping path.

:text

The current graphics object is a text object.

See: PDF1.7 s8.2

graphics_state[R]

The Content::GraphicsState object containing the current graphics state.

The graphics state must not be changed directly, only by using the provided methods. If it is changed directly, the output will not be correct.

operators[R]

The operator name/implementation map used when invoking or serializing an operator.

stream_data[R]

A StreamData object representing the serialized contents produced by the various canvas operations.

In contrast to contents, it is ensured that an open graphics object is closed and all saved graphics states are restored when the contents of the stream data object is read. Note that this means that reading the stream data object may change the state of the canvas.

Public Class Methods

new(context)

Creates a new Canvas object for the given context object (either a Page or a Form).

Public Instance Methods

arc(cx, cy, a:, b: a, start_angle: 0, end_angle: 360, clockwise: false, inclination: 0) → canvas

Appends an elliptical arc to the path. The endpoint of the arc becomes the new current point.

cx

x-coordinate of the center point of the arc

cy

y-coordinate of the center point of the arc

a

Length of semi-major axis

b

Length of semi-minor axis (default: a)

start_angle

Angle in degrees at which to start the arc (default: 0)

end_angle

Angle in degrees at which to end the arc (default: 360)

clockwise

If true the arc is drawn in clockwise direction, otherwise in counterclockwise direction.

inclination

Angle in degrees between the x-axis and the semi-major axis (default: 0)

If a and b are equal, a circular arc is drawn. If the difference of the start angle and end angle is equal to 360, a full ellipse (or circle) is drawn.

If there is no current path when the method is invoked, a new path is automatically begun.

Since PDF doesn't have operators for drawing elliptical or circular arcs, they have to be approximated using Bezier curves (see curve_to). The accuracy of the approximation can be controlled using the configuration option 'graphic_object.arc.max_curves'.

Examples:

canvas.arc(0, 0, a: 10)                         # Circle at (0, 0) with radius 10
canvas.arc(0, 0, a: 10, b: 5)                   # Ellipse at (0, 0) with radii 10 and 5
canvas.arc(0, 0, a: 10, b: 5, inclination: 45)  # The above ellipse inclined 45 degrees

# Circular and elliptical arcs from 45 degrees to 135 degrees
canvas.arc(0, 0, a: 10, start_angle: 45, end_angle: 135)
canvas.arc(0, 0, a: 10, b: 5, start_angle: 45, end_angle: 135)

# Arcs from 135 degrees to 15 degrees, the first in counterclockwise direction (i.e. the
# big arc), the other in clockwise direction (i.e. the small arc)
canvas.arc(0, 0, a: 10, start_angle: 135, end_angle: 15)
canvas.arc(0, 0, a: 10, start_angle: 135, end_angle: 15, clockwise: true)

See: Content::GraphicObject::Arc

begin_text(force_new: false) → canvas

Begins a new text object.

If force is true and the current graphics object is already a text object, it is ended and a new text object is begun.

See: PDF1.7 s9.4.1

character_spacing → current_character_spacing
character_spacing(amount) → canvas
character_spacing(amount) { block } → canvas

The character spacing determines how much additional space is added between two consecutive characters. For horizontal writing positive values increase the distance between two characters, whereas for vertical writing negative values increase the distance.

Returns the current character spacing value (see HexaPDF::Content::GraphicsState#character_spacing) when no argument is given. Otherwise sets the character spacing using the amount argument and returns self. The setter version can also be called in the #character_spacing= form.

If the amount and a block are provided, the changed character spacing is only active during the block by saving and restoring the graphics state.

Examples:

canvas.character_spacing(0.25)
canvas.character_spacing                      # => 0.25
canvas.character_spacing = 0.5                # => 0.5

canvas.character_spacing(0.10) do
  canvas.character_spacing                    # => 0.10
end
canvas.character_spacing                      # => 0.5

See: PDF1.7 s9.3.2

Also aliased as: character_spacing=
character_spacing=(amount = nil, &bk)
Alias for: character_spacing
circle(cx, cy, radius) → canvas

Appends a circle with center (cx, cy) and the given radius (in degrees) to the path as a complete subpath (drawn in counterclockwise direction). The point (center_x + radius, center_y) becomes the new current point.

If there is no current path when the method is invoked, a new path is automatically begun.

Examples:

canvas.circle(100, 100, 10)

See: arc (for approximation accuracy)

clip_path(rule = :nonzero) → canvas

Modifies the clipping path by intersecting it with the current path.

The argument rule may either be :nonzero to use the nonzero winding number rule or :even_odd to use the even-odd rule for determining which regions lie inside the clipping path.

Note that the current path cannot be modified after invoking this method! This means that one of the path painting methods or end_path must be called immediately afterwards.

See: PDF1.7 s8.5.4

close_fill_stroke(rule = :nonzero) → canvas

Closes the last subpath and then fills and strokes the path using the given rule.

The argument rule may either be :nonzero to use the nonzero winding number rule or :even_odd to use the even-odd rule for determining which regions to fill in.

See: PDF1.7 s8.5.3

close_stroke → canvas

Closes the last subpath and then strokes the path.

See: PDF1.7 s8.5.3.1, s8.5.3.2

close_subpath → canvas

Closes the current subpath by appending a straight line from the current point to the start point of the subpath which also becomes the new current point.

color_from_specification(spec)

Creates a color object from the given color specification. See stroke_color for details on the possible color specifications.

curve_to(x, y, p1:, p2:) → canvas
curve_to(x, y, p1:) → canvas
curve_to(x, y, p2:) → canvas

Appends a cubic Bezier curve to the current subpath starting from the current point. The end point becomes the new current point.

A Bezier curve consists of the start point, the end point and the two control points p1 and p2. The start point is always the current point and the end point is specified as x and y arguments.

Additionally, either the first control point p1 or the second control p2 or both control points have to be specified (as arrays containing two numbers). If the first control point is not specified, the current point is used as first control point. If the second control point is not specified, the end point is used as the second control point.

Examples:

canvas.curve_to(100, 100, p1: [100, 50], p2: [50, 100])
canvas.curve_to(100, 100, p1: [100, 50])
canvas.curve_to(100, 100, p2: [50, 100])
draw(obj, **options) → canvas
draw(name, **options) → canvas

Draws the given graphic object on the canvas.

See graphic_object for information on the arguments.

Examples:

canvas.draw(:arc, cx: 10, cy: 10)
ellipse(cx, cy, a:, b:, inclination: 0) → canvas

Appends an ellipse with center (cx, cy), semi-major axis a, semi-minor axis b and an inclination from the x-axis of inclination degrees to the path as a complete subpath. The outer-most point on the semi-major axis becomes the new current point.

If there is no current path when the method is invoked, a new path is automatically begun.

Examples:

# Ellipse aligned to x-axis and y-axis
canvas.ellipse(100, 100, a: 10, b: 5)

# Inclined ellipse
canvas.ellipse(100, 100, a: 10, b: 5, inclination: 45)

See: arc (for approximation accuracy)

end_marked_content_sequence → canvas

Ends a marked-content sequence.

See marked_content_sequence for details.

See: PDF1.7 s14.6, marked_content_sequence

end_path → canvas

Ends the path without stroking or filling it.

This method is normally used in conjunction with the clipping path methods to define the clipping.

See: PDF1.7 s8.5.3.1 clip

end_text → canvas

Ends the current text object.

See: PDF1.7 s9.4.1

fill(rule = :nonzero) → canvas

Fills the path using the given rule.

The argument rule may either be :nonzero to use the nonzero winding number rule or :even_odd to use the even-odd rule for determining which regions to fill in.

Any open subpaths are implicitly closed before being filled.

See: PDF1.7 s8.5.3.1, s8.5.3.3

fill_color(*color, &block)

The fill color defines the color used for non-stroking operations, i.e. for filling paths.

Works exactly the same stroke_color but for the fill color. See stroke_color for details on invocation and use.

Also aliased as: fill_color=
fill_color=(*color, &block)
Alias for: fill_color
fill_stroke(rule = :nonzero) → canvas

Fills and then strokes the path using the given rule.

The argument rule may either be :nonzero to use the nonzero winding number rule or :even_odd to use the even-odd rule for determining which regions to fill in.

See: PDF1.7 s8.5.3

font → current_font
font(name, size: nil, **options) → canvas

Specifies the font that should be used when showing text.

A valid font size need to be provided on the first invocation, otherwise an error is raised.

Note that this method returns the font object itself, not the PDF dictionary representing the font!

If size is specified, the font_size method is invoked with it as argument. All other options are passed on to the font loaders (see HexaPDF::FontLoader) that are used for loading the specified font.

Returns the current font object when no argument is given.

Examples:

canvas.font("Times", variant: :bold, size: 12)
canvas.font                                          # => font object
canvas.font = "Times"

See: PDF1.7 s9.2.2

Also aliased as: font=
font=(name = nil, size: nil, **options)
Alias for: font
font_size → font_size
font_size(size → canvas

Specifies the font size.

Note that an error is raised if no font has been set before!

Returns the current font size when no argument is given.

Examples:

canvas.font_size(12)
canvas.font_size                       # => 12
canvas.font_size = 12

See: PDF1.7 s9.2.2

Also aliased as: font_size=
font_size=(size = nil)
Alias for: font_size
graphic_object(obj, **options) → obj
graphic_object(name, **options) → graphic_object

Returns the named graphic object, configured with the given options.

If an object responding to :configure is given, it is used. Otherwise the graphic object is looked up via the given name in the configuration option 'graphic_object.map'. Then the graphic object is configured with the given options if at least one is given.

Examples:

obj = canvas.graphic_object(:arc, cx: 10, cy: 10)
canvas.draw(obj)
horizontal_scaling → current_horizontal_scaling
horizontal_scaling(percent) → canvas
horizontal_scaling(percent) { block } → canvas

The horizontal scaling adjusts the width of text character glyphs by stretching or compressing them in the horizontal direction. The value is specified as percent of the normal width.

Returns the current horizontal scaling value (see HexaPDF::Content::GraphicsState#horizontal_scaling) when no argument is given. Otherwise sets the horizontal scaling using the percent argument and returns self. The setter version can also be called in the #horizontal_scaling= form.

If the percent and a block are provided, the changed horizontal scaling is only active during the block by saving and restoring the graphics state.

Examples:

canvas.horizontal_scaling(50)                  # each glyph has only 50% width
canvas.horizontal_scaling                      # => 50
canvas.horizontal_scaling = 125                # => 125

canvas.horizontal_scaling(75) do
  canvas.horizontal_scaling                    # => 75
end
canvas.horizontal_scaling                      # => 125

See: PDF1.7 s9.3.4

Also aliased as: horizontal_scaling=
horizontal_scaling=(amount = nil, &bk)
Alias for: horizontal_scaling
image(obj, at:, width: nil, height: nil)
Alias for: xobject
leading → current_leading
leading(amount) → canvas
leading(amount) { block } → canvas

The leading specifies the vertical distance between the baselines of adjacent text lines.

Returns the current leading value (see HexaPDF::Content::GraphicsState#leading) when no argument is given. Otherwise sets the leading using the amount argument and returns self. The setter version can also be called in the leading= form.

If the amount and a block are provided, the changed leading is only active during the block by saving and restoring the graphics state.

Examples:

canvas.leading(14.5)
canvas.leading                      # => 14.5
canvas.leading = 10                 # => 10

canvas.leading(25) do
  canvas.leading                    # => 25
end
canvas.leading                      # => 10

See: PDF1.7 s9.3.5

Also aliased as: leading=
leading=(amount = nil, &bk)
Alias for: leading
line(x0, y0, x1, y1) → canvas

Moves the current point to (x0, y0) and appends a line to (x1, y1) to the current path.

This method is equal to “canvas.move_to(x0, y0).line_to(x1, y1)”.

Examples:

canvas.line(10, 10, 100, 100)
line_cap_style → current_line_cap_style
line_cap_style(style) → canvas
line_cap_style(style) { block } → canvas

The line cap style specifies how the ends of stroked open paths should look like. The style parameter can either be a valid integer or one of the symbols :butt, :round or :projecting_square (see LineCapStyle.normalize for details). Note that the return value is always a normalized line cap style.

Returns the current line cap style (see HexaPDF::Content::GraphicsState#line_cap_style) when no argument is given. Otherwise sets the line cap style to the given style and returns self. The setter version can also be called in the #line_cap_style= form.

If the style and a block are provided, the changed line cap style is only active during the block by saving and restoring the graphics state.

Examples:

canvas.line_cap_style(:butt)
canvas.line_cap_style               # => #<NamedValue @name=:butt, @value=0>
canvas.line_cap_style = :round      # => #<NamedValue @name=:round, @value=1>

canvas.line_cap_style(:butt) do
  canvas.line_cap_style             # => #<NamedValue @name=:butt, @value=0>
end
canvas.line_cap_style               # => #<NamedValue @name=:round, @value=1>

See: PDF1.7 s8.4.3.3

Also aliased as: line_cap_style=
line_cap_style=(style = nil, &block)
Alias for: line_cap_style
line_dash_pattern → current_line_dash_pattern
line_dash_pattern(line_dash_pattern) → canvas
line_dash_pattern(length, phase = 0) → canvas
line_dash_pattern(array, phase = 0) → canvas
line_dash_pattern(value, phase = 0) { block } → canvas

The line dash pattern defines the appearance of a stroked path (line or curve), ie. if it is solid or if it contains dashes and gaps.

There are multiple ways to set the line dash pattern:

  • By providing a Content::LineDashPattern object

  • By providing a single Integer/Float that is used for both dashes and gaps

  • By providing an array of Integers/Floats that specify the alternating dashes and gaps

The phase (i.e. the distance into the dashes/gaps at which to start) can additionally be set in the last two cases.

A solid line can be achieved by using 0 for the length or by using an empty array.

Returns the current line dash pattern (see HexaPDF::Content::GraphicsState#line_dash_pattern) when no argument is given. Otherwise sets the line dash pattern using the given arguments and returns self. The setter version can also be called in the #line_dash_pattern= form (but only without the second argument!).

If arguments and a block are provided, the changed line dash pattern is only active during the block by saving and restoring the graphics state.

Examples:

canvas.line_dash_pattern(10)
canvas.line_dash_pattern                # => LineDashPattern.new([10], 0)
canvas.line_dash_pattern(10, 2)
canvas.line_dash_pattern([5, 3, 1], 2)
canvas.line_dash_pattern = LineDashPattern.new([5, 3, 1], 1)

canvas.line_dash_pattern(10) do
  canvas.line_dash_pattern              # => LineDashPattern.new([10], 0)
end
canvas.line_dash_pattern                # => LineDashPattern.new([5, 3, 1], 1)

See: PDF1.7 s8.4.3.5, LineDashPattern

Also aliased as: line_dash_pattern=
line_dash_pattern=(value = nil, phase = 0, &block)
Alias for: line_dash_pattern
line_join_style → current_line_join_style
line_join_style(style) → canvas
line_join_style(style) { block } → canvas

The line join style specifies the shape that is used at the corners of stroked paths. The style parameter can either be a valid integer or one of the symbols :miter, :round or :bevel (see LineJoinStyle.normalize for details). Note that the return value is always a normalized line join style.

Returns the current line join style (see HexaPDF::Content::GraphicsState#line_join_style) when no argument is given. Otherwise sets the line join style to the given style and returns self. The setter version can also be called in the #line_join_style= form.

If the style and a block are provided, the changed line join style is only active during the block by saving and restoring the graphics state.

Examples:

canvas.line_join_style(:miter)
canvas.line_join_style               # => #<NamedValue @name=:miter, @value=0>
canvas.line_join_style = :round      # => #<NamedValue @name=:round, @value=1>

canvas.line_join_style(:bevel) do
  canvas.line_join_style             # => #<NamedValue @name=:bevel, @value=2>
end
canvas.line_join_style               # => #<NamedValue @name=:round, @value=1>

See: PDF1.7 s8.4.3.4

Also aliased as: line_join_style=
line_join_style=(style = nil, &block)
Alias for: line_join_style
line_to(x, y) → canvas

Appends a straight line segment from the current point to the given point (which becomes the new current point) to the current subpath.

Examples:

canvas.line_to(100, 100)
line_width → current_line_width
line_width(width) → canvas
line_width(width) { block } → canvas

The line width determines the thickness of a stroked path.

Returns the current line width (see HexaPDF::Content::GraphicsState#line_width) when no argument is given. Otherwise sets the line width to the given width and returns self. The setter version can also be called in the #line_width= form.

If the width and a block are provided, the changed line width is only active during the block by saving and restoring the graphics state.

Examples:

canvas.line_width(10)
canvas.line_width          # => 10
canvas.line_width = 5      # => 5

canvas.line_width(10) do
  canvas.line_width        # => 10
end
canvas.line_width          # => 5

See: PDF1.7 s8.4.3.2

Also aliased as: line_width=
line_width=(width = nil, &block)
Alias for: line_width
marked_content_point(tag, property_list: nil) → canvas

Inserts a marked-content point, optionally associated with a property list.

A marked-content point is used to identify a position in the content stream for later use by other applications. The symbol tag is used to uniquely identify the role of the marked-content point and should be registered with ISO to avoid conflicts.

The optional property_list argument can either be a valid PDF dictionary or a symbol referencing an already used property list in the resource dictionary's /Properties dictionary.

Examples:

canvas.marked_content_point(:Divider)
canvas.marked_content_point(:Divider, property_list: {Key: 'value'})

See: PDF1.7 s14.6

marked_content_sequence(tag, property_list: nil) → canvas
marked_content_sequence(tag, property_list: nil) { block } → canvas

Inserts a marked-content sequence, optionally associated with a property list.

A marked-content sequence is used to identify a sequence of complete graphics objects in the content stream for later use by other applications. The symbol tag is used to uniquely identify the role of the marked-content sequence and should be registered with ISO to avoid conflicts.

The optional property_list argument can either be a valid PDF dictionary or a symbol referencing an already used property list in the resource dictionary's /Properties dictionary.

If invoked without a block, a corresponding call to end_marked_content_sequence must be done. Otherwise the marked-content sequence automatically ends when the block is finished.

Although the PDF specification would allow using marked-content sequences inside text objects, this is prohibited.

Examples:

canvas.marked_content_sequence(:Divider)
# Other instructions
canvas.end_marked_content_sequence

canvas.marked_content_sequence(:Divider, property_list: {Key: 'value'}) do
  # Other instructions
end

See: PDF1.7 s14.6, end_marked_content_sequence

miter_limit → current_miter_limit
miter_limit(limit) → canvas
miter_limit(limit) { block } → canvas

The miter limit specifies the maximum ratio of the miter length to the line width for mitered line joins (see line_join_style). When the limit is exceeded, a bevel join is used instead of a miter join.

Returns the current miter limit (see HexaPDF::Content::GraphicsState#miter_limit) when no argument is given. Otherwise sets the miter limit to the given limit and returns self. The setter version can also be called in the #miter_limit= form.

If the limit and a block are provided, the changed miter limit is only active during the block by saving and restoring the graphics state.

Examples:

canvas.miter_limit(10)
canvas.miter_limit          # => 10
canvas.miter_limit = 5      # => 5

canvas.miter_limit(10) do
  canvas.miter_limit        # => 10
end
canvas.miter_limit          # => 5

See: PDF1.7 s8.4.3.5

Also aliased as: miter_limit=
miter_limit=(limit = nil, &block)
Alias for: miter_limit
move_text_cursor(offset: nil, absolute: true) → canvas

Moves the text cursor by modifying the text and text line matrices.

If offset is not specified, the text cursor is moved to the start of the next text line using leading as vertical offset.

Otherwise, the arguments offset, which has to be an array of the form [x, y], and absolute work together:

  • If absolute is true, then the text and text line matrices are set to [1, 0, 0, 1, x, y], placing the origin of text space, and therefore the text cursor, at [x, y].

    Note that absolute has to be understood in terms of the text matrix since for the actual rendering the current transformation matrix is multiplied with the text matrix.

  • If absolute is false, then the text cursor is moved to the start of the next line, offset from the start of the current line (the origin of the text line matrix) by offset.

See: show_glyphs

move_to(x, y) → canvas

Begins a new subpath (and possibly a new path) by moving the current point to the given point.

Examples:

canvas.move_to(100, 50)
opacity → current_values
opacity(fill_alpha:) → canvas
opacity(stroke_alpha:) → canvas
opacity(fill_alpha:, stroke_alpha:) → canvas
opacity(fill_alpha:, stroke_alpha:) { block } → canvas

The fill and stroke alpha values determine how opaque drawn elements will be. Note that the fill alpha value applies not just to fill values but to all non-stroking operations (e.g. images, …).

Returns the current fill alpha (see HexaPDF::Content::GraphicsState#fill_alpha) and stroke alpha (see HexaPDF::Content::GraphicsState#stroke_alpha) values using a hash with the keys :fill_alpha and :stroke_alpha when no argument is given. Otherwise sets the fill and stroke alpha values and returns self. The setter version can also be called in the opacity= form.

If the values are set and a block is provided, the changed alpha values are only active during the block by saving and restoring the graphics state.

Examples:

canvas.opacity(fill_alpha: 0.5)
canvas.opacity                               # => {fill_alpha: 0.5, stroke_alpha: 1.0}
canvas.opacity(fill_alpha: 0.4, stroke_alpha: 0.9)
canvas.opacity                               # => {fill_alpha: 0.4, stroke_alpha: 0.9}

canvas.opacity(stroke_alpha: 0.7) do
  canvas.opacity                             # => {fill_alpha: 0.4, stroke_alpha: 0.7}
end
canvas.opacity                               # => {fill_alpha: 0.4, stroke_alpha: 0.9}

See: PDF1.7 s11.6.4.4

polygon(x0, y0, x1, y1, x2, y2, ..., radius: 0) → canvas

Appends a polygon consisting of the given points to the path as a complete subpath. The point (x0, y0 + radius) becomes the new current point.

If radius is greater than 0, the corners are rounded with the given radius.

If there is no current path when the method is invoked, a new path is automatically begun.

Examples:

canvas.polygon(0, 0, 100, 0, 100, 100, 0, 100)
canvas.polygon(0, 0, 100, 0, 100, 100, 0, 100, radius: 10)
polyline(x0, y0, x1, y1, x2, y2, ...) → canvas

Moves the current point to (x0, y0) and appends line segments between all given consecutive points, i.e. between (x0, y0) and (x1, y1), between (x1, y1) and (x2, y2) and so on. The last point becomes the new current point.

Examples:

canvas.polyline(0, 0, 100, 0, 100, 100, 0, 100, 0, 0)
rectangle(x, y, width, height, radius: 0) → canvas

Appends a rectangle to the current path as a complete subpath (drawn in counterclockwise direction), with the bottom left corner specified by x and y and the given width and height.

If radius is greater than 0, the corners are rounded with the given radius.

If there is no current path when the method is invoked, a new path is automatically begun.

The current point is set to the bottom left corner if radius is zero, otherwise it is set to (x, y + radius).

Examples:

canvas.rectangle(100, 100, 100, 50)
canvas.rectangle(100, 100, 100, 50, radius: 10)
rendering_intent → current_rendering_intent
rendering_intent(intent) → canvas
rendering_intent(intent) { block } → canvas

The rendering intent is used to specify the intent on how colors should be rendered since sometimes compromises have to be made when the capabilities of an output device are not sufficient. The intent parameter can be one of the following symbols:

  • :AbsoluteColorimetric

  • :RelativeColorimetric

  • :Saturation

  • :Perceptual

Returns the current rendering intent (see HexaPDF::Content::GraphicsState#rendering_intent) when no argument is given. Otherwise sets the rendering intent using the intent argument and returns self. The setter version can also be called in the #rendering_intent= form.

If the intent and a block are provided, the changed rendering intent is only active during the block by saving and restoring the graphics state.

Examples:

canvas.rendering_intent(:Perceptual)
canvas.rendering_intent                      # => :Perceptual
canvas.rendering_intent = :Saturation        # => :Saturation

canvas.rendering_intent(:Perceptual) do
  canvas.rendering_intent                    # => :Perceptual
end
canvas.rendering_intent                      # => :Saturation

See: PDF1.7 s8.6.5.8, RenderingIntent

Also aliased as: rendering_intent=
rendering_intent=(intent = nil, &bk)
Alias for: rendering_intent
resources()

Returns the resource dictionary of the context object.

restore_graphics_state → canvas

Restores the current graphics state and returns self.

Must not be invoked more times than save_graphics_state.

See: PDF1.7 s8.4.2, save_graphics_state

rotate(angle, origin: nil) → canvas
rotate(angle, origin: nil) { block } → canvas

Rotates the user space angle degrees around the coordinate system origin or around the given point and returns self.

If invoked with a block, the rotation of the user space is only active during the block by saving and restoring the graphics state.

Note that the origin of the coordinate system itself doesn't change!

origin

The point around which the user space should be rotated.

Examples:

canvas.rotate(90) do                 # Positive x-axis is now pointing upwards
  canvas.line(0, 0, 100, 0)          # Actually from (0, 0) to (0, 100)
end
canvas.line(0, 0, 100, 0)            # Again from (0, 0) to (100, 0)

canvas.rotate(90, origin: [100, 100]) do
  canvas.line(100, 100, 200, 0)      # Actually from (100, 100) to (100, 200)
end

See: transform

save_graphics_state → canvas
save_graphics_state { block } → canvas

Saves the current graphics state and returns self.

If invoked without a block a corresponding call to restore_graphics_state must be done. Otherwise the graphics state is automatically restored when the block is finished.

Examples:

# With a block
canvas.save_graphics_state do
  canvas.line_width(10)
  canvas.line(100, 100, 200, 200)
end

# Same without a block
canvas.save_graphics_state
canvas.line_width(10)
canvas.line(100, 100, 200, 200)
canvas.restore_graphics_state

See: PDF1.7 s8.4.2, restore_graphics_state

scale(sx, sy = sx, origin: nil) → canvas
scale(sx, sy = sx, origin: nil) { block } → canvas

Scales the user space sx units in the horizontal and sy units in the vertical direction and returns self. If the optional origin is specified, scaling is done from that point.

If invoked with a block, the scaling is only active during the block by saving and restoring the graphics state.

Note that the origin of the coordinate system itself doesn't change!

origin

The point from which the user space should be scaled.

Examples:

canvas.scale(2, 3) do                # Point (1, 1) is now actually (2, 3)
  canvas.line(50, 50, 100, 100)      # Actually from (100, 150) to (200, 300)
end
canvas.line(0, 0, 100, 0)            # Again from (0, 0) to (100, 0)

canvas.scale(2, 3, origin: [50, 50]) do
  canvas.line(50, 50, 100, 100)      # Actually from (50, 50) to (200, 300)
end

See: transform

show_glyphs(glyphs) → canvas

Low-level method for actually showing text on the canvas.

The argument glyphs needs to be a an array of glyph objects valid for the current font, optionally interspersed with numbers for kerning.

Text is always shown at the current position of the text cursor, i.e. the origin of the text matrix. To move the text cursor to somewhere else use move_text_cursor before calling this method.

The text matrix is updated to correctly represent the graphics state after the invocation.

This method is usually not invoked directly but by higher level methods like text.

show_glyphs_only(glyphs) → canvas

Same operation as with show_glyphs but without updating the text matrix.

This method should only be used by advanced text layouting algorithms which perform the necessary calculations themselves!

Warning: Since this method doesn't update the text matrix, all following results from text_cursor and other methods using the current text matrix are invalid until the next call to text_matrix or end_text.

skew(a, b, origin: nil) → canvas
skew(a, b, origin: nil) { block } → canvas

Skews the the x-axis by a degrees and the y-axis by b degress and returns self. If the optional origin is specified, skewing is done from that point.

If invoked with a block, the skewing is only active during the block by saving and restoring the graphics state.

Note that the origin of the coordinate system itself doesn't change!

origin

The point from which the axes are skewed.

Examples:

canvas.skew(0, 45) do                 # Point (1, 1) is now actually (2, 1)
  canvas.line(50, 50, 100, 100)       # Actually from (100, 50) to (200, 100)
end
canvas.line(0, 0, 100, 0)             # Again from (0, 0) to (100, 0)

canvas.skew(0, origin: [50, 50]) do
  canvas.line(50, 50, 100, 100)       # Actually from (50, 50) to (200, 300)
end

See: transform

stroke → canvas

Strokes the path.

See: PDF1.7 s8.5.3.1, s8.5.3.2

stroke_color → current_stroke_color
stroke_color(gray) → canvas
stroke_color(r, g, b) → canvas
stroke_color(c, m, y, k) → canvas
stroke_color(string) → canvas
stroke_color(color_object) → canvas
stroke_color(array) → canvas
stroke_color(color_spec) { block } → canvas

The stroke color defines the color used for stroking operations, i.e. for painting paths.

There are several ways to define the color that should be used:

  • A single numeric argument specifies a gray color (see Content::ColorSpace::DeviceGray::Color).

  • Three numeric arguments specify an RGB color (see Content::ColorSpace::DeviceRGB::Color).

  • A string in the format “RRGGBB” where “RR” is the hexadecimal number for the red, “GG” for the green and “BB” for the blue color value also specifies an RGB color.

  • Four numeric arguments specify a CMYK color (see Content::ColorSpace::DeviceCMYK::Color).

  • A color object is used directly (normally used for color spaces other than DeviceRGB, DeviceCMYK and DeviceGray).

  • An array is treated as if its items were specified separately as arguments.

Returns the current stroke color (see HexaPDF::Content::GraphicsState#stroke_color) when no argument is given. Otherwise sets the stroke color using the given arguments and returns self. The setter version can also be called in the #stroke_color= form.

If the arguments and a block are provided, the changed stroke color is only active during the block by saving and restoring the graphics state.

Examples:

# With no arguments just returns the current color
canvas.stroke_color                        # => DeviceGray.color(0.0)

# Same gray color because integer values are normalized to the range of 0.0 to 1.0
canvas.stroke_color(102)
canvas.stroke_color(0.4)

# Specifying RGB colors
canvas.stroke_color(255, 255, 0)
canvas.stroke_color("FFFF00")

# Specifying CMYK colors
canvas.stroke_color(255, 255, 0, 128)

# Can use a color object directly
color = HexaPDF::Content::ColorSpace::DeviceRGB.color(255, 255, 0)
canvas.stroke_color(color)

# An array argument is destructured - these calls are all equal
cnavas.stroke_color(255, 255, 0)
canvas.stroke_color([255, 255, 0])
canvas.stroke_color = [255, 255, 0]

# As usual, can be invoked with a block to limit the effects
canvas.stroke_color(102) do
  canvas.stroke_color                      # => ColorSpace::DeviceGray.color(0.4)
end

See: PDF1.7 s8.6, ColorSpace

Also aliased as: stroke_color=
stroke_color=(*color, &block)
Alias for: stroke_color
text(text) → canvas
text(text, at: [x, y]) → canvas

Shows the given text string.

If no position is provided, the text is positioned at the current position of the text cursor (the origin in case of a new text object or otherwise after the last shown text).

The text string may contain any valid Unicode newline separator and if so, multiple lines are shown, using leading for offsetting the lines.

Note that there are no provisions to make sure that all text is visible! So if the text string is too long, it will just flow off the page and be cut off.

Examples:

canvas.font('Times', size: 12)
canvas.text("This is a \n multiline text", at: [100, 100])

See: www.unicode.org/reports/tr18/#Line_Boundaries

text_cursor → [x, y]

Returns the position of the text cursor, i.e. the origin of the current text matrix.

Note that this method can only be called while the current graphic object is a text object since the text matrix is otherwise undefined.

text_matrix(a, b, c, d, e, f) → canvas

Sets the text matrix (and the text line matrix) to the given matrix and returns self.

The given values are interpreted as a matrix in the following way:

a b 0
c d 0
e f 1

Examples:

canvas.begin_text
canvas.text_matrix(1, 0, 0, 1, 100, 100)

See: PDF1.7 s9.4.2

text_rendering_mode → current_text_rendering_mode
text_rendering_mode(mode) → canvas
text_rendering_mode(mode) { block } → canvas

The text rendering mode determines if and how glyphs are rendered. The mode parameter can either be a valid integer or one of the symbols :fill, :stroke, :fill_stroke, :invisible, :fill_clip, :stroke_clip, :fill_stroke_clip or :clip (see TextRenderingMode.normalize for details). Note that the return value is always a normalized text rendering mode value.

Returns the current text rendering mode value (see HexaPDF::Content::GraphicsState#text_rendering_mode) when no argument is given. Otherwise sets the text rendering mode using the mode argument and returns self. The setter version can also be called in the #text_rendering_mode= form.

If the mode and a block are provided, the changed text rendering mode is only active during the block by saving and restoring the graphics state.

Examples:

canvas.text_rendering_mode(:fill)
canvas.text_rendering_mode               # => #<NamedValue @name=:fill, @value = 0>
canvas.text_rendering_mode = :stroke     # => #<NamedValue @name=:stroke, @value = 1>

canvas.text_rendering_mode(3) do
  canvas.text_rendering_mode             # => #<NamedValue @name=:invisible, @value = 3>
end
canvas.text_rendering_mode               # => #<NamedValue @name=:stroke, @value = 1>

See: PDF1.7 s9.3.6

Also aliased as: text_rendering_mode=
text_rendering_mode=(m = nil, &bk)
Alias for: text_rendering_mode
text_rise → current_text_rise
text_rise(amount) → canvas
text_rise(amount) { block } → canvas

The text rise specifies the vertical distance to move the baseline up or down from its default location. Positive values move the baseline up, negative values down.

Returns the current text rise value (see HexaPDF::Content::GraphicsState#text_rise) when no argument is given. Otherwise sets the text rise using the amount argument and returns self. The setter version can also be called in the #text_rise= form.

If the amount and a block are provided, the changed text rise is only active during the block by saving and restoring the graphics state.

Examples:

canvas.text_rise(5)
canvas.text_rise                      # => 5
canvas.text_rise = 10                 # => 10

canvas.text_rise(15) do
  canvas.text_rise                    # => 15
end
canvas.text_rise                      # => 10

See: PDF1.7 s9.3.7

Also aliased as: text_rise=
text_rise=(amount = nil, &bk)
Alias for: text_rise
transform(a, b, c, d, e, f) → canvas
transform(a, b, c, d, e, f) { block } → canvas

Transforms the user space by applying the given matrix to the current transformation matrix and returns self.

If invoked with a block, the transformation is only active during the block by saving and restoring the graphics state.

The given values are interpreted as a matrix in the following way:

a b 0
c d 0
e f 1

Examples:

canvas.transform(1, 0, 0, 1, 100, 100) do  # Translate origin to (100, 100)
  canvas.line(0, 0, 100, 100)              # Actually from (100, 100) to (200, 200)
end
canvas.line(0, 0, 100, 100)                # Again from (0, 0) to (100, 100)

See: PDF1.7 s8.3, s8.4.4

translate(x, y) → canvas
translate(x, y) { block } → canvas

Translates the user space coordinate system origin to the given x and y coordinates and returns self.

If invoked with a block, the translation of the user space is only active during the block by saving and restoring the graphics state.

Examples:

canvas.translate(100, 100) do        # Origin is now at (100, 100)
  canvas.line(0, 0, 100, 0)          # Actually from (100, 100) to (200, 100)
end
canvas.line(0, 0, 100, 0)            # Again from (0, 0) to (100, 0)

See: transform

word_spacing → current_word_spacing
word_spacing(amount) → canvas
word_spacing(amount) { block } → canvas

If the font's PDF encoding supports this, the word spacing determines how much additional space is added when the ASCII space character is encountered in a text. For horizontal writing positive values increase the distance between two words, whereas for vertical writing negative values increase the distance.

Note that in HexaPDF only the standard 14 PDF Type1 fonts support this property! When using any other font, for example a TrueType font, this property has no effect.

Returns the current word spacing value (see HexaPDF::Content::GraphicsState#word_spacing) when no argument is given. Otherwise sets the word spacing using the amount argument and returns self. The setter version can also be called in the #word_spacing= form.

If the amount and a block are provided, the changed word spacing is only active during the block by saving and restoring the graphics state.

Examples:

canvas.word_spacing(0.25)
canvas.word_spacing                      # => 0.25
canvas.word_spacing = 0.5                # => 0.5

canvas.word_spacing(0.10) do
  canvas.word_spacing                    # => 0.10
end
canvas.word_spacing                      # => 0.5

See: PDF1.7 s9.3.3

Also aliased as: word_spacing=
word_spacing=(amount = nil, &bk)
Alias for: word_spacing
xobject(filename, at:, width: nil, height: nil) → xobject
xobject(io, at:, width: nil, height: nil) → xobject
xobject(image_object, at:, width: nil, height: nil) → image_object
xobject(form_object, at:, width: nil, height: nil) → form_object

Draws the given XObject (either an image XObject or a form XObject) at the specified position and returns the XObject.

Any image format for which a HexaPDF::ImageLoader object is available and registered with the configuration option 'image_loader' can be used. PNG and JPEG images are supported out of the box.

If the filename or the IO specifies a PDF file, the first page of this file is used to create a form XObject which is then drawn.

The at argument has to be an array containing two numbers specifying the bottom left corner at which to draw the XObject.

If width and height are specified, the drawn XObject will have exactly these dimensions. If only one of them is specified, the other dimension is automatically calculated so that the aspect ratio is retained. If neither is specified, the width and height of the XObject are used (for images, 1 pixel being represented by 1 PDF point, i.e. 72 DPI).

Note: If a form XObject is drawn, all currently set graphics state parameters influence the rendering of the form XObject. This means, for example, that when the line width is set to 20, all lines of the form XObject are drawn with that line width unless the line width is changed in the form XObject itself.

Examples:

canvas.xobject('test.png', at: [100, 100])
canvas.xobject('test.pdf', at: [100, 100])

File.new('test.jpg', 'rb') do |io|
  canvas.xobject(io, at: [100, 200], width: 300)
end

image = document.object(5)    # Object with oid=5 is an image XObject in this example
canvas.xobject(image, at: [100, 200], width: 200, heigth: 300)

See: PDF1.7 s8.8, s.8.10.1

Also aliased as: image