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 must be balanced with a corresponding popClip.

CONSTRUCTOR METHODS

constructor(callbacks: DeviceFunctions)#

Create a Device with callback functions.

Parameters:

callbacks – object containing optional functions which conform to the DeviceFunctions interface.

INSTANCE METHODS

fillPath(path: Path, evenOdd: boolean, ctm: Matrix, colorspace: ColorSpace, color: Color, alpha: number)#

Fill a path.

Parameters:

EXAMPLE

device.fillPath(path, false, mupdfjs.Matrix.identity, mupdfjs.ColorSpace.DeviceRGB, [1,0,0], true);
strokePath(path: Path, stroke: StrokeState, ctm: Matrix, colorspace: ColorSpace, color: Color, alpha: number)#

Stroke a path.

Parameters:

EXAMPLE

device.strokePath(path,
                  {dashes:[5,10], lineWidth:3, lineCap:'Round'},
                  mupdfjs.Matrix.identity,
                  mupdfjs.ColorSpace.DeviceRGB,
                  [0,1,0],
                  0.5);
clipPath(path: Path, evenOdd: boolean, ctm: Matrix)#

Clip a path.

Parameters:

EXAMPLE

device.clipPath(path, true, mupdfjs.Matrix.identity);
clipStrokePath(path: Path, stroke: StrokeState, ctm: Matrix)#

Clip & stroke a path.

Parameters:

EXAMPLE

device.clipStrokePath(path, true, mupdfjs.Matrix.identity);
fillText(text: Text, ctm: Matrix, colorspace: ColorSpace, color: Color, alpha: number)#

Fill a text object.

Parameters:

EXAMPLE

device.fillText(text, mupdfjs.Matrix.identity, mupdfjs.ColorSpace.DeviceRGB, [1,0,0], 1);
strokeText(text: Text, stroke: StrokeState, ctm: Matrix, colorspace: ColorSpace, color: Color, alpha: number)#

Stroke a text object.

Parameters:

EXAMPLE

device.strokeText(text,
                  {dashes:[5,10], lineWidth:3, lineCap:'Round'},
                  mupdfjs.Matrix.identity, mupdfjs.ColorSpace.DeviceRGB,
                  [1,0,0],
                  1);
clipText(text: Text, ctm: Matrix)#

Clip a text object.

Parameters:

EXAMPLE

device.clipText(text, mupdfjs.Matrix.identity);
clipStrokeText(text: Text, stroke: StrokeState, ctm: Matrix)#

Clip & stroke a text object.

Parameters:

EXAMPLE

device.clipStrokeText(text, {dashes:[5,10], lineWidth:3, lineCap:'Round'},  mupdfjs.Matrix.identity);
ignoreText(text: Text, ctm: Matrix)#

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

Parameters:

EXAMPLE

device.ignoreText(text, mupdfjs.Matrix.identity);
fillShade(shade: Shade, ctm: Matrix, alpha: number)#

Fill a shade (a.k.a. gradient).

Note

The details of gradient fills are not exposed to JavaScript yet.

Parameters:
  • shadeShade. The gradient.

  • ctmMatrix.

  • alphanumber. The alpha.

EXAMPLE

device.fillShade(shade, mupdfjs.Matrix.identity, true, {overPrinting:true});
fillImage(image: Image, ctm: Matrix, alpha: number)#

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.

Parameters:

EXAMPLE

device.fillImage(image, mupdfjs.Matrix.identity, false, {overPrinting:true});
fillImageMask(image: Image, ctm: Matrix, colorspace: ColorSpace, color: Color, alpha: number)#

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

Parameters:

EXAMPLE

device.fillImageMask(image, mupdfjs.Matrix.identity, mupdfjs.ColorSpace.DeviceRGB, [0,1,0], true);
clipImageMask(image: Image, ctm: Matrix)#

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

Parameters:

EXAMPLE

device.clipImageMask(image, mupdfjs.Matrix.identity);
popClip()#

Pop the clip mask installed by the last clipping operation.

EXAMPLE

device.popClip();
beginMask(area: Rect, luminosity: boolean, colorspace: ColorSpace, color: Color)#

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

Parameters:
  • areaRect. Mask area.

  • luminosityboolean. If luminosity is true, the mask is derived from the luminosity (grayscale value) of the graphics drawn; otherwise the color is ignored completely and the mask is derived from the alpha of the group.

  • colorspaceColorSpace.

  • colorColor.

EXAMPLE

device.beginMask([0,0,100,100], true, mupdfjs.ColorSpace.DeviceRGB, [1,0,1]);
endMask()#

Ends the mask.

EXAMPLE

device.endMask();
beginGroup(area: Rect, colorspace: ColorSpace, isolated: boolean, knockout: boolean, blendmode: BlendMode, alpha: number)#

Push/pop a transparency blending group. See the PDF reference for details on isolated and knockout.

Parameters:
  • areaRect. The blend area.

  • colorspaceColorSpace.

  • isolatedboolean.

  • knockoutboolean.

  • blendmodeBlendMode is one of the standard PDF blend modes.

  • alphanu,ber. The alpha.

../_images/isolated-and-knockout.png

EXAMPLE

device.beginGroup([0,0,100,100], mupdfjs.ColorSpace.DeviceRGB, true, true, "Multiply", 0.5);
endGroup()#

Ends the blending group.

EXAMPLE

device.endGroup();
beginTile(area: Rect, view: Rect, xstep: number, ystep: number, ctm: Matrix, id: number)#

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.

Parameters:
  • areaRect.

  • viewRect.

  • xstepnumber representing x step.

  • ystepnumber representing y step.

  • ctmMatrix.

  • idnumber. The purpose of id is to allow for efficient caching of rendered tiles. If id is 0, then no caching is performed. If it is non-zero, then it assumed to uniquely identify this tile.

EXAMPLE

device.beginTile([0,0,100,100], [100,100,200,200], 10, 10, mupdfjs.Matrix.identity, 0);
endTile()#

Ends the tiling pattern.

EXAMPLE

device.endTile();
beginLayer(name: string)#

Begin a marked-content layer with the given name.

Parameters:

namestring.

EXAMPLE

device.beginLayer("my tag");
endLayer()#

End a marked-content layer.

EXAMPLE

device.endLayer();
close()#

Tell the device that we are done, and flush any pending output. Ensure that no items are left on the stack before closing.

EXAMPLE

device.close();

Code samples

Code samples are in TypeScript and assume that the following requirements are defined in your TypeScript file header as follows:

import * as fs from "fs"
import * as mupdfjs from "mupdf/mupdfjs"