app_options.js 7.6 KB

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