Blame SOURCES/genfontconf.py

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