custom_spec.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. var _api = require("../../display/api.js");
  24. var _test_utils = require("./test_utils.js");
  25. function getTopLeftPixel(canvasContext) {
  26. const imgData = canvasContext.getImageData(0, 0, 1, 1);
  27. return {
  28. r: imgData.data[0],
  29. g: imgData.data[1],
  30. b: imgData.data[2],
  31. a: imgData.data[3]
  32. };
  33. }
  34. describe("custom canvas rendering", function () {
  35. const transparentGetDocumentParams = (0, _test_utils.buildGetDocumentParams)("transparent.pdf");
  36. let CanvasFactory;
  37. let loadingTask;
  38. let page;
  39. beforeAll(async function () {
  40. CanvasFactory = new _api.DefaultCanvasFactory();
  41. loadingTask = (0, _api.getDocument)(transparentGetDocumentParams);
  42. const doc = await loadingTask.promise;
  43. const data = await doc.getPage(1);
  44. page = data;
  45. });
  46. afterAll(async function () {
  47. CanvasFactory = null;
  48. page = null;
  49. await loadingTask.destroy();
  50. });
  51. it("renders to canvas with a default white background", async function () {
  52. const viewport = page.getViewport({
  53. scale: 1
  54. });
  55. const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  56. const renderTask = page.render({
  57. canvasContext: canvasAndCtx.context,
  58. viewport
  59. });
  60. await renderTask.promise;
  61. expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
  62. r: 255,
  63. g: 255,
  64. b: 255,
  65. a: 255
  66. });
  67. CanvasFactory.destroy(canvasAndCtx);
  68. });
  69. it("renders to canvas with a custom background", async function () {
  70. const viewport = page.getViewport({
  71. scale: 1
  72. });
  73. const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  74. const renderTask = page.render({
  75. canvasContext: canvasAndCtx.context,
  76. viewport,
  77. background: "rgba(255,0,0,1.0)"
  78. });
  79. await renderTask.promise;
  80. expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
  81. r: 255,
  82. g: 0,
  83. b: 0,
  84. a: 255
  85. });
  86. CanvasFactory.destroy(canvasAndCtx);
  87. });
  88. });
  89. describe("custom ownerDocument", function () {
  90. const FontFace = globalThis.FontFace;
  91. const checkFont = font => /g_d\d+_f1/.test(font.family);
  92. const checkFontFaceRule = rule => /^@font-face {font-family:"g_d\d+_f1";src:/.test(rule);
  93. beforeEach(() => {
  94. globalThis.FontFace = function MockFontFace(name) {
  95. this.family = name;
  96. };
  97. });
  98. afterEach(() => {
  99. globalThis.FontFace = FontFace;
  100. });
  101. function getMocks() {
  102. const elements = [];
  103. const createElement = name => {
  104. let element = typeof document !== "undefined" && document.createElement(name);
  105. if (name === "style") {
  106. element = {
  107. tagName: name,
  108. sheet: {
  109. cssRules: [],
  110. insertRule(rule) {
  111. this.cssRules.push(rule);
  112. }
  113. }
  114. };
  115. Object.assign(element, {
  116. remove() {
  117. this.remove.called = true;
  118. }
  119. });
  120. }
  121. elements.push(element);
  122. return element;
  123. };
  124. const ownerDocument = {
  125. fonts: new Set(),
  126. createElement,
  127. documentElement: {
  128. getElementsByTagName: () => [{
  129. appendChild: () => {}
  130. }]
  131. }
  132. };
  133. const CanvasFactory = new _api.DefaultCanvasFactory({
  134. ownerDocument
  135. });
  136. return {
  137. elements,
  138. ownerDocument,
  139. CanvasFactory
  140. };
  141. }
  142. it("should use given document for loading fonts (with Font Loading API)", async function () {
  143. const {
  144. ownerDocument,
  145. elements,
  146. CanvasFactory
  147. } = getMocks();
  148. const getDocumentParams = (0, _test_utils.buildGetDocumentParams)("TrueType_without_cmap.pdf", {
  149. disableFontFace: false,
  150. ownerDocument
  151. });
  152. const loadingTask = (0, _api.getDocument)(getDocumentParams);
  153. const doc = await loadingTask.promise;
  154. const page = await doc.getPage(1);
  155. const viewport = page.getViewport({
  156. scale: 1
  157. });
  158. const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  159. await page.render({
  160. canvasContext: canvasAndCtx.context,
  161. viewport
  162. }).promise;
  163. const style = elements.find(element => element.tagName === "style");
  164. expect(style).toBeFalsy();
  165. expect(ownerDocument.fonts.size).toBeGreaterThanOrEqual(1);
  166. expect(Array.from(ownerDocument.fonts).find(checkFont)).toBeTruthy();
  167. await doc.destroy();
  168. await loadingTask.destroy();
  169. CanvasFactory.destroy(canvasAndCtx);
  170. expect(ownerDocument.fonts.size).toBe(0);
  171. });
  172. it("should use given document for loading fonts (with CSS rules)", async function () {
  173. const {
  174. ownerDocument,
  175. elements,
  176. CanvasFactory
  177. } = getMocks();
  178. ownerDocument.fonts = null;
  179. const getDocumentParams = (0, _test_utils.buildGetDocumentParams)("TrueType_without_cmap.pdf", {
  180. disableFontFace: false,
  181. ownerDocument
  182. });
  183. const loadingTask = (0, _api.getDocument)(getDocumentParams);
  184. const doc = await loadingTask.promise;
  185. const page = await doc.getPage(1);
  186. const viewport = page.getViewport({
  187. scale: 1
  188. });
  189. const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  190. await page.render({
  191. canvasContext: canvasAndCtx.context,
  192. viewport
  193. }).promise;
  194. const style = elements.find(element => element.tagName === "style");
  195. expect(style.sheet.cssRules.length).toBeGreaterThanOrEqual(1);
  196. expect(style.sheet.cssRules.find(checkFontFaceRule)).toBeTruthy();
  197. await doc.destroy();
  198. await loadingTask.destroy();
  199. CanvasFactory.destroy(canvasAndCtx);
  200. expect(style.remove.called).toBe(true);
  201. });
  202. });