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