html-to-docx
html-to-docx copied to clipboard
feat(font-family): use the first element as the font name and the last a generic font name
Hello,
Goal of this PR
- extract the font information from the CSS
font-family
, - insert the font in the text using
rFonts
in thedocument.xml
- register the used fonts in
fontTable.xml
, hinting the client about the font style if it doesn't have it installed.
CSS to DOCX font mapping
Given the following CSS: font-family: MyFont, Font1, Font2, serif
:
-
MyFont
will be the font name, -
Font1, Font2
will be ignored, -
serif
will be used to extract font properties.
generic font names
The three main generic font names are translated to font properties:
serif
<w:font w:name="MyFont">
<w:altName w:val="Times New Roman"/>
<w:family w:val="roman"/>
<w:pitch w:val="variable"/>
</w:font>
sans-serif
<w:font w:name="MyFont">
<w:altName w:val="Arial"/>
<w:family w:val="swiss"/>
<w:pitch w:val="variable"/>
</w:font>
monospace
<w:font w:name="MyFont">
<w:altName w:val="Courier New"/>
<w:family w:val="modern"/>
<w:pitch w:val="fixed"/>
</w:font>
Results really depends on the Word client:
-
Word Desktop
work as intended, -
LibreOffice
ignore thefontTable.xml
file, and find a font by itself -
Word Online
ignore thefontTable.xml
file, and find a font by itself but, since he is packed with a lot of fonts, find the wanted font most of the time.
@erenard Could you please cherry-pick these changes onto develop?
Very nice feature, but I accidentally found that it doesn't seem to work with Chinese. 😊
I found that it was caused by <w:rFonts>
missing the w:eastAsia
attribute.
eastAsia: Specifies the font to be used to format characters in an East Asian Unicode range. For example, Japanese text might be displayed using the MS Mincho font: <w:rFonts w:eastAsia="MS Mincho"/>.
Hey @erenard this is quite useful! Can you rebase with develop? Maybe I can QA it and get it merged
Hello, the rebase is done.