2
0

base_factory.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.BaseStandardFontDataFactory = exports.BaseSVGFactory = exports.BaseCanvasFactory = exports.BaseCMapReaderFactory = void 0;
  27. var _util = require("../shared/util.js");
  28. class BaseCanvasFactory {
  29. constructor() {
  30. if (this.constructor === BaseCanvasFactory) {
  31. (0, _util.unreachable)("Cannot initialize BaseCanvasFactory.");
  32. }
  33. }
  34. create(width, height) {
  35. if (width <= 0 || height <= 0) {
  36. throw new Error("Invalid canvas size");
  37. }
  38. const canvas = this._createCanvas(width, height);
  39. return {
  40. canvas,
  41. context: canvas.getContext("2d")
  42. };
  43. }
  44. reset(canvasAndContext, width, height) {
  45. if (!canvasAndContext.canvas) {
  46. throw new Error("Canvas is not specified");
  47. }
  48. if (width <= 0 || height <= 0) {
  49. throw new Error("Invalid canvas size");
  50. }
  51. canvasAndContext.canvas.width = width;
  52. canvasAndContext.canvas.height = height;
  53. }
  54. destroy(canvasAndContext) {
  55. if (!canvasAndContext.canvas) {
  56. throw new Error("Canvas is not specified");
  57. }
  58. canvasAndContext.canvas.width = 0;
  59. canvasAndContext.canvas.height = 0;
  60. canvasAndContext.canvas = null;
  61. canvasAndContext.context = null;
  62. }
  63. _createCanvas(width, height) {
  64. (0, _util.unreachable)("Abstract method `_createCanvas` called.");
  65. }
  66. }
  67. exports.BaseCanvasFactory = BaseCanvasFactory;
  68. class BaseCMapReaderFactory {
  69. constructor({
  70. baseUrl = null,
  71. isCompressed = false
  72. }) {
  73. if (this.constructor === BaseCMapReaderFactory) {
  74. (0, _util.unreachable)("Cannot initialize BaseCMapReaderFactory.");
  75. }
  76. this.baseUrl = baseUrl;
  77. this.isCompressed = isCompressed;
  78. }
  79. async fetch({
  80. name
  81. }) {
  82. if (!this.baseUrl) {
  83. throw new Error('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.');
  84. }
  85. if (!name) {
  86. throw new Error("CMap name must be specified.");
  87. }
  88. const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
  89. const compressionType = this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE;
  90. return this._fetchData(url, compressionType).catch(reason => {
  91. throw new Error(`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`);
  92. });
  93. }
  94. _fetchData(url, compressionType) {
  95. (0, _util.unreachable)("Abstract method `_fetchData` called.");
  96. }
  97. }
  98. exports.BaseCMapReaderFactory = BaseCMapReaderFactory;
  99. class BaseStandardFontDataFactory {
  100. constructor({
  101. baseUrl = null
  102. }) {
  103. if (this.constructor === BaseStandardFontDataFactory) {
  104. (0, _util.unreachable)("Cannot initialize BaseStandardFontDataFactory.");
  105. }
  106. this.baseUrl = baseUrl;
  107. }
  108. async fetch({
  109. filename
  110. }) {
  111. if (!this.baseUrl) {
  112. throw new Error('The standard font "baseUrl" parameter must be specified, ensure that ' + 'the "standardFontDataUrl" API parameter is provided.');
  113. }
  114. if (!filename) {
  115. throw new Error("Font filename must be specified.");
  116. }
  117. const url = `${this.baseUrl}${filename}`;
  118. return this._fetchData(url).catch(reason => {
  119. throw new Error(`Unable to load font data at: ${url}`);
  120. });
  121. }
  122. _fetchData(url) {
  123. (0, _util.unreachable)("Abstract method `_fetchData` called.");
  124. }
  125. }
  126. exports.BaseStandardFontDataFactory = BaseStandardFontDataFactory;
  127. class BaseSVGFactory {
  128. constructor() {
  129. if (this.constructor === BaseSVGFactory) {
  130. (0, _util.unreachable)("Cannot initialize BaseSVGFactory.");
  131. }
  132. }
  133. create(width, height, skipDimensions = false) {
  134. if (width <= 0 || height <= 0) {
  135. throw new Error("Invalid SVG dimensions");
  136. }
  137. const svg = this._createSVG("svg:svg");
  138. svg.setAttribute("version", "1.1");
  139. if (!skipDimensions) {
  140. svg.setAttribute("width", `${width}px`);
  141. svg.setAttribute("height", `${height}px`);
  142. }
  143. svg.setAttribute("preserveAspectRatio", "none");
  144. svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
  145. return svg;
  146. }
  147. createElement(type) {
  148. if (typeof type !== "string") {
  149. throw new Error("Invalid SVG element type");
  150. }
  151. return this._createSVG(type);
  152. }
  153. _createSVG(type) {
  154. (0, _util.unreachable)("Abstract method `_createSVG` called.");
  155. }
  156. }
  157. exports.BaseSVGFactory = BaseSVGFactory;