ink.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.InkEditor = void 0;
  27. Object.defineProperty(exports, "fitCurve", {
  28. enumerable: true,
  29. get: function () {
  30. return _fit_curve.fitCurve;
  31. }
  32. });
  33. var _util = require("../../shared/util.js");
  34. var _editor = require("./editor.js");
  35. var _fit_curve = require("./fit_curve");
  36. var _tools = require("./tools.js");
  37. const RESIZER_SIZE = 16;
  38. const TIME_TO_WAIT_BEFORE_FIXING_DIMS = 100;
  39. class InkEditor extends _editor.AnnotationEditor {
  40. #aspectRatio = 0;
  41. #baseHeight = 0;
  42. #baseWidth = 0;
  43. #boundCanvasPointermove = this.canvasPointermove.bind(this);
  44. #boundCanvasPointerleave = this.canvasPointerleave.bind(this);
  45. #boundCanvasPointerup = this.canvasPointerup.bind(this);
  46. #boundCanvasPointerdown = this.canvasPointerdown.bind(this);
  47. #disableEditing = false;
  48. #isCanvasInitialized = false;
  49. #lastPoint = null;
  50. #observer = null;
  51. #realWidth = 0;
  52. #realHeight = 0;
  53. #requestFrameCallback = null;
  54. static _defaultColor = null;
  55. static _defaultOpacity = 1;
  56. static _defaultThickness = 1;
  57. static _l10nPromise;
  58. static _type = "ink";
  59. constructor(params) {
  60. super({
  61. ...params,
  62. name: "inkEditor"
  63. });
  64. this.color = params.color || null;
  65. this.thickness = params.thickness || null;
  66. this.opacity = params.opacity || null;
  67. this.paths = [];
  68. this.bezierPath2D = [];
  69. this.currentPath = [];
  70. this.scaleFactor = 1;
  71. this.translationX = this.translationY = 0;
  72. this.x = 0;
  73. this.y = 0;
  74. }
  75. static initialize(l10n) {
  76. this._l10nPromise = new Map(["editor_ink_canvas_aria_label", "editor_ink2_aria_label"].map(str => [str, l10n.get(str)]));
  77. }
  78. static updateDefaultParams(type, value) {
  79. switch (type) {
  80. case _util.AnnotationEditorParamsType.INK_THICKNESS:
  81. InkEditor._defaultThickness = value;
  82. break;
  83. case _util.AnnotationEditorParamsType.INK_COLOR:
  84. InkEditor._defaultColor = value;
  85. break;
  86. case _util.AnnotationEditorParamsType.INK_OPACITY:
  87. InkEditor._defaultOpacity = value / 100;
  88. break;
  89. }
  90. }
  91. updateParams(type, value) {
  92. switch (type) {
  93. case _util.AnnotationEditorParamsType.INK_THICKNESS:
  94. this.#updateThickness(value);
  95. break;
  96. case _util.AnnotationEditorParamsType.INK_COLOR:
  97. this.#updateColor(value);
  98. break;
  99. case _util.AnnotationEditorParamsType.INK_OPACITY:
  100. this.#updateOpacity(value);
  101. break;
  102. }
  103. }
  104. static get defaultPropertiesToUpdate() {
  105. return [[_util.AnnotationEditorParamsType.INK_THICKNESS, InkEditor._defaultThickness], [_util.AnnotationEditorParamsType.INK_COLOR, InkEditor._defaultColor || _editor.AnnotationEditor._defaultLineColor], [_util.AnnotationEditorParamsType.INK_OPACITY, Math.round(InkEditor._defaultOpacity * 100)]];
  106. }
  107. get propertiesToUpdate() {
  108. return [[_util.AnnotationEditorParamsType.INK_THICKNESS, this.thickness || InkEditor._defaultThickness], [_util.AnnotationEditorParamsType.INK_COLOR, this.color || InkEditor._defaultColor || _editor.AnnotationEditor._defaultLineColor], [_util.AnnotationEditorParamsType.INK_OPACITY, Math.round(100 * (this.opacity ?? InkEditor._defaultOpacity))]];
  109. }
  110. #updateThickness(thickness) {
  111. const savedThickness = this.thickness;
  112. this.parent.addCommands({
  113. cmd: () => {
  114. this.thickness = thickness;
  115. this.#fitToContent();
  116. },
  117. undo: () => {
  118. this.thickness = savedThickness;
  119. this.#fitToContent();
  120. },
  121. mustExec: true,
  122. type: _util.AnnotationEditorParamsType.INK_THICKNESS,
  123. overwriteIfSameType: true,
  124. keepUndo: true
  125. });
  126. }
  127. #updateColor(color) {
  128. const savedColor = this.color;
  129. this.parent.addCommands({
  130. cmd: () => {
  131. this.color = color;
  132. this.#redraw();
  133. },
  134. undo: () => {
  135. this.color = savedColor;
  136. this.#redraw();
  137. },
  138. mustExec: true,
  139. type: _util.AnnotationEditorParamsType.INK_COLOR,
  140. overwriteIfSameType: true,
  141. keepUndo: true
  142. });
  143. }
  144. #updateOpacity(opacity) {
  145. opacity /= 100;
  146. const savedOpacity = this.opacity;
  147. this.parent.addCommands({
  148. cmd: () => {
  149. this.opacity = opacity;
  150. this.#redraw();
  151. },
  152. undo: () => {
  153. this.opacity = savedOpacity;
  154. this.#redraw();
  155. },
  156. mustExec: true,
  157. type: _util.AnnotationEditorParamsType.INK_OPACITY,
  158. overwriteIfSameType: true,
  159. keepUndo: true
  160. });
  161. }
  162. rebuild() {
  163. super.rebuild();
  164. if (this.div === null) {
  165. return;
  166. }
  167. if (!this.canvas) {
  168. this.#createCanvas();
  169. this.#createObserver();
  170. }
  171. if (!this.isAttachedToDOM) {
  172. this.parent.add(this);
  173. this.#setCanvasDims();
  174. }
  175. this.#fitToContent();
  176. }
  177. remove() {
  178. if (this.canvas === null) {
  179. return;
  180. }
  181. if (!this.isEmpty()) {
  182. this.commit();
  183. }
  184. this.canvas.width = this.canvas.height = 0;
  185. this.canvas.remove();
  186. this.canvas = null;
  187. this.#observer.disconnect();
  188. this.#observer = null;
  189. super.remove();
  190. }
  191. enableEditMode() {
  192. if (this.#disableEditing || this.canvas === null) {
  193. return;
  194. }
  195. super.enableEditMode();
  196. this.div.draggable = false;
  197. this.canvas.addEventListener("pointerdown", this.#boundCanvasPointerdown);
  198. this.canvas.addEventListener("pointerup", this.#boundCanvasPointerup);
  199. }
  200. disableEditMode() {
  201. if (!this.isInEditMode() || this.canvas === null) {
  202. return;
  203. }
  204. super.disableEditMode();
  205. this.div.draggable = !this.isEmpty();
  206. this.div.classList.remove("editing");
  207. this.canvas.removeEventListener("pointerdown", this.#boundCanvasPointerdown);
  208. this.canvas.removeEventListener("pointerup", this.#boundCanvasPointerup);
  209. }
  210. onceAdded() {
  211. this.div.draggable = !this.isEmpty();
  212. }
  213. isEmpty() {
  214. return this.paths.length === 0 || this.paths.length === 1 && this.paths[0].length === 0;
  215. }
  216. #getInitialBBox() {
  217. const {
  218. width,
  219. height,
  220. rotation
  221. } = this.parent.viewport;
  222. switch (rotation) {
  223. case 90:
  224. return [0, width, width, height];
  225. case 180:
  226. return [width, height, width, height];
  227. case 270:
  228. return [height, 0, width, height];
  229. default:
  230. return [0, 0, width, height];
  231. }
  232. }
  233. #setStroke() {
  234. this.ctx.lineWidth = this.thickness * this.parent.scaleFactor / this.scaleFactor;
  235. this.ctx.lineCap = "round";
  236. this.ctx.lineJoin = "round";
  237. this.ctx.miterLimit = 10;
  238. this.ctx.strokeStyle = `${this.color}${(0, _tools.opacityToHex)(this.opacity)}`;
  239. }
  240. #startDrawing(x, y) {
  241. this.isEditing = true;
  242. if (!this.#isCanvasInitialized) {
  243. this.#isCanvasInitialized = true;
  244. this.#setCanvasDims();
  245. this.thickness ||= InkEditor._defaultThickness;
  246. this.color ||= InkEditor._defaultColor || _editor.AnnotationEditor._defaultLineColor;
  247. this.opacity ??= InkEditor._defaultOpacity;
  248. }
  249. this.currentPath.push([x, y]);
  250. this.#lastPoint = null;
  251. this.#setStroke();
  252. this.ctx.beginPath();
  253. this.ctx.moveTo(x, y);
  254. this.#requestFrameCallback = () => {
  255. if (!this.#requestFrameCallback) {
  256. return;
  257. }
  258. if (this.#lastPoint) {
  259. if (this.isEmpty()) {
  260. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  261. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  262. } else {
  263. this.#redraw();
  264. }
  265. this.ctx.lineTo(...this.#lastPoint);
  266. this.#lastPoint = null;
  267. this.ctx.stroke();
  268. }
  269. window.requestAnimationFrame(this.#requestFrameCallback);
  270. };
  271. window.requestAnimationFrame(this.#requestFrameCallback);
  272. }
  273. #draw(x, y) {
  274. const [lastX, lastY] = this.currentPath.at(-1);
  275. if (x === lastX && y === lastY) {
  276. return;
  277. }
  278. this.currentPath.push([x, y]);
  279. this.#lastPoint = [x, y];
  280. }
  281. #stopDrawing(x, y) {
  282. this.ctx.closePath();
  283. this.#requestFrameCallback = null;
  284. x = Math.min(Math.max(x, 0), this.canvas.width);
  285. y = Math.min(Math.max(y, 0), this.canvas.height);
  286. const [lastX, lastY] = this.currentPath.at(-1);
  287. if (x !== lastX || y !== lastY) {
  288. this.currentPath.push([x, y]);
  289. }
  290. let bezier;
  291. if (this.currentPath.length !== 1) {
  292. bezier = (0, _fit_curve.fitCurve)(this.currentPath, 30, null);
  293. } else {
  294. const xy = [x, y];
  295. bezier = [[xy, xy.slice(), xy.slice(), xy]];
  296. }
  297. const path2D = InkEditor.#buildPath2D(bezier);
  298. this.currentPath.length = 0;
  299. const cmd = () => {
  300. this.paths.push(bezier);
  301. this.bezierPath2D.push(path2D);
  302. this.rebuild();
  303. };
  304. const undo = () => {
  305. this.paths.pop();
  306. this.bezierPath2D.pop();
  307. if (this.paths.length === 0) {
  308. this.remove();
  309. } else {
  310. if (!this.canvas) {
  311. this.#createCanvas();
  312. this.#createObserver();
  313. }
  314. this.#fitToContent();
  315. }
  316. };
  317. this.parent.addCommands({
  318. cmd,
  319. undo,
  320. mustExec: true
  321. });
  322. }
  323. #redraw() {
  324. if (this.isEmpty()) {
  325. this.#updateTransform();
  326. return;
  327. }
  328. this.#setStroke();
  329. const {
  330. canvas,
  331. ctx
  332. } = this;
  333. ctx.setTransform(1, 0, 0, 1, 0, 0);
  334. ctx.clearRect(0, 0, canvas.width, canvas.height);
  335. this.#updateTransform();
  336. for (const path of this.bezierPath2D) {
  337. ctx.stroke(path);
  338. }
  339. }
  340. commit() {
  341. if (this.#disableEditing) {
  342. return;
  343. }
  344. super.commit();
  345. this.isEditing = false;
  346. this.disableEditMode();
  347. this.setInForeground();
  348. this.#disableEditing = true;
  349. this.div.classList.add("disabled");
  350. this.#fitToContent(true);
  351. this.parent.addInkEditorIfNeeded(true);
  352. this.parent.moveEditorInDOM(this);
  353. this.div.focus();
  354. }
  355. focusin(event) {
  356. super.focusin(event);
  357. this.enableEditMode();
  358. }
  359. canvasPointerdown(event) {
  360. if (event.button !== 0 || !this.isInEditMode() || this.#disableEditing) {
  361. return;
  362. }
  363. this.setInForeground();
  364. if (event.type !== "mouse") {
  365. this.div.focus();
  366. }
  367. event.stopPropagation();
  368. this.canvas.addEventListener("pointerleave", this.#boundCanvasPointerleave);
  369. this.canvas.addEventListener("pointermove", this.#boundCanvasPointermove);
  370. this.#startDrawing(event.offsetX, event.offsetY);
  371. }
  372. canvasPointermove(event) {
  373. event.stopPropagation();
  374. this.#draw(event.offsetX, event.offsetY);
  375. }
  376. canvasPointerup(event) {
  377. if (event.button !== 0) {
  378. return;
  379. }
  380. if (this.isInEditMode() && this.currentPath.length !== 0) {
  381. event.stopPropagation();
  382. this.#endDrawing(event);
  383. this.setInBackground();
  384. }
  385. }
  386. canvasPointerleave(event) {
  387. this.#endDrawing(event);
  388. this.setInBackground();
  389. }
  390. #endDrawing(event) {
  391. this.#stopDrawing(event.offsetX, event.offsetY);
  392. this.canvas.removeEventListener("pointerleave", this.#boundCanvasPointerleave);
  393. this.canvas.removeEventListener("pointermove", this.#boundCanvasPointermove);
  394. this.parent.addToAnnotationStorage(this);
  395. }
  396. #createCanvas() {
  397. this.canvas = document.createElement("canvas");
  398. this.canvas.width = this.canvas.height = 0;
  399. this.canvas.className = "inkEditorCanvas";
  400. InkEditor._l10nPromise.get("editor_ink_canvas_aria_label").then(msg => this.canvas?.setAttribute("aria-label", msg));
  401. this.div.append(this.canvas);
  402. this.ctx = this.canvas.getContext("2d");
  403. }
  404. #createObserver() {
  405. let timeoutId = null;
  406. this.#observer = new ResizeObserver(entries => {
  407. const rect = entries[0].contentRect;
  408. if (rect.width && rect.height) {
  409. if (timeoutId !== null) {
  410. clearTimeout(timeoutId);
  411. }
  412. timeoutId = setTimeout(() => {
  413. this.fixDims();
  414. timeoutId = null;
  415. }, TIME_TO_WAIT_BEFORE_FIXING_DIMS);
  416. this.setDimensions(rect.width, rect.height);
  417. }
  418. });
  419. this.#observer.observe(this.div);
  420. }
  421. render() {
  422. if (this.div) {
  423. return this.div;
  424. }
  425. let baseX, baseY;
  426. if (this.width) {
  427. baseX = this.x;
  428. baseY = this.y;
  429. }
  430. super.render();
  431. InkEditor._l10nPromise.get("editor_ink2_aria_label").then(msg => this.div?.setAttribute("aria-label", msg));
  432. const [x, y, w, h] = this.#getInitialBBox();
  433. this.setAt(x, y, 0, 0);
  434. this.setDims(w, h);
  435. this.#createCanvas();
  436. if (this.width) {
  437. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  438. this.setAt(baseX * parentWidth, baseY * parentHeight, this.width * parentWidth, this.height * parentHeight);
  439. this.#isCanvasInitialized = true;
  440. this.#setCanvasDims();
  441. this.setDims(this.width * parentWidth, this.height * parentHeight);
  442. this.#redraw();
  443. this.#setMinDims();
  444. this.div.classList.add("disabled");
  445. } else {
  446. this.div.classList.add("editing");
  447. this.enableEditMode();
  448. }
  449. this.#createObserver();
  450. return this.div;
  451. }
  452. #setCanvasDims() {
  453. if (!this.#isCanvasInitialized) {
  454. return;
  455. }
  456. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  457. this.canvas.width = Math.ceil(this.width * parentWidth);
  458. this.canvas.height = Math.ceil(this.height * parentHeight);
  459. this.#updateTransform();
  460. }
  461. setDimensions(width, height) {
  462. const roundedWidth = Math.round(width);
  463. const roundedHeight = Math.round(height);
  464. if (this.#realWidth === roundedWidth && this.#realHeight === roundedHeight) {
  465. return;
  466. }
  467. this.#realWidth = roundedWidth;
  468. this.#realHeight = roundedHeight;
  469. this.canvas.style.visibility = "hidden";
  470. if (this.#aspectRatio && Math.abs(this.#aspectRatio - width / height) > 1e-2) {
  471. height = Math.ceil(width / this.#aspectRatio);
  472. this.setDims(width, height);
  473. }
  474. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  475. this.width = width / parentWidth;
  476. this.height = height / parentHeight;
  477. if (this.#disableEditing) {
  478. this.#setScaleFactor(width, height);
  479. }
  480. this.#setCanvasDims();
  481. this.#redraw();
  482. this.canvas.style.visibility = "visible";
  483. }
  484. #setScaleFactor(width, height) {
  485. const padding = this.#getPadding();
  486. const scaleFactorW = (width - padding) / this.#baseWidth;
  487. const scaleFactorH = (height - padding) / this.#baseHeight;
  488. this.scaleFactor = Math.min(scaleFactorW, scaleFactorH);
  489. }
  490. #updateTransform() {
  491. const padding = this.#getPadding() / 2;
  492. this.ctx.setTransform(this.scaleFactor, 0, 0, this.scaleFactor, this.translationX * this.scaleFactor + padding, this.translationY * this.scaleFactor + padding);
  493. }
  494. static #buildPath2D(bezier) {
  495. const path2D = new Path2D();
  496. for (let i = 0, ii = bezier.length; i < ii; i++) {
  497. const [first, control1, control2, second] = bezier[i];
  498. if (i === 0) {
  499. path2D.moveTo(...first);
  500. }
  501. path2D.bezierCurveTo(control1[0], control1[1], control2[0], control2[1], second[0], second[1]);
  502. }
  503. return path2D;
  504. }
  505. #serializePaths(s, tx, ty, h) {
  506. const NUMBER_OF_POINTS_ON_BEZIER_CURVE = 4;
  507. const paths = [];
  508. const padding = this.thickness / 2;
  509. let buffer, points;
  510. for (const bezier of this.paths) {
  511. buffer = [];
  512. points = [];
  513. for (let i = 0, ii = bezier.length; i < ii; i++) {
  514. const [first, control1, control2, second] = bezier[i];
  515. const p10 = s * (first[0] + tx) + padding;
  516. const p11 = h - s * (first[1] + ty) - padding;
  517. const p20 = s * (control1[0] + tx) + padding;
  518. const p21 = h - s * (control1[1] + ty) - padding;
  519. const p30 = s * (control2[0] + tx) + padding;
  520. const p31 = h - s * (control2[1] + ty) - padding;
  521. const p40 = s * (second[0] + tx) + padding;
  522. const p41 = h - s * (second[1] + ty) - padding;
  523. if (i === 0) {
  524. buffer.push(p10, p11);
  525. points.push(p10, p11);
  526. }
  527. buffer.push(p20, p21, p30, p31, p40, p41);
  528. this.#extractPointsOnBezier(p10, p11, p20, p21, p30, p31, p40, p41, NUMBER_OF_POINTS_ON_BEZIER_CURVE, points);
  529. }
  530. paths.push({
  531. bezier: buffer,
  532. points
  533. });
  534. }
  535. return paths;
  536. }
  537. #extractPointsOnBezier(p10, p11, p20, p21, p30, p31, p40, p41, n, points) {
  538. if (this.#isAlmostFlat(p10, p11, p20, p21, p30, p31, p40, p41)) {
  539. points.push(p40, p41);
  540. return;
  541. }
  542. for (let i = 1; i < n - 1; i++) {
  543. const t = i / n;
  544. const mt = 1 - t;
  545. let q10 = t * p10 + mt * p20;
  546. let q11 = t * p11 + mt * p21;
  547. let q20 = t * p20 + mt * p30;
  548. let q21 = t * p21 + mt * p31;
  549. const q30 = t * p30 + mt * p40;
  550. const q31 = t * p31 + mt * p41;
  551. q10 = t * q10 + mt * q20;
  552. q11 = t * q11 + mt * q21;
  553. q20 = t * q20 + mt * q30;
  554. q21 = t * q21 + mt * q31;
  555. q10 = t * q10 + mt * q20;
  556. q11 = t * q11 + mt * q21;
  557. points.push(q10, q11);
  558. }
  559. points.push(p40, p41);
  560. }
  561. #isAlmostFlat(p10, p11, p20, p21, p30, p31, p40, p41) {
  562. const tol = 10;
  563. const ax = (3 * p20 - 2 * p10 - p40) ** 2;
  564. const ay = (3 * p21 - 2 * p11 - p41) ** 2;
  565. const bx = (3 * p30 - p10 - 2 * p40) ** 2;
  566. const by = (3 * p31 - p11 - 2 * p41) ** 2;
  567. return Math.max(ax, bx) + Math.max(ay, by) <= tol;
  568. }
  569. #getBbox() {
  570. let xMin = Infinity;
  571. let xMax = -Infinity;
  572. let yMin = Infinity;
  573. let yMax = -Infinity;
  574. for (const path of this.paths) {
  575. for (const [first, control1, control2, second] of path) {
  576. const bbox = _util.Util.bezierBoundingBox(...first, ...control1, ...control2, ...second);
  577. xMin = Math.min(xMin, bbox[0]);
  578. yMin = Math.min(yMin, bbox[1]);
  579. xMax = Math.max(xMax, bbox[2]);
  580. yMax = Math.max(yMax, bbox[3]);
  581. }
  582. }
  583. return [xMin, yMin, xMax, yMax];
  584. }
  585. #getPadding() {
  586. return this.#disableEditing ? Math.ceil(this.thickness * this.parent.scaleFactor) : 0;
  587. }
  588. #fitToContent(firstTime = false) {
  589. if (this.isEmpty()) {
  590. return;
  591. }
  592. if (!this.#disableEditing) {
  593. this.#redraw();
  594. return;
  595. }
  596. const bbox = this.#getBbox();
  597. const padding = this.#getPadding();
  598. this.#baseWidth = Math.max(RESIZER_SIZE, bbox[2] - bbox[0]);
  599. this.#baseHeight = Math.max(RESIZER_SIZE, bbox[3] - bbox[1]);
  600. const width = Math.ceil(padding + this.#baseWidth * this.scaleFactor);
  601. const height = Math.ceil(padding + this.#baseHeight * this.scaleFactor);
  602. const [parentWidth, parentHeight] = this.parent.viewportBaseDimensions;
  603. this.width = width / parentWidth;
  604. this.height = height / parentHeight;
  605. this.#aspectRatio = width / height;
  606. this.#setMinDims();
  607. const prevTranslationX = this.translationX;
  608. const prevTranslationY = this.translationY;
  609. this.translationX = -bbox[0];
  610. this.translationY = -bbox[1];
  611. this.#setCanvasDims();
  612. this.#redraw();
  613. this.#realWidth = width;
  614. this.#realHeight = height;
  615. this.setDims(width, height);
  616. const unscaledPadding = firstTime ? padding / this.scaleFactor / 2 : 0;
  617. this.translate(prevTranslationX - this.translationX - unscaledPadding, prevTranslationY - this.translationY - unscaledPadding);
  618. }
  619. #setMinDims() {
  620. const {
  621. style
  622. } = this.div;
  623. if (this.#aspectRatio >= 1) {
  624. style.minHeight = `${RESIZER_SIZE}px`;
  625. style.minWidth = `${Math.round(this.#aspectRatio * RESIZER_SIZE)}px`;
  626. } else {
  627. style.minWidth = `${RESIZER_SIZE}px`;
  628. style.minHeight = `${Math.round(RESIZER_SIZE / this.#aspectRatio)}px`;
  629. }
  630. }
  631. static deserialize(data, parent) {
  632. const editor = super.deserialize(data, parent);
  633. editor.thickness = data.thickness;
  634. editor.color = _util.Util.makeHexColor(...data.color);
  635. editor.opacity = data.opacity;
  636. const [pageWidth, pageHeight] = parent.pageDimensions;
  637. const width = editor.width * pageWidth;
  638. const height = editor.height * pageHeight;
  639. const scaleFactor = parent.scaleFactor;
  640. const padding = data.thickness / 2;
  641. editor.#aspectRatio = width / height;
  642. editor.#disableEditing = true;
  643. editor.#realWidth = Math.round(width);
  644. editor.#realHeight = Math.round(height);
  645. for (const {
  646. bezier
  647. } of data.paths) {
  648. const path = [];
  649. editor.paths.push(path);
  650. let p0 = scaleFactor * (bezier[0] - padding);
  651. let p1 = scaleFactor * (height - bezier[1] - padding);
  652. for (let i = 2, ii = bezier.length; i < ii; i += 6) {
  653. const p10 = scaleFactor * (bezier[i] - padding);
  654. const p11 = scaleFactor * (height - bezier[i + 1] - padding);
  655. const p20 = scaleFactor * (bezier[i + 2] - padding);
  656. const p21 = scaleFactor * (height - bezier[i + 3] - padding);
  657. const p30 = scaleFactor * (bezier[i + 4] - padding);
  658. const p31 = scaleFactor * (height - bezier[i + 5] - padding);
  659. path.push([[p0, p1], [p10, p11], [p20, p21], [p30, p31]]);
  660. p0 = p30;
  661. p1 = p31;
  662. }
  663. const path2D = this.#buildPath2D(path);
  664. editor.bezierPath2D.push(path2D);
  665. }
  666. const bbox = editor.#getBbox();
  667. editor.#baseWidth = Math.max(RESIZER_SIZE, bbox[2] - bbox[0]);
  668. editor.#baseHeight = Math.max(RESIZER_SIZE, bbox[3] - bbox[1]);
  669. editor.#setScaleFactor(width, height);
  670. return editor;
  671. }
  672. serialize() {
  673. if (this.isEmpty()) {
  674. return null;
  675. }
  676. const rect = this.getRect(0, 0);
  677. const height = this.rotation % 180 === 0 ? rect[3] - rect[1] : rect[2] - rect[0];
  678. const color = _editor.AnnotationEditor._colorManager.convert(this.ctx.strokeStyle);
  679. return {
  680. annotationType: _util.AnnotationEditorType.INK,
  681. color,
  682. thickness: this.thickness,
  683. opacity: this.opacity,
  684. paths: this.#serializePaths(this.scaleFactor / this.parent.scaleFactor, this.translationX, this.translationY, height),
  685. pageIndex: this.parent.pageIndex,
  686. rect,
  687. rotation: this.rotation
  688. };
  689. }
  690. }
  691. exports.InkEditor = InkEditor;