emoji alignment special character alignment text alignment

How to align emoji, how to align special characters, advanced text alignment

Question elicitation

When we print out the program, if the characters we output contain emoji text or other special character text, our print alignment effect will be misaligned. The following code reproduces this effect (tips: Teacher Ma, the | symbol in front is to observe the alignment effect):

# -*- coding:UTF-8 -*-

# region introduces necessary dependencies
from DebugInfo.DebugInfo import *

#endregion



if __name__ == "__main__":
    artboard = printtemplate()
    Artboard.Print blank lines(2)

    Artboard.PrepareForm()
    Drawing board. Add a line ('serial number', 'name', 'self-evaluation', 'class teacher'). Modify the line (green characters) # as the title line
    Drawing board. Add a line ('1', 'Xiaohong', 'I am very cheerful', '|Teacher Ma')
    Artboard.AddDelimitedRow()
    Drawing board. Add a line ('2', 'Zhu Xiaoming', 'I got a lot of flowers?', '|Mr. Ma')
    Artboard.AddDelimitedRow()
    Drawing board. Add a line ('3', 'Zhao Hong', 'I have love?', '|Teacher Ma')
    Artboard.AddDelimitedRow()
    Drawing board. Add a line ('4', 'Zhang Tianzhi', 'I am a girl♀?I have long hair', '|Teacher Ma')

    Artboard.Add blank line()
    Artboard.DisplayTable()

The running print alignment of the above code is as follows:

20231104175834

We can observe that Zhu Xiaoming is moving and Teacher Ma is moving forward.
We can observe again that Zhang Tianzhixing and Teacher Ma stayed behind

Explanation of reasons

Why does the above alignment phenomenon occur? This starts with fonts.
On our computers, the symbols displayed must be displayed in a certain font. Therefore, the effect we see of the same character in Song font is different from the effect we see in bold font.

The number of screen pixels occupied by a character to display its effect is called the display pixel width of the character; for convenience, we take the ratio of the pixel width of the character to the pixel width occupied by the English character space as the following we will Describes the character display width value.

  • Obviously, the character display width value of English spaces is 1
  • The display width value of an English character (uppercase and lowercase) is 1
  • For Chinese in the italic font shown below, the display width value of one character is 2
    20231104181232

Display, for any character that can be displayed on the computer, it has a display width, but different characters, in different fonts, have different display width values.

With the above rules in mind, it will be easier if we need to align text. We calculate the display width of the characters. According to the display width, after calculating and adding the corresponding number of spaces, the text can be aligned.

Now let’s talk about the problem of Teacher Ma’s inconsistency above. According to the explanation of the alignment principle above, Teacher Ma’s misalignment must be because there must be such a character in the text. The calculated display width of this character is inconsistent with the width occupied by its actual display, resulting in an error in calculating the number of spaces when adding spaces. .

Find out the real murderer

In the table above, we find that the symbol ? ? ♀? is an unusual character. Let’s remove these characters and observe the alignment effect, as follows:

# -*- coding:UTF-8 -*-

# region introduces necessary dependencies
from DebugInfo.DebugInfo import *

#endregion



if __name__ == "__main__":
    artboard = printtemplate()
    Artboard.Print blank lines(2)

    Artboard.PrepareForm()

    Drawing board. Add a line ('serial number', 'name', 'self-evaluation', 'class teacher'). Modify the line (green characters) # as the title line
    Drawing board. Add a line ('1', 'Xiaohong', 'I am very cheerful', '|Teacher Ma')
    Artboard.AddDelimitedRow()
    # Drawing board. Add a line ('2', 'Zhu Xiaoming', 'I got a lot of flowers?', '|Mr. Ma')
    Drawing board. Add a line ('2', 'Zhu Xiaoming', 'I got a lot of flowers', '|Mr. Ma')
    Artboard.AddDelimitedRow()
    # Drawing board. Add a line ('3', 'Zhao Hong', 'I have love?', '|Teacher Ma')
    Drawing board. Add a line ('3', 'Zhao Hong', 'I have love', '|Teacher Ma')
    Artboard.AddDelimitedRow()
    # Drawing board. Add a line ('4', 'Zhang Tianzhi', 'I am a girl♀?I have long hair', '|Teacher Ma')
    Drawing board. Add a line ('4', 'Zhang Tianzhi', 'I am a girl and I have long hair', '|Teacher Ma')

    Artboard.Add blank line()
    Artboard.DisplayTable()

The above code runs as follows:

20231104222547

