Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cyrillic support via "add_input_text" #2212

Closed
Den4ina opened this issue Oct 30, 2023 · 2 comments
Closed

Cyrillic support via "add_input_text" #2212

Den4ina opened this issue Oct 30, 2023 · 2 comments
Labels
state: pending not addressed yet type: bug bug

Comments

@Den4ina
Copy link

Den4ina commented Oct 30, 2023

Hello, dear developers!
I have been actively using your project for a long time. Cyrillic characters are displayed in my interface (the appropriate font is installed and a range is added). However, I can't type Cyrillic into text cells when using "Windows 10". In "Linux" everything works fine. I am using "add_char_remap", however when saving the value from the cell using "get_value" in ".json file" I get distorted characters. Therefore, I have to convert them back to the desired code. It sounds very heavy and "crutchy". Maybe I'm not taking something into account.
Is there a ready-made way to support Cyrillic input for win32?
I have reviewed many forums, but have not found a suitable solution.

The Cyrillic alphabet is displayed correctly in the interface and text input fields:
Screenshot_1

However, when saving, I get a distortion of the characters:
Screenshot_2

If I have to translate the string back to the correct encoding when saving the file, the question can be closed. But if there is another solution, I will be glad to get answers

@Den4ina Den4ina added state: pending not addressed yet type: bug bug labels Oct 30, 2023
@v-ein
Copy link
Contributor

v-ein commented Oct 30, 2023

Hello @Den4ina - I wonder if your issue is related to #1075. From your description, it's not exactly the same issue, but may be related. If you are interested in learning more about #1075 - it's actually an issue in Dear ImGui, here's the corresponding ImGui ticket: ocornut/imgui#5725.

There's a chance that #1075 can be fixed in DPG, but I don't know for sure yet.

As for your issue, I'm not sure what's going on. If I remember correctly, in my tests I was unable to display non-ANSI characters (e.g. Cyrillic) in input_text. I wonder why that part works for you on Windows.

@Den4ina
Copy link
Author

Den4ina commented Oct 30, 2023

Thank you so much for the quick response!
Yes, it's the same problem. I managed to solve it with the help of information from the "Issues" section of your repository. But as I said, the solutions are quite cumbersome and it seemed to me that there might already be a more concise solution.

Below I presented a solution based on the information I found. This is far from perfect and clean code, but it is a working draft version that allows me to enter Cyrillic characters and decode the string using the "decode_string" method.
I'll leave a code snippet here in case it might be useful to someone.

A class for correcting Cyrillic characters

class CyrillicSupport:
    # Parameters for Cyrillic conversion
    big_let_start = 0x00C0  # Capital "А" in Cyrillic.
    big_let_end = 0x00DF  # Capital "Я" in Cyrillic.
    small_let_end = 0x00FF  # Little "я" in Cyrillic
    remap_big_let = 0x0410  # Initial number for the reassigned Cyrillic alphabet

    alph_len = big_let_end - big_let_start + 1  # adds a shift from large letters to small ones
    alph_shift = remap_big_let - big_let_start  # adds a transition from reassignment to non-reassignment

    def __init__(self, app_path):
        self.app_path = app_path

        # Path to the font file 
        font_file = 'UbuntuMono-R.ttf'
        self.font_path = os.path.join(self.app_path, 'fonts', font_file)

Font loading

def registry_font(self):

        with dpg.font_registry():
            with dpg.font(self.font_path, size=18) as self.font:
                dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic)
                dpg.add_font_range_hint(dpg.mvFontRangeHint_Default)

                dpg.add_font_range(0x0391, 0x03C9)  #Greek character range
                dpg.add_font_range(0x2070, 0x209F)  #Range of upper and lower numerical indices

                # Fixing keyboard input on Windows
                if sys.platform == 'win32':
                    self._remap_chars()

               # Set font
                dpg.bind_font(self.font)

Reassigning characters for input

def _remap_chars(self):
        biglet = self.remap_big_let  # Initial number for the reassigned Cyrillic alphabet
        for i1 in range(self.big_let_start, self.big_let_end + 1):  # Cyclic switching of large letters
            dpg.add_char_remap(i1, biglet)  # Reassigning the big letter
            dpg.add_char_remap(i1 + self.alph_len, biglet + self.alph_len)  # Reassign a small letter
            biglet += 1  # choose the next letter

        #The letters "Ёё" must be added separately, since they are located elsewhere in the table
        dpg.add_char_remap(0x00A8, 0x0401)
        dpg.add_char_remap(0x00B8, 0x0451)

Decode the string into normal Cyrillic to save to a file

  def decode_string(self, instr : str):

        if sys.platform == 'win32':
            outstr = []
            for i in range(0, len(instr)):
                char_byte = ord(instr[i])
                if char_byte in range(self.big_let_start, self.small_let_end + 1):
                    char = chr(ord(instr[i]) + self.alph_shift)
                    outstr.append(char)
                # Checking for "Ё"
                elif char_byte == 0x00A8:
                    char = chr(0x0401)
                    outstr.append(char)
                 # Checking for "ё"
                elif char_byte == 0x00B8:
                    char = chr(0x0451)
                    outstr.append(char)
                else:
                    outstr.append(instr[i])

            return ''.join(outstr)

        else:
            return instr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: pending not addressed yet type: bug bug
Projects
None yet
Development

No branches or pull requests

2 participants