html-to-docx icon indicating copy to clipboard operation
html-to-docx copied to clipboard

feat(font-family): use the first element as the font name and the last a generic font name

Open erenard opened this issue 2 years ago • 2 comments

Hello,

Goal of this PR

  • extract the font information from the CSS font-family,
  • insert the font in the text using rFonts in the document.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 the fontTable.xml file, and find a font by itself
  • Word Online ignore the fontTable.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 avatar Dec 28 '21 15:12 erenard

@erenard Could you please cherry-pick these changes onto develop?

privateOmega avatar Mar 10 '22 18:03 privateOmega

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"/>.

drawmoon avatar Apr 20 '22 06:04 drawmoon

Hey @erenard this is quite useful! Can you rebase with develop? Maybe I can QA it and get it merged

nicolasiscoding avatar Nov 10 '22 04:11 nicolasiscoding

Hello, the rebase is done.

erenard avatar Nov 28 '22 13:11 erenard