Blame SOURCES/genfontconf.py

ea5be2
#!/usr/bin/python3
ea5be2
ea5be2
ea5be2
import argparse
ea5be2
ea5be2
ea5be2
'''
ea5be2
Generate the font config file for Noto CJK fonts
ea5be2
ea5be2
genfontconf.py "lang list" "common font name" "font name" "fallback font name" "prepend latin font" ...
ea5be2
ea5be2
like
ea5be2
ea5be2
genfontconf.py --fallback-font --prepend-latin-font "zh-cn:zh-sg" "monospace" "Source Han Sans CN" "Source Han Sans TW" "DejaVu Sans Mono" "zh-cn:zh-sg" "serif" "Source Han Sans CN" "Source Han Sans TW" "" "zh-cn:zh-sg" "sans-serif" "Source Han Sans CN" "Source Han Sans TW" ""
ea5be2
ea5be2
genfontconf.py "zh-cn:zh-sg" "monospace" "Noto Sans Mono CJK SC" "zh-cn:zh-sg" "serif" "Noto Serif CJK SC" "zh-cn:zh-sg" "sans-serif" "Noto Sans CJK SC"
ea5be2
ea5be2
The above information is in variable length array.
ea5be2
'''
ea5be2
ea5be2
'''
ea5be2
Some Noto CJK fonts may not need "fallback font name"
ea5be2
ea5be2
Skip the "fallback font name" argument if not needed.
ea5be2
'''
ea5be2
ea5be2
fallback_font_name = False
ea5be2
ea5be2
'''
ea5be2
Noto CJK may not need "prepend latin font".
ea5be2
ea5be2
Skip the "prepend latin font" argument if not needed.
ea5be2
'''
ea5be2
ea5be2
prepend_latin_font = False
ea5be2
ea5be2
ea5be2
class FontConfRecord:
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderRecord(langlist, common, font, fallback=None, latin=None):
ea5be2
        for lang in langlist.split(":"):
ea5be2
            FontConfRecord.renderMatch(lang, common, font, fallback, latin)
ea5be2
            print()
ea5be2
ea5be2
        FontConfRecord.renderAlias(font, common)
ea5be2
        print()
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderMatch(lang, common, font, fallback, latin):
ea5be2
        print('<match>')
ea5be2
        FontConfRecord.renderTestLang(lang)
ea5be2
        FontConfRecord.renderTestFamily(common)
ea5be2
        FontConfRecord.renderEditFamily(font, fallback)
ea5be2
        FontConfRecord.renderEditLatinFamily(latin)
ea5be2
        print('</match>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderTestLang(lang):
ea5be2
        print('<test name="lang">')
ea5be2
        print('<string>{0}</string>'.format(lang))
ea5be2
        print('</test>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderTestFamily(common):
ea5be2
        print('<test name="family">')
ea5be2
        print('<string>{0}</string>'.format(common))
ea5be2
        print('</test>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderEditFamily(font, fallback):
ea5be2
        print('<edit name="family" mode="prepend">')
ea5be2
        print('<string>{0}</string>'.format(font))
ea5be2
        if fallback:
ea5be2
            print('<string>{0}</string>'.format(fallback))
ea5be2
        print('</edit>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderEditLatinFamily(latin):
ea5be2
        if not latin:
ea5be2
            return
ea5be2
        print('<edit name="family" mode="prepend" binding="strong">')
ea5be2
        print('<string>{0}</string>'.format(latin))
ea5be2
        print('</edit>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderAlias(font, common):
ea5be2
        print('<alias>')
ea5be2
        print('<family>{0}</family>'.format(font))
ea5be2
        print('<default>')
ea5be2
        print('<family>{0}</family>'.format(common))
ea5be2
        print('</default>')
ea5be2
        print('</alias>')
ea5be2
ea5be2
ea5be2
class FontConfFile:
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderFile(strings):
ea5be2
        FontConfFile.renderHeader()
ea5be2
        FontConfFile.renderBody(strings)
ea5be2
        FontConfFile.renderFooter()
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderHeader():
ea5be2
        print('')
ea5be2
        print('')
ea5be2
        print('<fontconfig>')
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderBody(strings):
ea5be2
        num = 3
ea5be2
        if fallback_font_name:
ea5be2
            num += 1
ea5be2
        if prepend_latin_font:
ea5be2
            num += 1
ea5be2
ea5be2
        while len(strings):
ea5be2
            items = strings[0:num]
ea5be2
            strings = strings[num:]
ea5be2
ea5be2
            if num == 3:
ea5be2
                FontConfRecord.renderRecord(items[0], items[1], items[2])
ea5be2
ea5be2
            if num == 4:
ea5be2
                if fallback_font_name:
ea5be2
                    FontConfRecord.renderRecord \
ea5be2
                        (items[0], items[1], items[2], items[3])
ea5be2
                if prepend_latin_font:
ea5be2
                    FontConfRecord.renderRecord \
ea5be2
                        (items[0], items[1], items[2], None, items[3])
ea5be2
ea5be2
            if num == 5:
ea5be2
                FontConfRecord.renderRecord \
ea5be2
                    (items[0], items[1], items[2], items[3], items[4])
ea5be2
ea5be2
    @staticmethod
ea5be2
    def renderFooter():
ea5be2
        print('</fontconfig>')
ea5be2
ea5be2
ea5be2
if __name__ == "__main__":
ea5be2
    parser = argparse.ArgumentParser(description='Generate font config.')
ea5be2
    parser.add_argument('strings', metavar='string', type=str, nargs='+',
ea5be2
                        help='strings')
ea5be2
ea5be2
    parser.add_argument('--fallback-font', dest='fallback_font', action='store_true')
ea5be2
    parser.add_argument('--disable-fallback-font', dest='fallback_font', action='store_false')
ea5be2
    parser.set_defaults(fallback_font=False)
ea5be2
ea5be2
    parser.add_argument('--prepend-latin-font', dest='prepend_latin_font', action='store_true')
ea5be2
    parser.add_argument('--disable-prepend-latin-font', dest='prepend_latin_font', action='store_false')
ea5be2
    parser.set_defaults(prepend_latin_font=False)
ea5be2
ea5be2
    args = parser.parse_args()
ea5be2
    #print(args)
ea5be2
ea5be2
    fallback_font_name = args.fallback_font
ea5be2
    prepend_latin_font = args.prepend_latin_font
ea5be2
ea5be2
    FontConfFile.renderFile(args.strings)