Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { MathClamp } from "../shared/math_clamp.js";
import { Metadata } from "./metadata.js";
import { ObjectHandler } from "./object_handler.js";
import { OptionalContentConfig } from "./optional_content_config.js";
import { OutputScale } from "./output_scale.js";
import { PagesMapper } from "./pages_mapper.js";
import { PageViewport } from "./page_viewport.js";
import { PDFDataTransportStream } from "./transport_stream.js";
Expand Down Expand Up @@ -3523,7 +3524,7 @@ class InternalRenderTask {
this.objs,
this.canvasFactory,
this.filterFactory,
{ optionalContentConfig },
{ optionalContentConfig, pixelRatio: OutputScale.pixelRatio },
this.annotationCanvasMap,
this.pageColors,
dependencyTracker,
Expand Down
7 changes: 1 addition & 6 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
getCurrentTransformInverse,
getRGBA,
makePathFromDrawOPS,
OutputScale,
PixelsPerInch,
} from "./display_utils.js";
import {
Expand Down Expand Up @@ -610,11 +609,7 @@ class CanvasGraphics {
objs,
canvasFactory,
filterFactory,
{
optionalContentConfig,
markedContentStack = null,
pixelRatio = OutputScale.pixelRatio,
},
{ optionalContentConfig, markedContentStack = null, pixelRatio },
annotationCanvasMap,
pageColors,
dependencyTracker,
Expand Down
86 changes: 0 additions & 86 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,91 +100,6 @@ function getCurrentTransformInverse(ctx) {
return [a, b, c, d, e, f];
}

/**
* Scale factors for the canvas, necessary with HiDPI displays.
*/
class OutputScale {
constructor() {
const { pixelRatio } = OutputScale;

/**
* @type {number} Horizontal scale.
*/
this.sx = pixelRatio;

/**
* @type {number} Vertical scale.
*/
this.sy = pixelRatio;
}

/**
* @type {boolean} Returns `true` when scaling is required, `false` otherwise.
*/
get scaled() {
return this.sx !== 1 || this.sy !== 1;
}

/**
* @type {boolean} Returns `true` when scaling is symmetric,
* `false` otherwise.
*/
get symmetric() {
return this.sx === this.sy;
}

/**
* @returns {boolean} Returns `true` if scaling was limited,
* `false` otherwise.
*/
limitCanvas(width, height, maxPixels, maxDim, capAreaFactor = -1) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("WORKER_THREAD")) {
return false;
}
let maxAreaScale = Infinity,
maxWidthScale = Infinity,
maxHeightScale = Infinity;

maxPixels = OutputScale.capPixels(maxPixels, capAreaFactor);
if (maxPixels > 0) {
maxAreaScale = Math.sqrt(maxPixels / (width * height));
}
if (maxDim > 0) {
maxWidthScale = maxDim / width;
maxHeightScale = maxDim / height;
}
const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale);

if (this.sx > maxScale || this.sy > maxScale) {
this.sx = maxScale;
this.sy = maxScale;
return true;
}
return false;
}

static get pixelRatio() {
return globalThis.devicePixelRatio || 1;
}

static capPixels(maxPixels, capAreaFactor) {
if (
(typeof PDFJSDev === "undefined" || !PDFJSDev.test("WORKER_THREAD")) &&
capAreaFactor >= 0
) {
const winPixels = Math.ceil(
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")
? window.innerWidth * window.innerHeight
: window.screen.availWidth * window.screen.availHeight) *
this.pixelRatio ** 2 *
(1 + capAreaFactor / 100)
);
return maxPixels > 0 ? Math.min(maxPixels, winPixels) : winPixels;
}
return maxPixels;
}
}

