diff --git a/src/applyparseattributes.ts b/src/applyparseattributes.ts index 36b5ef8..81fc46f 100644 --- a/src/applyparseattributes.ts +++ b/src/applyparseattributes.ts @@ -122,7 +122,18 @@ export function parseAttributes(context: Context, svgNode: SvgNode, node?: Eleme const fontFamily = getAttribute(domNode, context.styleSheets, 'font-family') if (fontFamily) { - const fontFamilies = FontFamily.parse(fontFamily) + // font-family-papandreou rejects unquoted names with non-ASCII (and some + // other) characters. Browsers still honor those attribute values, so fall + // back to comma-splitting and trimming the family names. + let fontFamilies: string[] + try { + fontFamilies = FontFamily.parse(fontFamily) + } catch { + fontFamilies = fontFamily + .split(',') + .map(f => f.trim().replace(/^['"]|['"]$/g, '')) + .filter(Boolean) + } context.attributeState.fontFamily = findFirstAvailableFontFamily( context.attributeState, fontFamilies, diff --git a/test/font-family-unquoted-utf8/font-family-unquoted-utf8.spec.ts b/test/font-family-unquoted-utf8/font-family-unquoted-utf8.spec.ts new file mode 100644 index 0000000..054ae6d --- /dev/null +++ b/test/font-family-unquoted-utf8/font-family-unquoted-utf8.spec.ts @@ -0,0 +1,28 @@ +import { describe, it, expect } from 'vitest' +import { svg2pdf } from '../../src/svg2pdf' +import jsPDF from 'jspdf' +import { loadSvg } from '../utils/loadSvg' + +describe('font-family-unquoted-utf8', () => { + it('accepts unquoted non-ASCII font-family fallback list and renders matching custom font', async () => { + const { svgElement, width, height } = await loadSvg( + '/test/font-family-unquoted-utf8/font-family-unquoted-utf8.svg' + ) + const pdf = new jsPDF(width > height ? 'l' : 'p', 'pt', [width, height]) + const fontData = await fetch('/test/custom-fonts/Batang.ttf') + .then(r => r.arrayBuffer()) + .then(buffer => { + const bytes = new Uint8Array(buffer) + let binary = '' + for (let i = 0; i < bytes.byteLength; i++) { + binary += String.fromCharCode(bytes[i]) + } + return binary + }) + pdf.addFileToVFS('/test/custom-fonts/Batang.ttf', fontData) + pdf.addFont('/test/custom-fonts/Batang.ttf', 'Batang', 'normal') + + await svg2pdf(svgElement, pdf, { loadExternalStyleSheets: true }) + await expect(pdf.output('arraybuffer')).toMatchPdfSnapshot('../custom-fonts/custom-fonts.pdf') + }) +}) diff --git a/test/font-family-unquoted-utf8/font-family-unquoted-utf8.svg b/test/font-family-unquoted-utf8/font-family-unquoted-utf8.svg new file mode 100644 index 0000000..6436792 --- /dev/null +++ b/test/font-family-unquoted-utf8/font-family-unquoted-utf8.svg @@ -0,0 +1,3 @@ + + 안녕하세요 만나서 반갑습니다. +