|
| 1 | +#!/bin/node |
| 2 | +/* |
| 3 | + Takes language files that have been written with unicode chars that Bangle.js cannot render |
| 4 | + with its built-in fonts, and pre-render them. |
| 5 | +*/ |
| 6 | + |
| 7 | +const FONT_SIZE = 18; |
| 8 | + |
| 9 | +var createCanvas; |
| 10 | +try { |
| 11 | + createCanvas = require("canvas").createCanvas; |
| 12 | +} catch(e) { |
| 13 | + console.log("ERROR: needc canvas library"); |
| 14 | + console.log("Try: npm install canvas"); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | +// we could use registerFont here to ensure a consitent experience? |
| 18 | + |
| 19 | +var imageconverter = require(__dirname+"/../webtools/imageconverter.js"); |
| 20 | + |
| 21 | +const canvas = createCanvas(200, 20) |
| 22 | +const ctx = canvas.getContext('2d') |
| 23 | + |
| 24 | +function renderText(txt) { |
| 25 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 26 | + ctx.font = FONT_SIZE+'px Sans'; |
| 27 | + ctx.fillStyle = "white"; |
| 28 | + ctx.fillText(txt, 0, FONT_SIZE); |
| 29 | + var str = imageconverter.canvastoString(canvas, { autoCrop:true, output:"raw", mode:"1bit", transparent:true } ); |
| 30 | + // for testing: |
| 31 | + //console.log("g.drawImage(",imageconverter.canvastoString(canvas, { autoCrop:true, output:"string", mode:"1bit" } ),");"); |
| 32 | + //process.exit(1); |
| 33 | + return "\0"+str; |
| 34 | +} |
| 35 | + |
| 36 | +function renderLangFile(file) { |
| 37 | + var fileIn = __dirname + "/../lang/unicode-based/"+file; |
| 38 | + var fileOut = __dirname + "/../lang/"+file; |
| 39 | + console.log("Reading",fileIn); |
| 40 | + var inJSON = JSON.parse(require("fs").readFileSync(fileIn)); |
| 41 | + var outJSON = { "// created with bin/language_render.js" : ""}; |
| 42 | + for (var categoryName in inJSON) { |
| 43 | + if (categoryName.includes("//")) continue; |
| 44 | + var category = inJSON[categoryName]; |
| 45 | + outJSON[categoryName] = {}; |
| 46 | + for (var english in category) { |
| 47 | + if (english.includes("//")) continue; |
| 48 | + var translated = category[english]; |
| 49 | + //console.log(english,"=>",translated); |
| 50 | + outJSON[categoryName][english] = renderText(translated); |
| 51 | + } |
| 52 | + } |
| 53 | + require("fs").writeFileSync(fileOut, JSON.stringify(outJSON,null,2)); |
| 54 | + console.log("Written",fileOut); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +renderLangFile("ja_JA.json"); |
0 commit comments