docx icon indicating copy to clipboard operation
docx copied to clipboard

Embedding font

Open filippomuscolino opened this issue 6 years ago • 18 comments

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

filippomuscolino avatar Jan 10 '19 10:01 filippomuscolino

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/

dolanmiu avatar Jan 10 '19 12:01 dolanmiu

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)

filippomuscolino avatar Jan 10 '19 13:01 filippomuscolino

This document is 5039 pages long, can you direct me to the specific page?

dolanmiu avatar Jan 10 '19 14:01 dolanmiu

You're right, sorry! :) It's page 669 (chapter 17.8)

filippomuscolino avatar Jan 10 '19 16:01 filippomuscolino

Seems doable actually!

Thanks! I'll most likely prepare it for the v5 release

dolanmiu avatar Jan 10 '19 21:01 dolanmiu

Great! I'll wait for it, then! Thanks

filippomuscolino avatar Jan 11 '19 08:01 filippomuscolino

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?

dolanmiu avatar Mar 05 '19 21:03 dolanmiu

I wonder if you could include a font engine such as fontkit to embed the font in the document.

jwerre avatar Aug 22 '19 23:08 jwerre

Never heard of it, but I'll take a look

dolanmiu avatar Aug 22 '19 23:08 dolanmiu

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

dolanmiu avatar Aug 22 '19 23:08 dolanmiu

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

dolanmiu avatar Aug 22 '19 23:08 dolanmiu

any update?

vijayarun avatar Dec 18 '20 13:12 vijayarun

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

dolanmiu avatar Dec 24 '20 01:12 dolanmiu

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?

hueyhe avatar Oct 14 '21 11:10 hueyhe

I would also be very interested in this.

LunaSquee avatar Oct 15 '21 09:10 LunaSquee

Are there any free TTF to ODTTF converters online?

dolanmiu avatar Oct 18 '21 12:10 dolanmiu

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

LunaSquee avatar Oct 18 '21 12:10 LunaSquee

If you can figure it out and make a PR, that would be much appreciated!

dolanmiu avatar Oct 18 '21 23:10 dolanmiu

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.

  1. A 128-bit Global Unique Identifier (GUID) is generated.
  2. An XOR operation on the first 32 byte of the TTF is performed.
  3. The ODTTF font file is embedded into Microsoft Office Word. image 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.
  4. fontKey = keys.decode("hex")     
  5. obfFontString = open(ttfFontFile, 'rb').read()
  6. fontString = [ord(x) for x in obfFontString]
  7. fori in range(16):
  8.      fontString[i] = ord(obfFontString[i]) ^ ord(fontKey[15‐i])
  9.      fontString[i+16] = ord(obfFontString[i+16]) ^ ord(fontKey[15‐i]) image Figure 7: Successfully embedded a crafted TTF font file in Microsoft

Hunderhan avatar Mar 27 '23 02:03 Hunderhan

@Hunderhan Thank you so much for this!

dolanmiu avatar Mar 28 '23 02:03 dolanmiu

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. 😄

jacwright avatar May 24 '23 22:05 jacwright

@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])

Gin-Quin avatar Dec 06 '23 15:12 Gin-Quin