Skip to content

Commit

Permalink
Adds code to save config data
Browse files Browse the repository at this point in the history
  • Loading branch information
catusphan committed Nov 14, 2024
1 parent 4095a4e commit 94287c7
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 5 deletions.
59 changes: 59 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@ import jsonfile from 'jsonfile'
import webfont from 'webfont'
import { argv } from 'yargs'








import helpers from './src/helpers'
import ruby from './src/ruby'
import svg from './src/svg'








function generateSvg(data, config) {
const baseEngine = ruby.loadFont(config.baseFontFilepath)
const rubyEngine = ruby.loadFont(config.rubyFontFilepath)
Expand Down Expand Up @@ -42,19 +56,64 @@ function buildFont(config) {
.catch(err => console.log(err))
}

// Function to extract value of --config
function getConfigValue(args) {
const configArg = args.find(arg => arg.startsWith('--config='));
if (configArg) {
// Extract value after the '=' sign
return configArg.split('=')[1];
}
return null; // Return null if --config is not found
}

// Modify the filename to use .json instead of .js, or return 'default.json' if no config found
function getConfigData(configValue) {
if (configValue) {
// Replace .js extension with .json
return configValue.replace('.js', '.json');
}
// Return 'default.json' if no config file is found
return 'default.json';
}


function start(cliArguments) {
let config = helpers.setBuildConfig(cliArguments)
config = helpers.setDataSource(config, cliArguments)
config = helpers.setFontName(config, cliArguments)
config = helpers.setBaseFontFilepath(config, cliArguments)
config = helpers.setRubyFontFilepath(config, cliArguments)

const args = process.argv;

const config_value = getConfigValue(args);
const config_data_file = getConfigData(config_value);

// Convert the object to a JSON string
const jsonData = JSON.stringify(config, null, 2);

const fs = require('fs');

// Write the JSON string to a file synchronously
try {
fs.writeFileSync(config_data_file, jsonData);
console.log('Data has been saved to data.json');
} catch (err) {
console.log('Error writing to file:', err);
}

// Log the entire arguments array
console.log(args);

jsonfile.readFile(config.dataSource, (err, data) => {
if (err) {
throw err
}

helpers.prepare(config)

return;

generateSvg(data, config)

buildFont(config).then(fontData =>
Expand Down
19 changes: 19 additions & 0 deletions list_glyphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from fontTools.ttLib import TTFont

def list_glyphs(font_path):
# Open the font file
font = TTFont(font_path)

# Get the 'glyf' table, which contains the glyphs
glyf_table = font['glyf']

# List all glyph names
glyph_names = glyf_table.keys()

# Print the glyph names
for glyph_name in glyph_names:
print(glyph_name)

if __name__ == "__main__":
font_path = "resources/fonts/Mengshen-Handwritten.ttf" # Replace with your font file path
list_glyphs(font_path)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"license": "MIT",
"scripts": {
"build": "node --max_old_space_size=8192 --optimize_for_size --stack_size=4096 --require babel-core/register ./index.js",
"build": "node --max_old_space_size=8192 --optimize_for_size --stack_size=4096 --require babel-core/register ./index.js --config=./src/config/catus.bottom.js --data=src/data-small-org.json",
"test": "ava test --fail-fast --verbose",
"precommit": "./node_modules/.bin/prettier-eslint --write '**/*.js'",
"prepush": "npm test"
Expand Down
Binary file removed resources/fonts/Noto-Sans-Mono-Top.ttf
Binary file not shown.
Binary file removed resources/fonts/Pinyin-Tigris-Sans_v1.0.ttf
Binary file not shown.
7 changes: 7 additions & 0 deletions resources/fonts/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
Should contains fonts to use (also need by travis).

Vietnamese font ranges
- C0 Controls and Basic Latin: U+0000-U+0000
- C1 Controls and Latin-1 Supplement: U+0080-U+00FF
- Latin Extended-A: U+0100-U+017F
- Latin Extended-B: U+0180-U+024F
- Combining Diacritical Marks: U+0300-U+036F
39 changes: 39 additions & 0 deletions src/config/catus.bottom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"canvas": {
"width": 80,
"height": 80
},
"baseFontFilepath": "C:\\Users\\ITFDL\\OneDrive - FPT Corporation\\Personal\\Playground\\ruby-font-creator\\resources\\fonts\\NotoSansSC-Regular.ttf",
"rubyFontFilepath": "C:\\Users\\ITFDL\\OneDrive - FPT Corporation\\Personal\\Playground\\ruby-font-creator\\resources\\fonts\\LXGWWenKaiMono-Regular.ttf",
"fontName": "Catus-Pinyin-Bottom-Sans",
"layout": {
"base": {
"x": 40,
"y": 64,
"fontSize": 56,
"anchor": "bottom center",
"attributes": {
"fill": "black",
"stroke": "black",
"id": "glyph"
}
},
"ruby": {
"x": 40,
"y": 86,
"fontSize": 28,
"anchor": "bottom center",
"attributes": {
"fill": "black",
"stroke": "black",
"id": "ruby"
}
}
},
"dataSource": "C:\\Users\\ITFDL\\OneDrive - FPT Corporation\\Personal\\Playground\\ruby-font-creator\\src\\data-small-org.json",
"formats": [
"ttf"
],
"inputFiles": "./build/**/*.svg",
"workingDir": "C:\\Users\\ITFDL\\OneDrive - FPT Corporation\\Personal\\Playground\\ruby-font-creator\\build\\svg"
}
12 changes: 9 additions & 3 deletions src/config/common.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import path from 'path'



import layout from '../layouts'




export default {
dataSource: path.resolve('./src/data.json'),
get destFilename() {
return path.resolve(`./output/${this.fontName}`)
},
// get destFilename() {
// return path.resolve(`./output/${this.fontName}`)
// },
formats: ['ttf'],
inputFiles: './build/**/*.svg',
workingDir: path.resolve('./build/svg')
Expand Down
2 changes: 2 additions & 0 deletions tools/do_merge.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyftmerge "../output/Tigris-Pinyin-Top-Handwritten.ttf" "../output/Pinyin-Test-Bottom-Small.ttf" --output-file=merged.ttf

20 changes: 19 additions & 1 deletion tools/font_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@
]


def get_upeem(font_path):
# Load the font file
font = TTFont(font_path)

# Check if the 'head' table exists (it should exist for most fonts)
if 'head' not in font:
raise ValueError("No 'head' table found in font.")

# Get the head table, which contains the UPEM value
head_table = font['head']

# The UPEM value is stored in the 'unitsPerEm' attribute of the 'head' table
upeem = head_table.unitsPerEm

return upeem

def get_unicode_ranges_with_names(font_path):
# Load the font file
font = TTFont(font_path)
Expand Down Expand Up @@ -539,7 +555,7 @@ def get_unicode_ranges_with_names(font_path):

for block_name, start, end, covered, percentage in block_ranges:
print(
f" {block_name}: U+{start:04X} - U+{end:04X}, Covered: {covered} ({percentage:.2f}%)"
f" U+{start:04X}-U+{end:04X} {block_name}\tCovered: {covered} ({percentage:.2f}%)"
)

# print(f"Number of U blocks: {len(UNICODE_BLOCKS)}")
Expand All @@ -551,6 +567,8 @@ def get_unicode_ranges_with_names(font_path):
f"Covered code points: {covered_code_points} ({(covered_code_points / total_code_points) * 100:.4f}%)"
)
print(f"Missing: {count_codepoints-covered_code_points}")
upeem = get_upeem(font_path)
print(f'Font UPEEM: {upeem}')


# font_path = "resources/Hanzi-Pinyin-Font.top (1).ttf" # Path to your font file
Expand Down
32 changes: 32 additions & 0 deletions tools/merge_fonts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import argparse
from fontTools.ttLib import TTFont
from fontTools.merge import Merger

def merge_fonts(output, input1, input2):
# Merge the fonts using file paths (not TTFont objects)
merger = Merger()
new_font = merger.merge([input1, input2])

# Save the merged font to the specified output path
new_font.save(output)

def main():
# Set up the argument parser
parser = argparse.ArgumentParser(description="Merge two font files into one.")

# Add arguments for input and output files
parser.add_argument('input1', metavar='input1', type=str,
help="Path to the first font file (input1).")
parser.add_argument('input2', metavar='input2', type=str,
help="Path to the second font file (input2).")
parser.add_argument('output', metavar='output', type=str,
help="Path to save the merged font file (output).")

# Parse the arguments
args = parser.parse_args()

# Call the merge_fonts function with the parsed arguments
merge_fonts(args.output, args.input1, args.input2)

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions tools/subset.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pyftsubset "C:\Users\ITFDL\OneDrive - FPT Corporation\Personal\Playground\ruby-font-creator\resources\fonts\NotoSerifSC-Regular.ttf" --unicodes="U+0000-007F" --unicodes="U+0080-00FF" --unicodes="U+0100-017F" --unicodes="U+0180-024F" --unicodes="U+0300-036F" --output-file=subset1.ttf

0 comments on commit 94287c7

Please sign in to comment.