Well, the alignment is really beautiful.
Therefore, we focus the problem on the three characters ? ? ♀?. The following code prints and displays the displayed width and actual occupied width of these three characters.

# -*- coding:UTF-8 -*-

# region introduces necessary dependencies
from DebugInfo.DebugInfo import *

#endregion



if __name__ == "__main__":
    artboard = printtemplate()
    Artboard.Print blank lines(2)

    Artboard.PrepareForm()

    Artboard. Add a line ('Character', 'Display width value'). Modify the line (green characters)
    Artboard.AddDelimitedRow()
    Artboard.Add a line('?|', display width('?'))
    Artboard.Add a line('?|', display width('?'))
    Artboard.Add a line('♀?|', display width('♀?'))

    Artboard.Add blank line()
    Artboard.DisplayTable()

The above code runs as follows:
20231104213053
We can see in the picture above (note the position of the reference symbol |):

  • The character ? displays a occupied width of 1, and the calculated display width is 2 (this will result in one less space to be filled when aligning and filling spaces)
  • Character ? The display occupied width is 1, and the calculated display width is also 1 (the actual display width is consistent with the calculated display width, and the alignment and filling of spaces are normal)
  • The character ♀? displays an occupied width of 2, but the calculated display width is 1 (this will result in one more space being added when aligning spaces)

Therefore, the root cause of the alignment error is that when the symbols ? and ♀? are displayed on the terminal, the character width occupied by these two characters is inconsistent with the calculated character width, resulting in an incorrect number of spaces being filled in when calculating the alignment padding.

Remedial measures

With the above analysis, we know that the root cause of alignment misalignment is that the calculation of the display width value of some special characters does not match the actual value. Then our corresponding solution is clear: correct the display width value of the special characters.

In the following code, we specify the display width value of special characters and tell it to the [Print Template Object] (here is the artboard). Then we print and display the text with special characters and observe its alignment effect:

# -*- coding:UTF-8 -*-

# region introduces necessary dependencies
from DebugInfo.DebugInfo import *

#endregion



if __name__ == "__main__":
    artboard = printtemplate()
    Artboard.Print blank lines(2)

    Artboard.PrepareForm()
    Artboard. Set special character width dictionary ({<!-- -->'?': 1, '♀': 2}) # Correct the display width value of special symbols

    Drawing board. Add a line ('serial number', 'name', 'self-evaluation', 'class teacher'). Modify the line (green characters) # as the title line
    Drawing board. Add a line ('1', 'Xiaohong', 'I am very cheerful', '|Teacher Ma')
    Drawing board. Add a line ('2', 'Zhu Xiaoming', 'I got a lot of flowers?', '|Mr. Ma')
    Drawing board. Add a line ('3', 'Zhao Hong', 'I have love?', '|Teacher Ma')
    Drawing board. Add a line ('4', 'Zhang Tianzhi', 'I am a girl♀? I have long hair', '|Teacher Ma')

    Artboard.Add blank line()
    Artboard.DisplayTable()

In the above code, please pay attention to the Set special character width dictionary method, which corrects the display width values of the symbols ? and ♀?, which helps the program correctly understand and calculate the actual display width of the characters , so that when aligning and filling spaces, the correct number of spaces can be filled.

20231104222101

Other pitfalls

In the above analysis process, we mentioned that all symbols displayed and processed on the computer depend on the font, which means that the same symbol may be displayed in different fonts (relative to spaces or symbols). -), it may be different. In some fonts currently observed, if the width of English characters is calculated as unit 1, the width of Chinese characters is 1.5. In this non-integer multiple font, a mixture of Chinese and English is encountered. When doing this, alignment is very difficult or even impossible.

For special characters, the display width is also different in different fonts, so more attention should be paid to handling in actual applications.

In addition, in order to facilitate the calculation of text alignment, when using it, you need to pay attention to whether the font used by the terminal is a fixed-width font. In non-monospaced fonts, the font width may not be an integer, and the alignment processing effect will be affected.

Summary

Thank you very much for your attention and reading. If there is something wrong, please criticize and point it out so that it can be corrected in time.
.

For special characters, the display width is also different in different fonts, so more attention should be paid to handling in actual applications.

In addition, in order to facilitate the calculation of text alignment, when using it, you need to pay attention to whether the font used by the terminal is a fixed-width font. In non-monospaced fonts, the font width may not be an integer, and the alignment processing effect will be affected.

Summary

Thank you very much for your attention and reading. If there is something wrong, please criticize and point it out so that it can be corrected in time.