...master 2026-11-12 (1037e2069+unstaged)

Device

All built-in devices have the methods listed below. Any function that accepts a device will also accept a Javascript object with the same methods. Any missing methods are simply ignored, so you only need to create methods for the device calls you care about.

Many of the methods take graphics objects as arguments: Path, Text, Image and Shade.

Colors are specified as arrays with the appropriate number of components for the color space.

The methods that clip graphics, e.g. Device.prototype.clipPath(), Device.prototype.clipStrokePath(), Device.prototype.clipText(), etc., must be balanced with a corresponding Device.prototype.popClip().

Constructors

class Device(callbacks)

Create a Device which calls back to Javascript.

The callback object may provide functions matching the methods on the Device class. Any device calls that don’t have a corresponding function will simply be ignored.

You can create other devices with DrawDevice and DisplayListDevice.

Constants

Blend mode constants for use with Device.prototype.beginGroup:

Device.BLEND_NORMAL

Device.BLEND_MULTIPLY

Device.BLEND_SCREEN

Device.BLEND_OVERLAY

Device.BLEND_DARKEN

Device.BLEND_LIGHTEN

Device.BLEND_COLOR_DODGE

Device.BLEND_COLOR_BURN

Device.BLEND_HARD_LIGHT

Device.BLEND_SOFT_LIGHT

Device.BLEND_DIFFERENCE

Device.BLEND_EXCLUSION

Device.BLEND_HUE

Device.BLEND_SATURATION

Device.BLEND_COLOR

Device.BLEND_LUMINOSITY

Instance methods

Device.prototype.close()

Tell this device that we are done, and flush any pending output.

Before closing, ensure that there have been as many calls to popClip() as there have been to the clipping functions: clipPath(), clipStrokePath(), clipText(), etc.

device.close()

Line art

Device.prototype.fillPath(path, evenOdd, ctm, colorspace, color, alpha)

Fill a path.

device.fillPath(path, false, mupdf.Matrix.identity, mupdf.ColorSpace.DeviceRGB, [1, 0, 0], true)

Device.prototype.strokePath(path, stroke, ctm, colorspace, color, alpha)

Stroke a path.

device.strokePath(path,
        {dashes: [5, 10], lineWidth: 3, lineCap: 'Round' },
        mupdf.Matrix.identity,
        mupdf.ColorSpace.DeviceRGB,
        [0, 1, 0],
        0.5
)

Device.prototype.clipPath(path, evenOdd, ctm)

Clip a path.

device.clipPath(path, true, mupdf.Matrix.identity)

Device.prototype.clipStrokePath(path, stroke, ctm)

Clip & stroke a path.

device.clipStrokePath(path, true, mupdf.Matrix.identity)

Text

Device.prototype.fillText(text, ctm, colorspace, color, alpha)

Fill a text object.

device.fillText(text, mupdf.Matrix.identity, mupdf.ColorSpace.DeviceRGB, [1, 0, 0], 1)

Device.prototype.strokeText(text, stroke, ctm, colorspace, color, alpha)

Stroke a text object.

device.strokeText(text,
        { dashes: [5, 10], lineWidth: 3, lineCap: 'Round' },
        mupdf.Matrix.identity,
        mupdf.ColorSpace.DeviceRGB,
        [1, 0, 0],
        1
)

Device.prototype.clipText(text, ctm)

Clip a text object.

device.clipText(text, mupdf.Matrix.identity)

Device.prototype.clipStrokeText(text, stroke, ctm)

Clip & stroke a text object.

device.clipStrokeText(text,
        { dashes: [5, 10], lineWidth: 3, lineCap: 'Round' },
        mupdf.Matrix.identity
)

Device.prototype.ignoreText(text, ctm)

Invisible text that can be searched but should not be visible, such as for overlaying a scanned OCR image.

device.ignoreText(text, mupdf.Matrix.identity)

Shadings

Device.prototype.fillShade(shade, ctm, alpha)

Fill a shading, also known as a gradient.

device.fillShade(shade, mupdf.Matrix.identity, true, { overPrinting: true })

Images

Device.prototype.fillImage(image, ctm, alpha)

Draw an image. An image always fills a unit rectangle [0, 0, 1, 1], so must be transformed to be placed and drawn at the appropriate size.

device.fillImage(image, mupdf.Matrix.identity, false, { overPrinting: true })

Device.prototype.fillImageMask(image, ctm, colorspace, color, alpha)

An image mask is an image without color. Fill with the color where the image is opaque.

device.fillImageMask(image, mupdf.Matrix.identity, mupdf.ColorSpace.DeviceRGB, [0, 1, 0], true)

Device.prototype.clipImageMask(image, ctm)

Clip graphics using the image to mask the areas to be drawn.

device.clipImageMask(image, mupdf.Matrix.identity)

Clipping and masking

Device.prototype.popClip()

Pop the clip mask installed by the last clipping operation.

device.popClip()

Device.prototype.beginMask(area, luminosity, colorspace, color)

Create a soft mask. Any drawing commands between beginMask and endMask are grouped and used as a clip mask.

device.beginMask([0, 0, 100, 100], true, mupdf.ColorSpace.DeviceRGB, [1, 0, 1])

Device.prototype.endMask()

Ends the mask.

device.endMask()

Groups and transparency

Device.prototype.beginGroup(area, colorspace, isolated, knockout, blendmode, alpha)

Begin a transparency blending group. See knockout and isolation and blend mode in the glossary for a cursory overview of the concepts.

The blendmode is one of these string values or the corresponding enum constants: Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity.

You can also use the Device.BLEND_NORMAL constant:

device.beginGroup(
        [0, 0, 100, 100],
        mupdf.ColorSpace.DeviceRGB,
        true,
        true,
        "Multiply",
        0.5
)

Device.prototype.endGroup()

Ends the blending group.

device.endGroup()

Tiling

Device.prototype.beginTile(area, view, xstep, ystep, ctm, id, doc_id)

Draw a tiling pattern. Any drawing commands between beginTile and endTile are grouped and then repeated across the whole page. Apply a clip mask to restrict the pattern to the desired shape.

device.beginTile([0, 0, 100, 100], [100, 100, 200, 200], 10, 10, mupdf.Matrix.identity, 0)

Device.prototype.endTile()

Ends the tiling pattern.

device.endTile()

Render flags

Device.prototype.renderFlags(set, clear)

only mutool run

The specified rendering flags are set, and some others are cleared.

Both set and clear are arrays where each element one of these flag names:

device.renderFlags(["mask", "startcap-undefined"], [])

Device colorspaces

Device.prototype.setDefaultColorSpaces(defaultCS)

only mutool run

Change the set of default device colorspaces to one given.

var defaultCS = new DefaultColorSpaces()
defaultCS.setDefaultRGB(defaultCS.getDefaultGray())
device.setDefaultColorSpaces(new DefaultColorSpaces())

Layers

Device.prototype.beginLayer(name)

Begin a marked-content layer with the given name.

device.beginLayer("my tag")

Device.prototype.endLayer()

End a marked-content layer.

device.endLayer()

Structures

Device.prototype.beginStructure(structure, raw, index)

only mutool run

Begin a standard structure type with the raw tag name and a unique identifier.

device.beginStructure("Document", "my_tag_name", 123)

Device.prototype.endStructure()

only mutool run

End a standard structure element.

device.endStructure()

Metatext

Device.prototype.beginMetatext(meta, text)

only mutool run

Begin meta text where meta is either of:

device.beginMetatext("Title", "My title")

Device.prototype.endMetatext()

only mutool run

End meta text information.

device.endMetatext()