Skip to content

Commit

Permalink
Simplify #minFontSize access
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 25, 2024
1 parent ffcefe2 commit 2f1b526
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ class TextLayer {
div: null,
properties: null,
ctx: null,
minFontSize: null,
};
const { pageWidth, pageHeight, pageX, pageY } = viewport.rawDims;
this.#transform = [1, 0, 0, -1, -pageX, pageY + pageHeight];
this.#pageWidth = pageWidth;
this.#pageHeight = pageHeight;

TextLayer.#ensureMinFontSizeComputed();

setLayerDimensions(container, viewport);

// Always clean-up the temporary canvas once rendering is no longer pending.
Expand Down Expand Up @@ -199,7 +200,6 @@ class TextLayer {
div: null,
properties: null,
ctx: TextLayer.#getCtx(this.#lang),
minFontSize: TextLayer.#getMinFontSize(),
};
for (const div of this.#textDivs) {
params.properties = this.#textDivProperties.get(div);
Expand Down Expand Up @@ -247,7 +247,6 @@ class TextLayer {
return;
}
this.#layoutTextParams.ctx ??= TextLayer.#getCtx(this.#lang);
this.#layoutTextParams.minFontSize ??= TextLayer.#getMinFontSize();

const textDivs = this.#textDivs,
textContentItemsStr = this.#textContentItemsStr;
Expand Down Expand Up @@ -331,11 +330,11 @@ class TextLayer {
divStyle.left = `${scaleFactorStr}${left.toFixed(2)}px)`;
divStyle.top = `${scaleFactorStr}${top.toFixed(2)}px)`;
}
// We multiply the font size by #getMinFontSize(), and then #layout will
// scale the element by 1/#getMinFontSize(). This allows us to effectively
// We multiply the font size by #minFontSize, and then #layout will
// scale the element by 1/#minFontSize. This allows us to effectively
// ignore the minimum font size enforced by the browser, so that the text
// layer <span>s can always match the size of the text in the canvas.
divStyle.fontSize = `${scaleFactorStr}${(TextLayer.#getMinFontSize() * fontHeight).toFixed(2)}px)`;
divStyle.fontSize = `${scaleFactorStr}${(TextLayer.#minFontSize * fontHeight).toFixed(2)}px)`;
divStyle.fontFamily = fontFamily;

textDivProperties.fontSize = fontHeight;
Expand Down Expand Up @@ -395,13 +394,12 @@ class TextLayer {
}

#layout(params) {
const { div, properties, ctx, prevFontSize, prevFontFamily, minFontSize } =
params;
const { div, properties, ctx, prevFontSize, prevFontFamily } = params;
const { style } = div;

let transform = "";
if (minFontSize > 1) {
transform = `scale(${1 / minFontSize})`;
if (TextLayer.#minFontSize > 1) {
transform = `scale(${1 / TextLayer.#minFontSize})`;
}

if (properties.canvasWidth !== 0 && properties.hasText) {
Expand Down Expand Up @@ -474,7 +472,7 @@ class TextLayer {
/**
* @returns {number} The minimum font size enforced by the browser
*/
static #getMinFontSize() {
static #ensureMinFontSizeComputed() {
if (this.#minFontSize === null) {
const div = document.createElement("div");
div.style.opacity = 0;
Expand All @@ -488,8 +486,6 @@ class TextLayer {
this.#minFontSize = div.getBoundingClientRect().height;
div.remove();
}

return this.#minFontSize;
}

static #getAscent(fontFamily, lang) {
Expand Down

0 comments on commit 2f1b526

Please sign in to comment.