custom_spec.js 6.4 KB

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