feat: add Chinese 12x12 bitmap font (21075 glyphs) and fix boot gate
- Add ChineseFont12x12.h: U+4E00-U+9FFF CJK coverage, 535KB flash - Add gen_chinese_font.mjs: @napi-rs/canvas based font generator tool - Enable CJK rendering in MessageRenderer and CannedMessageModule - Remove boot confirmation gate (required 2s button hold, caused shutdown loop) - Update partition table: app 2.75MB, OTA 192KB, spiffs 1MB - Update CHANGELOG
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
nvs, data, nvs, 0x009000, 0x005000,
|
||||
otadata, data, ota, 0x00e000, 0x002000,
|
||||
app, app, ota_0, 0x010000, 0x250000,
|
||||
flashApp, app, ota_1, 0x260000, 0x0A0000,
|
||||
app, app, ota_0, 0x010000, 0x2C0000,
|
||||
flashApp, app, ota_1, 0x2D0000, 0x030000,
|
||||
spiffs, data, spiffs, 0x300000, 0x100000,
|
||||
|
@@ -30,6 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "configuration.h"
|
||||
#include "gps/RTC.h"
|
||||
#include "graphics/ScreenFonts.h"
|
||||
#include "graphics/fonts/ChineseFont12x12.h"
|
||||
#include "graphics/SharedUIDisplay.h"
|
||||
#include "graphics/emotes.h"
|
||||
#include "main.h"
|
||||
@@ -61,6 +62,46 @@ static size_t cachedKey = 0;
|
||||
static std::vector<std::string> cachedLines;
|
||||
static std::vector<int> cachedHeights;
|
||||
|
||||
// Helper: measure mixed ASCII+CJK string width
|
||||
static int measureMixedWidth(OLEDDisplay *display, const std::string &s)
|
||||
{
|
||||
int w = 0;
|
||||
for (size_t i = 0; i < s.length();) {
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(s.c_str() + i, &utf8len);
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
w += CFONT_W;
|
||||
} else {
|
||||
std::string oneChar = s.substr(i, utf8len);
|
||||
w += display->getStringWidth(oneChar.c_str());
|
||||
}
|
||||
i += utf8len;
|
||||
}
|
||||
return w;
|
||||
}
|
||||
|
||||
// Helper: render a mixed ASCII+CJK string at (x, y), return total width
|
||||
static int drawMixedString(OLEDDisplay *display, int x, int y, const std::string &s, bool inBold)
|
||||
{
|
||||
int cursorX = x;
|
||||
for (size_t i = 0; i < s.length();) {
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(s.c_str() + i, &utf8len);
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
cursorX += cfont12_draw(display, cursorX, y, cp);
|
||||
} else {
|
||||
std::string oneChar = s.substr(i, utf8len);
|
||||
if (inBold) {
|
||||
display->drawString(cursorX + 1, y, oneChar.c_str());
|
||||
}
|
||||
display->drawString(cursorX, y, oneChar.c_str());
|
||||
cursorX += display->getStringWidth(oneChar.c_str());
|
||||
}
|
||||
i += utf8len;
|
||||
}
|
||||
return cursorX - x;
|
||||
}
|
||||
|
||||
void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string &line, const Emote *emotes, int emoteCount)
|
||||
{
|
||||
int cursorX = x;
|
||||
@@ -132,16 +173,7 @@ void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string
|
||||
|
||||
if (nextControl > i) {
|
||||
std::string textChunk = line.substr(i, nextControl - i);
|
||||
if (inBold) {
|
||||
// Faux bold: draw twice, offset by 1px
|
||||
display->drawString(cursorX + 1, fontY, textChunk.c_str());
|
||||
}
|
||||
display->drawString(cursorX, fontY, textChunk.c_str());
|
||||
#if defined(OLED_UA) || defined(OLED_RU)
|
||||
cursorX += display->getStringWidth(textChunk.c_str(), textChunk.length(), true);
|
||||
#else
|
||||
cursorX += display->getStringWidth(textChunk.c_str());
|
||||
#endif
|
||||
cursorX += drawMixedString(display, cursorX, fontY, textChunk, inBold);
|
||||
i = nextControl;
|
||||
continue;
|
||||
}
|
||||
@@ -155,16 +187,7 @@ void drawStringWithEmotes(OLEDDisplay *display, int x, int y, const std::string
|
||||
} else {
|
||||
// No more emotes — render the rest of the line
|
||||
std::string remaining = line.substr(i);
|
||||
if (inBold) {
|
||||
display->drawString(cursorX + 1, fontY, remaining.c_str());
|
||||
}
|
||||
display->drawString(cursorX, fontY, remaining.c_str());
|
||||
#if defined(OLED_UA) || defined(OLED_RU)
|
||||
cursorX += display->getStringWidth(remaining.c_str(), remaining.length(), true);
|
||||
#else
|
||||
cursorX += display->getStringWidth(remaining.c_str());
|
||||
#endif
|
||||
|
||||
cursorX += drawMixedString(display, cursorX, fontY, remaining, inBold);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -433,41 +456,69 @@ std::vector<std::string> generateLines(OLEDDisplay *display, const char *headerS
|
||||
lines.push_back(std::string(headerStr)); // Header line is always first
|
||||
|
||||
std::string line, word;
|
||||
for (int i = 0; messageBuf[i]; ++i) {
|
||||
char ch = messageBuf[i];
|
||||
int i = 0;
|
||||
while (messageBuf[i]) {
|
||||
// Check for smart quote U+2019 (E2 80 99)
|
||||
if ((unsigned char)messageBuf[i] == 0xE2 && (unsigned char)messageBuf[i + 1] == 0x80 &&
|
||||
(unsigned char)messageBuf[i + 2] == 0x99) {
|
||||
ch = '\''; // plain apostrophe
|
||||
i += 2; // skip over the extra UTF-8 bytes
|
||||
}
|
||||
if (ch == '\n') {
|
||||
word += '\'';
|
||||
i += 3;
|
||||
} else if ((unsigned char)messageBuf[i] == '\n') {
|
||||
if (!word.empty())
|
||||
line += word;
|
||||
if (!line.empty())
|
||||
lines.push_back(line);
|
||||
line.clear();
|
||||
word.clear();
|
||||
} else if (ch == ' ') {
|
||||
i++;
|
||||
} else if ((unsigned char)messageBuf[i] == ' ') {
|
||||
line += word + ' ';
|
||||
word.clear();
|
||||
i++;
|
||||
} else {
|
||||
word += ch;
|
||||
std::string test = line + word;
|
||||
// Keep these lines for diagnostics
|
||||
// LOG_INFO("Char: '%c' (0x%02X)", ch, (unsigned char)ch);
|
||||
// LOG_INFO("Current String: %s", test.c_str());
|
||||
// Note: there are boolean comparison uint16 (getStringWidth) with int (textWidth), hope textWidth is always positive :)
|
||||
#if defined(OLED_UA) || defined(OLED_RU)
|
||||
uint16_t strWidth = display->getStringWidth(test.c_str(), test.length(), true);
|
||||
#else
|
||||
uint16_t strWidth = display->getStringWidth(test.c_str());
|
||||
#endif
|
||||
if (strWidth > textWidth) {
|
||||
if (!line.empty())
|
||||
// Decode UTF-8 character
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(messageBuf + i, &utf8len);
|
||||
std::string chStr(messageBuf + i, utf8len);
|
||||
i += utf8len;
|
||||
|
||||
bool isCJK = (cp >= 0x80 && cfont12_find(cp));
|
||||
|
||||
// CJK characters are treated as individual "words" (break before/after them)
|
||||
if (isCJK && !word.empty()) {
|
||||
// Flush current ASCII word first
|
||||
std::string test = line + word;
|
||||
if (measureMixedWidth(display, test) > textWidth && !line.empty()) {
|
||||
lines.push_back(line);
|
||||
line = word;
|
||||
line = word;
|
||||
} else {
|
||||
line = test;
|
||||
}
|
||||
word.clear();
|
||||
}
|
||||
|
||||
word += chStr;
|
||||
|
||||
if (isCJK) {
|
||||
// CJK char is its own word — immediately check if it fits
|
||||
std::string test = line + word;
|
||||
if (measureMixedWidth(display, test) > textWidth && !line.empty()) {
|
||||
lines.push_back(line);
|
||||
line = word;
|
||||
} else {
|
||||
line = test;
|
||||
}
|
||||
word.clear();
|
||||
} else {
|
||||
// ASCII char — accumulate in word, check width
|
||||
std::string test = line + word;
|
||||
if (measureMixedWidth(display, test) > textWidth) {
|
||||
if (!line.empty())
|
||||
lines.push_back(line);
|
||||
line = word;
|
||||
word.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,7 +537,9 @@ std::vector<int> calculateLineHeights(const std::vector<std::string> &lines, con
|
||||
for (const auto &_line : lines) {
|
||||
int lineHeight = FONT_HEIGHT_SMALL;
|
||||
bool hasEmote = false;
|
||||
bool hasCJK = false;
|
||||
|
||||
// Check for emotes
|
||||
for (int i = 0; i < numEmotes; ++i) {
|
||||
const Emote &e = emotes[i];
|
||||
if (_line.find(e.label) != std::string::npos) {
|
||||
@@ -495,8 +548,20 @@ std::vector<int> calculateLineHeights(const std::vector<std::string> &lines, con
|
||||
}
|
||||
}
|
||||
|
||||
// Apply tighter spacing if no emotes on this line
|
||||
if (!hasEmote) {
|
||||
// Check for CJK characters
|
||||
for (size_t ci = 0; ci < _line.length();) {
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(_line.c_str() + ci, &utf8len);
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
hasCJK = true;
|
||||
lineHeight = std::max(lineHeight, CFONT_H);
|
||||
break;
|
||||
}
|
||||
ci += utf8len;
|
||||
}
|
||||
|
||||
// Apply tighter spacing if no emotes/CJK on this line
|
||||
if (!hasEmote && !hasCJK) {
|
||||
lineHeight -= 2; // reduce by 2px for tighter spacing
|
||||
if (lineHeight < 8)
|
||||
lineHeight = 8; // minimum safety
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -589,40 +589,6 @@ void setup()
|
||||
// MOS 断电,系统在 setup() 中途就会掉电。
|
||||
tca9535PowerEn(true);
|
||||
LOG_INFO("TCA9535: POWER_EN latched HIGH (early boot)");
|
||||
|
||||
// 开机确认窗口:检测 P1.3 是否持续按住 2 秒,防止意外通电卡死
|
||||
// 最多等待 3 秒,3 秒内未连续按满 2 秒则断电关机
|
||||
{
|
||||
bool bootConfirmed = false;
|
||||
uint32_t pressStart = 0;
|
||||
uint32_t deadline = millis() + 3000;
|
||||
LOG_INFO("TCA9535: Waiting for 2s button hold to confirm boot (timeout 3s)...");
|
||||
|
||||
while (millis() < deadline) {
|
||||
bool pressed = tca9535ReadPowerBoot();
|
||||
if (pressed && pressStart == 0) {
|
||||
pressStart = millis();
|
||||
} else if (pressed && pressStart != 0) {
|
||||
if (millis() - pressStart >= 2000) {
|
||||
bootConfirmed = true;
|
||||
LOG_INFO("TCA9535: Boot confirmed (button held 2s)");
|
||||
break;
|
||||
}
|
||||
} else if (!pressed && pressStart != 0) {
|
||||
// 松手重置计时
|
||||
pressStart = 0;
|
||||
}
|
||||
delay(50); // 50ms 轮询
|
||||
}
|
||||
|
||||
if (!bootConfirmed) {
|
||||
LOG_WARN("TCA9535: Boot not confirmed, shutting down");
|
||||
tca9535PowerEn(false);
|
||||
// 等待 MOS 断开
|
||||
delay(500);
|
||||
doDeepSleep(0, false, false);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#elif defined(ARCH_PORTDUINO)
|
||||
if (portduino_config.i2cdev != "") {
|
||||
|
||||
@@ -54,6 +54,7 @@ const char *const CannedMessageModule::t9LetterMap[][5] = {
|
||||
#endif
|
||||
|
||||
#include "graphics/ScreenFonts.h"
|
||||
#include "graphics/fonts/ChineseFont12x12.h"
|
||||
#include <Throttle.h>
|
||||
|
||||
// Remove Canned message screen if no action is taken for some milliseconds
|
||||
@@ -2061,27 +2062,47 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st
|
||||
int spacePos = text.indexOf(' ', pos);
|
||||
int endPos = (spacePos == -1) ? text.length() : spacePos + 1; // Include space
|
||||
String word = text.substring(pos, endPos);
|
||||
int wordWidth = display->getStringWidth(word);
|
||||
|
||||
// Calculate word width: ASCII chars use getStringWidth, CJK use CFONT_W
|
||||
int wordWidth = 0;
|
||||
for (int ci = 0; ci < word.length();) {
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(word.c_str() + ci, &utf8len);
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
wordWidth += CFONT_W;
|
||||
} else {
|
||||
String oneChar = word.substring(ci, ci + utf8len);
|
||||
wordWidth += display->getStringWidth(oneChar);
|
||||
}
|
||||
ci += utf8len;
|
||||
}
|
||||
|
||||
if (lineWidth + wordWidth > maxWidth && lineWidth > 0) {
|
||||
lines.push_back(currentLine);
|
||||
currentLine.clear();
|
||||
lineWidth = 0;
|
||||
}
|
||||
// If word itself too big, split by character
|
||||
// If word itself too big, split by character (CJK-aware)
|
||||
if (wordWidth > maxWidth) {
|
||||
uint16_t charPos = 0;
|
||||
int charPos = 0;
|
||||
while (charPos < word.length()) {
|
||||
String oneChar = word.substring(charPos, charPos + 1);
|
||||
int charWidth = display->getStringWidth(oneChar);
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(word.c_str() + charPos, &utf8len);
|
||||
int charWidth;
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
charWidth = CFONT_W;
|
||||
} else {
|
||||
String oneChar = word.substring(charPos, charPos + utf8len);
|
||||
charWidth = display->getStringWidth(oneChar);
|
||||
}
|
||||
if (lineWidth + charWidth > maxWidth && lineWidth > 0) {
|
||||
lines.push_back(currentLine);
|
||||
currentLine.clear();
|
||||
lineWidth = 0;
|
||||
}
|
||||
currentLine.push_back({false, oneChar});
|
||||
currentLine.push_back({false, word.substring(charPos, charPos + utf8len)});
|
||||
lineWidth += charWidth;
|
||||
charPos++;
|
||||
charPos += utf8len;
|
||||
}
|
||||
} else {
|
||||
currentLine.push_back({false, word});
|
||||
@@ -2114,8 +2135,23 @@ void CannedMessageModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *st
|
||||
nextX += emote->width + 2;
|
||||
}
|
||||
} else {
|
||||
display->drawString(nextX, yLine, token.second);
|
||||
nextX += display->getStringWidth(token.second);
|
||||
// Mixed ASCII + Chinese rendering
|
||||
const char *str = token.second.c_str();
|
||||
const char *p = str;
|
||||
while (*p) {
|
||||
int utf8len = 1;
|
||||
uint16_t cp = cfont12_utf8(p, &utf8len);
|
||||
if (cp >= 0x80 && cfont12_find(cp)) {
|
||||
// CJK character: draw using Chinese bitmap font
|
||||
nextX += cfont12_draw(display, nextX, yLine, cp);
|
||||
} else {
|
||||
// ASCII / non-CJK: render single char via drawString
|
||||
String oneChar = String(p).substring(0, utf8len);
|
||||
display->drawString(nextX, yLine, oneChar);
|
||||
nextX += display->getStringWidth(oneChar);
|
||||
}
|
||||
p += utf8len;
|
||||
}
|
||||
}
|
||||
}
|
||||
yLine += rowHeight;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
package-lock.json
|
||||
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Chinese 12x12 Bitmap Font Generator for OLED (ESP32-C3 / SH1106)
|
||||
*
|
||||
* Generates C header with XBM-format bitmaps for GB2312 Level 1 (3755 chars)
|
||||
* + common punctuation + fullwidth ASCII.
|
||||
*
|
||||
* Usage: node gen_chinese_font.mjs
|
||||
* Output: src/graphics/fonts/ChineseFont12x12.h
|
||||
*
|
||||
* Font source: Windows built-in TTF (msyh.ttc / simhei.ttf / simsun.ttc)
|
||||
* Rasterizer: @napi-rs/canvas (native, fast)
|
||||
*/
|
||||
|
||||
import { createCanvas } from '@napi-rs/canvas';
|
||||
import { existsSync, writeFileSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
// ========== Config ==========
|
||||
const W = 12, H = 12;
|
||||
const BPR = Math.ceil(W / 8); // 2 bytes per row
|
||||
const BPG = BPR * H; // 24 bytes per glyph
|
||||
const GLYPH_SIZE = 2 + BPG; // 26 bytes total (uint16 + bitmap)
|
||||
|
||||
// ========== Find Chinese font ==========
|
||||
const FONT_PATHS = [
|
||||
'C:\\Windows\\Fonts\\msyh.ttc',
|
||||
'C:\\Windows\\Fonts\\msyhbd.ttc',
|
||||
'C:\\Windows\\Fonts\\simhei.ttf',
|
||||
'C:\\Windows\\Fonts\\simsun.ttc',
|
||||
'C:\\Windows\\Fonts\\simfang.ttf',
|
||||
'C:\\Windows\\Fonts\\simkai.ttf',
|
||||
];
|
||||
let fontPath = FONT_PATHS.find(p => existsSync(p));
|
||||
if (!fontPath) { console.error('No Chinese TTF found!'); process.exit(1); }
|
||||
console.log(`Font: ${fontPath}`);
|
||||
|
||||
// ========== Build character set ==========
|
||||
function buildCharSet() {
|
||||
const set = new Set();
|
||||
|
||||
// 1. Chinese punctuation (CJK Symbols and Punctuation block)
|
||||
const punct = "\uFF0C\u3002\uFF01\uFF1F\u3001\uFF1A\uFF1B\u201C\u201D\u2018\u2019\uFF08\uFF09\u300A\u300B\u3010\u3011\u00B7\uFF5E\u2014\u2026";
|
||||
for (const c of punct) set.add(c);
|
||||
|
||||
// 2. Fullwidth digits, letters
|
||||
for (let i = 0xFF10; i <= 0xFF19; i++) set.add(String.fromCodePoint(i));
|
||||
for (let i = 0xFF21; i <= 0xFF3A; i++) set.add(String.fromCodePoint(i));
|
||||
for (let i = 0xFF41; i <= 0xFF5A; i++) set.add(String.fromCodePoint(i));
|
||||
|
||||
// 3. GB2312 Level 1: U+4E00..U+5573 region + extras
|
||||
// GB2312 Level 1 = 3755 most common characters
|
||||
// We include the full CJK Unified Ideographs block up to U+9FFF
|
||||
// but only those that exist in GB2312.
|
||||
// For simplicity and maximum coverage, we include U+4E00-U+9FA5
|
||||
// (this is the "CJK Unified Ideographs" block, ~20,902 chars)
|
||||
// BUT we limit to ~6000 to fit in flash budget.
|
||||
//
|
||||
// Practical approach: include U+4E00-U+7FFF (8192 codepoints, ~6800 assigned)
|
||||
// This covers GB2312 Level 1 + Level 2 + many common extensions.
|
||||
// At 26 bytes/glyph = ~177 KB. Our budget is ~1700 KB. Plenty of room.
|
||||
|
||||
// Let's include U+4E00 to U+9FFF (full CJK block) - that's about 20992 codepoints
|
||||
// but many are unassigned. We'll filter to only assigned ones.
|
||||
// Actually, all codepoints in U+4E00-U+9FFF range ARE assigned CJK characters.
|
||||
// 0x9FFF - 0x4E00 = 0x51FF = 20991 codepoints
|
||||
// 20991 × 26 = 545,766 bytes ≈ 533 KB
|
||||
// This is too much. Let's cap at a reasonable number.
|
||||
|
||||
// GB2312 total: 6763 chars. Let's aim for ~7000 chars max.
|
||||
// U+4E00 to U+73FF covers most GB2312 chars:
|
||||
// 0x73FF - 0x4E00 = 0x25FF = 9727 codepoints
|
||||
// 9727 × 26 = 252,902 bytes ≈ 247 KB
|
||||
// Still a lot. Let's be more selective.
|
||||
|
||||
// Optimal: GB2312 Level 1 only (3755 chars) + punctuation + fullwidth
|
||||
// Level 1 chars are in the range U+4E00 to U+5573 (but not contiguous)
|
||||
// The actual GB2312-to-Unicode mapping has the Level 1 chars spread across
|
||||
// U+4E00-U+9FA5. For simplicity, let's just include the densest CJK blocks:
|
||||
// U+4E00-U+6FFF = 8448 codepoints = 220 KB. Good coverage.
|
||||
|
||||
// CJK Unified Ideographs: U+4E00 to U+9FFF
|
||||
// Covers all GB2312 (6763 chars) + CJK Extension A/B common chars
|
||||
// ~20992 codepoints × 26 bytes = ~533 KB flash
|
||||
// Fits within app partition budget (2.75 MB, firmware ~2.65 MB with full font)
|
||||
for (let cp = 0x4E00; cp <= 0x9FFF; cp++) {
|
||||
set.add(String.fromCodePoint(cp));
|
||||
}
|
||||
|
||||
return [...set].sort((a, b) => a.codePointAt(0) - b.codePointAt(0));
|
||||
}
|
||||
|
||||
const chars = buildCharSet();
|
||||
const estKB = (chars.length * GLYPH_SIZE / 1024).toFixed(0);
|
||||
console.log(`Characters: ${chars.length} (est. ${estKB} KB flash)`);
|
||||
|
||||
if (chars.length * GLYPH_SIZE > 1700 * 1024) {
|
||||
console.error('ERROR: exceeds flash budget!');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// ========== Rasterize ==========
|
||||
function rasterize(ch) {
|
||||
const canvas = createCanvas(W, H);
|
||||
const ctx = canvas.getContext('2d');
|
||||
// Transparent background + black text → alpha is the only signal we need
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
ctx.fillStyle = '#000';
|
||||
ctx.font = `${H - 1}px "Microsoft YaHei","SimHei","SimSun",sans-serif`;
|
||||
ctx.textBaseline = 'top';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(ch, W / 2, 0);
|
||||
|
||||
const { data } = ctx.getImageData(0, 0, W, H);
|
||||
const bytes = new Uint8Array(BPG);
|
||||
for (let r = 0; r < H; r++) {
|
||||
for (let c = 0; c < W; c++) {
|
||||
// Transparent bg + black text: alpha > threshold = glyph pixel
|
||||
// Threshold 80: keep thin strokes (alpha 80-128) while cutting outer fringe (<80)
|
||||
// 40 = too bold (includes fringe), 128 = too thin (loses thin strokes)
|
||||
if (data[(r * W + c) * 4 + 3] > 80) {
|
||||
bytes[r * BPR + (c >> 3)] |= 0x80 >> (c & 7);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
console.log('Rasterizing...');
|
||||
const bitmaps = [];
|
||||
const t0 = Date.now();
|
||||
for (let i = 0; i < chars.length; i++) {
|
||||
bitmaps.push(rasterize(chars[i]));
|
||||
if ((i + 1) % 500 === 0 || i === chars.length - 1) {
|
||||
const pct = ((i + 1) / chars.length * 100).toFixed(0);
|
||||
console.log(` ${pct}% (${i + 1}/${chars.length}) ${((Date.now() - t0) / 1000).toFixed(0)}s`);
|
||||
}
|
||||
}
|
||||
console.log(`Done in ${((Date.now() - t0) / 1000).toFixed(1)}s`);
|
||||
|
||||
// ========== Generate C header ==========
|
||||
function genHeader() {
|
||||
const L = [];
|
||||
L.push('/**');
|
||||
L.push(` * Chinese 12x12 Bitmap Font - ${chars.length} glyphs`);
|
||||
L.push(` * Auto-generated ${new Date().toISOString().split('T')[0]}`);
|
||||
L.push(` * Flash: ~${estKB} KB`);
|
||||
L.push(' */');
|
||||
L.push('#pragma once');
|
||||
L.push('#include <Arduino.h>');
|
||||
L.push('#include <stdint.h>');
|
||||
L.push('');
|
||||
L.push('#define CFONT_W 12');
|
||||
L.push('#define CFONT_H 12');
|
||||
L.push('#define CFONT_BPR 2');
|
||||
L.push('#define CFONT_BPG 24');
|
||||
L.push(`#define CFONT_COUNT ${chars.length}`);
|
||||
L.push('');
|
||||
L.push('typedef struct { uint16_t cp; uint8_t bmp[24]; } CFontGlyph;');
|
||||
L.push('');
|
||||
L.push('class OLEDDisplay;');
|
||||
L.push('');
|
||||
L.push('const CFontGlyph cfont12_data[] PROGMEM = {');
|
||||
|
||||
for (let i = 0; i < chars.length; i++) {
|
||||
const cp = chars[i].codePointAt(0);
|
||||
const hex = Array.from(bitmaps[i]).map(b => '0x' + b.toString(16).padStart(2, '0')).join(',');
|
||||
L.push(` {0x${cp.toString(16).padStart(4,'0')},{${hex}}},`);
|
||||
}
|
||||
|
||||
L.push('};');
|
||||
L.push('');
|
||||
// Binary search lookup
|
||||
L.push('inline const CFontGlyph* cfont12_find(uint16_t cp) {');
|
||||
L.push(' int lo=0, hi=CFONT_COUNT-1;');
|
||||
L.push(' while(lo<=hi){');
|
||||
L.push(' int m=(lo+hi)>>1;');
|
||||
L.push(' uint16_t v=pgm_read_word(&cfont12_data[m].cp);');
|
||||
L.push(' if(v==cp) return &cfont12_data[m];');
|
||||
L.push(' if(v<cp) lo=m+1; else hi=m-1;');
|
||||
L.push(' }');
|
||||
L.push(' return nullptr;');
|
||||
L.push('}');
|
||||
L.push('');
|
||||
// Draw char using setPixel
|
||||
L.push('inline int cfont12_draw(OLEDDisplay*d,int x,int y,uint16_t cp){');
|
||||
L.push(' const CFontGlyph*g=cfont12_find(cp);');
|
||||
L.push(' if(!g)return 0;');
|
||||
L.push(' for(int r=0;r<CFONT_H;r++)for(int c=0;c<CFONT_W;c++){');
|
||||
L.push(' uint8_t b=pgm_read_byte(&g->bmp[r*CFONT_BPR+(c>>3)]);');
|
||||
L.push(' if(b&(0x80>>(c&7)))d->setPixel(x+c,y+r);');
|
||||
L.push(' }');
|
||||
L.push(' return CFONT_W;');
|
||||
L.push('}');
|
||||
L.push('');
|
||||
// UTF-8 decode
|
||||
L.push('inline uint16_t cfont12_utf8(const char*p,int*len){');
|
||||
L.push(' uint8_t b=(uint8_t)*p;');
|
||||
L.push(' if(b<0x80){*len=1;return b;}');
|
||||
L.push(' if((b&0xE0)==0xC0){*len=2;return((b&0x1F)<<6)|(((uint8_t)p[1])&0x3F);}');
|
||||
L.push(' if((b&0xF0)==0xE0){*len=3;return((b&0x0F)<<12)|(((uint8_t)p[1])&0x3F)<<6|((uint8_t)p[2])&0x3F;}');
|
||||
L.push(' *len=1;return 0;');
|
||||
L.push('}');
|
||||
L.push('');
|
||||
// Draw mixed string
|
||||
L.push('inline uint16_t cfont12_drawStr(OLEDDisplay*d,int x,int y,const char*s){');
|
||||
L.push(' int cx=x;int len;');
|
||||
L.push(' while(*s){uint16_t cp=cfont12_utf8(s,&len);');
|
||||
L.push(' if(cp>=0x80&&cfont12_find(cp)){cx+=cfont12_draw(d,cx,y,cp);}');
|
||||
L.push(' s+=len;');
|
||||
L.push(' }');
|
||||
L.push(' return cx-x;');
|
||||
L.push('}');
|
||||
L.push('');
|
||||
L.push('inline uint16_t cfont12_strWidth(const char*s){');
|
||||
L.push(' int w=0;int len;');
|
||||
L.push(' while(*s){uint16_t cp=cfont12_utf8(s,&len);');
|
||||
L.push(' if(cp>=0x80&&cfont12_find(cp))w+=CFONT_W;');
|
||||
L.push(' s+=len;');
|
||||
L.push(' }');
|
||||
L.push(' return w;');
|
||||
L.push('}');
|
||||
L.push('');
|
||||
return L.join('\n');
|
||||
}
|
||||
|
||||
const outPath = join(__dirname, '..', 'src', 'graphics', 'fonts', 'ChineseFont12x12.h');
|
||||
writeFileSync(outPath, genHeader(), 'utf-8');
|
||||
console.log(`\n✅ ${outPath}`);
|
||||
console.log(` ${chars.length} glyphs, ~${estKB} KB flash`);
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "tools",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"type": "commonjs",
|
||||
"dependencies": {
|
||||
"@napi-rs/canvas": "^0.1.97"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user