forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_document_properties.js
363 lines (325 loc) · 11.1 KB
/
pdf_document_properties.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @typedef {import("./event_utils.js").EventBus} EventBus */
/** @typedef {import("./interfaces.js").IL10n} IL10n */
/** @typedef {import("./overlay_manager.js").OverlayManager} OverlayManager */
// eslint-disable-next-line max-len
/** @typedef {import("../src/display/api.js").PDFDocumentProxy} PDFDocumentProxy */
import { getPageSizeInches, isPortraitOrientation } from "./ui_utils.js";
import { PDFDateString } from "pdfjs-lib";
const DEFAULT_FIELD_CONTENT = "-";
// See https://en.wikibooks.org/wiki/Lentis/Conversion_to_the_Metric_Standard_in_the_United_States
const NON_METRIC_LOCALES = ["en-us", "en-lr", "my"];
// Should use the format: `width x height`, in portrait orientation. The names,
// which are l10n-ids, should be lowercase.
// See https://en.wikipedia.org/wiki/Paper_size
const US_PAGE_NAMES = {
"8.5x11": "letter",
"8.5x14": "legal",
};
const METRIC_PAGE_NAMES = {
"297x420": "a-three",
"210x297": "a-four",
};
function getPageName(size, isPortrait, pageNames) {
const width = isPortrait ? size.width : size.height;
const height = isPortrait ? size.height : size.width;
return pageNames[`${width}x${height}`];
}
/**
* @typedef {Object} PDFDocumentPropertiesOptions
* @property {HTMLDialogElement} dialog - The overlay's DOM element.
* @property {Object} fields - Names and elements of the overlay's fields.
* @property {HTMLButtonElement} closeButton - Button for closing the overlay.
*/
class PDFDocumentProperties {
#fieldData = null;
/**
* @param {PDFDocumentPropertiesOptions} options
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
* @param {EventBus} eventBus - The application event bus.
* @param {IL10n} l10n - Localization service.
* @param {function} fileNameLookup - The function that is used to lookup
* the document fileName.
*/
constructor(
{ dialog, fields, closeButton },
overlayManager,
eventBus,
l10n,
fileNameLookup
) {
this.dialog = dialog;
this.fields = fields;
this.overlayManager = overlayManager;
this.l10n = l10n;
this._fileNameLookup = fileNameLookup;
this.#reset();
// Bind the event listener for the Close button.
closeButton.addEventListener("click", this.close.bind(this));
this.overlayManager.register(this.dialog);
eventBus._on("pagechanging", evt => {
this._currentPageNumber = evt.pageNumber;
});
eventBus._on("rotationchanging", evt => {
this._pagesRotation = evt.pagesRotation;
});
this._isNonMetricLocale = NON_METRIC_LOCALES.includes(l10n.getLanguage());
}
/**
* Open the document properties overlay.
*/
async open() {
await Promise.all([
this.overlayManager.open(this.dialog),
this._dataAvailableCapability.promise,
]);
const currentPageNumber = this._currentPageNumber;
const pagesRotation = this._pagesRotation;
// If the document properties were previously fetched (for this PDF file),
// just update the dialog immediately to avoid redundant lookups.
if (
this.#fieldData &&
currentPageNumber === this.#fieldData._currentPageNumber &&
pagesRotation === this.#fieldData._pagesRotation
) {
this.#updateUI();
return;
}
// Get the document properties.
const {
info,
/* metadata, */
/* contentDispositionFilename, */
contentLength,
} = await this.pdfDocument.getMetadata();
const [
fileName,
fileSize,
creationDate,
modificationDate,
pageSize,
isLinearized,
] = await Promise.all([
this._fileNameLookup(),
this.#parseFileSize(contentLength),
this.#parseDate(info.CreationDate),
this.#parseDate(info.ModDate),
// eslint-disable-next-line arrow-body-style
this.pdfDocument.getPage(currentPageNumber).then(pdfPage => {
return this.#parsePageSize(getPageSizeInches(pdfPage), pagesRotation);
}),
this.#parseLinearization(info.IsLinearized),
]);
this.#fieldData = Object.freeze({
fileName,
fileSize,
title: info.Title,
author: info.Author,
subject: info.Subject,
keywords: info.Keywords,
creationDate,
modificationDate,
creator: info.Creator,
producer: info.Producer,
version: info.PDFFormatVersion,
pageCount: this.pdfDocument.numPages,
pageSize,
linearized: isLinearized,
_currentPageNumber: currentPageNumber,
_pagesRotation: pagesRotation,
});
this.#updateUI();
// Get the correct fileSize, since it may not have been available
// or could potentially be wrong.
const { length } = await this.pdfDocument.getDownloadInfo();
if (contentLength === length) {
return; // The fileSize has already been correctly set.
}
const data = Object.assign(Object.create(null), this.#fieldData);
data.fileSize = await this.#parseFileSize(length);
this.#fieldData = Object.freeze(data);
this.#updateUI();
}
/**
* Close the document properties overlay.
*/
async close() {
this.overlayManager.close(this.dialog);
}
/**
* Set a reference to the PDF document in order to populate the dialog fields
* with the document properties. Note that the dialog will contain no
* information if this method is not called.
*
* @param {PDFDocumentProxy} pdfDocument - A reference to the PDF document.
*/
setDocument(pdfDocument) {
if (this.pdfDocument) {
this.#reset();
this.#updateUI(true);
}
if (!pdfDocument) {
return;
}
this.pdfDocument = pdfDocument;
this._dataAvailableCapability.resolve();
}
#reset() {
this.pdfDocument = null;
this.#fieldData = null;
this._dataAvailableCapability = Promise.withResolvers();
this._currentPageNumber = 1;
this._pagesRotation = 0;
}
/**
* Always updates all of the dialog fields, to prevent inconsistent UI state.
* NOTE: If the contents of a particular field is neither a non-empty string,
* nor a number, it will fall back to `DEFAULT_FIELD_CONTENT`.
*/
#updateUI(reset = false) {
if (reset || !this.#fieldData) {
for (const id in this.fields) {
this.fields[id].textContent = DEFAULT_FIELD_CONTENT;
}
return;
}
if (this.overlayManager.active !== this.dialog) {
// Don't bother updating the dialog if has already been closed,
// since it will be updated the next time `this.open` is called.
return;
}
for (const id in this.fields) {
const content = this.#fieldData[id];
this.fields[id].textContent =
content || content === 0 ? content : DEFAULT_FIELD_CONTENT;
}
}
async #parseFileSize(fileSize = 0) {
const kb = fileSize / 1024,
mb = kb / 1024;
if (!kb) {
return undefined;
}
return this.l10n.get(`pdfjs-document-properties-${mb >= 1 ? "mb" : "kb"}`, {
size_mb: mb >= 1 && (+mb.toPrecision(3)).toLocaleString(),
size_kb: mb < 1 && (+kb.toPrecision(3)).toLocaleString(),
size_b: fileSize.toLocaleString(),
});
}
async #parsePageSize(pageSizeInches, pagesRotation) {
if (!pageSizeInches) {
return undefined;
}
// Take the viewer rotation into account as well; compare with Adobe Reader.
if (pagesRotation % 180 !== 0) {
pageSizeInches = {
width: pageSizeInches.height,
height: pageSizeInches.width,
};
}
const isPortrait = isPortraitOrientation(pageSizeInches);
let sizeInches = {
width: Math.round(pageSizeInches.width * 100) / 100,
height: Math.round(pageSizeInches.height * 100) / 100,
};
// 1in == 25.4mm; no need to round to 2 decimals for millimeters.
let sizeMillimeters = {
width: Math.round(pageSizeInches.width * 25.4 * 10) / 10,
height: Math.round(pageSizeInches.height * 25.4 * 10) / 10,
};
let rawName =
getPageName(sizeInches, isPortrait, US_PAGE_NAMES) ||
getPageName(sizeMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (
!rawName &&
!(
Number.isInteger(sizeMillimeters.width) &&
Number.isInteger(sizeMillimeters.height)
)
) {
// Attempt to improve the page name detection by falling back to fuzzy
// matching of the metric dimensions, to account for e.g. rounding errors
// and/or PDF files that define the page sizes in an imprecise manner.
const exactMillimeters = {
width: pageSizeInches.width * 25.4,
height: pageSizeInches.height * 25.4,
};
const intMillimeters = {
width: Math.round(sizeMillimeters.width),
height: Math.round(sizeMillimeters.height),
};
// Try to avoid false positives, by only considering "small" differences.
if (
Math.abs(exactMillimeters.width - intMillimeters.width) < 0.1 &&
Math.abs(exactMillimeters.height - intMillimeters.height) < 0.1
) {
rawName = getPageName(intMillimeters, isPortrait, METRIC_PAGE_NAMES);
if (rawName) {
// Update *both* sizes, computed above, to ensure that the displayed
// dimensions always correspond to the detected page name.
sizeInches = {
width: Math.round((intMillimeters.width / 25.4) * 100) / 100,
height: Math.round((intMillimeters.height / 25.4) * 100) / 100,
};
sizeMillimeters = intMillimeters;
}
}
}
const [{ width, height }, unit, name, orientation] = await Promise.all([
this._isNonMetricLocale ? sizeInches : sizeMillimeters,
this.l10n.get(
`pdfjs-document-properties-page-size-unit-${
this._isNonMetricLocale ? "inches" : "millimeters"
}`
),
rawName &&
this.l10n.get(`pdfjs-document-properties-page-size-name-${rawName}`),
this.l10n.get(
`pdfjs-document-properties-page-size-orientation-${
isPortrait ? "portrait" : "landscape"
}`
),
]);
return this.l10n.get(
`pdfjs-document-properties-page-size-dimension-${
name ? "name-" : ""
}string`,
{
width: width.toLocaleString(),
height: height.toLocaleString(),
unit,
name,
orientation,
}
);
}
async #parseDate(inputDate) {
const dateObject = PDFDateString.toDateObject(inputDate);
if (!dateObject) {
return undefined;
}
return this.l10n.get("pdfjs-document-properties-date-string", {
date: dateObject.toLocaleDateString(),
time: dateObject.toLocaleTimeString(),
});
}
#parseLinearization(isLinearized) {
return this.l10n.get(
`pdfjs-document-properties-linearized-${isLinearized ? "yes" : "no"}`
);
}
}
export { PDFDocumentProperties };