Skip to content

Implement option to embed fonts#45

Merged
jangko merged 11 commits intojangko:masterfrom
ThomasTJdev:feature/ttj/option-to-embed-fonts
Aug 13, 2025
Merged

Implement option to embed fonts#45
jangko merged 11 commits intojangko:masterfrom
ThomasTJdev:feature/ttj/option-to-embed-fonts

Conversation

@ThomasTJdev
Copy link
Contributor

This PR adds the ability to embed fonts directly into the generated PDF files.

Embedding increases the PDF file size slightly, but enables much better post-processing compatibility - especially when copying text or handling non-ASCII characters.

This also solves my issue from 2021 :) #39


Example

To enable font embedding, simply set the new option:

var opts = newPDFOptions()
opts.setEmbedFont(true)

var doc = newPDF(opts)

Notes

I've aimed to keep the implementation as non-intrusive as possible, but it will always load the font cache in the makeTTFont().

discard

method EscapeString*(f: Base14, text: string): string =
method EscapeString*(f: Base14, text: string, embedFont: bool = false): string =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

embedFont should passed through setFont, then the table initialization is done once in makeFont.
I prefer users have liberty to choose which font they want to embed. And for the global embedFont flag,
it will override the setFont's embedFont flag.

Built in font have no effect when setting this font.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. During my testing I used some more fonts from the demo folder an got in trouble with the width, so that's the first commit #da483d4.

For the setFont() I added an embedFont: bool and got it to work partially, so the font got embedded (copy-paste'able) but the rendering broke. So I'm gonna pass on that one for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @jangko

I have gotten help from @bung87 to implement the embedFont parameter. This now works, and it also works with the global setting. For me when setting the embed to false it also becomes a real drawing instead of allowing you to copy which would result in gibberish chars.

Do you need anything else?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add options to keep the old mapping instead of replacing it with glyph drawing? Drawing glyph using PDF path will increase the document size significantly compared to simple mapping. I mean, make the user able to choose how to render the text. Copyable vs small document size are options, give the user freedom, don't force them if possible.

Copy link
Contributor

@bung87 bung87 Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we replace embedFont param to enum type ?

type
  FontRenderMode* = enum
    frmDefault       ## Do not embed font, render as text
    frmEmbed       ## Embed font and render as text
    frmPathRendering ## Draw glyphs as paths

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jangko

Please see the changes made by bung. The global option is gone, but you can now use the enum on each draw.

ThomasTJdev and others added 10 commits April 18, 2025 11:50
- Add FontRenderMode enum with frmDefault, frmEmbed, and frmPathRendering options
- Implement full font embedding with proper Unicode mapping for text extraction
- Fix ToUnicode CMap generation to use correct GID mapping based on render mode
- Update font subsetting logic to handle different rendering modes
- Improve character-to-glyph mapping for embedded vs subset fonts
- Add renderMode parameter to font creation and management functions
feat: implement font embedding modes and improve text selectability
@jangko jangko merged commit bf599ce into jangko:master Aug 13, 2025
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants