From 53c71de6a1916c07b0eb4e102a080c05bb302b70 Mon Sep 17 00:00:00 2001 From: no84by <50858621+no84by@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:30:34 +0000 Subject: [PATCH] fix(signature): render typed signature on one line at the largest size that fits The typed signature used a fixed 30px font, leaving short names small on the final document. Draw it on a single line, sized to the canvas width minus a small margin and capped at 80% of the canvas height. When the fitted size would fall below the current 30px, keep the existing word-wrapping at 30px. Signed-off-by: no84by <50858621+no84by@users.noreply.github.com> --- src/components/Draw/TextInput.vue | 33 ++++++++--- src/helpers/textSignatureFontSize.ts | 55 +++++++++++++++++++ .../helpers/textSignatureFontSize.spec.ts | 47 ++++++++++++++++ 3 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 src/helpers/textSignatureFontSize.ts create mode 100644 src/tests/helpers/textSignatureFontSize.spec.ts diff --git a/src/components/Draw/TextInput.vue b/src/components/Draw/TextInput.vue index 5be8986edf..b47a4ae518 100644 --- a/src/components/Draw/TextInput.vue +++ b/src/components/Draw/TextInput.vue @@ -48,6 +48,7 @@ import NcButton from '@nextcloud/vue/components/NcButton' import NcDialog from '@nextcloud/vue/components/NcDialog' import NcTextField from '@nextcloud/vue/components/NcTextField' import PreviewSignature from '../PreviewSignature/PreviewSignature.vue' +import { resolveTextSignatureLayout } from '../../helpers/textSignatureFontSize' import type { LibresignCapabilities } from '../../types/index' defineOptions({ @@ -85,11 +86,31 @@ watch(value, (newValue) => { } context.clearRect(0, 0, currentCanvas.width, currentCanvas.height) context.fillStyle = 'black' - context.font = "30px 'Dancing Script'" + const text = String(newValue).trim() + if (!text) { + return + } + const referenceFontSize = 100 + context.font = `${referenceFontSize}px 'Dancing Script'` + const layout = resolveTextSignatureLayout({ + canvasWidth: currentCanvas.width, + canvasHeight: currentCanvas.height, + measuredWidth: context.measureText(text).width, + referenceFontSize, + }) + context.font = `${layout.fontSize}px 'Dancing Script'` + context.textAlign = 'center' + context.textBaseline = 'middle' + const centerX = currentCanvas.width / 2 + if (!layout.wrap) { + context.fillText(text, centerX, currentCanvas.height / 2) + return + } + const paddingX = 15 const maxWidth = Math.max(0, currentCanvas.width - (paddingX * 2)) const lineHeight = 36 - const words = String(newValue).trim().split(/\s+/).filter(Boolean) + const words = text.split(/\s+/).filter(Boolean) const lines: string[] = [] let line = '' @@ -106,15 +127,11 @@ watch(value, (newValue) => { lines.push(line) } - context.textAlign = 'center' - context.textBaseline = 'middle' - const totalHeight = lines.length * lineHeight const startY = (currentCanvas.height / 2) - ((totalHeight - lineHeight) / 2) - const centerX = currentCanvas.width / 2 - lines.forEach((text, index) => { - context.fillText(text, centerX, startY + (index * lineHeight)) + lines.forEach((lineText, index) => { + context.fillText(lineText, centerX, startY + (index * lineHeight)) }) }) diff --git a/src/helpers/textSignatureFontSize.ts b/src/helpers/textSignatureFontSize.ts new file mode 100644 index 0000000000..ab7d7c30ef --- /dev/null +++ b/src/helpers/textSignatureFontSize.ts @@ -0,0 +1,55 @@ +/** + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +export const TEXT_SIGNATURE_MARGIN = 6 +export const TEXT_SIGNATURE_MAX_HEIGHT_RATIO = 0.8 + +interface TextSignatureFontSizeOptions { + canvasWidth: number + canvasHeight: number + // Width of the text measured at referenceFontSize + measuredWidth: number + referenceFontSize: number + margin?: number + maxHeightRatio?: number +} + +/** + * Largest font size (px) at which the text fits on one line inside the + * canvas width minus the margin on both sides, capped at maxHeightRatio of + * the canvas height. Text width grows linearly with the font size, so one + * measurement is enough. + */ +export function computeTextSignatureFontSize({ + canvasWidth, + canvasHeight, + measuredWidth, + referenceFontSize, + margin = TEXT_SIGNATURE_MARGIN, + maxHeightRatio = TEXT_SIGNATURE_MAX_HEIGHT_RATIO, +}: TextSignatureFontSizeOptions): number { + const maxByHeight = canvasHeight * maxHeightRatio + const availableWidth = canvasWidth - (margin * 2) + if (availableWidth <= 0 || measuredWidth <= 0 || referenceFontSize <= 0) { + return Math.max(0, Math.min(maxByHeight, referenceFontSize)) + } + const maxByWidth = (availableWidth / measuredWidth) * referenceFontSize + return Math.max(0, Math.min(maxByHeight, maxByWidth)) +} + +// Size used before single-line fitting existed; below it the text wraps instead. +export const TEXT_SIGNATURE_MIN_FONT_SIZE = 30 + +/** + * Single line at the fitted size when that is at least the minimum size, + * otherwise wrap at the minimum size. + */ +export function resolveTextSignatureLayout(options: TextSignatureFontSizeOptions): { fontSize: number, wrap: boolean } { + const fontSize = computeTextSignatureFontSize(options) + if (fontSize >= TEXT_SIGNATURE_MIN_FONT_SIZE) { + return { fontSize, wrap: false } + } + return { fontSize: TEXT_SIGNATURE_MIN_FONT_SIZE, wrap: true } +} diff --git a/src/tests/helpers/textSignatureFontSize.spec.ts b/src/tests/helpers/textSignatureFontSize.spec.ts new file mode 100644 index 0000000000..a3461df48c --- /dev/null +++ b/src/tests/helpers/textSignatureFontSize.spec.ts @@ -0,0 +1,47 @@ +/** + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { describe, expect, it } from 'vitest' +import { computeTextSignatureFontSize, resolveTextSignatureLayout } from '../../helpers/textSignatureFontSize' + +describe('computeTextSignatureFontSize', () => { + const base = { canvasWidth: 700, canvasHeight: 200, referenceFontSize: 100 } + + it('caps a short text at 80% of the canvas height', () => { + expect(computeTextSignatureFontSize({ ...base, measuredWidth: 150 })).toBe(160) + }) + + it('shrinks a long text to fit the width minus the margins', () => { + // 688px available for 2000px at 100px => 34.4px + expect(computeTextSignatureFontSize({ ...base, measuredWidth: 2000 })).toBeCloseTo(34.4) + }) + + it('scales linearly with the measurement reference', () => { + const size = computeTextSignatureFontSize({ ...base, referenceFontSize: 50, measuredWidth: 1000 }) + expect(size).toBeCloseTo(34.4) + }) + + it('honours custom margin and ratio', () => { + expect(computeTextSignatureFontSize({ ...base, measuredWidth: 1400, margin: 0 })).toBeCloseTo(50) + expect(computeTextSignatureFontSize({ ...base, measuredWidth: 100, maxHeightRatio: 0.5 })).toBe(100) + }) + + it('falls back to the height cap when nothing can be measured', () => { + expect(computeTextSignatureFontSize({ ...base, measuredWidth: 0 })).toBe(100) + expect(computeTextSignatureFontSize({ ...base, canvasWidth: 10, measuredWidth: 500 })).toBe(100) + }) +}) + +describe('resolveTextSignatureLayout', () => { + const base = { canvasWidth: 350, canvasHeight: 100, referenceFontSize: 100 } + + it('uses one line at the fitted size for a short name', () => { + expect(resolveTextSignatureLayout({ ...base, measuredWidth: 150 })).toEqual({ fontSize: 80, wrap: false }) + }) + + it('keeps the 30px wrapping for a long name on the default 350x100 canvas', () => { + expect(resolveTextSignatureLayout({ ...base, measuredWidth: 1930 })).toEqual({ fontSize: 30, wrap: true }) + }) +})