sudipshil9862 / fonts-compare

fonts rendering and comparing
GNU General Public License v2.0
6 stars 2 forks source link

shifting from otfinfo to freetype to get fontversion but fontversion gives non printable strings #49

Closed sudipshil9862 closed 1 year ago

sudipshil9862 commented 1 year ago

shifting from otfinfo to freetype to get fontversion but fontversion gives non printable strings which cause disappearing the text of Gtk.Label

sudipshil9862 commented 1 year ago

first require python3-freetype and freetype-devel for fedora packaging of fonts_compare.

sudipshil9862 commented 1 year ago

first I checked, though the fontversion of otfinfo and freetype is same, still python compared them as NON EQUAL string.

fontbutton1:
-------
otfinfo_version:  Version 2.010
freetype_version:  Version 2.010
otfinfo_version NOT EQUAL freetype_version

fontbutton2:
--------
otfinfo_version:  Version 2.009
freetype_version:  Version 2.009
otfinfo_version NOT EQUAL freetype_version
sudipshil9862 commented 1 year ago

now lets check for non-printable string that are given by freetype I wrote a block of code to find that

for char in font_version:
            print(f"Character: {char}, ASCII Code: {ord(char)}")
for char in version_string:
            print(f"Character: {char}, ASCII Code: {ord(char)}")

and the result is what we accepted to come:

 otfinfo_version:  Version 2.010
Character: V, ASCII Code: 86
Character: e, ASCII Code: 101
Character: r, ASCII Code: 114
Character: s, ASCII Code: 115
Character: i, ASCII Code: 105
Character: o, ASCII Code: 111
Character: n, ASCII Code: 110
Character:  , ASCII Code: 32
Character: 2, ASCII Code: 50
Character: ., ASCII Code: 46
Character: 0, ASCII Code: 48
Character: 1, ASCII Code: 49
Character: 0, ASCII Code: 48
freetype_version:  Version 2.010
Character: , ASCII Code: 0
Character: V, ASCII Code: 86
Character: , ASCII Code: 0
Character: e, ASCII Code: 101
Character: , ASCII Code: 0
Character: r, ASCII Code: 114
Character: , ASCII Code: 0
Character: s, ASCII Code: 115
Character: , ASCII Code: 0
Character: i, ASCII Code: 105
Character: , ASCII Code: 0
Character: o, ASCII Code: 111
Character: , ASCII Code: 0
Character: n, ASCII Code: 110
Character: , ASCII Code: 0
Character:  , ASCII Code: 32
Character: , ASCII Code: 0
Character: 2, ASCII Code: 50
Character: , ASCII Code: 0
Character: ., ASCII Code: 46
Character: , ASCII Code: 0
Character: 0, ASCII Code: 48
Character: , ASCII Code: 0
Character: 1, ASCII Code: 49
Character: , ASCII Code: 0
Character: 0, ASCII Code: 48
otfinfo_version NOT EQUAL freetype_version
sudipshil9862 commented 1 year ago

so the only way we can fix it making the freetype returned-string to be cleaned by removing all non-printable characters and whitespaces I wrote a block of code to clean these:

    def clean_string(self,s):
        return ''.join(filter(lambda x: x in string.printable, s)).strip()
        cleaned_font_version = self.clean_string(font_version)
        cleaned_version_string = self.clean_string(version_string)
        for char in cleaned_font_version:
            print(f"Character: {char}, ASCII Code: {ord(char)}")
        for char in cleaned_version_string:
            print(f"Character: {char}, ASCII Code: {ord(char)}")
sudipshil9862 commented 1 year ago

and the output is:

otfinfo_version:  Version 2.010
freetype_version:  Version 2.010
Character: V, ASCII Code: 86
Character: e, ASCII Code: 101
Character: r, ASCII Code: 114
Character: s, ASCII Code: 115
Character: i, ASCII Code: 105
Character: o, ASCII Code: 111
Character: n, ASCII Code: 110
Character:  , ASCII Code: 32
Character: 2, ASCII Code: 50
Character: ., ASCII Code: 46
Character: 0, ASCII Code: 48
Character: 1, ASCII Code: 49
Character: 0, ASCII Code: 48
Character: V, ASCII Code: 86
Character: e, ASCII Code: 101
Character: r, ASCII Code: 114
Character: s, ASCII Code: 115
Character: i, ASCII Code: 105
Character: o, ASCII Code: 111
Character: n, ASCII Code: 110
Character:  , ASCII Code: 32
Character: 2, ASCII Code: 50
Character: ., ASCII Code: 46
Character: 0, ASCII Code: 48
Character: 1, ASCII Code: 49
Character: 0, ASCII Code: 48
otfinfo_version EQUAL freetype_version
otfinfo_version:  Version 2.009
freetype_version:  Version 2.009
Character: V, ASCII Code: 86
Character: e, ASCII Code: 101
Character: r, ASCII Code: 114
Character: s, ASCII Code: 115
Character: i, ASCII Code: 105
Character: o, ASCII Code: 111
Character: n, ASCII Code: 110
Character:  , ASCII Code: 32
Character: 2, ASCII Code: 50
Character: ., ASCII Code: 46
Character: 0, ASCII Code: 48
Character: 0, ASCII Code: 48
Character: 9, ASCII Code: 57
Character: V, ASCII Code: 86
Character: e, ASCII Code: 101
Character: r, ASCII Code: 114
Character: s, ASCII Code: 115
Character: i, ASCII Code: 105
Character: o, ASCII Code: 111
Character: n, ASCII Code: 110
Character:  , ASCII Code: 32
Character: 2, ASCII Code: 50
Character: ., ASCII Code: 46
Character: 0, ASCII Code: 48
Character: 0, ASCII Code: 48
Character: 9, ASCII Code: 57
otfinfo_version EQUAL freetype_version
sudipshil9862 commented 1 year ago

FIXED

sudipshil9862 commented 1 year ago

these are the steps I followed to debug this issue.

sudipshil9862 commented 1 year ago

removing otfinfo dependencies and code blocks as I'm gonna use freetype instead of otfinfo to get the fontversion

sudipshil9862 commented 1 year ago

here is the whole code I used for debugging:

        otfinfo_command = ['otfinfo', '-i', font_file_path]
        result = subprocess.run(otfinfo_command, capture_output=True, text=True)
        version_line = [line for line in result.stdout.strip().split('\n') if 'Version:' in line]
        if len(version_line) == 0:
            print("Font version not found")
            return 'no fontversion found'
        font_version = version_line[0].split(':')[1].strip()
        print('otfinfo_version: ',font_version)
        #return font_version

        # Getting font version using Freetype
        face = freetype.Face(font_file_path)
        num_name_strings = face.sfnt_name_count
        version_string = None
        for index in range(num_name_strings):
            name_string = face.get_sfnt_name(index)
            if name_string.name_id == 5:  # Font version name ID
                version_string = name_string.string.decode('utf-8')
                version_string = version_string.strip()#jft
                break
        print('freetype_version: ',version_string)

        cleaned_font_version = self.clean_string(font_version)
        cleaned_version_string = self.clean_string(version_string)
        for char in cleaned_font_version:
            print(f"Character: {char}, ASCII Code: {ord(char)}")
        for char in cleaned_version_string:
            print(f"Character: {char}, ASCII Code: {ord(char)}")
        #return ''+str(version_string)
        if cleaned_font_version == cleaned_version_string:
            print('otfinfo_version EQUAL freetype_version')
        else:
            print('otfinfo_version NOT EQUAL freetype_version')
juhp commented 1 year ago

Can you report the problem to upstream https://github.com/rougier/freetype-py/issues please?