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

Embedding font #239

Closed
filippomuscolino opened this issue Jan 10, 2019 · 22 comments · Fixed by #2174
Closed

Embedding font #239

filippomuscolino opened this issue Jan 10, 2019 · 22 comments · Fixed by #2174

Comments

@filippomuscolino
Copy link
Contributor

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

@dolanmiu
Copy link
Owner

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/

@filippomuscolino
Copy link
Contributor Author

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)

@dolanmiu
Copy link
Owner

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

@filippomuscolino
Copy link
Contributor Author

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

@dolanmiu
Copy link
Owner

Seems doable actually!

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

@filippomuscolino
Copy link
Contributor Author

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

@dolanmiu
Copy link
Owner

dolanmiu commented Mar 5, 2019

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?

@jwerre
Copy link

jwerre commented Aug 22, 2019

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

@dolanmiu
Copy link
Owner

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

@dolanmiu
Copy link
Owner

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
Copy link
Owner

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

@vijayarun
Copy link

any update?

@dolanmiu
Copy link
Owner

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

@hueyhe
Copy link

hueyhe commented Oct 14, 2021

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?

@LunaSquee
Copy link

I would also be very interested in this.

@dolanmiu
Copy link
Owner

Are there any free TTF to ODTTF converters online?

@LunaSquee
Copy link

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

@dolanmiu
Copy link
Owner

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

@Hunderhan
Copy link

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

@dolanmiu
Copy link
Owner

dolanmiu commented Mar 28, 2023

@Hunderhan Thank you so much for this!

dolanmiu added a commit that referenced this issue Mar 30, 2023
@jacwright
Copy link
Contributor

jacwright commented May 24, 2023

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

dolanmiu added a commit that referenced this issue Jun 12, 2023
Stosiu pushed a commit to Stosiu/docx that referenced this issue Sep 1, 2023
longgtruong added a commit to OpenDeur/docx that referenced this issue Oct 11, 2023
@Gin-Quin
Copy link

Gin-Quin commented Dec 6, 2023

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

dolanmiu added a commit that referenced this issue Dec 22, 2023
dolanmiu added a commit that referenced this issue Dec 27, 2023
dolanmiu added a commit that referenced this issue Dec 29, 2023
dolanmiu added a commit that referenced this issue Dec 30, 2023
* #239 Embedded fonts

* Add boilerplate for font table

* Fix linting

* Fix linting

* Fix odttf naming

* Correct writing of fonts to relationships and font table

new uuid function

* Add font to run

* Add demo

Fix tests

* Add character set support

* Add tests

* Write tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants