Bug 166357 - LibreOffice Writer and Impress stores and reads wrong font weights
Summary: LibreOffice Writer and Impress stores and reads wrong font weights
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
7.6.2.1 release
Hardware: All Linux (All)
: medium normal
Assignee: Khaled Hosny
URL:
Whiteboard: target:26.2.0
Keywords:
Depends on:
Blocks: Fonts
  Show dependency treegraph
 
Reported: 2025-04-26 15:27 UTC by Zirneklītis
Modified: 2025-06-10 22:23 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Document before saving (24.33 KB, image/png)
2025-04-26 15:30 UTC, Zirneklītis
Details
The document saved and opened (23.96 KB, image/png)
2025-04-26 15:31 UTC, Zirneklītis
Details
PDF exported from newly created document. (268.50 KB, application/pdf)
2025-04-26 21:32 UTC, Zirneklītis
Details
PDF exported from re-opened document. (239.60 KB, application/pdf)
2025-04-26 21:33 UTC, Zirneklītis
Details
Saved example. (21.78 KB, application/vnd.oasis.opendocument.text)
2025-04-26 21:34 UTC, Zirneklītis
Details
Saved example (safe mode) (21.94 KB, application/vnd.oasis.opendocument.text)
2025-04-26 21:34 UTC, Zirneklītis
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zirneklītis 2025-04-26 15:27:49 UTC
Description:
Both “LibreOffice Writer” and “LibreOffice Impress” stores wrong font
weight values. The saved document containing a font with all acceptable
font weights (“Thin” (100); “Ultra Light, Extra Light” (200); “Light”
(300); “Regular, Normal” (400); “Medium” (500); “Semi Bold, Demi Bold”
(600); “Bold” (700); “Extra Bold, Ultra Bold” (800); “Heavy, Black”
(900)) has wrong weight values in the “content.xml” file.

The wrong values are for three font weights:
- “Ultra Light, Extra Light” (200): fo:font-weight="150";
- “Light” (300): fo:font-weight="250";
- “Medium” (500): fo:font-weight="normal".

So there is no surprise that some of the font weight information is
lost on reopening the document.

I have tried both the ODP or ODT file to unzip and replace these wrong
values with the correct ones in the “content.xml” file. Zipped them and
renamed. No luck. The fonts weights are not the same as they should be.


Steps to Reproduce:
1. For writing text, use font which besides regular weight has at least Medium font weight.
2. Change some characters to Medium weight.
3. Save, close and open document.


Actual Results:
The Medium font weight is changed to Regular weight.


Expected Results:
The document should look as it was before closing.



Reproducible: Always


User Profile Reset: No

Additional Info:
Creates document with wrong weight values.
Comment 1 Zirneklītis 2025-04-26 15:30:25 UTC
Created attachment 200542 [details]
Document before saving
Comment 2 Zirneklītis 2025-04-26 15:31:23 UTC
Created attachment 200543 [details]
The document saved and opened
Comment 3 m_a_riosv 2025-04-26 16:56:42 UTC
Please, where is the sample file?

Please test in safe mode, Menu/Help/Restart in Safe Mode

Please paste here the information on Menu/Help/About LibreOffice (There is an icon to copy)
Comment 4 Zirneklītis 2025-04-26 21:31:21 UTC
$ ./soffice --version
LibreOffice 25.2.2.2 7370d4be9e3cf6031a51beef54ff3bda878e3fac

Version: 25.2.2.2 (X86_64) / LibreOffice Community
Build ID: 7370d4be9e3cf6031a51beef54ff3bda878e3fac
CPU threads: 4; OS: Linux 6.11; UI render: default; VCL: gtk3
Locale: lv-LV (lv_LV.utf8); UI: en-US
Calc: threaded

Safe-mode gives the same result.
Comment 5 Zirneklītis 2025-04-26 21:32:47 UTC
Created attachment 200549 [details]
PDF exported from newly created document.
Comment 6 Zirneklītis 2025-04-26 21:33:37 UTC
Created attachment 200550 [details]
PDF exported from re-opened document.
Comment 7 Zirneklītis 2025-04-26 21:34:17 UTC
Created attachment 200551 [details]
Saved example.
Comment 8 Zirneklītis 2025-04-26 21:34:48 UTC
Created attachment 200552 [details]
Saved example (safe mode)
Comment 9 Zirneklītis 2025-04-26 21:40:00 UTC
LibreOffice 7.6.2.1 has already this bug. Not sure which version is earliest.
Comment 10 m_a_riosv 2025-04-27 07:48:59 UTC

*** This bug has been marked as a duplicate of bug 35538 ***
Comment 11 Khaled Hosny 2025-06-02 08:47:07 UTC
Bug 35538 is a too broad issue, the issue here is more specific and can potentially be fixed independent of bug 35538.
Comment 12 Khaled Hosny 2025-06-02 09:44:05 UTC
This appears to be caused by the weight mapping in https://git.libreoffice.org/core/+/refs/heads/master/xmloff/source/style/weighhdl.cxx#44:

FontWeightMapper const aFontWeightMap[] =
{
    { css::awt::FontWeight::DONTKNOW,              0 },
    { css::awt::FontWeight::THIN,                  100 },
    { css::awt::FontWeight::ULTRALIGHT,            150 },
    { css::awt::FontWeight::LIGHT,                 250 },
    { css::awt::FontWeight::SEMILIGHT,             350 },
    { css::awt::FontWeight::NORMAL,                400 },
    { css::awt::FontWeight::NORMAL,                450 },
    { css::awt::FontWeight::SEMIBOLD,              600 },
    { css::awt::FontWeight::BOLD,                  700 },
    { css::awt::FontWeight::ULTRABOLD,             800 },
    { css::awt::FontWeight::BLACK,                 900 },
    { css::awt::FontWeight::DONTKNOW,             1000 }
};

The numbers seems to match what is saved in the ODF fo:font-weight. The list also has SEMILIGHT which OpenType does not have (https://learn.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass) and missed medium weight.

Elsewhere, medium weight seems to map to css::awt::FontWeight::NORMAL, which matches what is saved in ODF as well https://git.libreoffice.org/core/+/refs/heads/master/vcl/source/app/unohelp.cxx#112:

    else if( ( eWeight == WEIGHT_NORMAL ) || ( eWeight == WEIGHT_MEDIUM ) )
        return css::awt::FontWeight::NORMAL;

I think fixing this requires modifying css::awt::FontWeight to support medium weight and fixing the mapping, but this is a a “Published UNO API” (whatever that is) and must not be changed incompatibility, and changing the mapping to weight numbers might break existing documents, so this might also need a compatibility flag.

Hopefully the above can be helpful for someone looking into this.
Comment 13 Khaled Hosny 2025-06-02 10:12:44 UTC
Here is a preliminary fix https://gerrit.libreoffice.org/c/core/+/186128. With it opening the attached document shows all correct weights except for medium (because it is saved as normal), and fixing medium manually and saving the document the correct weight persists.

I have no time to work further on this fix, though.
Comment 14 Khaled Hosny 2025-06-02 16:50:46 UTC
Checking the OpenDocument spec (https://docs.oasis-open.org/office/v1.2/cd05/OpenDocument-v1.2-cd05-part1.html#a_20_184_fo_font-weight), it has the following text:
The values of the fo:font-weight attribute are normal, bold, 100, 200, 300, 400, 500, 600, 700, 800 or 900.

So the values LibreOffice currently saves are non-standard. So may be this can be considered a bug fix and backward compatibility concerns can be ignored (it is probably also low impact given that extended families are not supported on Windows, nor on Linux except for variable fonts).
Comment 15 Eyal Rozenberg 2025-06-02 22:44:24 UTC
Can this not be reproduced with less-exotic typefaces than Exo 2 and Saira? e.g. with Liberation Sans or Serif?

Also, is there something special you see with the second typeface (Saira)? In terms of the wrong font weights?
Comment 16 Eyal Rozenberg 2025-06-02 22:46:18 UTC
(In reply to Khaled Hosny from comment #14)
> the OpenDocument spec ... has the following text:
> The values of the fo:font-weight attribute are normal, bold, 100, 200, 300,
> 400, 500, 600, 700, 800 or 900.
> 
> So the values LibreOffice currently saves are non-standard.

You mean, 150 and 250?

I wonder where we got those from.
Comment 17 Khaled Hosny 2025-06-03 13:05:35 UTC
(In reply to Eyal Rozenberg from comment #16)
> (In reply to Khaled Hosny from comment #14)
> > the OpenDocument spec ... has the following text:
> > The values of the fo:font-weight attribute are normal, bold, 100, 200, 300,
> > 400, 500, 600, 700, 800 or 900.
> > 
> > So the values LibreOffice currently saves are non-standard.
> 
> You mean, 150 and 250?

Yes.

> I wonder where we got those from.

No idea. The mapping has been like this since the first public commit in 2000. This probably was untested, since fonts with such weight range was not common and not supported in Windows (the font family name would be "Exo 2 Light" with style "Regular" under GDI, so the numeric weight would not affect font selection).
Comment 18 Khaled Hosny 2025-06-10 10:00:41 UTC
I’m now convinced this is the right fix and the backward compatibility considerations are negligible, so I’m going to merge the fix, and if it proved to be problematic we can revert it.
Comment 19 Commit Notification 2025-06-10 16:26:33 UTC
Khaled Hosny committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/b2b31dd8be9d39434aa950a96d116abd4030f4b8

tdf#166357: Fix saving font weight to documents

It will be available in 26.2.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 20 Eyal Rozenberg 2025-06-10 22:23:41 UTC
(In reply to Commit Notification from comment #19)

I'm actually kind of worried about the awt::FontWeight values, generally. What are they used for, directly?