function applyOpacity(color, opacity) {
opacity = MathClamp(opacity ?? 1, 0, 1);
const white = 255 * (1 - opacity);
Expand Down Expand Up @@ -403,7 +318,6 @@ export {
getRGB,
getRGBA,
makePathFromDrawOPS,
OutputScale,
PixelsPerInch,
RenderingCancelledException,
};
3 changes: 2 additions & 1 deletion src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

import { AnnotationEditorType, AnnotationPrefix } from "../../shared/util.js";
import { ColorScheme, SupportedImageMimeTypes } from "../dom_utils.js";
import { OutputScale, PixelsPerInch } from "../display_utils.js";
import { AnnotationEditor } from "./editor.js";
import { OutputScale } from "../output_scale.js";
import { PixelsPerInch } from "../display_utils.js";
import { StampAnnotationElement } from "../annotation_layer.js";

/**
Expand Down
95 changes: 95 additions & 0 deletions src/display/output_scale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* Copyright 2015 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.
*/

/**
* Scale factors for the canvas, necessary with HiDPI displays.
*/
class OutputScale {
constructor() {
const { pixelRatio } = OutputScale;

/**
* @type {number} Horizontal scale.
*/
this.sx = pixelRatio;

/**
* @type {number} Vertical scale.
*/
this.sy = pixelRatio;
}

/**
* @type {boolean} Returns `true` when scaling is required, `false` otherwise.
*/
get scaled() {
return this.sx !== 1 || this.sy !== 1;
}

/**
* @type {boolean} Returns `true` when scaling is symmetric,
* `false` otherwise.
*/
get symmetric() {
return this.sx === this.sy;
}

/**
* @returns {boolean} Returns `true` if scaling was limited,
* `false` otherwise.
*/
limitCanvas(width, height, maxPixels, maxDim, capAreaFactor = -1) {
let maxAreaScale = Infinity,
maxWidthScale = Infinity,
maxHeightScale = Infinity;

maxPixels = OutputScale.capPixels(maxPixels, capAreaFactor);
if (maxPixels > 0) {
maxAreaScale = Math.sqrt(maxPixels / (width * height));
}
if (maxDim > 0) {
maxWidthScale = maxDim / width;
maxHeightScale = maxDim / height;
}
const maxScale = Math.min(maxAreaScale, maxWidthScale, maxHeightScale);

if (this.sx > maxScale || this.sy > maxScale) {
this.sx = maxScale;
this.sy = maxScale;
return true;
}
return false;
}

static get pixelRatio() {
return globalThis.devicePixelRatio || 1;
}

static capPixels(maxPixels, capAreaFactor) {
if (capAreaFactor >= 0) {
const winPixels = Math.ceil(
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")
? window.innerWidth * window.innerHeight
: window.screen.availWidth * window.screen.availHeight) *
this.pixelRatio ** 2 *
(1 + capAreaFactor / 100)
);
return maxPixels > 0 ? Math.min(maxPixels, winPixels) : winPixels;
}
return maxPixels;
}
}

export { OutputScale };
2 changes: 1 addition & 1 deletion src/display/renderer_worker_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { BBoxReader } from "./canvas_dependency_tracker.js";
import { getWorkerSrc } from "./api_utils.js";
import { GlobalWorkerOptions } from "./worker_options.js";
import { MessageHandler } from "../shared/message_handler.js";
import { OutputScale } from "./display_utils.js";
import { OutputScale } from "./output_scale.js";
import { setAnnotationCanvasName } from "./canvas.js";

const PORT_CONNECTION_TIMEOUT = 5000; // ms
Expand Down
2 changes: 1 addition & 1 deletion src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
warn,
} from "../shared/util.js";
import { FontLoader } from "./font_loader.js";
import { OutputScale } from "./display_utils.js";
import { OutputScale } from "./output_scale.js";
import { setLayerDimensions } from "./dom_utils.js";

/**
Expand Down
2 changes: 1 addition & 1 deletion src/display/touch_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { OutputScale } from "./display_utils.js";
import { OutputScale } from "./output_scale.js";
import { stopEvent } from "./dom_utils.js";

function preventDefault(evt) {
Expand Down
2 changes: 1 addition & 1 deletion src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
findContrastColor,
getRGB,
getRGBA,
OutputScale,
PixelsPerInch,
RenderingCancelledException,
} from "./display/display_utils.js";
Expand Down Expand Up @@ -90,6 +89,7 @@ import { GlobalWorkerOptions } from "./display/worker_options.js";
import { HighlightOutliner } from "./display/editor/drawers/highlight.js";
import { isValidExplicitDest } from "./display/api_utils.js";
import { MathClamp } from "./shared/math_clamp.js";
import { OutputScale } from "./display/output_scale.js";
import { SignatureExtractor } from "./display/editor/drawers/signaturedraw.js";
import { TextLayer } from "./display/text_layer.js";
import { TextLayerImages } from "./display/text_layer_images.js";
Expand Down
2 changes: 1 addition & 1 deletion test/unit/pdf_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import {
findContrastColor,
getRGB,
getRGBA,
OutputScale,
PixelsPerInch,
RenderingCancelledException,
} from "../../src/display/display_utils.js";
Expand Down Expand Up @@ -80,6 +79,7 @@ import { DrawLayer } from "../../src/display/draw_layer.js";
import { GlobalWorkerOptions } from "../../src/display/worker_options.js";
import { isValidExplicitDest } from "../../src/display/api_utils.js";
import { MathClamp } from "../../src/shared/math_clamp.js";
import { OutputScale } from "../../src/display/output_scale.js";
import { SignatureExtractor } from "../../src/display/editor/drawers/signaturedraw.js";
import { TextLayer } from "../../src/display/text_layer.js";
import { TextLayerImages } from "../../src/display/text_layer_images.js";
Expand Down
2 changes: 1 addition & 1 deletion test/unit/text_layer_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AbortException, isNodeJS } from "../../src/shared/util.js";
import { DEFAULT_FONT_SIZE, TextLayer } from "../../src/display/text_layer.js";
import { buildGetDocumentParams } from "./test_utils.js";
import { getDocument } from "../../src/display/api.js";
import { OutputScale } from "../../src/display/display_utils.js";
import { OutputScale } from "../../src/display/output_scale.js";

describe("textLayer", function () {
it("creates textLayer from ReadableStream", async function () {
Expand Down
Loading