PDF-Writer
PDF-Writer copied to clipboard
Making use of Base 14 fonts
How can I create a PDFUsedFont that references one of the Base 14 fonts in pdf? The idea is to not embed the fonts, but still have a "deterministic" choice of fonts (quite obviously? :P)
Thanks!
PS: I tried using TfLow("Helvetica", 14); but then mupdf complains that it couldn't find font dictionary. I'm not knowledgeble in pdf specification at all, apart from what I read here.
Two options:
- use helvetica font with PDFUsedFont but don't embed glyphs. Not embedding happens when calling the PDFWriter objct
StartPDF
and using false on EmbedFonts flag - https://github.com/galkahana/PDF-Writer/blob/master/PDFWriter/PDFWriter.h#L58 - The TFLow method: create you own font dictionary, register as a page/form resource and use with TFLow. For this follow the example in the PDF specs 5.1.1:
Create a font object
23 0 obj
<< /Type /Font
/Subtype /Type1
/BaseFont /Helvetica
>>
endobj
You can use the library extensibility options here to create the object and the dictionary. see here for documentation - extensibility and in particular the objects context. There's an example here of creating a new object, which if you get can save you reading the help.
When creating the object you will use the method StartNewIndirectObject
. it will return the new object ID. save it somewhere, you'll need it for the later stage.
Create reference from page/form resources dictionary
To use with the TF operator you have to create reference to the object with a font name in the relevant page/form resource dictionary, as is shown in the specs here:
/Resources
<< /Font << /F13 23 0 R >>
>>
You will probably have a PDFPage object lying around somewhere, so assuming you got the object ID for the font dictionary object you just created in fontDictObjID
you should be able to register the font like this:
page.GetResourcesDictionary(). AddFontMapping(fontDictObjID)
The return value from this call will give you as string that you can now use with TFLow
.
use with TFLow
In the content stream just call the content context TFLow method with the name that you got.
Regards, Gal.
Nice, thank you for the very detailed response! I think you should include this in the wiki it's already nicely structured.
I do have a follow-up question: What about metrics? I don't have a handle to PDFUsedFont this way. What would you suggest? I've considered creating a FreeTypeWrapper, calling NewFace and manually creating a PDFUsedFont, but this seems to duplicate work your library already does, and extra FT_Library stuff.
An alternative would be to only embed the required fonts. I noticed the WriteState() and WriteFontDefinition() methods of PDFUsedFont. Can I (and how to) use these and StartPDF() without embedding fonts to "safely/cleanly" create a PDFUsedFont from PDFWriter and have extra fonts embedded?
Which do you htink would be the better method?
Thank you.
Sounds like you want to simply have a choice of which fonts to embed and which not to, no? Right now i have all or nothing. How about we change the lib to allow definig on a used font whether it should be embedded or not? Or add a general flag on pdfwrite to embed (when embedding) onlu fonts that are not part of the default 14th?
Sounds like you want to simply have a choice of which fonts to embed and which not to, no?
That and easy metrics for Base 14. From what I read in specs, metrics can be found somewhere here.
Maybe you can add PDFUsedFont* GetFontBase14() methods in which one can specify which of the Base 14 fonts this is (files are still required for metrics and glyphs translation). Then add 2 flags to PDFUsedFont: isBase14 (for the auto embedding of non base 14 fonts) and allowEmbedding (this one settable by user). Final cost: 2 bools and 1 or 2 methods, no major rewrite/incompatibilities.
Or something like that... As a user I'd like that interface :)