Skip to content

Commit

Permalink
Adds fonts summary
Browse files Browse the repository at this point in the history
  • Loading branch information
catusphan committed Nov 15, 2024
1 parent f12e33d commit e0767bb
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Font creator to help students **learn and read foreign languages faster** by app

# Font Samplers

- This archive contains additional font files used in the samplers: [PinyinFonts.zip](https://github.com/catusf/ruby-font-creator/releases/download/v1.0/PinyinFonts.zip)
This archive contains additional font files used in the samplers: [PinyinFonts.zip](https://github.com/catusf/ruby-font-creator/releases/download/v1.0/PinyinFonts.zip)

The base fonts they are created from are shown in [font_summary.md](font_summary.md).


### Pinyin-Tigris-Bottom-Handwritten
Expand Down
106 changes: 106 additions & 0 deletions font_summary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
[
{
"config": "catus.bottom.json",
"baseFontFilepath": "resources/fonts/NotoSansSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Catus Bottom Sans",
"baseFontName": "Noto Sans SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "catus.left.json",
"baseFontFilepath": "resources/fonts/NotoSansSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Catus Left Sans",
"baseFontName": "Noto Sans SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "catus.top.json",
"baseFontFilepath": "resources/fonts/NotoSansSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Catus Top Sans",
"baseFontName": "Noto Sans SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "default.json",
"baseFontFilepath": "resources/fonts/NotoSansSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Catus Top Sans",
"baseFontName": "Noto Sans SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "leo.bottom.json",
"baseFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Leo Bottom Serif",
"baseFontName": "LXGW WenKai Mono",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "leo.left.json",
"baseFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Leo Left Serif",
"baseFontName": "LXGW WenKai Mono",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "leo.top.json",
"baseFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Leo Top Serif",
"baseFontName": "LXGW WenKai Mono",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "onca.bottom.json",
"baseFontFilepath": "resources/fonts/NotoSerifSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Onca Bottom Serif",
"baseFontName": "Noto Serif SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "onca.left.json",
"baseFontFilepath": "resources/fonts/NotoSerifSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Onca Left Serif",
"baseFontName": "Noto Serif SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "onca.top.json",
"baseFontFilepath": "resources/fonts/NotoSerifSC-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Onca Top Serif",
"baseFontName": "Noto Serif SC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "tigris.bottom.json",
"baseFontFilepath": "resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Tigris Bottom Handwritten",
"baseFontName": "xiaolaiMonoSC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "tigris.left.json",
"baseFontFilepath": "resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Tigris Left Handwritten",
"baseFontName": "xiaolaiMonoSC",
"rubyFontName": "LXGW WenKai Mono"
},
{
"config": "tigris.top.json",
"baseFontFilepath": "resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf",
"rubyFontFilepath": "resources/fonts/LXGWWenKaiMono-Regular.ttf",
"fontName": "Pinyin Tigris Top Handwritten",
"baseFontName": "xiaolaiMonoSC",
"rubyFontName": "LXGW WenKai Mono"
}
]
17 changes: 17 additions & 0 deletions font_summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Font Summary

| Config File | Base Font Name | Ruby Font Name | Base Font Filepath | Ruby Font Filepath |
|-------------|----------------|----------------|--------------------|--------------------|
| catus.bottom.json | Noto Sans SC | LXGW WenKai Mono | resources/fonts/NotoSansSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| catus.left.json | Noto Sans SC | LXGW WenKai Mono | resources/fonts/NotoSansSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| catus.top.json | Noto Sans SC | LXGW WenKai Mono | resources/fonts/NotoSansSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| default.json | Noto Sans SC | LXGW WenKai Mono | resources/fonts/NotoSansSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| leo.bottom.json | LXGW WenKai Mono | LXGW WenKai Mono | resources/fonts/LXGWWenKaiMono-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| leo.left.json | LXGW WenKai Mono | LXGW WenKai Mono | resources/fonts/LXGWWenKaiMono-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| leo.top.json | LXGW WenKai Mono | LXGW WenKai Mono | resources/fonts/LXGWWenKaiMono-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| onca.bottom.json | Noto Serif SC | LXGW WenKai Mono | resources/fonts/NotoSerifSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| onca.left.json | Noto Serif SC | LXGW WenKai Mono | resources/fonts/NotoSerifSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| onca.top.json | Noto Serif SC | LXGW WenKai Mono | resources/fonts/NotoSerifSC-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| tigris.bottom.json | xiaolaiMonoSC | LXGW WenKai Mono | resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| tigris.left.json | xiaolaiMonoSC | LXGW WenKai Mono | resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
| tigris.top.json | xiaolaiMonoSC | LXGW WenKai Mono | resources/fonts/XiaolaiMonoSC-without-Hangul-Regular.ttf | resources/fonts/LXGWWenKaiMono-Regular.ttf |
124 changes: 124 additions & 0 deletions tools/summarize_fonts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import os
import json
from fontTools.ttLib import TTFont

# Path to your local 'src/config' folder
config_folder_path = 'src/config'
font_summary_file = 'font_summary.json'
markdown_file = 'font_summary.md'
prefix = "ruby-font-creator/"

def fix_file_path(path):
return path[pos + len(prefix):] if (pos := path.find(prefix)) >= 0 else path

# Function to extract relevant data from JSON files in the 'src/config' folder
def extract_font_config_from_folder(config_folder_path):
# Initialize an empty list to store summaries
summary_list = []

# List all files in the 'src/config' folder
json_files = [f for f in os.listdir(config_folder_path) if f.endswith('.json')]

for json_file in json_files:
summary = {
"config": json_file,
"baseFontFilepath": None,
"rubyFontFilepath": None,
"fontName": None,
"baseFontName": None,
"rubyFontName": None
}

file_path = os.path.join(config_folder_path, json_file)

# Open and read each JSON file
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()

# Parse JSON content
try:
config_data = json.loads(content)
except json.JSONDecodeError:
print(f"Error parsing JSON in {json_file}")
continue

# Extract relevant data based on expected keys
if 'baseFontFilepath' in config_data:
summary["baseFontFilepath"] = fix_file_path(config_data['baseFontFilepath'])
if 'rubyFontFilepath' in config_data:
summary["rubyFontFilepath"] = fix_file_path(config_data['rubyFontFilepath'])
if 'fontName' in config_data:
summary["fontName"] = config_data['fontName'].replace("-", " ")

# Extract font names from font files
extract_font_names(summary)

# Append the summary to the list
summary_list.append(summary)

# Save summary list to font_summary.json file
save_summary_to_json(summary_list)

return summary_list

# Function to extract the font name from a font file (e.g., .ttf or .otf)
def extract_font_names(summary):
# Extract font names from base and ruby font files
base_font_filepath = summary.get("baseFontFilepath")
ruby_font_filepath = summary.get("rubyFontFilepath")

# Extract font name from base font file if available
if base_font_filepath and os.path.isfile(base_font_filepath):
base_font_name = get_font_name_from_file(base_font_filepath)
summary["baseFontName"] = base_font_name

# Extract font name from ruby font file if available
if ruby_font_filepath and os.path.isfile(ruby_font_filepath):
ruby_font_name = get_font_name_from_file(ruby_font_filepath)
summary["rubyFontName"] = ruby_font_name

# Function to extract font name from font file using fontTools
def get_font_name_from_file(font_file):
try:
font = TTFont(font_file)
name_table = font['name']
return name_table.getBestFullName()

except Exception as e:
print(f"Error reading font file {font_file}: {e}")
return None

# Function to save summary to a JSON file
def save_summary_to_json(summary_list):
with open(font_summary_file, 'w', encoding='utf-8') as json_file:
json.dump(summary_list, json_file, ensure_ascii=False, indent=4)
print(f"Summary saved to {font_summary_file}")

# Function to convert font summary JSON into Markdown format
def convert_summary_to_markdown(summary_list):
markdown_content = "# Font Summary\n\n"

# Markdown table headers (changed order here)
markdown_content += "| Config File | Base Font Name | Ruby Font Name | Base Font Filepath | Ruby Font Filepath |\n"
markdown_content += "|-------------|----------------|----------------|--------------------|--------------------|\n"

# Add rows for each summary entry
for summary in summary_list:
markdown_content += f"| {summary['config']} | {summary.get('baseFontName', 'N/A')} | {summary.get('rubyFontName', 'N/A')} | {summary.get('baseFontFilepath', 'N/A')} | {summary.get('rubyFontFilepath', 'N/A')} |\n"

return markdown_content

# Function to save the markdown content to a .md file
def save_markdown_file(markdown_content):
with open(markdown_file, 'w', encoding='utf-8') as md_file:
md_file.write(markdown_content)
print(f"Markdown file saved to {markdown_file}")

# Example usage:
summary_list = extract_font_config_from_folder(config_folder_path)

# Convert the summary list to markdown format
markdown_content = convert_summary_to_markdown(summary_list)

# Save the markdown content to a .md file
save_markdown_file(markdown_content)

0 comments on commit e0767bb

Please sign in to comment.