Custom font behaviour when Windows system locale changed
-
I have an issue in a QML application, but I don't know whether it is specific to QML or indeed to Qt, but I am hoping that someone might be able to point me in the right direction.
I am using a custom font that contains some special icon characters. I use this to embed the icons into rich text in a QML
Text
component using markup like this'<font face="%1" size="4" >%2</font> '
. Here%1
is the name of my custom font, and%2
is the unicode character from the font that corresponds to the icon I want to display.The custom font is only used for these few characters, and they are all in the "Private Use Area" of the font. So, for example,
%2
in the above holds codes such as"\ue902"
.What has been reported to me is that if the System Locale is changed (this is on Windows), the icons disappear from the places in the GUI where these icons are meant to appear.
I would have expected that the combination of using a custom font, and referencing only characters in the Private Use Area of the font, should mean that a locale change has no effect here.
Does anyone have any idea what could be happening and whether it might be a Qt-specific issue?
-
The solution to this turned out to be straightforward.
When debugging with the Chinese locale set, I noticed that the text parsing code was not going into the rich text branch and that the
face
specification was simply being ignored. Therefore it would not have been reading the specified unicode character from my custom font but from whatever font was in effect for the text.Recalling that I am working in QML, I noticed I did not have an explicit
textFormat
setting in myText
components. According to the docs, the default isText.AutoText
which uses a heuristic to detect rich text. I assume that in the US locale this was setting the format to rich text. It is still a mystery as to why this does not always work, but I have not had chance to delve into it further. In any case, ensuring an explicittextFormat: Text.RichText
setting in all the relevant places fixes the issue. -
Hi,
Sounds pretty strange indeed.
Do you have dynamic translation in place ?
Does your font gets unloaded for some reason ?
Does it happen while the application is running or during the next start ? -
Hi,
Sounds pretty strange indeed.
Do you have dynamic translation in place ?
Does your font gets unloaded for some reason ?
Does it happen while the application is running or during the next start ? -
I love a good mystery (sorry if this makes me seem like a "rubber-necker", which on this thread I suppose I mostly am).
Another "obvious" question or two.
(You say this was reported to you.) Can you reproduce it?
Does it matter which 2 locales we are talking about when switching locales?
-
I haven't had a lot of chance to investigate yet but I have been able to reproduce it. The bug was reported for a switch from US to Chinese (Simplified) and this is what I tried (I'm in the UK but my locale was already set to US apparently - this is a work machine).
Switching locale requires a reboot so this is what I did, and I then see the issue when I run the application. This answers the second of SGaist's questions above. I'm not sure yet about the font unloading question that he asked.
FWIW, while I have the Chinese locale active, I can load my custom font in FontForge (a free application) and can see the expected symbols in the Private Use Area.
-
Interesting. (Good that you can reproduce!)
When you run the app in Chinese (Simplified), are you sure it is even using your custom font? It sounds to me (just an intuitive guess) that maybe something about the environment (or even something in Qt, though I would hope not) could be "overruling" your choice of custom font and essentially trying to say "i know better. the only font that works in this locale is font ABCDEF, so i'm going to force that font instead of yours."
-
I have spent some time in the debugger today. First, while I had the Chinese locale activated I put some breakpoints in qfontdatabase.cpp, specifically in the
match
function. I observed that when my custom font was requested, it was not matching it and appeared to be falling back to a different font. I assumed that this was some progress in narrowing down the issue. Unfortunately when I switched back to US locale I saw exactly the same behaviour when stepping through the code!I can't spend a lot more time on this at the moment as it's not a high priority defect but I will update here as and when I find more information.
-
The solution to this turned out to be straightforward.
When debugging with the Chinese locale set, I noticed that the text parsing code was not going into the rich text branch and that the
face
specification was simply being ignored. Therefore it would not have been reading the specified unicode character from my custom font but from whatever font was in effect for the text.Recalling that I am working in QML, I noticed I did not have an explicit
textFormat
setting in myText
components. According to the docs, the default isText.AutoText
which uses a heuristic to detect rich text. I assume that in the US locale this was setting the format to rich text. It is still a mystery as to why this does not always work, but I have not had chance to delve into it further. In any case, ensuring an explicittextFormat: Text.RichText
setting in all the relevant places fixes the issue. -
Nice !
Thanks for the feedback !
Since you have it working now, please mark the thread as solved so other forum users may know a solution has been found :-)
-