File: Device.rst

package info (click to toggle)
mupdf 1.27.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,224 kB
  • sloc: ansic: 335,320; python: 20,906; java: 7,520; javascript: 2,213; makefile: 1,152; xml: 675; cpp: 639; sh: 513; cs: 307; awk: 10; sed: 7; lisp: 3
file content (497 lines) | stat: -rw-r--r-- 12,731 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
.. default-domain:: js

.. highlight:: javascript

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.

	:param callbacks: object containing callback functions

You can create other devices with `DrawDevice` and `DisplayListDevice`.

Constants
---------

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

.. data:: Device.BLEND_NORMAL
.. data:: Device.BLEND_MULTIPLY
.. data:: Device.BLEND_SCREEN
.. data:: Device.BLEND_OVERLAY
.. data:: Device.BLEND_DARKEN
.. data:: Device.BLEND_LIGHTEN
.. data:: Device.BLEND_COLOR_DODGE
.. data:: Device.BLEND_COLOR_BURN
.. data:: Device.BLEND_HARD_LIGHT
.. data:: Device.BLEND_SOFT_LIGHT
.. data:: Device.BLEND_DIFFERENCE
.. data:: Device.BLEND_EXCLUSION
.. data:: Device.BLEND_HUE
.. data:: Device.BLEND_SATURATION
.. data:: Device.BLEND_COLOR
.. data:: Device.BLEND_LUMINOSITY

Instance methods
----------------

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

	.. code-block::

		device.close()

Line art
^^^^^^^^

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

	Fill a path.

	:param Path path: Path object.
	:param boolean evenOdd: Use :term:`even-odd rule` or :term:`non-zero winding number rule` to fill the path.
	:param Matrix ctm: The transform to apply.
	:param ColorSpace colorspace: The colorspace of the color to fill with.
	:param Color color: The color to fill the path with.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

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

	Stroke a path.

	:param Path path: Path object.
	:param StrokeState stroke: Stroke state.
	:param Matrix ctm: The transform to apply.
	:param ColorSpace colorspace: Colorspace.
	:param Color color: The color to stroke the path with.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

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

	Clip a path.

	:param Path path: Path object.
	:param boolean evenOdd: Use :term:`even-odd rule` or :term:`non-zero winding number rule` to fill the path.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

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

	Clip & stroke a path.

	:param Path path: Path object.
	:param StrokeState stroke: Stroke state.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

Text
^^^^

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

	Fill a text object.

	:param Text text: Text object.
	:param Matrix ctm: The transform to apply.
	:param ColorSpace colorspace: Colorspace
	:param Color color: The color used to fill the text.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

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

	Stroke a text object.

	:param Text text: Text object.
	:param StrokeState stroke: Stroke state.
	:param Matrix ctm: The transform to apply.
	:param ColorSpace colorspace: Colorspace
	:param Color color: The color used to stroke the text.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

.. method:: Device.prototype.clipText(text, ctm)

	Clip a text object.

	:param Text text: Text object.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

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

	Clip & stroke a text object.

	:param Text text: Text object.
	:param StrokeState stroke: stroke state.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

.. method:: Device.prototype.ignoreText(text, ctm)

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

	:param Text text: Text object.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

Shadings
^^^^^^^^

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

	Fill a shading, also known as a gradient.

	:param Shade shade: The gradient.
	:param Matrix ctm: The transform to apply.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

Images
^^^^^^

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

	:param Image image: Image object.
	:param Matrix ctm: The transform to apply.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

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

	:param Image image: Image object.
	:param Matrix ctm: The transform to apply.
	:param ColorSpace colorspace: Colorspace
	:param Color color: The color to be used.
	:param number alpha: The :term:`opacity`.

	.. code-block::

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

.. method:: Device.prototype.clipImageMask(image, ctm)

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

	:param Image image: Image object.
	:param Matrix ctm: The transform to apply.

	.. code-block::

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

Clipping and masking
^^^^^^^^^^^^^^^^^^^^

.. method:: Device.prototype.popClip()

	Pop the clip mask installed by the last clipping operation.

	.. code-block::

		device.popClip()

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

	:param Rect area: Mask area.
	:param boolean luminosity: 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.
	:param ColorSpace colorspace: Colorspace
	:param Color color: The color to be used.

	.. code-block::

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

.. method:: Device.prototype.endMask()

	Ends the mask.

	.. code-block::

		device.endMask()

Groups and transparency
^^^^^^^^^^^^^^^^^^^^^^^

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

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

	:param Rect area: The blend area.
	:param ColorSpace colorspace: Colorspace
	:param boolean isolated: Whether the group is isolated.
	:param boolean knockout: Whether the group is knockout.
	:param string blendmode: The blend mode used when compositing this group with its backdrop.
	:param number alpha: The :term:`opacity`.

	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:

	.. code-block::

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

.. method:: Device.prototype.endGroup()

	Ends the blending group.

	.. code-block::

		device.endGroup()

Tiling
^^^^^^

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

	:param Rect area: Area
	:param Rect view: View
	:param number xstep: x step.
	:param number ystep: y step.
	:param Matrix ctm: The transform to apply.
	:param number id:
	:param number doc_id:
		The purpose of id/doc_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 id/doc_id are assumed to uniquely
		identify this tile.

	.. code-block::

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

.. method:: Device.prototype.endTile()

	Ends the tiling pattern.

	.. code-block::

		device.endTile()

Render flags
^^^^^^^^^^^^

.. method:: Device.prototype.renderFlags(set, clear)

	|only_mutool|

	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:

	- "mask"
	- "color"
	- "uncacheable"
	- "fillcolor-undefined"
	- "strokecolor-undefined"
	- "startcap-undefined"
	- "dashcap-undefined"
	- "endcap-undefined"
	- "linejoin-undefined"
	- "miterlimit-undefined"
	- "linewidth-undefined"
	- "bbox-defined"
	- "gridfit-as-tiled"

	:param Array of string set: Rendering flags to set.
	:param Array of string clear: Rendering flags to clear.

	.. code-block::

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

Device colorspaces
^^^^^^^^^^^^^^^^^^

.. method:: Device.prototype.setDefaultColorSpaces(defaultCS)

	|only_mutool|

	Change the set of default device colorspaces to one given.

	:param DefaultColorSpaces defaultCS: The new set of default colorspaces.

	.. code-block::

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

Layers
^^^^^^

.. method:: Device.prototype.beginLayer(name)

	Begin a marked-content layer with the given name.

	:param string name: Name of this marked-content layer.

	.. code-block::

		device.beginLayer("my tag")

.. method:: Device.prototype.endLayer()

	End a marked-content layer.

	.. code-block::

		device.endLayer()

Structures
^^^^^^^^^^

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

	|only_mutool|

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

	:param string structure: One of the pre-defined structure types in PDF.
	:param string raw: The tag name.
	:param number index: A unique identifier.

	.. code-block::

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

.. method:: Device.prototype.endStructure()

	|only_mutool|

	End a standard structure element.

	.. code-block::

		device.endStructure()

Metatext
^^^^^^^^

.. method:: Device.prototype.beginMetatext(meta, text)

	|only_mutool|

	Begin meta text where meta is either of:

	- "ActualText"
	- "Alt"
	- "Abbreviation"
	- "Title"

	:param string meta: The meta text type
	:param string text: The text value.

	.. code-block::

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

.. method:: Device.prototype.endMetatext()

	|only_mutool|

	End meta text information.

	.. code-block::

		device.endMetatext()