duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

update_glyph_ranges.py (2006B)


      1 #!/usr/bin/env python3
      2 
      3 import os
      4 import re
      5 import xml.etree.ElementTree as ET
      6 
      7 languages_to_update = [
      8     "ja",
      9     "ko",
     10     "zh-cn",
     11 ]
     12 
     13 src_path = os.path.join(os.path.dirname(__file__), "..", "qttranslations.cpp")
     14 ts_dir = os.path.join(os.path.dirname(__file__))
     15 
     16 def parse_xml(path):
     17     tree = ET.parse(path)
     18     root = tree.getroot()
     19     translations = ""
     20     for node in root.findall("context/message/translation"):
     21         if node.text:
     22             translations += node.text
     23 
     24     ords = list(set([ord(ch) for ch in translations if ord(ch) >= 0x2000]))
     25     if len(ords) == 0:
     26         return ""
     27 
     28     # Try to organize it into ranges
     29     ords.sort()
     30     ord_pairs = []
     31     start_ord = None
     32     last_ord = None
     33     for nord in ords:
     34         if start_ord is not None and nord == (last_ord + 1):
     35             last_ord = nord
     36             continue
     37         if start_ord is not None:
     38             ord_pairs.append(start_ord)
     39             ord_pairs.append(last_ord)
     40         start_ord = nord
     41         last_ord = nord
     42 
     43     if start_ord is not None:
     44         ord_pairs.append(start_ord)
     45         ord_pairs.append(last_ord)
     46 
     47     chars = "".join([chr(ch) for ch in ord_pairs])
     48     return chars
     49 
     50 def update_src_file(ts_file, chars):
     51     ts_name = os.path.basename(ts_file)
     52     pattern = re.compile('(// auto update.*' + ts_name + '.*\n[^"]+")[^"]*(".*)')
     53     with open(src_path, "r", encoding="utf-8") as f:
     54         original = f.read()
     55         update = pattern.sub("\\1" + chars + "\\2", original)
     56     if original != update:
     57         with open(src_path, "w", encoding="utf-8") as f:
     58             f.write(update)
     59         print(f"Updated character list for {ts_file}.")
     60     else:
     61         print(f"Character list is unchanged for {ts_file}.")
     62 
     63 if __name__ == "__main__":
     64     for language in languages_to_update:
     65         ts_file = os.path.join(ts_dir, f"duckstation-qt_{language}.ts")
     66         chars = parse_xml(ts_file)
     67         print(f"{language}: {len(chars)} character(s) detected.")
     68         update_src_file(ts_file, chars)