Skip to content
Open
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
33 changes: 25 additions & 8 deletions src/components/Draw/TextInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 = ''
Expand All @@ -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))
})
})

Expand Down
55 changes: 55 additions & 0 deletions src/helpers/textSignatureFontSize.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
47 changes: 47 additions & 0 deletions src/tests/helpers/textSignatureFontSize.spec.ts
Original file line number Diff line number Diff line change
@@ -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 })
})
})