mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Add support of Ink annotation
This commit is contained in:
parent
1cfb723dd4
commit
b5806735d8
7 changed files with 173 additions and 1 deletions
|
@ -106,6 +106,9 @@ class AnnotationFactory {
|
|||
case 'Polygon':
|
||||
return new PolygonAnnotation(parameters);
|
||||
|
||||
case 'Ink':
|
||||
return new InkAnnotation(parameters);
|
||||
|
||||
case 'Highlight':
|
||||
return new HighlightAnnotation(parameters);
|
||||
|
||||
|
@ -1013,6 +1016,34 @@ class PolygonAnnotation extends PolylineAnnotation {
|
|||
}
|
||||
}
|
||||
|
||||
class InkAnnotation extends Annotation {
|
||||
constructor(parameters) {
|
||||
super(parameters);
|
||||
|
||||
this.data.annotationType = AnnotationType.INK;
|
||||
|
||||
let dict = parameters.dict;
|
||||
const xref = parameters.xref;
|
||||
|
||||
let originalInkLists = dict.getArray('InkList');
|
||||
this.data.inkLists = [];
|
||||
for (let i = 0, ii = originalInkLists.length; i < ii; ++i) {
|
||||
// The raw ink lists array contains arrays of numbers representing
|
||||
// the alternating horizontal and vertical coordinates, respectively,
|
||||
// of each vertex. Convert this to an array of objects with x and y
|
||||
// coordinates.
|
||||
this.data.inkLists.push([]);
|
||||
for (let j = 0, jj = originalInkLists[i].length; j < jj; j += 2) {
|
||||
this.data.inkLists[i].push({
|
||||
x: xref.fetchIfRef(originalInkLists[i][j]),
|
||||
y: xref.fetchIfRef(originalInkLists[i][j + 1]),
|
||||
});
|
||||
}
|
||||
}
|
||||
this._preparePopup(dict);
|
||||
}
|
||||
}
|
||||
|
||||
class HighlightAnnotation extends Annotation {
|
||||
constructor(parameters) {
|
||||
super(parameters);
|
||||
|
|
|
@ -83,6 +83,9 @@ class AnnotationElementFactory {
|
|||
case AnnotationType.POLYLINE:
|
||||
return new PolylineAnnotationElement(parameters);
|
||||
|
||||
case AnnotationType.INK:
|
||||
return new InkAnnotationElement(parameters);
|
||||
|
||||
case AnnotationType.POLYGON:
|
||||
return new PolygonAnnotationElement(parameters);
|
||||
|
||||
|
@ -628,7 +631,14 @@ class PopupAnnotationElement extends AnnotationElement {
|
|||
render() {
|
||||
// Do not render popup annotations for parent elements with these types as
|
||||
// they create the popups themselves (because of custom trigger divs).
|
||||
const IGNORE_TYPES = ['Line', 'Square', 'Circle', 'PolyLine', 'Polygon'];
|
||||
const IGNORE_TYPES = [
|
||||
'Line',
|
||||
'Square',
|
||||
'Circle',
|
||||
'PolyLine',
|
||||
'Polygon',
|
||||
'Ink',
|
||||
];
|
||||
|
||||
this.container.className = 'popupAnnotation';
|
||||
|
||||
|
@ -1006,6 +1016,73 @@ class PolygonAnnotationElement extends PolylineAnnotationElement {
|
|||
}
|
||||
}
|
||||
|
||||
class InkAnnotationElement extends AnnotationElement {
|
||||
constructor(parameters) {
|
||||
let isRenderable = !!(parameters.data.hasPopup ||
|
||||
parameters.data.title || parameters.data.contents);
|
||||
super(parameters, isRenderable, /* ignoreBorder = */ true);
|
||||
|
||||
this.containerClassName = 'inkAnnotation';
|
||||
|
||||
// Use the polyline SVG element since it allows us to use coordinates
|
||||
// directly and to draw both straight lines and curves.
|
||||
this.svgElementName = 'svg:polyline';
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the ink annotation's HTML element in the empty container.
|
||||
*
|
||||
* @public
|
||||
* @memberof InkAnnotationElement
|
||||
* @returns {HTMLSectionElement}
|
||||
*/
|
||||
render() {
|
||||
this.container.className = this.containerClassName;
|
||||
|
||||
// Create an invisible polyline with the same points that acts as the
|
||||
// trigger for the popup.
|
||||
let data = this.data;
|
||||
let width = data.rect[2] - data.rect[0];
|
||||
let height = data.rect[3] - data.rect[1];
|
||||
let svg = this.svgFactory.create(width, height);
|
||||
|
||||
let inkLists = data.inkLists;
|
||||
for (let i = 0, ii = inkLists.length; i < ii; i++) {
|
||||
let inkList = inkLists[i];
|
||||
let points = [];
|
||||
|
||||
// Convert the ink list to a single points string that the SVG
|
||||
// polyline element expects ("x1,y1 x2,y2 ..."). PDF coordinates are
|
||||
// calculated from a bottom left origin, so transform the polyline
|
||||
// coordinates to a top left origin for the SVG element.
|
||||
for (let j = 0, jj = inkList.length; j < jj; j++) {
|
||||
let x = inkList[j].x - data.rect[0];
|
||||
let y = data.rect[3] - inkList[j].y;
|
||||
points.push(x + ',' + y);
|
||||
}
|
||||
|
||||
points = points.join(' ');
|
||||
|
||||
let borderWidth = data.borderStyle.width;
|
||||
let polyline = this.svgFactory.createElement(this.svgElementName);
|
||||
polyline.setAttribute('points', points);
|
||||
polyline.setAttribute('stroke-width', borderWidth);
|
||||
polyline.setAttribute('stroke', 'transparent');
|
||||
polyline.setAttribute('fill', 'none');
|
||||
|
||||
// Create the popup ourselves so that we can bind it to the polyline
|
||||
// instead of to the entire container (which is the default).
|
||||
this._createPopup(this.container, polyline, data);
|
||||
|
||||
svg.appendChild(polyline);
|
||||
}
|
||||
|
||||
this.container.append(svg);
|
||||
|
||||
return this.container;
|
||||
}
|
||||
}
|
||||
|
||||
class HighlightAnnotationElement extends AnnotationElement {
|
||||
constructor(parameters) {
|
||||
let isRenderable = !!(parameters.data.hasPopup ||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue