diff --git a/index.js b/index.js index 9e2f779..f4d3e5a 100644 --- a/index.js +++ b/index.js @@ -2,15 +2,86 @@ 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' + + + + + + + + + + + + + + + + + +const fs = require('fs'); + + + + + + + + + + + + function generateSvg(data, config) { const baseEngine = ruby.loadFont(config.baseFontFilepath) const rubyEngine = ruby.loadFont(config.rubyFontFilepath) const build_folder = `${config.workingDir}` + // Define the codepoint you're searching for + const targetCodePoint = "U+1ED9"; + + // Check if the code point exists in the existingData array (by checking codepoint field) + const inExistingData = data.some(item => item.codepoint === targetCodePoint); + + if (inExistingData) { + console.log(`${targetCodePoint} exists in the existingData array.`); + } else { + console.log(`${targetCodePoint} does not exist in the existingData array.`); + } + // config.set_canvas(baseEngine.font.unitsPerEm, baseEngine.font.unitsPerEm) + // config.layout; const fs = require('fs') if (!fs.existsSync(build_folder)) { @@ -22,13 +93,26 @@ function generateSvg(data, config) { for (let datum = 0; datum < data.length; datum += 1) { const char = data[datum] - const svgContent = svg.wrap( - ruby.getBase(baseEngine, char.glyph, config.layout.base), - ruby.getRuby(rubyEngine, char.ruby, config.layout.ruby) - ) const unicode = char.codepoint.replace('U+', 'u') - svg.save(`${config.workingDir}/${unicode}-${char.glyph}.svg`, svgContent) + if (!char.ruby){ + const svgContent = svg.wrap( + ruby.getBase(baseEngine, char.glyph, config.layout.norm) + ) + svg.save(`${config.workingDir}/${unicode}-${char.glyph}.svg`, svgContent) + } + else { + const svgContent = svg.wrap( + ruby.getBase(baseEngine, char.glyph, config.layout.base), + ruby.getRuby(rubyEngine, char.ruby, config.layout.ruby) + ) + svg.save(`${config.workingDir}/${unicode}-${char.glyph}.svg`, svgContent) + } + + + + + // svg.save(`${config.workingDir}/${unicode}.svg`, svgContent) } } @@ -62,6 +146,32 @@ function getConfigData(configValue) { return 'default.json' } + +// Function to load the code points from the JSON file +function loadExtraCodePoints() { + const data = fs.readFileSync('src/added_codepoints.json', 'utf-8'); + return JSON.parse(data); +} +// Function to add code points to the existing data +function addCodePointsToExistingData(codePoints, existingData) { + codePoints.forEach(codePoint => { + // Convert the code point (e.g., "U+6211") to the actual character + const character = String.fromCodePoint(parseInt(codePoint.replace('U+', ''), 16)); // Remove 'U+' and convert to number + + // Add new object to the existing array + existingData.push({ + codepoint: codePoint, + ruby: "", // Ruby is set to an empty string + glyph: character // Glyph is the character corresponding to the code point + }); + }); + + // Write the updated array back to the file + fs.writeFileSync('existingData.json', JSON.stringify(existingData, null, 2)); + + console.log('Updated data written to existingData.json'); +} + function start(cliArguments) { let config = helpers.setBuildConfig(cliArguments) config = helpers.setDataSource(config, cliArguments) @@ -95,6 +205,11 @@ function start(cliArguments) { throw err } + // Example usage + const extra_data = loadExtraCodePoints(); + + addCodePointsToExistingData(extra_data, data); + helpers.prepare(config) // Use when only generate the config then quit @@ -108,4 +223,39 @@ function start(cliArguments) { }) } +/* +// Define the Unicode ranges in an array +const unicodeRanges = [ + { start: 0x0000, end: 0x007F }, // Basic Latin + { start: 0x0080, end: 0x00FF }, // Latin-1 Supplement + { start: 0x0100, end: 0x017F }, // Latin Extended-A + { start: 0x0180, end: 0x024F }, // Latin Extended-B + { start: 0x1E00, end: 0x1EFF }, // Latin Extended Additional + { start: 0x0300, end: 0x036F }, // Combining Diacritical Marks +]; + + +// Function to generate code points in the specified ranges +function generateUnicodeCodePoints(ranges) { + const codePoints = []; + + ranges.forEach(range => { + for (let i = range.start; i <= range.end; i++) { + codePoints.push(`U+${i.toString(16).toUpperCase().padStart(4, '0')}`); + } + }); + + return codePoints; +} + +// Generate the Unicode code points array +const codePoints = generateUnicodeCodePoints(unicodeRanges); + +// Write the code points to a JSON file +fs.writeFileSync('src/added_codepoints.json', JSON.stringify(codePoints, null, 2)); + +console.log('Unicode code points written to unicodeCodePoints.json'); + +*/ + start(argv) diff --git a/resources/fonts/README.md b/resources/fonts/README.md index 9884fce..045bb22 100644 --- a/resources/fonts/README.md +++ b/resources/fonts/README.md @@ -1,8 +1,9 @@ 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 + - Controls and Basic Latin: U+0000-U+007F + - Latin-1 Supplement Supplement: U+0080-U+00FF - Latin Extended-A: U+0100-U+017F - Latin Extended-B: U+0180-U+024F + - Latin Extended Additional: U+1E00-U+1EFF - Combining Diacritical Marks: U+0300-U+036F diff --git a/src/added_codepoints.json b/src/added_codepoints.json index 0637a08..e1db773 100644 --- a/src/added_codepoints.json +++ b/src/added_codepoints.json @@ -1 +1,922 @@ -[] \ No newline at end of file +[ + "U+0020", + "U+0021", + "U+0023", + "U+0024", + "U+0025", + "U+0026", + "U+0027", + "U+0028", + "U+0029", + "U+002B", + "U+002C", + "U+002D", + "U+002E", + "U+0030", + "U+0031", + "U+0032", + "U+0033", + "U+0034", + "U+0035", + "U+0036", + "U+0037", + "U+0038", + "U+0039", + "U+003A", + "U+003B", + "U+003D", + "U+0040", + "U+0041", + "U+0042", + "U+0043", + "U+0044", + "U+0045", + "U+0046", + "U+0047", + "U+0048", + "U+0049", + "U+004A", + "U+004B", + "U+004C", + "U+004D", + "U+004E", + "U+004F", + "U+0050", + "U+0051", + "U+0052", + "U+0053", + "U+0054", + "U+0055", + "U+0056", + "U+0057", + "U+0058", + "U+0059", + "U+005A", + "U+005B", + "U+005D", + "U+005E", + "U+005F", + "U+0060", + "U+0061", + "U+0062", + "U+0063", + "U+0064", + "U+0065", + "U+0066", + "U+0067", + "U+0068", + "U+0069", + "U+006A", + "U+006B", + "U+006C", + "U+006D", + "U+006E", + "U+006F", + "U+0070", + "U+0071", + "U+0072", + "U+0073", + "U+0074", + "U+0075", + "U+0076", + "U+0077", + "U+0078", + "U+0079", + "U+007A", + "U+007B", + "U+007D", + "U+007E", + "U+007F", + "U+0080", + "U+0081", + "U+0082", + "U+0083", + "U+0084", + "U+0085", + "U+0086", + "U+0087", + "U+0088", + "U+0089", + "U+008A", + "U+008B", + "U+008C", + "U+008D", + "U+008E", + "U+008F", + "U+0090", + "U+0091", + "U+0092", + "U+0093", + "U+0094", + "U+0095", + "U+0096", + "U+0097", + "U+0098", + "U+0099", + "U+009A", + "U+009B", + "U+009C", + "U+009D", + "U+009E", + "U+009F", + "U+00A0", + "U+00A1", + "U+00A2", + "U+00A3", + "U+00A4", + "U+00A5", + "U+00A6", + "U+00A7", + "U+00A8", + "U+00A9", + "U+00AA", + "U+00AB", + "U+00AC", + "U+00AD", + "U+00AE", + "U+00AF", + "U+00B0", + "U+00B1", + "U+00B2", + "U+00B3", + "U+00B4", + "U+00B5", + "U+00B6", + "U+00B7", + "U+00B8", + "U+00B9", + "U+00BA", + "U+00BB", + "U+00BC", + "U+00BD", + "U+00BE", + "U+00BF", + "U+00C0", + "U+00C1", + "U+00C2", + "U+00C3", + "U+00C4", + "U+00C5", + "U+00C6", + "U+00C7", + "U+00C8", + "U+00C9", + "U+00CA", + "U+00CB", + "U+00CC", + "U+00CD", + "U+00CE", + "U+00CF", + "U+00D0", + "U+00D1", + "U+00D2", + "U+00D3", + "U+00D4", + "U+00D5", + "U+00D6", + "U+00D7", + "U+00D8", + "U+00D9", + "U+00DA", + "U+00DB", + "U+00DC", + "U+00DD", + "U+00DE", + "U+00DF", + "U+00E0", + "U+00E1", + "U+00E2", + "U+00E3", + "U+00E4", + "U+00E5", + "U+00E6", + "U+00E7", + "U+00E8", + "U+00E9", + "U+00EA", + "U+00EB", + "U+00EC", + "U+00ED", + "U+00EE", + "U+00EF", + "U+00F0", + "U+00F1", + "U+00F2", + "U+00F3", + "U+00F4", + "U+00F5", + "U+00F6", + "U+00F7", + "U+00F8", + "U+00F9", + "U+00FA", + "U+00FB", + "U+00FC", + "U+00FD", + "U+00FE", + "U+00FF", + "U+0100", + "U+0101", + "U+0102", + "U+0103", + "U+0104", + "U+0105", + "U+0106", + "U+0107", + "U+0108", + "U+0109", + "U+010A", + "U+010B", + "U+010C", + "U+010D", + "U+010E", + "U+010F", + "U+0110", + "U+0111", + "U+0112", + "U+0113", + "U+0114", + "U+0115", + "U+0116", + "U+0117", + "U+0118", + "U+0119", + "U+011A", + "U+011B", + "U+011C", + "U+011D", + "U+011E", + "U+011F", + "U+0120", + "U+0121", + "U+0122", + "U+0123", + "U+0124", + "U+0125", + "U+0126", + "U+0127", + "U+0128", + "U+0129", + "U+012A", + "U+012B", + "U+012C", + "U+012D", + "U+012E", + "U+012F", + "U+0130", + "U+0131", + "U+0132", + "U+0133", + "U+0134", + "U+0135", + "U+0136", + "U+0137", + "U+0138", + "U+0139", + "U+013A", + "U+013B", + "U+013C", + "U+013D", + "U+013E", + "U+013F", + "U+0140", + "U+0141", + "U+0142", + "U+0143", + "U+0144", + "U+0145", + "U+0146", + "U+0147", + "U+0148", + "U+0149", + "U+014A", + "U+014B", + "U+014C", + "U+014D", + "U+014E", + "U+014F", + "U+0150", + "U+0151", + "U+0152", + "U+0153", + "U+0154", + "U+0155", + "U+0156", + "U+0157", + "U+0158", + "U+0159", + "U+015A", + "U+015B", + "U+015C", + "U+015D", + "U+015E", + "U+015F", + "U+0160", + "U+0161", + "U+0162", + "U+0163", + "U+0164", + "U+0165", + "U+0166", + "U+0167", + "U+0168", + "U+0169", + "U+016A", + "U+016B", + "U+016C", + "U+016D", + "U+016E", + "U+016F", + "U+0170", + "U+0171", + "U+0172", + "U+0173", + "U+0174", + "U+0175", + "U+0176", + "U+0177", + "U+0178", + "U+0179", + "U+017A", + "U+017B", + "U+017C", + "U+017D", + "U+017E", + "U+017F", + "U+0180", + "U+0181", + "U+0182", + "U+0183", + "U+0184", + "U+0185", + "U+0186", + "U+0187", + "U+0188", + "U+0189", + "U+018A", + "U+018B", + "U+018C", + "U+018D", + "U+018E", + "U+018F", + "U+0190", + "U+0191", + "U+0192", + "U+0193", + "U+0194", + "U+0195", + "U+0196", + "U+0197", + "U+0198", + "U+0199", + "U+019A", + "U+019B", + "U+019C", + "U+019D", + "U+019E", + "U+019F", + "U+01A0", + "U+01A1", + "U+01A2", + "U+01A3", + "U+01A4", + "U+01A5", + "U+01A6", + "U+01A7", + "U+01A8", + "U+01A9", + "U+01AA", + "U+01AB", + "U+01AC", + "U+01AD", + "U+01AE", + "U+01AF", + "U+01B0", + "U+01B1", + "U+01B2", + "U+01B3", + "U+01B4", + "U+01B5", + "U+01B6", + "U+01B7", + "U+01B8", + "U+01B9", + "U+01BA", + "U+01BB", + "U+01BC", + "U+01BD", + "U+01BE", + "U+01BF", + "U+01C0", + "U+01C1", + "U+01C2", + "U+01C3", + "U+01C4", + "U+01C5", + "U+01C6", + "U+01C7", + "U+01C8", + "U+01C9", + "U+01CA", + "U+01CB", + "U+01CC", + "U+01CD", + "U+01CE", + "U+01CF", + "U+01D0", + "U+01D1", + "U+01D2", + "U+01D3", + "U+01D4", + "U+01D5", + "U+01D6", + "U+01D7", + "U+01D8", + "U+01D9", + "U+01DA", + "U+01DB", + "U+01DC", + "U+01DD", + "U+01DE", + "U+01DF", + "U+01E0", + "U+01E1", + "U+01E2", + "U+01E3", + "U+01E4", + "U+01E5", + "U+01E6", + "U+01E7", + "U+01E8", + "U+01E9", + "U+01EA", + "U+01EB", + "U+01EC", + "U+01ED", + "U+01EE", + "U+01EF", + "U+01F0", + "U+01F1", + "U+01F2", + "U+01F3", + "U+01F4", + "U+01F5", + "U+01F6", + "U+01F7", + "U+01F8", + "U+01F9", + "U+01FA", + "U+01FB", + "U+01FC", + "U+01FD", + "U+01FE", + "U+01FF", + "U+0200", + "U+0201", + "U+0202", + "U+0203", + "U+0204", + "U+0205", + "U+0206", + "U+0207", + "U+0208", + "U+0209", + "U+020A", + "U+020B", + "U+020C", + "U+020D", + "U+020E", + "U+020F", + "U+0210", + "U+0211", + "U+0212", + "U+0213", + "U+0214", + "U+0215", + "U+0216", + "U+0217", + "U+0218", + "U+0219", + "U+021A", + "U+021B", + "U+021C", + "U+021D", + "U+021E", + "U+021F", + "U+0220", + "U+0221", + "U+0222", + "U+0223", + "U+0224", + "U+0225", + "U+0226", + "U+0227", + "U+0228", + "U+0229", + "U+022A", + "U+022B", + "U+022C", + "U+022D", + "U+022E", + "U+022F", + "U+0230", + "U+0231", + "U+0232", + "U+0233", + "U+0234", + "U+0235", + "U+0236", + "U+0237", + "U+0238", + "U+0239", + "U+023A", + "U+023B", + "U+023C", + "U+023D", + "U+023E", + "U+023F", + "U+0240", + "U+0241", + "U+0242", + "U+0243", + "U+0244", + "U+0245", + "U+0246", + "U+0247", + "U+0248", + "U+0249", + "U+024A", + "U+024B", + "U+024C", + "U+024D", + "U+024E", + "U+024F", + "U+1E00", + "U+1E01", + "U+1E02", + "U+1E03", + "U+1E04", + "U+1E05", + "U+1E06", + "U+1E07", + "U+1E08", + "U+1E09", + "U+1E0A", + "U+1E0B", + "U+1E0C", + "U+1E0D", + "U+1E0E", + "U+1E0F", + "U+1E10", + "U+1E11", + "U+1E12", + "U+1E13", + "U+1E14", + "U+1E15", + "U+1E16", + "U+1E17", + "U+1E18", + "U+1E19", + "U+1E1A", + "U+1E1B", + "U+1E1C", + "U+1E1D", + "U+1E1E", + "U+1E1F", + "U+1E20", + "U+1E21", + "U+1E22", + "U+1E23", + "U+1E24", + "U+1E25", + "U+1E26", + "U+1E27", + "U+1E28", + "U+1E29", + "U+1E2A", + "U+1E2B", + "U+1E2C", + "U+1E2D", + "U+1E2E", + "U+1E2F", + "U+1E30", + "U+1E31", + "U+1E32", + "U+1E33", + "U+1E34", + "U+1E35", + "U+1E36", + "U+1E37", + "U+1E38", + "U+1E39", + "U+1E3A", + "U+1E3B", + "U+1E3C", + "U+1E3D", + "U+1E3E", + "U+1E3F", + "U+1E40", + "U+1E41", + "U+1E42", + "U+1E43", + "U+1E44", + "U+1E45", + "U+1E46", + "U+1E47", + "U+1E48", + "U+1E49", + "U+1E4A", + "U+1E4B", + "U+1E4C", + "U+1E4D", + "U+1E4E", + "U+1E4F", + "U+1E50", + "U+1E51", + "U+1E52", + "U+1E53", + "U+1E54", + "U+1E55", + "U+1E56", + "U+1E57", + "U+1E58", + "U+1E59", + "U+1E5A", + "U+1E5B", + "U+1E5C", + "U+1E5D", + "U+1E5E", + "U+1E5F", + "U+1E60", + "U+1E61", + "U+1E62", + "U+1E63", + "U+1E64", + "U+1E65", + "U+1E66", + "U+1E67", + "U+1E68", + "U+1E69", + "U+1E6A", + "U+1E6B", + "U+1E6C", + "U+1E6D", + "U+1E6E", + "U+1E6F", + "U+1E70", + "U+1E71", + "U+1E72", + "U+1E73", + "U+1E74", + "U+1E75", + "U+1E76", + "U+1E77", + "U+1E78", + "U+1E79", + "U+1E7A", + "U+1E7B", + "U+1E7C", + "U+1E7D", + "U+1E7E", + "U+1E7F", + "U+1E80", + "U+1E81", + "U+1E82", + "U+1E83", + "U+1E84", + "U+1E85", + "U+1E86", + "U+1E87", + "U+1E88", + "U+1E89", + "U+1E8A", + "U+1E8B", + "U+1E8C", + "U+1E8D", + "U+1E8E", + "U+1E8F", + "U+1E90", + "U+1E91", + "U+1E92", + "U+1E93", + "U+1E94", + "U+1E95", + "U+1E96", + "U+1E97", + "U+1E98", + "U+1E99", + "U+1E9A", + "U+1E9B", + "U+1E9C", + "U+1E9D", + "U+1E9E", + "U+1E9F", + "U+1EA0", + "U+1EA1", + "U+1EA2", + "U+1EA3", + "U+1EA4", + "U+1EA5", + "U+1EA6", + "U+1EA7", + "U+1EA8", + "U+1EA9", + "U+1EAA", + "U+1EAB", + "U+1EAC", + "U+1EAD", + "U+1EAE", + "U+1EAF", + "U+1EB0", + "U+1EB1", + "U+1EB2", + "U+1EB3", + "U+1EB4", + "U+1EB5", + "U+1EB6", + "U+1EB7", + "U+1EB8", + "U+1EB9", + "U+1EBA", + "U+1EBB", + "U+1EBC", + "U+1EBD", + "U+1EBE", + "U+1EBF", + "U+1EC0", + "U+1EC1", + "U+1EC2", + "U+1EC3", + "U+1EC4", + "U+1EC5", + "U+1EC6", + "U+1EC7", + "U+1EC8", + "U+1EC9", + "U+1ECA", + "U+1ECB", + "U+1ECC", + "U+1ECD", + "U+1ECE", + "U+1ECF", + "U+1ED0", + "U+1ED1", + "U+1ED2", + "U+1ED3", + "U+1ED4", + "U+1ED5", + "U+1ED6", + "U+1ED7", + "U+1ED8", + "U+1ED9", + "U+1EDA", + "U+1EDB", + "U+1EDC", + "U+1EDD", + "U+1EDE", + "U+1EDF", + "U+1EE0", + "U+1EE1", + "U+1EE2", + "U+1EE3", + "U+1EE4", + "U+1EE5", + "U+1EE6", + "U+1EE7", + "U+1EE8", + "U+1EE9", + "U+1EEA", + "U+1EEB", + "U+1EEC", + "U+1EED", + "U+1EEE", + "U+1EEF", + "U+1EF0", + "U+1EF1", + "U+1EF2", + "U+1EF3", + "U+1EF4", + "U+1EF5", + "U+1EF6", + "U+1EF7", + "U+1EF8", + "U+1EF9", + "U+1EFA", + "U+1EFB", + "U+1EFC", + "U+1EFD", + "U+1EFE", + "U+1EFF", + "U+0300", + "U+0301", + "U+0302", + "U+0303", + "U+0304", + "U+0305", + "U+0306", + "U+0307", + "U+0308", + "U+0309", + "U+030A", + "U+030B", + "U+030C", + "U+030D", + "U+030E", + "U+030F", + "U+0310", + "U+0311", + "U+0312", + "U+0313", + "U+0314", + "U+0315", + "U+0316", + "U+0317", + "U+0318", + "U+0319", + "U+031A", + "U+031B", + "U+031C", + "U+031D", + "U+031E", + "U+031F", + "U+0320", + "U+0321", + "U+0322", + "U+0323", + "U+0324", + "U+0325", + "U+0326", + "U+0327", + "U+0328", + "U+0329", + "U+032A", + "U+032B", + "U+032C", + "U+032D", + "U+032E", + "U+032F", + "U+0330", + "U+0331", + "U+0332", + "U+0333", + "U+0334", + "U+0335", + "U+0336", + "U+0337", + "U+0338", + "U+0339", + "U+033A", + "U+033B", + "U+033C", + "U+033D", + "U+033E", + "U+033F", + "U+0340", + "U+0341", + "U+0342", + "U+0343", + "U+0344", + "U+0345", + "U+0346", + "U+0347", + "U+0348", + "U+0349", + "U+034A", + "U+034B", + "U+034C", + "U+034D", + "U+034E", + "U+034F", + "U+0350", + "U+0351", + "U+0352", + "U+0353", + "U+0354", + "U+0355", + "U+0356", + "U+0357", + "U+0358", + "U+0359", + "U+035A", + "U+035B", + "U+035C", + "U+035D", + "U+035E", + "U+035F", + "U+0360", + "U+0361", + "U+0362", + "U+0363", + "U+0364", + "U+0365", + "U+0366", + "U+0367", + "U+0368", + "U+0369", + "U+036A", + "U+036B", + "U+036C", + "U+036D", + "U+036E", + "U+036F" +] \ No newline at end of file diff --git a/src/config/catus.bottom.js b/src/config/catus.bottom.js index b6a5bfc..25fb589 100644 --- a/src/config/catus.bottom.js +++ b/src/config/catus.bottom.js @@ -1,6 +1,24 @@ import path from 'path' + + + + + + + + + import layout from '../layouts' + + + + + + + + + export default { canvas: { width: 80, height: 80 }, baseFontFilepath: path.resolve('./resources/fonts/NotoSansSC-Regular.ttf'), @@ -11,9 +29,17 @@ export default { get layout() { return { base: this.baseLayout(this.canvas), - ruby: this.rubyLayout(this.canvas) + ruby: this.rubyLayout(this.canvas), + norm: this.normLayout(this.canvas) } }, + normLayout: options => ({ + x: options.width / 2, + y: options.height, + fontSize: 80, + anchor: 'bottom center', + attributes: { fill: 'black', stroke: 'black', id: 'ruby' } + }), rubyLayout: options => ({ x: options.width / 2, y: options.height + 6, diff --git a/tools/add_missing_glyphs.py b/tools/add_missing_glyphs.py new file mode 100644 index 0000000..00d847e --- /dev/null +++ b/tools/add_missing_glyphs.py @@ -0,0 +1,108 @@ +import json +import os +import subprocess + +OUTPUT_DIR = 'output' + +# Function to load JSON data +def load_json(json_filepath): + with open(json_filepath, 'r', encoding='utf-8') as f: + return json.load(f) + + +# Function to run pyftsubset command +def run_pyftsubset(base_font_filepath, output_directory): + # Prepare the pyftsubset command + _, filepath = os.path.split(base_font_filepath) + subset_font = filepath.replace(".ttf", "-subset.ttf") + subset_font_path = os.path.join(output_directory, subset_font) + command = [ + 'pyftsubset', base_font_filepath.replace("/", "\\"), + '--unicodes="U+0000-007F"', + '--unicodes="U+0080-00FF"', + '--unicodes="U+0100-017F"', + '--unicodes="U+0180-024F"', + '--unicodes="U+0300-036F"', + f'--output-file={subset_font_path}', + ] + + output = subprocess.run(command, check=False) + + return subset_font_path + +# Function to run pyftmerge command +def run_pyftmerge(infont_name1, infont_name2, output_font): + # Prepare the pyftmerge command + command = [ + 'pyftmerge', + infont_name1, + infont_name2, + f'--output-file={output_font}', + ] + + subprocess.run(command, check=True) + +BASE_FOLDER = "ruby-font-creator" + +# Function to find the ruby-font-creator directory path from baseFontFilepath +def make_relative(base_font_filepath): + + pos = base_font_filepath.find(BASE_FOLDER) + + if pos < 0: + raise FileNotFoundError(f"ruby-font-creator path not found: {base_font_filepath}") + # return None + + return base_font_filepath[pos + len(BASE_FOLDER) + 1:] + + # # Get the directory of the base font + # font_dir = os.path.dirname(base_font_filepath) + + # # Find the ruby-font-creator directory (assuming it's a sibling directory) + # ruby_font_creator_dir = os.path.abspath(os.path.join(font_dir, '../../ruby-font-creator/resources/fonts')) + + # # Check if the directory exists + # if not os.path.exists(ruby_font_creator_dir): + # raise FileNotFoundError(f"Ruby font creator path not found: {ruby_font_creator_dir}") + + # return ruby_font_creator_dir + +# Main function to load the json, check for subset and run necessary commands +def process_font(json_filepath, output_directory): + # Load the JSON file + json_data = load_json(json_filepath) + + # Get the base font filepath and font name from the JSON data + base_font_filepath = json_data.get('baseFontFilepath') + font_name = json_data.get('fontName') + + generated_font_path = os.path.join(OUTPUT_DIR, font_name+'.ttf') + + if not base_font_filepath or not font_name: + print("Base font file path or font name not found in JSON.") + return + + # Get the ruby-font-creator directory based on baseFontFilepath + try: + base_font_rel_path = make_relative(base_font_filepath) + # print(f"Found ruby-font-creator path: {base_font_rel_path}") + except FileNotFoundError as e: + print(e) + return + + # Update the baseFontFilepath if necessary (to point to the correct location) + # base_font_filepath = os.path.join(ruby_font_creator_path, os.path.basename(base_font_filepath)) + + # Check if the subset font already exists + subset_font_path = run_pyftsubset(base_font_rel_path, output_directory) + + # Run pyftmerge + print("Running pyftmerge...") + run_pyftmerge(subset_font_path, generated_font_path, generated_font_path.replace(".ttf", "-final.ttf")) + +# Example usage +if __name__ == "__main__": + json_filepath = './src/config/catus.bottom.json' # Path to your JSON file + output_directory = 'output' # Output directory + process_font(json_filepath, output_directory) +