Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed font: subset breaks if text contains a dash #1396

Open
2 tasks done
timotheeg opened this issue Feb 10, 2023 · 0 comments
Open
2 tasks done

Embed font: subset breaks if text contains a dash #1396

timotheeg opened this issue Feb 10, 2023 · 0 comments

Comments

@timotheeg
Copy link

What were you trying to do?

Embed just a subset of a font into a pdf.

How did you attempt to do it?

I used the property subset:true. If I set subset:false, the documents gets generated correctly, including the dashes character

What actually happened?

The program crashed with message:

/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:1531
	    if (offset + ext > buf.length) throw new RangeError('Index out of range');
	                                   ^

RangeError: Index out of range
    at checkInt (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:1531:43)
    at Uint8Array.writeUInt16BE (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:1596:21)
    at TTFSubset._addGlyph (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:40914:17)
    at TTFSubset.encode (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:40952:13)
    at /Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:40692:14
    at Item.run (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:2632:13)
    at Timeout.drainQueue [as _onTimeout] (/Users/timotheeg/work/signify/backend/node_modules/@pdf-lib/fontkit/dist/fontkit.umd.js:2597:35)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

What did you expect to happen?

Document generation should succeed and export just the glyphs being used.

How can we reproduce the issue?

const { PDFDocument, rgb } = require('pdf-lib');
const fontkit =  require('@pdf-lib/fontkit');
const fs = require('fs/promises');

(async () => {
    const poppinsRegularFontBytesWeb = await (await fetch('https://fonts.gstatic.com/s/poppins/v20/pxiEyp8kv8JHgFVrJJfecnFHGPc.woff2')).arrayBuffer()
    const docContent = await fs.readFile('./Some_Dummy_PDF.pdf')

    const pdfDoc = await PDFDocument.load(docContent)

    pdfDoc.registerFontkit(fontkit)

    const fontPoppinsRegular = await pdfDoc.embedFont(poppinsRegularFontBytesWeb, {
        customName: 'PoppinsRegular',
        subset: true // set to false to see output with dash
    })

    const pages = pdfDoc.getPages()
    const page = pages[0]

    const textOffsetX = page.getWidth() * 0.1
    const textOffsetY = page.getHeight() * 0.5

    page.drawText('Text with Dash -', { // remove dash and it works, colon, coma, work either way
        x: textOffsetX,
        y: textOffsetY,
        size: 10,
        color: rgb(0, 0, 0),
        font: fontPoppinsRegular,
    })

    const pdfBytes = await pdfDoc.save()

    await fs.writeFile('./out.pdf', pdfBytes);
})();

Some_Dummy_PDF.pdf
out.pdf

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

@timotheeg timotheeg changed the title Ember font subset breaks if input contains a dash Embed font: subset breaks if text contains a dash Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant