app_options.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.compatibilityParams = exports.OptionKind = exports.AppOptions = void 0;
  27. const compatibilityParams = Object.create(null);
  28. exports.compatibilityParams = compatibilityParams;
  29. {
  30. if (typeof navigator === "undefined") {
  31. globalThis.navigator = Object.create(null);
  32. }
  33. const userAgent = navigator.userAgent || "";
  34. const platform = navigator.platform || "";
  35. const maxTouchPoints = navigator.maxTouchPoints || 1;
  36. const isAndroid = /Android/.test(userAgent);
  37. const isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent) || platform === "MacIntel" && maxTouchPoints > 1;
  38. (function checkCanvasSizeLimitation() {
  39. if (isIOS || isAndroid) {
  40. compatibilityParams.maxCanvasPixels = 5242880;
  41. }
  42. })();
  43. }
  44. const OptionKind = {
  45. VIEWER: 0x02,
  46. API: 0x04,
  47. WORKER: 0x08,
  48. PREFERENCE: 0x80
  49. };
  50. exports.OptionKind = OptionKind;
  51. const defaultOptions = {
  52. annotationEditorMode: {
  53. value: 0,
  54. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  55. },
  56. annotationMode: {
  57. value: 2,
  58. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  59. },
  60. cursorToolOnLoad: {
  61. value: 0,
  62. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  63. },
  64. defaultZoomValue: {
  65. value: "",
  66. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  67. },
  68. disableHistory: {
  69. value: false,
  70. kind: OptionKind.VIEWER
  71. },
  72. disablePageLabels: {
  73. value: false,
  74. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  75. },
  76. enablePermissions: {
  77. value: false,
  78. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  79. },
  80. enablePrintAutoRotate: {
  81. value: true,
  82. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  83. },
  84. enableScripting: {
  85. value: true,
  86. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  87. },
  88. externalLinkRel: {
  89. value: "noopener noreferrer nofollow",
  90. kind: OptionKind.VIEWER
  91. },
  92. externalLinkTarget: {
  93. value: 0,
  94. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  95. },
  96. historyUpdateUrl: {
  97. value: false,
  98. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  99. },
  100. ignoreDestinationZoom: {
  101. value: false,
  102. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  103. },
  104. imageResourcesPath: {
  105. value: "./images/",
  106. kind: OptionKind.VIEWER
  107. },
  108. maxCanvasPixels: {
  109. value: 16777216,
  110. kind: OptionKind.VIEWER
  111. },
  112. forcePageColors: {
  113. value: false,
  114. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  115. },
  116. pageColorsBackground: {
  117. value: "Canvas",
  118. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  119. },
  120. pageColorsForeground: {
  121. value: "CanvasText",
  122. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  123. },
  124. pdfBugEnabled: {
  125. value: false,
  126. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  127. },
  128. printResolution: {
  129. value: 150,
  130. kind: OptionKind.VIEWER
  131. },
  132. sidebarViewOnLoad: {
  133. value: -1,
  134. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  135. },
  136. scrollModeOnLoad: {
  137. value: -1,
  138. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  139. },
  140. spreadModeOnLoad: {
  141. value: -1,
  142. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  143. },
  144. textLayerMode: {
  145. value: 1,
  146. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  147. },
  148. useOnlyCssZoom: {
  149. value: false,
  150. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  151. },
  152. viewerCssTheme: {
  153. value: 0,
  154. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  155. },
  156. viewOnLoad: {
  157. value: 0,
  158. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  159. },
  160. cMapPacked: {
  161. value: true,
  162. kind: OptionKind.API
  163. },
  164. cMapUrl: {
  165. value: "../web/cmaps/",
  166. kind: OptionKind.API
  167. },
  168. disableAutoFetch: {
  169. value: false,
  170. kind: OptionKind.API + OptionKind.PREFERENCE
  171. },
  172. disableFontFace: {
  173. value: false,
  174. kind: OptionKind.API + OptionKind.PREFERENCE
  175. },
  176. disableRange: {
  177. value: false,
  178. kind: OptionKind.API + OptionKind.PREFERENCE
  179. },
  180. disableStream: {
  181. value: false,
  182. kind: OptionKind.API + OptionKind.PREFERENCE
  183. },
  184. docBaseUrl: {
  185. value: "",
  186. kind: OptionKind.API
  187. },
  188. enableXfa: {
  189. value: true,
  190. kind: OptionKind.API + OptionKind.PREFERENCE
  191. },
  192. fontExtraProperties: {
  193. value: false,
  194. kind: OptionKind.API
  195. },
  196. isEvalSupported: {
  197. value: true,
  198. kind: OptionKind.API
  199. },
  200. isOffscreenCanvasSupported: {
  201. value: true,
  202. kind: OptionKind.API
  203. },
  204. maxImageSize: {
  205. value: -1,
  206. kind: OptionKind.API
  207. },
  208. pdfBug: {
  209. value: false,
  210. kind: OptionKind.API
  211. },
  212. standardFontDataUrl: {
  213. value: "../web/standard_fonts/",
  214. kind: OptionKind.API
  215. },
  216. verbosity: {
  217. value: 1,
  218. kind: OptionKind.API
  219. },
  220. workerPort: {
  221. value: null,
  222. kind: OptionKind.WORKER
  223. },
  224. workerSrc: {
  225. value: "../build/pdf.worker.js",
  226. kind: OptionKind.WORKER
  227. }
  228. };
  229. {
  230. defaultOptions.defaultUrl = {
  231. value: "compressed.tracemonkey-pldi-09.pdf",
  232. kind: OptionKind.VIEWER
  233. };
  234. defaultOptions.disablePreferences = {
  235. value: false,
  236. kind: OptionKind.VIEWER
  237. };
  238. defaultOptions.locale = {
  239. value: navigator.language || "en-US",
  240. kind: OptionKind.VIEWER
  241. };
  242. defaultOptions.renderer = {
  243. value: "canvas",
  244. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  245. };
  246. defaultOptions.sandboxBundleSrc = {
  247. value: "../build/pdf.sandbox.js",
  248. kind: OptionKind.VIEWER
  249. };
  250. }
  251. const userOptions = Object.create(null);
  252. class AppOptions {
  253. constructor() {
  254. throw new Error("Cannot initialize AppOptions.");
  255. }
  256. static get(name) {
  257. const userOption = userOptions[name];
  258. if (userOption !== undefined) {
  259. return userOption;
  260. }
  261. const defaultOption = defaultOptions[name];
  262. if (defaultOption !== undefined) {
  263. return compatibilityParams[name] ?? defaultOption.value;
  264. }
  265. return undefined;
  266. }
  267. static getAll(kind = null) {
  268. const options = Object.create(null);
  269. for (const name in defaultOptions) {
  270. const defaultOption = defaultOptions[name];
  271. if (kind) {
  272. if ((kind & defaultOption.kind) === 0) {
  273. continue;
  274. }
  275. if (kind === OptionKind.PREFERENCE) {
  276. const value = defaultOption.value,
  277. valueType = typeof value;
  278. if (valueType === "boolean" || valueType === "string" || valueType === "number" && Number.isInteger(value)) {
  279. options[name] = value;
  280. continue;
  281. }
  282. throw new Error(`Invalid type for preference: ${name}`);
  283. }
  284. }
  285. const userOption = userOptions[name];
  286. options[name] = userOption !== undefined ? userOption : compatibilityParams[name] ?? defaultOption.value;
  287. }
  288. return options;
  289. }
  290. static set(name, value) {
  291. userOptions[name] = value;
  292. }
  293. static setAll(options) {
  294. for (const name in options) {
  295. userOptions[name] = options[name];
  296. }
  297. }
  298. static remove(name) {
  299. delete userOptions[name];
  300. }
  301. static _hasUserOptions() {
  302. return Object.keys(userOptions).length > 0;
  303. }
  304. }
  305. exports.AppOptions = AppOptions;