summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/pdf/ink/ink_api.js
blob: 04e0d56dcf692e807be75cb02ad037745c5c6fce (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @typedef {{
 *   tool: string,
 *   size: number,
 *   color: string,
 * }}
 */
let AnnotationTool;

/**
 * @typedef {{
 *   canUndo: boolean,
 *   canRedo: boolean,
 * }}
 */
let UndoState;

/**
 * Wraps the Ink component with an API that can be called
 * across an IFrame boundary.
 */
class InkAPI {
  /** @param {!ink.embed.EmbedComponent} embed */
  constructor(embed) {
    this.embed_ = embed;
    this.brush_ = ink.BrushModel.getInstance(embed);
    this.camera_ = null;
  }

  /** @param {function(!UndoState)} listener */
  addUndoStateListener(listener) {
    /** @param {!ink.UndoStateChangeEvent} e */
    function wrapper(e) {
      listener({
        canUndo: e.getCanUndo(),
        canRedo: e.getCanRedo(),
      });
    }

    this.embed_.addEventListener(ink.UndoStateChangeEvent.EVENT_TYPE, wrapper);
  }

  /**
   * @param {!ArrayBuffer} buffer
   */
  setPDF(buffer) {
    // We change the type from ArrayBuffer to Uint8Array due to the consequences
    // of the buffer being passed across the iframe boundary. This realm has a
    // different ArrayBuffer constructor than `buffer`.
    // TODO(dstockwell): Update Ink to allow Uint8Array here.
    this.embed_.setPDF(
        /** @type {!ArrayBuffer} */ (
            /** @type {!*} */ (new Uint8Array(buffer))));
  }

  /**
   * @return {!Promise<Uint8Array>}
   */
  getPDF() {
    return this.embed_.getPDF();
  }

  /**
   * @return {!Uint8Array}
   */
  getPDFDestructive() {
    return this.embed_.getPDFDestructive();
  }

  async setCamera(camera) {
    this.camera_ = camera;
    this.embed_.setCamera(camera);
    // Wait for the next task to avoid a race where Ink drops the camera value
    // when the canvas is rotated in low-latency mode.
    setTimeout(() => this.embed_.setCamera(this.camera_), 0);
  }

  /** @param {AnnotationTool} tool */
  setAnnotationTool(tool) {
    const shape = {
      eraser: 'MAGIC_ERASE',
      pen: 'INKPEN',
      highlighter: 'SMART_HIGHLIGHTER_TOOL',
    }[tool.tool];
    this.brush_.setShape(shape);
    if (tool.tool != 'eraser') {
      this.brush_.setColor(tool.color);
    }
    this.brush_.setStrokeWidth(tool.size);
  }

  flush() {
    return new Promise(resolve => this.embed_.flush(resolve));
  }

  /** @param {string} hexColor */
  setOutOfBoundsColor(hexColor) {
    this.embed_.setOutOfBoundsColor(ink.Color.fromString(hexColor));
  }

  /** @param {string} url */
  setBorderImage(url) {
    this.embed_.setBorderImage(url);
  }

  /** @param {number} spacing in points */
  setPageSpacing(spacing) {
    this.embed_.setVerticalPageLayout(spacing);
  }

  dispatchPointerEvent(type, init) {
    const engine = document.querySelector('#ink-engine');
    const match = engine.style.transform.match(/(\d+)deg/);
    const rotation = match ? Number(match[1]) : 0;
    let offsetX = init.clientX;
    let offsetY = init.clientY;
    // If Ink's canvas has been re-orientated away from 0, we must transform
    // the event's offsetX and offsetY to correspond with the rotation and
    // offset applied.
    if ([90, 180, 270].includes(rotation)) {
      const width = window.innerWidth;
      const height = window.innerHeight;
      const matrix = new DOMMatrix();
      matrix.translateSelf(width / 2, height / 2);
      matrix.rotateSelf(0, 0, -rotation);
      matrix.translateSelf(-width / 2, -height / 2);
      const result = matrix.transformPoint({x: offsetX, y: offsetY});
      offsetX = result.x - engine.offsetLeft;
      offsetY = result.y - engine.offsetTop;
    }

    const event = new PointerEvent(type, init);
    // Ink uses offsetX and offsetY, but we can only override them, not pass
    // as part of the init.
    Object.defineProperty(event, 'offsetX', {value: offsetX});
    Object.defineProperty(event, 'offsetY', {value: offsetY});
    engine.dispatchEvent(event);
  }

  undo() {
    this.embed_.undo();
  }

  redo() {
    this.embed_.redo();
  }
}

/** @return {Promise<InkAPI>} */
window.initInk = async function() {
  const config = new ink.embed.Config();
  const embed = await ink.embed.EmbedComponent.execute(config);
  embed.assignFlag(ink.proto.Flag.ENABLE_HOST_CAMERA_CONTROL, true);
  return new InkAPI(embed);
};