summaryrefslogtreecommitdiff
path: root/apps/web/src/services/renderer/nodes
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-02-25 01:37:56 +0100
committerMaze Winther <mazewinther@gmail.com>2026-02-25 01:37:56 +0100
commitf75b5d3fd28349ee69f03be34c1316122992c283 (patch)
tree45e233d9b4c83b02be2a7bfe13ea943a676bd9be /apps/web/src/services/renderer/nodes
parent4d77e3f2bb9884ad9e43761eaf0ede8c1a9cfccc (diff)
downloadopencut-f75b5d3fd28349ee69f03be34c1316122992c283.tar.gz
fix a ton of issues + improvement
Diffstat (limited to 'apps/web/src/services/renderer/nodes')
-rw-r--r--apps/web/src/services/renderer/nodes/text-node.ts112
1 files changed, 35 insertions, 77 deletions
diff --git a/apps/web/src/services/renderer/nodes/text-node.ts b/apps/web/src/services/renderer/nodes/text-node.ts
index b7e36d64..76d78b31 100644
--- a/apps/web/src/services/renderer/nodes/text-node.ts
+++ b/apps/web/src/services/renderer/nodes/text-node.ts
@@ -2,9 +2,16 @@ import type { CanvasRenderer } from "../canvas-renderer";
import { BaseNode } from "./base-node";
import type { TextElement } from "@/types/timeline";
import {
+ DEFAULT_TEXT_ELEMENT,
DEFAULT_LINE_HEIGHT,
FONT_SIZE_SCALE_REFERENCE,
} from "@/constants/text-constants";
+import {
+ getMetricAscent,
+ getMetricDescent,
+ getTextBackgroundRect,
+ measureTextBlock,
+} from "@/lib/text/layout";
function scaleFontSize({
fontSize,
@@ -20,65 +27,6 @@ function quoteFontFamily({ fontFamily }: { fontFamily: string }): string {
return `"${fontFamily.replace(/"/g, '\\"')}"`;
}
-function getMetricAscent({
- metrics,
- fallback,
-}: {
- metrics: TextMetrics;
- fallback: number;
-}): number {
- return metrics.actualBoundingBoxAscent ?? fallback * 0.8;
-}
-
-function getMetricDescent({
- metrics,
- fallback,
-}: {
- metrics: TextMetrics;
- fallback: number;
-}): number {
- return metrics.actualBoundingBoxDescent ?? fallback * 0.2;
-}
-
-interface TextBlockMeasurement {
- visualCenterOffset: number;
- height: number;
- maxWidth: number;
-}
-
-function measureTextBlock({
- lineMetrics,
- lineHeightPx,
- fallbackFontSize,
-}: {
- lineMetrics: TextMetrics[];
- lineHeightPx: number;
- fallbackFontSize: number;
-}): TextBlockMeasurement {
- let top = Number.POSITIVE_INFINITY;
- let bottom = Number.NEGATIVE_INFINITY;
- let maxWidth = 0;
-
- for (let i = 0; i < lineMetrics.length; i++) {
- const metrics = lineMetrics[i];
- const y = i * lineHeightPx;
- top = Math.min(
- top,
- y - getMetricAscent({ metrics, fallback: fallbackFontSize }),
- );
- bottom = Math.max(
- bottom,
- y + getMetricDescent({ metrics, fallback: fallbackFontSize }),
- );
- maxWidth = Math.max(maxWidth, metrics.width);
- }
-
- const height = bottom - top;
- const visualCenterOffset = (top + bottom) / 2;
-
- return { visualCenterOffset, height, maxWidth };
-}
-
function drawTextDecoration({
ctx,
textDecoration,
@@ -99,8 +47,8 @@ function drawTextDecoration({
if (textDecoration === "none" || !textDecoration) return;
const thickness = Math.max(1, scaledFontSize * 0.07);
- const ascent = getMetricAscent({ metrics, fallback: scaledFontSize });
- const descent = getMetricDescent({ metrics, fallback: scaledFontSize });
+ const ascent = getMetricAscent({ metrics, fallbackFontSize: scaledFontSize });
+ const descent = getMetricDescent({ metrics, fallbackFontSize: scaledFontSize });
let xStart = -lineWidth / 2;
if (textAlign === "left") xStart = 0;
@@ -171,6 +119,7 @@ export class TextNode extends BaseNode<TextNodeParams> {
const lines = this.params.content.split("\n");
const lineHeightPx = scaledFontSize * lineHeight;
+ const fontSizeRatio = this.params.fontSize / DEFAULT_TEXT_ELEMENT.fontSize;
const baseline = this.params.textBaseline ?? "middle";
renderer.context.textBaseline = baseline;
@@ -191,22 +140,31 @@ export class TextNode extends BaseNode<TextNodeParams> {
) as GlobalCompositeOperation;
renderer.context.globalAlpha = this.params.opacity;
- if (this.params.backgroundColor && lineCount > 0) {
- const padX = 8;
- const padY = 4;
- renderer.context.fillStyle = this.params.backgroundColor;
- let bgLeft = -block.maxWidth / 2;
- if (renderer.context.textAlign === "left") bgLeft = 0;
- if (renderer.context.textAlign === "right") bgLeft = -block.maxWidth;
-
- renderer.context.fillRect(
- bgLeft - padX,
- -block.height / 2 - padY,
- block.maxWidth + padX * 2,
- block.height + padY * 2,
- );
-
- renderer.context.fillStyle = this.params.color;
+ if (
+ this.params.background.color &&
+ this.params.background.color !== "transparent" &&
+ lineCount > 0
+ ) {
+ const { color, cornerRadius = 0 } = this.params.background;
+ const backgroundRect = getTextBackgroundRect({
+ textAlign: this.params.textAlign,
+ block,
+ background: this.params.background,
+ fontSizeRatio,
+ });
+ if (backgroundRect) {
+ renderer.context.fillStyle = color;
+ renderer.context.beginPath();
+ renderer.context.roundRect(
+ backgroundRect.left,
+ backgroundRect.top,
+ backgroundRect.width,
+ backgroundRect.height,
+ cornerRadius,
+ );
+ renderer.context.fill();
+ renderer.context.fillStyle = this.params.color;
+ }
}
for (let i = 0; i < lineCount; i++) {