commit 8ec868d55e77006cd97d17ea2c135d7e1b863f35
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sat Jan 3 15:42:30 2026 -0700
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sat Jan 3 15:42:30 2026 -0700
Fix per character width and font sizing edge cases
---
source/graphics.cpp | 24 +++++++++++++++------
source/printHelper.cpp | 15 ++++++++++++-
test/carts/per_char_width_test.p8 | 21 ++++++++++++++++++
test/carts/screenshots/per_char_width_test_f01.png | Bin 0 -> 291 bytes
test/endtoendtests.cpp | 16 ++++++++++++++
5 files changed, 68 insertions(+), 8 deletions(-)
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 5df8ab6..8572f32 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1497,16 +1497,26 @@ int Graphics::drawCharacter(
int defaultCharHeight = useCustomFont ? _memory->data[0x5602] : 5;
if (ch > 0x0f) {
- uint8_t charWidth =
- ch < 0x80
- ? forceCharWidth > -1 && forceCharWidth < 4 ? forceCharWidth : defaultCharWidth
- : forceCharWidth > -1 && forceCharWidth < 4 ? (forceCharWidth + 4) : defaultWideCharWidth;
+ // Determine render width:
+ // - If forceCharWidth is set (0-3 via \^x), use it to limit rendering
+ // - For custom fonts without forced width, render all 8 columns (charWidth only affects cursor)
+ // - For standard font, use defaultCharWidth (glyph data matches the width)
+ bool widthIsForced = forceCharWidth > -1 && forceCharWidth < 4;
+ uint8_t renderWidth;
+ if (widthIsForced) {
+ renderWidth = ch < 0x80 ? forceCharWidth : (forceCharWidth + 4);
+ } else if (useCustomFont) {
+ renderWidth = 8; // Custom fonts: render all 8 columns
+ } else {
+ renderWidth = ch < 0x80 ? defaultCharWidth : defaultWideCharWidth; // Standard font
+ }
if (ch >= 0x80) {
extraCharWidth = defaultWideCharWidth - defaultCharWidth;
}
- uint8_t charHeight = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : defaultCharHeight;
+ // Height: use forced height if set, otherwise use defaultCharHeight from font metadata
+ uint8_t renderHeight = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : defaultCharHeight;
auto result = drawCharacterFromBytes(
useCustomFont ? &(_memory->data[0x5600 + ch*8]) : &(defaultFontBinaryData[ch*8]),
@@ -1515,8 +1525,8 @@ int Graphics::drawCharacter(
fgColor,
bgColor,
printMode,
- charWidth,
- charHeight
+ renderWidth,
+ renderHeight
);
extraCharWidth += get<0>(result);
}
diff --git a/source/printHelper.cpp b/source/printHelper.cpp
index e7255ec..8d6d699 100644
--- a/source/printHelper.cpp
+++ b/source/printHelper.cpp
@@ -544,7 +544,20 @@ int print(std::string str, int x, int y, uint8_t c) {
printMode,
forceCharWidth,
forceCharHeight);
- x += charWidth + extraWidth;
+
+ // Apply per-character width adjustment if flag 0x1 is set at 0x5605
+ int perCharWidthAdj = 0;
+ if ((printMode & PRINT_MODE_CUSTOM_FONT) && (_ph_mem->data[0x5605] & 0x01)) {
+ int nibbleIdx = ch - 16;
+ if (nibbleIdx >= 0 && nibbleIdx < 240) {
+ uint8_t adjByte = _ph_mem->data[0x5608 + nibbleIdx/2];
+ uint8_t adjNibble = (nibbleIdx % 2 == 0) ? (adjByte & 0x0f) : (adjByte >> 4);
+ perCharWidthAdj = (adjNibble & 0x07);
+ if (perCharWidthAdj >= 4) perCharWidthAdj = perCharWidthAdj - 8; // convert 4,5,6,7 to -4,-3,-2,-1
+ }
+ }
+
+ x += charWidth + extraWidth + perCharWidthAdj;
while (framesToPause > 0){
//TODO: yield here? not sure how to handle this
diff --git a/test/carts/per_char_width_test.p8 b/test/carts/per_char_width_test.p8
new file mode 100644
index 0000000..e1d4c2f
--- /dev/null
+++ b/test/carts/per_char_width_test.p8
@@ -0,0 +1,21 @@
+pico-8 cartridge // http://www.pico-8.com
+version 43
+__lua__
+-- import custom font - thank you @trog!
+?"\^!5600\6\8\7\0\0\1\0\0fffff@ue\6\6\0p\5\0\5\4`\0\0\0\0tv\6\0\0\0\0@\7$\0\0\0\0 \0pp\0\0\0\0\0`\0 \0\0\0\0 \0`t'\0\0`f`\0``\0`\0\0\0wf\0wgfvwvfp\0`\0\0vwgwfwwwefffgfggfvfvfvve`fwfvvgffv\5\0\0\0\0\0\0\0\0\0\0\7\7\7\0\0\0\0\0\7\5\7\0\0\0\0\0\5\2\5\0\0\0\0\0\5\0\5\0\0\0\0\0\5\5\5\0\0\0\0\4\6\7\6\4\0\0\0\1\3\7\3\1\0\0\0\7\1\1\1\0\0\0\0\0\4\4\4\7\0\0\0■\10\4\14\4\0\0\0\0\0\1\0\0\0\0\0\0\0\0\1\2\0\0\0\0\0\0\3\3\0\0\0\5\5\0\0\0\0\0\0\2\5\2\0\0\0\0\0\0\0\0\0\0\0\0\0「、\6\0\1\0\0\0\5\5\0\0\0\0\0\0\10゜\10゜\10\0\0\0゛\5\14⁘\15\0\0\0■\8\4\2■\0\0\0\2\5◀\9◀\0\0\0\2\1\0\0\0\0\0\0\2\1\1\1\2\0\0\0\1\2\2\2\1\0\0\0‖\14•\14‖\0\0\0\4\4゜\4\4\0\0\0\0\0\0\2\1\0\0\0\0\0゜\0\0\0\0\0\0\0\0\0\1\0\0\0▮\8\4\2\1\0\0\0\14」■⁙\14\0\0\0\2\3\2\2\7\0\0\0\15▮\14\1゜\0\0\0\14■\12■\14\0\0\0\12\10\9゜\8\0\0\0\15\1\15▮\15\0\0\0\14\1\15■\14\0\0\0゜■\8\4\4\0\0\0\14■\14■\14\0\0\0\14■゛▮\14\0\0\0\0\1\0\1\0\0\0\0\0\2\0\2\1\0\0\0\4\2\1\2\4\0\0\0\0\14\0\14\0\0\0\0\1\2\4\2\1\0\0\0\14■\12\0\4\0\0\0\14■⁘¥\12\0\0\0\0\14■■゛\0\0\0\1\15■■\15\0\0\0\0\14■\1\14\0\0\0▮゛■■゛\0\0\0\0\14゜\1\14\0\0\0\12□\2\7\2\0\0\0\0゛■゛▮\14\0\0\1\15■■■\0\0\0\1\0\1\1\1\0\0\0\8\0\12\8\9\6\0\0\1\1」\15」\0\0\0\1\1\1\1\1\0\0\0\0007iii\0\0\0\0\15■■■\0\0\0\0\14■■\14\0\0\0\0\15■■\15\1\0\0\0゛■■゛▮\0\0\0\13⁙\1\1\0\0\0\0゛\3「\15\0\0\0\2\15\2□\12\0\0\0\0■■■゛\0\0\0\0■■\10\4\0\0\0\0iii6\0\0\0\0■\14\14■\0\0\0\0■■゛▮\14\0\0\0゜\8\6゜\0\0\0\3\1\1\1\3\0\0\0\1\2\4\8▮\0\0\0\3\2\2\2\3\0\0\0\4\10■\0\0\0\0\0\0\0\0\0゜\0\0\0\1\2\0\0\0\0\0\0\14■■゜■\0\0\0\15■\15■\15\0\0\0\14■\1■\14\0\0\0\15■■■\15\0\0\0゜\1\15\1゜\0\0\0゜\1\15\1\1\0\0\0゛\1。■゛\0\0\0■■゜■■\0\0\0\7\2\2\2\7\0\0\0「▮▮■\14\0\0\0■\9\7\9■\0\0\0\1\1\1■゜\0\0\0acuia\0\0\0■⁙‖」■\0\0\0\14■■■\14\0\0\0\15■■\15\1\0\0\0\14■■\9◀\0\0\0\15■■\15」\0\0\0゛\1\14▮\15\0\0\0゜\4\4\4\4\0\0\0■■■■\14\0\0\0■■■\10\4\0\0\0iiii6\0\0\0■\10\4\10■\0\0\0■■\10\4\4\0\0\0゜\8\4\2゜\0\0\0\6\2\3\2\6\0\0\0\1\1\1\1\1\0\0\0\3\2\6\2\3\0\0\0\0\2‖\8\0\0\0\0\2\5\2\0\0\0\0\0○○○○○\0\0\0u*u*u\0\0\0a○]]>\0\0\0>ccw>\0\0\0■d■d■\0\0\0\2゛\14\15\8\0\0\0\14▶゜゜\14\0\0\0•゜゜\14\4\0\0\0、6w6、\0\0\0\14\14゜\14\10\0\0\0、>○*:\0\0\0>gcg>\0\0\0○]○a○\0\0\0、\4\4\7\7\0\0\0>ckc>\0\0\0\4\14゜\14\4\0\0\0\0\0u\0\0\0\0\0>scs>\0\0\0\8、○>\34\0\0\0゜\14\4\14゜\0\0\0>wcc>\0\0\0\0\5r \0\0\0\0\0■*d\0\0\0\0>kwk>\0\0\0○\0○\0○\0\0\0uuuuu\0\0\0\14\4゛-&\0\0\0■!!%\2\0\0\0\6\15▮▮\14\0\0\0\4\15\4□\13\0\0\0n\4>e&\0\0\0\34_□□\10\0\0\0゛\8<■\6\0\0\0\8\6\1\6\8\0\0\0■=■■\9\0\0\0\15▮\0\1゛\0\0\0\4゛\8\1\6\0\0\0\1\1\1■\14\0\0\0\4゜\4\6\4\0\0\0□?□\2、\0\0\0゛\8?\2、\0\0\0\2\0072\0022\0\0\0\15\2\14▮、\0\0\0゜ ▮\12\0\0\0゜\8\4\4\8\0\0\0\4、\2\1゛\0\0\0002\7□x「\0\0\0=!\1\0059\0\0\0\9>kmf\0\0\0¥'\34s2\0\0\0<jiif\0\0\0\9。\9。\13\0\0\0#b\34\34、\0\0\0\12\0\8*m\0\0\0\0\12□!@\0\0\0}y■=]\0\0\0゜゛\4\15▶\0\0\0\3□?⁙\8\0\0\0□'\2#゛\0\0\0\5゛-#「\0\0\0\15\2\15\34、\0\0\0\10゜□\4\4\0\0\0。+)「\4\0\0\0\2\14\2\15\3\0\0\0\4\1゜▮\14\0\0\0■■⁙▮\12\0\0\0゜\12□9「\0\0\0\2•◀⁙2\0\0\0゜\12□!「\0\0\0¥'\34#□\0\0\0\0072\14⁘<\0\0\0\4\2\6+」\0\0\0\0\0\7\8\4\0\0\0\0\10゜□\4\0\0\0\0\4\15‖\13\0\0\0\0\2\6\3\7\0\0\0゜▮\10\2\1\0\0\0「\4\7\4\4\0\0\0\4゜■▮\12\0\0\0゜\4\4\4゜\0\0\0\8?\12\10\9\0\0\0\2゜□■」\0\0\0\4゜\4゜\4\0\0\0゛□■\8\4\0\0\0\2>\9\8\4\0\0\0゜▮▮▮゜\0\0\0□?□▮\8\0\0\0\3▮⁙\8\6\0\0\0゜▮\8\12⁙\0\0\0\2゜□\2、\0\0\0■□▮\8\6\0\0\0>\34-0\12\0\0\0\14\4゜\4\2\0\0\0‖‖▮\8\6\0\0\0\14\0゜\4\2\0\0\0\1\1\7\9\1\0\0\0\4゜\4\4\2\0\0\0\0\14\0\0゜\0\0\0゜▮⁘\8◀\0\0\0\4゜「/\4\0\0\0▮▮▮\8\7\0\0\0\8□□\34!\0\0\0\1\15\1\1\14\0\0\0゜▮▮\8\6\0\0\0\12□!@\0\0\0\0\4゜\4‖‖\0\0\0゜▮\10\4\8\0\0\0゛\0゜\0\15\0\0\0\4\2□!?\0\0\0 ⁘\0084\3\0\0\0\15\2\15\2゛\0\0\0\2゜□\2\2\0\0\0\14\8\8\8゜\0\0\0\15\8\15\8\15\0\0\0゜\0゜▮\12\0\0\0\9\9\9\8\4\0\0\0\10\10\10*」\0\0\0\1\1■\9\7\0\0\0゜■■■゜\0\0\0゜■▮\8\6\0\0\0゜▮゛▮\12\0\0\0\3▮▮\8\7\0\0\0\0‖▮\8\6\0\0\0\0\2\15\10\2\0\0\0\0\0\6\4\15\0\0\0\0\7\6\4\7\0\0\0\8\4c▮\8\0\0\0\8▮c\4\8\0\0\0"
+
+wrote=false
+function _draw()
+
+cls()
+print("\14ove worm with", 13, 6, 5)
+--print("\14w", 13, 6, 5)
+
+end
+__gfx__
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
diff --git a/test/carts/screenshots/per_char_width_test_f01.png b/test/carts/screenshots/per_char_width_test_f01.png
new file mode 100644
index 0000000..2efd220
Binary files /dev/null and b/test/carts/screenshots/per_char_width_test_f01.png differ
diff --git a/test/endtoendtests.cpp b/test/endtoendtests.cpp
index b970b0c..8894bb4 100644
--- a/test/endtoendtests.cpp
+++ b/test/endtoendtests.cpp
@@ -1008,6 +1008,22 @@ TEST_CASE("Loading and running carts") {
vm->CloseCart();
}
+ SUBCASE("Per character width adjustment test"){
+ vm->LoadCart("per_char_width_test.p8", false);
+ vm->vm_run();
+ vm->Step();
+
+ SUBCASE("No error reported"){
+ CHECK(vm->GetBiosError() == "");
+ }
+ SUBCASE("screen matches screenshot"){
+ vm->Step();
+
+ CHECK(verifyScreenshot(vm, host, "carts/screenshots/per_char_width_test_f01.png"));
+ }
+
+ vm->CloseCart();
+ }
delete vm;
delete host;