custom_spec.js 6.8 KB

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