def analysis_font(font_url: str, mode=None) -> dict: ''' analysis font ''' if (not os.path.exists(base_font) or not os.path.exists(base_pkl)) and not mode: print('base file not exist!!!') return suffix = font_url.split('.')[-1] font = requests.get(font_url, headers=header, timeout=30) font_name = '%sfont.%s' % (data_dir, suffix) with codecs.open(font_name, 'wb') as f: f.write(font.content) font_map = TTFont(font_name).getBestCmap() ''' prepare base ''' if not mode is None: char_list = [ hex(ii).upper().replace('0X', '&#x') + ';' for ii in font_map.keys() ] base_unicode = [ int(mode[ii]) if ii in mode else '.' for ii in char_list ] pickle.dump(base_unicode, codecs.open(base_pkl, 'wb')) with codecs.open(base_font, 'wb') as f: f.write(font.content) return {} base_unicode = pickle.load(open(base_pkl, 'rb')) base_map = TTFont(base_font).getBestCmap() font_dict = { jj: base_unicode[ii] for ii, jj in enumerate(base_map.values()) } num_dict = { hex(ii).upper().replace('0X', '&#x') + ';': str(font_dict[jj]) for ii, jj in font_map.items() } return num_dict