app_options.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. annotationMode: {
  53. value: 2,
  54. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  55. },
  56. cursorToolOnLoad: {
  57. value: 0,
  58. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  59. },
  60. defaultUrl: {
  61. value: "compressed.tracemonkey-pldi-09.pdf",
  62. kind: OptionKind.VIEWER
  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. compatibility: compatibilityParams.maxCanvasPixels,
  111. kind: OptionKind.VIEWER
  112. },
  113. pageColorsBackground: {
  114. value: "Canvas",
  115. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  116. },
  117. pageColorsForeground: {
  118. value: "CanvasText",
  119. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  120. },
  121. pdfBugEnabled: {
  122. value: false,
  123. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  124. },
  125. printResolution: {
  126. value: 150,
  127. kind: OptionKind.VIEWER
  128. },
  129. renderer: {
  130. value: "canvas",
  131. kind: OptionKind.VIEWER
  132. },
  133. sidebarViewOnLoad: {
  134. value: -1,
  135. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  136. },
  137. scrollModeOnLoad: {
  138. value: -1,
  139. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  140. },
  141. spreadModeOnLoad: {
  142. value: -1,
  143. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  144. },
  145. textLayerMode: {
  146. value: 1,
  147. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  148. },
  149. useOnlyCssZoom: {
  150. value: false,
  151. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  152. },
  153. viewerCssTheme: {
  154. value: 0,
  155. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  156. },
  157. viewOnLoad: {
  158. value: 0,
  159. kind: OptionKind.VIEWER + OptionKind.PREFERENCE
  160. },
  161. cMapPacked: {
  162. value: true,
  163. kind: OptionKind.API
  164. },
  165. cMapUrl: {
  166. value: "../web/cmaps/",
  167. kind: OptionKind.API
  168. },
  169. disableAutoFetch: {
  170. value: false,
  171. kind: OptionKind.API + OptionKind.PREFERENCE
  172. },
  173. disableFontFace: {
  174. value: false,
  175. kind: OptionKind.API + OptionKind.PREFERENCE
  176. },
  177. disableRange: {
  178. value: false,
  179. kind: OptionKind.API + OptionKind.PREFERENCE
  180. },
  181. disableStream: {
  182. value: false,
  183. kind: OptionKind.API + OptionKind.PREFERENCE
  184. },
  185. docBaseUrl: {
  186. value: "",
  187. kind: OptionKind.API
  188. },
  189. enableXfa: {
  190. value: true,
  191. kind: OptionKind.API + OptionKind.PREFERENCE
  192. },
  193. fontExtraProperties: {
  194. value: false,
  195. kind: OptionKind.API
  196. },
  197. isEvalSupported: {
  198. value: true,
  199. kind: OptionKind.API
  200. },
  201. maxImageSize: {
  202. value: -1,
  203. kind: OptionKind.API
  204. },
  205. pdfBug: {
  206. value: false,
  207. kind: OptionKind.API
  208. },
  209. standardFontDataUrl: {
  210. value: "../web/standard_fonts/",
  211. kind: OptionKind.API
  212. },
  213. verbosity: {
  214. value: 1,
  215. kind: OptionKind.API
  216. },
  217. workerPort: {
  218. value: null,
  219. kind: OptionKind.WORKER
  220. },
  221. workerSrc: {
  222. value: "../build/pdf.worker.js",
  223. kind: OptionKind.WORKER
  224. }
  225. };
  226. {
  227. defaultOptions.disablePreferences = {
  228. value: false,
  229. kind: OptionKind.VIEWER
  230. };
  231. defaultOptions.locale = {
  232. value: navigator.language || "en-US",
  233. kind: OptionKind.VIEWER
  234. };
  235. defaultOptions.sandboxBundleSrc = {
  236. value: "../build/pdf.sandbox.js",
  237. kind: OptionKind.VIEWER
  238. };
  239. defaultOptions.renderer.kind += OptionKind.PREFERENCE;
  240. }
  241. const userOptions = Object.create(null);
  242. class AppOptions {
  243. constructor() {
  244. throw new Error("Cannot initialize AppOptions.");
  245. }
  246. static get(name) {
  247. const userOption = userOptions[name];
  248. if (userOption !== undefined) {
  249. return userOption;
  250. }
  251. const defaultOption = defaultOptions[name];
  252. if (defaultOption !== undefined) {
  253. return defaultOption.compatibility ?? defaultOption.value;
  254. }
  255. return undefined;
  256. }
  257. static getAll(kind = null) {
  258. const options = Object.create(null);
  259. for (const name in defaultOptions) {
  260. const defaultOption = defaultOptions[name];
  261. if (kind) {
  262. if ((kind & defaultOption.kind) === 0) {
  263. continue;
  264. }
  265. if (kind === OptionKind.PREFERENCE) {
  266. const value = defaultOption.value,
  267. valueType = typeof value;
  268. if (valueType === "boolean" || valueType === "string" || valueType === "number" && Number.isInteger(value)) {
  269. options[name] = value;
  270. continue;
  271. }
  272. throw new Error(`Invalid type for preference: ${name}`);
  273. }
  274. }
  275. const userOption = userOptions[name];
  276. options[name] = userOption !== undefined ? userOption : defaultOption.compatibility ?? defaultOption.value;
  277. }
  278. return options;
  279. }
  280. static set(name, value) {
  281. userOptions[name] = value;
  282. }
  283. static setAll(options) {
  284. for (const name in options) {
  285. userOptions[name] = options[name];
  286. }
  287. }
  288. static remove(name) {
  289. delete userOptions[name];
  290. }
  291. static _hasUserOptions() {
  292. return Object.keys(userOptions).length > 0;
  293. }
  294. }
  295. exports.AppOptions = AppOptions;