app_options.js 7.4 KB

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