docx
docx copied to clipboard
Embedding font
Hi! I was wondering if it could be possible to embed fonts into the document. Is it something that maybe you planned to do? Thanks!
Filippo
need to look into what work that would entail
seems possible, now need to replicate this in code
https://www.howtogeek.com/106681/how-to-embed-fonts-in-a-microsoft-word-document/
I did some research and in ECMA-376-1 there's a description on how to embed a font. It seems that you have to include a fontsTable.xml and the embedded version of the font (I tried with LibreOffice and the format is ODTTF)
This document is 5039 pages long, can you direct me to the specific page?
You're right, sorry! :) It's page 669 (chapter 17.8)
Seems doable actually!
Thanks! I'll most likely prepare it for the v5 release
Great! I'll wait for it, then! Thanks
From what i've been reading, the font needs to be coverted to a special type of format, im not sure of anywhere which does this
do you know?
I wonder if you could include a font engine such as fontkit to embed the font in the document.
Never heard of it, but I'll take a look
So I don't think fontkit creates ODTTF, which is the format Word documents need:
https://github.com/foliojs/fontkit/issues?utf8=%E2%9C%93&q=ODTTF
On a side note, I found this though:
https://gist.github.com/dungsaga/ab8d2379bb566c9925b27df3bc82ca8b
But it's in reverse, it converts ODTTF to TTF
Would welcome a PR which creates the reverse of that gist above
any update?
I am stuck on the conversion of TTF to ODTTF, need to figure out how to do this.
Word only accepts the ODTTF file format for fonts
Hi @dolanmiu thanks for your great work. My developing product highly depends on docx.
Recently, I'm trying to embed a custom font into the document but couldn't find out how. Any update on this feature? Is there any documentation about the structure of ODTTF file format? From the discussion above, it seems we could solve this problem if we find out a way to convert TTF to ODTTF?
I would also be very interested in this.
Are there any free TTF to ODTTF converters online?
This is the closest thing I could find: https://social.msdn.microsoft.com/Forums/vstudio/en-US/edbee9a4-866b-44fd-bbaa-d835b183831e/how-to-create-an-odttf-file-from-a-given-font-name?forum=windowsxps example code that creates an obfuscated stream but it's not in TypeScript or JavaScript.. I'm not sure where the font GUID comes from, though, or how you would include this in a word file.
I tried to be clever and took the "fonts" folder from another document and put it into my generated document using 7zip but it just broke the document and didn't work, so there must be more to this
If you can figure it out and make a PR, that would be much appreciated!
I found a paper that might be helpful to you @dolanmiu 4.2.2 Remote Font Attack Vector – Docx: The remote font attack can be launched by embedding a crafted font into a Microsoft Office document. To do this, a TTF file format has to be converted into an obfuscated TTF font file format (ODTTF) before performing the embedding process and this must satisfy the following requirements.
- A 128-bit Global Unique Identifier (GUID) is generated.
- An XOR operation on the first 32 byte of the TTF is performed.
- The ODTTF font file is embedded into Microsoft Office Word.
Figure 6: Performing the XOR operation on the first 32 byte of the TTF
The Python code below simplifies the obfuscation process by converting the
TTF file to an ODTTF file. - fontKey = keys.decode("hex")
- obfFontString = open(ttfFontFile, 'rb').read()
- fontString = [ord(x) for x in obfFontString]
- fori in range(16):
- fontString[i] = ord(obfFontString[i]) ^ ord(fontKey[15‐i])
- fontString[i+16] = ord(obfFontString[i+16]) ^ ord(fontKey[15‐i])
Figure 7: Successfully embedded a crafted TTF font file in Microsoft
@Hunderhan Thank you so much for this!
Not sure if it works, but I asked Chat GPT 4 to reverse the Gist posted above and got https://gist.github.com/jacwright/40c50faa29257c8b76666c1e077352f1. 😄
@Hunderhan Can you explain a bit more your algorithm? I tried to use it, but I was not successful.
DOCX files use a key in the form DCE3A64CF7794054BEB7A126DC76748C to obfuscate/unbofuscate .ttf files. This is the 128-bits GUID you are talking about.
But where do you use this GUID in your algorithm?
In your algorithm, it seems you are using a key of 16 characters, that you use twice, for the first 16 bytes, then again for the last 16 bytes:
fontString[i] = ord(obfFontString[i]) ^ ord(fontKey[15‐i])
fontString[i+16] = ord(obfFontString[i+16]) ^ ord(fontKey[15‐i])