Skip to content
Closed
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
13 changes: 12 additions & 1 deletion src/applyparseattributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions test/font-family-unquoted-utf8/font-family-unquoted-utf8.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
3 changes: 3 additions & 0 deletions test/font-family-unquoted-utf8/font-family-unquoted-utf8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading