class HexaPDF::Content::Processor

Parent

This class is used for processing content operators extracted from a content stream.

General Information

When a content stream is read, operators and their operands are extracted. After extracting these operators are usually processed with a Processor instance that ensures that the needed setup (like modifying the graphics state) is done before further processing.

How Processing Works

The operator implementations (see the Operator module) are called first and they ensure that the processing state is consistent. For example, operators that modify the graphics state do actually modify the graphics_state object. However, operator implementations are only used for this task and not more, so they are very specific and normally don’t need to be changed.

After that methods corresponding to the operator names are invoked on the processor object (if they exist). Each PDF operator name is mapped to a nicer message name via the OPERATOR_MESSAGE_NAME_MAP constant. For example, the operator ‘q’ is mapped to ‘save_graphics_state“.

The task of these methods is to do something useful with the content itself, it doesn’t need to concern itself with ensuring the consistency of the processing state. For example, the processor could use the processing state to extract the text. Or paint the content on a canvas.

For inline images only the ‘BI’ operator mapped to ‘inline_image’ is used. Although also the operators ‘ID’ and ‘EI’ exist for inline images, they are not used because they are consumed while parsing inline images and do not reflect separate operators.

Text Processing

Two utility methods decode_text and decode_text_with_positioning for extracting text are provided. Both can directly be invoked from the ‘show_text’ and ‘show_text_with_positioning’ methods.

Constants

OPERATOR_MESSAGE_NAME_MAP

Mapping of PDF operator names to message names that are sent to renderer implementations.

Attributes

graphics_object[RW]

The current graphics object.

It is not advised to change this attribute manually, it is automatically adjusted according to the processed operators!

This attribute can have the following values:

:none

No current graphics object, i.e. the processor is at 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 text.

See: PDF2.0 s8.2

graphics_state[R]

The GraphicsState object containing the current graphics state.

It is not advised to change this attribute manually, it is automatically adjusted according to the processed operators!

operators[R]

Mapping from operator name (Symbol) to a callable object.

This hash is prepopulated with the default operator implementations (see Operator::DEFAULT_OPERATORS). If a default operator implementation is not satisfactory, it can easily be changed by modifying this hash.

resources[R]

The resources dictionary used during processing.

Public Class Methods

new(resources = nil)

Initializes a new processor that uses the resources PDF dictionary for resolving resources while processing operators.

It is not mandatory to set the resources dictionary on initialization but it needs to be set prior to processing operators!

Public Instance Methods

process(operator, operands = [])

Processes the operator with the given operands.

The operator is first processed with an operator implementation (if any) to ensure correct operations and then the corresponding method on this object is invoked.

resources=(res)

Sets the resources dictionary used during processing.

The first time resources are set, they are also stored as the “original” resources. This is needed because form XObject don’t need to have a resources dictionary and can use the page’s resources dictionary instead.

Protected Instance Methods

decode_text(data)

Decodes the given text object and returns it as UTF-8 string.

The argument may either be a simple text string (Tj operator) or an array that contains text strings together with positioning information (TJ operator).

decode_text_with_positioning(data)

Decodes the given text object and returns it as a CompositeBox object.

The argument may either be a simple text string (Tj operator) or an array that contains text strings together with positioning information (TJ operator).

For each glyph a GlyphBox object is computed. For horizontal fonts the width is predetermined but not the height. The latter is chosen to be the height and offset of the font’s bounding box.

paint_xobject(name)

Provides a default implementation for the ‘Do’ operator.

It checks if the XObject is a Form XObject and if so, processes the contents of the Form XObject.