Author Topic: Editor enable DirectWrite or not have different font width  (Read 1444 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Editor enable DirectWrite or not have different font width
« on: October 08, 2022, 01:39:46 pm »
I'm using this font: be5invis/Sarasa-Gothic: Sarasa Gothic / 更纱黑体 / 更紗黑體 / 更紗ゴシック / 사라사 고딕

Especially the mono Simplified Chinese(SC) version. That is: The width of the Chinese character is 2 times as the English character.

Now, the text shows differently when I have DirectWrite enabled or not, see the attachments. When DirectWrite is enabled, the alignment is broken.

Any ideas?





If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline New Pagodi

  • Multiple posting newcomer
  • *
  • Posts: 42
Re: Editor enable DirectWrite or not have different font width
« Reply #1 on: October 08, 2022, 07:18:42 pm »
I'm not sure about codeblocks, but in the stc source the average char width for a font is set like this:

Code
m_averageCharWidth = textmetrics.width/extentTest.length();

where extentTest is is a wxString built from " `~!@#$%^&*()-_=+\\|[]{};:\"\'<,>.?/1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Obviously that's going to have trouble with nonlatin characters.  A better test for average char width is needed that can account for nonlatin characters, but I have no clue how to do that.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Editor enable DirectWrite or not have different font width
« Reply #2 on: October 09, 2022, 04:30:05 am »
Hi, New Pagodi, thanks for the reply.

I just test the wxWidgets' font sample code.

It looks like when I choose the font size = 10 or font size = 12, the alignment is correct, while if I select the font size = 11, the alignment is wrong.

I just checked this issue under Notepad++, it works correctly in all the three font sizes.

So, my guess is that this issue if from the wxWidgets' font handling, not from the stc source.

See the screen shot below:


EDIT:

Compared with Notepad++, it looks like that in wx font sample, when the mis-alignment happens, the Chinese characters width is correct, and the English character has one extra space in the horizontal direction. So, the English text line is a bit longer than the Chinese text line.


« Last Edit: October 09, 2022, 04:32:37 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Editor enable DirectWrite or not have different font width
« Reply #3 on: October 11, 2022, 02:49:50 pm »
Hi, everyone, it looks like this is related to the font issue.

See this issue and discussion:

CJK characters alignment issue in some applications Issue #1 jonz94/Sarasa-Gothic-Nerd-Fonts

And this fork of the Sarasa-Gothic font fixed such issue.

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Editor enable DirectWrite or not have different font width
« Reply #4 on: October 11, 2022, 03:00:27 pm »
There are mainly two issues here:
1, different font size are not aligned correctly, this issue is related to font itself, see my previous post

2, the DirectWrite/Non-DirectWrite issue still remains. The alignment is totally wrong when DirectWrite is enabled.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Editor enable DirectWrite or not have different font width
« Reply #5 on: October 12, 2022, 06:30:06 am »
I found another issue:

Code::Blocks' editor will be extremely slow when I use the be5invis/Sarasa-Gothic: Sarasa Gothic / 更纱黑体 / 更紗黑體 / 更紗ゴシック / 사라사 고딕. The font with hinting feature cause this issue. If I use the unhinted version, the editor renders fast.

Here is the related discussion:
https://github.com/be5invis/Sarasa-Gothic/issues/218#issuecomment-757409436

Quote
Sarasa intensively uses TT hints to maximize clarity - so a badly implemented rasterizer may become slow when scaling them.

I just check the Notepad++, and it works fast in either hinted or unhinted font.

Do you know how to fix the Code::Blocks' render?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.