Skip to content

Commit 4ddda41

Browse files
Merge pull request #14000 from Snuffleupagus/PixelsPerInch
Re-factor the `CSS_PIXELS_PER_INCH`/`PDF_PIXELS_PER_INCH` exports (PR 13991 follow-up)
2 parents 9ce63a6 + 0e54f56 commit 4ddda41

File tree

7 files changed

+20
-25
lines changed

7 files changed

+20
-25
lines changed

src/display/canvas.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
import { CSS_PIXELS_PER_INCH, PDF_PIXELS_PER_INCH } from "./display_utils.js";
15+
1616
import {
1717
FONT_IDENTITY_MATRIX,
1818
IDENTITY_MATRIX,
@@ -28,6 +28,7 @@ import {
2828
warn,
2929
} from "../shared/util.js";
3030
import { getShadingPattern, TilingPattern } from "./pattern_helper.js";
31+
import { PixelsPerInch } from "./display_utils.js";
3132

3233
// <canvas> contexts store most of the state we need natively.
3334
// However, PDF needs a bit more state, which we store here.
@@ -878,8 +879,7 @@ function getImageSmoothingEnabled(transform, interpolate) {
878879
scale[0] = Math.fround(scale[0]);
879880
scale[1] = Math.fround(scale[1]);
880881
const actualScale = Math.fround(
881-
((globalThis.devicePixelRatio || 1) * CSS_PIXELS_PER_INCH) /
882-
PDF_PIXELS_PER_INCH
882+
((globalThis.devicePixelRatio || 1) * PixelsPerInch.CSS) / PixelsPerInch.PDF
883883
);
884884
if (interpolate !== undefined) {
885885
// If the value is explicitly set use it.
@@ -2458,11 +2458,8 @@ class CanvasGraphics {
24582458
this.ctx = this.groupStack.pop();
24592459
// Turn off image smoothing to avoid sub pixel interpolation which can
24602460
// look kind of blurry for some pdfs.
2461-
if (this.ctx.imageSmoothingEnabled !== undefined) {
2462-
this.ctx.imageSmoothingEnabled = false;
2463-
} else {
2464-
this.ctx.mozImageSmoothingEnabled = false;
2465-
}
2461+
this.ctx.imageSmoothingEnabled = false;
2462+
24662463
if (group.smask) {
24672464
this.tempSMask = this.smaskStack.pop();
24682465
} else {

src/display/display_utils.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ import {
3232
const DEFAULT_LINK_REL = "noopener noreferrer nofollow";
3333
const SVG_NS = "http://www.w3.org/2000/svg";
3434

35-
const CSS_PIXELS_PER_INCH = 96.0;
36-
const PDF_PIXELS_PER_INCH = 72.0;
35+
const PixelsPerInch = {
36+
CSS: 96.0,
37+
PDF: 72.0,
38+
};
3739

3840
class DOMCanvasFactory extends BaseCanvasFactory {
3941
constructor({ ownerDocument = globalThis.document } = {}) {
@@ -625,7 +627,6 @@ function getXfaPageViewport(xfaPage, { scale = 1, rotation = 0 }) {
625627

626628
export {
627629
addLinkAttributes,
628-
CSS_PIXELS_PER_INCH,
629630
DEFAULT_LINK_REL,
630631
deprecated,
631632
DOMCanvasFactory,
@@ -641,8 +642,8 @@ export {
641642
LinkTarget,
642643
loadScript,
643644
PageViewport,
644-
PDF_PIXELS_PER_INCH,
645645
PDFDateString,
646+
PixelsPerInch,
646647
RenderingCancelledException,
647648
StatTimer,
648649
};

src/pdf.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
import {
1818
addLinkAttributes,
19-
CSS_PIXELS_PER_INCH,
2019
getFilenameFromUrl,
2120
getPdfFilenameFromUrl,
2221
getXfaPageViewport,
2322
isPdfFile,
2423
isValidFetchUrl,
2524
LinkTarget,
2625
loadScript,
27-
PDF_PIXELS_PER_INCH,
2826
PDFDateString,
27+
PixelsPerInch,
2928
RenderingCancelledException,
3029
} from "./display/display_utils.js";
3130
import {
@@ -105,14 +104,13 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
105104
export {
106105
// From "./display/display_utils.js":
107106
addLinkAttributes,
108-
CSS_PIXELS_PER_INCH,
109107
getFilenameFromUrl,
110108
getPdfFilenameFromUrl,
111109
isPdfFile,
112110
LinkTarget,
113111
loadScript,
114-
PDF_PIXELS_PER_INCH,
115112
PDFDateString,
113+
PixelsPerInch,
116114
RenderingCancelledException,
117115
getXfaPageViewport,
118116
// From "./shared/util.js":

test/driver.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
const {
2121
AnnotationLayer,
2222
AnnotationMode,
23-
CSS_PIXELS_PER_INCH,
2423
getDocument,
2524
GlobalWorkerOptions,
26-
PDF_PIXELS_PER_INCH,
25+
PixelsPerInch,
2726
renderTextLayer,
2827
XfaLayer,
2928
} = pdfjsLib;
3029
const { SimpleLinkService } = pdfjsViewer;
3130

3231
const WAITING_TIME = 100; // ms
33-
const PDF_TO_CSS_UNITS = CSS_PIXELS_PER_INCH / PDF_PIXELS_PER_INCH;
32+
const PDF_TO_CSS_UNITS = PixelsPerInch.CSS / PixelsPerInch.PDF;
3433
const CMAP_URL = "/build/generic/web/cmaps/";
3534
const CMAP_PACKED = true;
3635
const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";

web/firefox_print_service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import {
1717
AnnotationMode,
18-
PDF_PIXELS_PER_INCH,
18+
PixelsPerInch,
1919
RenderingCancelledException,
2020
shadow,
2121
} from "pdfjs-lib";
@@ -34,7 +34,7 @@ function composePage(
3434
const canvas = document.createElement("canvas");
3535

3636
// The size of the canvas in pixels for printing.
37-
const PRINT_UNITS = printResolution / PDF_PIXELS_PER_INCH;
37+
const PRINT_UNITS = printResolution / PixelsPerInch.PDF;
3838
canvas.width = Math.floor(size.width * PRINT_UNITS);
3939
canvas.height = Math.floor(size.height * PRINT_UNITS);
4040

web/pdf_print_service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { AnnotationMode, PDF_PIXELS_PER_INCH } from "pdfjs-lib";
16+
import { AnnotationMode, PixelsPerInch } from "pdfjs-lib";
1717
import { PDFPrintServiceFactory, PDFViewerApplication } from "./app.js";
1818
import { compatibilityParams } from "./app_options.js";
1919
import { getXfaHtmlForPrinting } from "./print_utils.js";
@@ -34,7 +34,7 @@ function renderPage(
3434
const scratchCanvas = activeService.scratchCanvas;
3535

3636
// The size of the canvas in pixels for printing.
37-
const PRINT_UNITS = printResolution / PDF_PIXELS_PER_INCH;
37+
const PRINT_UNITS = printResolution / PixelsPerInch.PDF;
3838
scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
3939
scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
4040

web/ui_utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* limitations under the License.
1414
*/
1515

16-
import { CSS_PIXELS_PER_INCH, PDF_PIXELS_PER_INCH } from "pdfjs-lib";
16+
import { PixelsPerInch } from "pdfjs-lib";
1717

18-
const CSS_UNITS = CSS_PIXELS_PER_INCH / PDF_PIXELS_PER_INCH;
18+
const CSS_UNITS = PixelsPerInch.CSS / PixelsPerInch.PDF;
1919
const DEFAULT_SCALE_VALUE = "auto";
2020
const DEFAULT_SCALE = 1.0;
2121
const MIN_SCALE = 0.1;

0 commit comments

Comments
 (0)