Przeglądaj źródła

add jpfont_test

Satoshi Yoneda 4 tygodni temu
rodzic
commit
aabbbf42d3

+ 2 - 0
jpfont_test/.gitignore

@@ -0,0 +1,2 @@
+build
+!.vscode/*

+ 22 - 0
jpfont_test/.vscode/c_cpp_properties.json

@@ -0,0 +1,22 @@
+{
+    "configurations": [
+        {
+            "name": "Pico",
+            "includePath": [
+                "${workspaceFolder}/**",
+                "${userHome}/.pico-sdk/sdk/2.2.0/**"
+            ],
+            "forcedInclude": [
+                "${userHome}/.pico-sdk/sdk/2.2.0/src/common/pico_base_headers/include/pico.h",
+                "${workspaceFolder}/build/generated/pico_base/pico/config_autogen.h"
+            ],
+            "defines": [],
+            "compilerPath": "${userHome}/.pico-sdk/toolchain/14_2_Rel1/bin/arm-none-eabi-gcc.exe",
+            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
+            "cStandard": "c17",
+            "cppStandard": "c++14",
+            "intelliSenseMode": "linux-gcc-arm"
+        }
+    ],
+    "version": 4
+}

+ 15 - 0
jpfont_test/.vscode/cmake-kits.json

@@ -0,0 +1,15 @@
+[
+    {
+        "name": "Pico",
+        "compilers": {
+            "C": "${command:raspberry-pi-pico.getCompilerPath}",
+            "CXX": "${command:raspberry-pi-pico.getCxxCompilerPath}"
+        },
+        "environmentVariables": {
+            "PATH": "${command:raspberry-pi-pico.getEnvPath};${env:PATH}"
+        },
+        "cmakeSettings": {
+            "Python3_EXECUTABLE": "${command:raspberry-pi-pico.getPythonPath}"
+        }
+    }
+]

+ 9 - 0
jpfont_test/.vscode/extensions.json

@@ -0,0 +1,9 @@
+{
+    "recommendations": [
+        "marus25.cortex-debug",
+        "ms-vscode.cpptools",
+        "ms-vscode.cpptools-extension-pack",
+        "ms-vscode.vscode-serial-monitor",
+        "raspberry-pi.raspberry-pi-pico"
+    ]
+}

+ 50 - 0
jpfont_test/.vscode/launch.json

@@ -0,0 +1,50 @@
+{
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Pico Debug (Cortex-Debug)",
+            "cwd": "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+            "executable": "${command:raspberry-pi-pico.launchTargetPath}",
+            "request": "launch",
+            "type": "cortex-debug",
+            "servertype": "openocd",
+            "serverpath": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
+            "device": "${command:raspberry-pi-pico.getChipUppercase}",
+            "configFiles": [
+                "interface/cmsis-dap.cfg",
+                "target/${command:raspberry-pi-pico.getTarget}.cfg"
+            ],
+            "svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
+            "runToEntryPoint": "main",
+            // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
+            // Also works fine for flash binaries
+            "overrideLaunchCommands": [
+                "monitor reset init",
+                "load \"${command:raspberry-pi-pico.launchTargetPath}\""
+            ],
+            "openOCDLaunchCommands": [
+                "adapter speed 5000"
+            ]
+        },
+        {
+            "name": "Pico Debug (Cortex-Debug with external OpenOCD)",
+            "cwd": "${workspaceRoot}",
+            "executable": "${command:raspberry-pi-pico.launchTargetPath}",
+            "request": "launch",
+            "type": "cortex-debug",
+            "servertype": "external",
+            "gdbTarget": "localhost:3333",
+            "gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
+            "device": "${command:raspberry-pi-pico.getChipUppercase}",
+            "svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
+            "runToEntryPoint": "main",
+            // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
+            // Also works fine for flash binaries
+            "overrideLaunchCommands": [
+                "monitor reset init",
+                "load \"${command:raspberry-pi-pico.launchTargetPath}\""
+            ]
+        },
+    ]
+}

+ 40 - 0
jpfont_test/.vscode/settings.json

@@ -0,0 +1,40 @@
+{
+    "cmake.showSystemKits": false,
+    "cmake.options.statusBarVisibility": "hidden",
+    "cmake.options.advanced": {
+        "build": {
+            "statusBarVisibility": "hidden"
+        },
+        "launch": {
+            "statusBarVisibility": "hidden"
+        },
+        "debug": {
+            "statusBarVisibility": "hidden"
+        }
+    },
+    "cmake.configureOnEdit": false,
+    "cmake.automaticReconfigure": false,
+    "cmake.configureOnOpen": false,
+    "cmake.generator": "Ninja",
+    "cmake.cmakePath": "${userHome}/.pico-sdk/cmake/v3.31.5/bin/cmake",
+    "C_Cpp.debugShortcut": false,
+    "terminal.integrated.env.windows": {
+        "PICO_SDK_PATH": "${env:USERPROFILE}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1",
+        "Path": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool;${env:USERPROFILE}/.pico-sdk/cmake/v3.31.5/bin;${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1;${env:PATH}"
+    },
+    "terminal.integrated.env.osx": {
+        "PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
+        "PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
+    },
+    "terminal.integrated.env.linux": {
+        "PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
+        "PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
+    },
+    "raspberry-pi-pico.cmakeAutoConfigure": true,
+    "raspberry-pi-pico.useCmakeTools": false,
+    "raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.31.5/bin/cmake",
+    "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja"
+}

+ 102 - 0
jpfont_test/.vscode/tasks.json

@@ -0,0 +1,102 @@
+{
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "Compile Project",
+            "type": "process",
+            "isBuildCommand": true,
+            "command": "${userHome}/.pico-sdk/ninja/v1.12.1/ninja",
+            "args": ["-C", "${workspaceFolder}/build"],
+            "group": "build",
+            "presentation": {
+                "reveal": "always",
+                "panel": "dedicated"
+            },
+            "problemMatcher": "$gcc",
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1/ninja.exe"
+            }
+        },
+        {
+            "label": "Run Project",
+            "type": "process",
+            "command": "${env:HOME}/.pico-sdk/picotool/2.2.0/picotool/picotool",
+            "args": [
+                "load",
+                "${command:raspberry-pi-pico.launchTargetPath}",
+                "-fx"
+            ],
+            "presentation": {
+                "reveal": "always",
+                "panel": "dedicated"
+            },
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool/picotool.exe"
+            }
+        },
+        {
+            "label": "Flash",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/${command:raspberry-pi-pico.getTarget}.cfg",
+                "-c",
+                "adapter speed 5000; program \"${command:raspberry-pi-pico.launchTargetPath}\" verify reset exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        },
+        {
+            "label": "Rescue Reset",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/${command:raspberry-pi-pico.getChip}-rescue.cfg",
+                "-c",
+                "adapter speed 5000; reset halt; exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        },
+        {
+            "label": "Risc-V Reset (RP2350)",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-c",
+                "set USE_CORE { rv0 rv1 cm0 cm1 }",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/rp2350.cfg",
+                "-c",
+                "adapter speed 5000; init;",
+                "-c",
+                "write_memory 0x40120158 8 { 0x3 }; echo [format \"Info : ARCHSEL 0x%02x\" [read_memory 0x40120158 8 1]];",
+                "-c",
+                "reset halt; targets rp2350.rv0; echo [format \"Info : ARCHSEL_STATUS 0x%02x\" [read_memory 0x4012015C 8 1]]; exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        }
+    ]
+}

+ 60 - 0
jpfont_test/CMakeLists.txt

@@ -0,0 +1,60 @@
+# Generated Cmake Pico project file
+
+cmake_minimum_required(VERSION 3.13)
+
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+# Initialise pico_sdk from installed location
+# (note this can come from environment, CMake cache etc)
+
+# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
+if(WIN32)
+    set(USERHOME $ENV{USERPROFILE})
+else()
+    set(USERHOME $ENV{HOME})
+endif()
+set(sdkVersion 2.2.0)
+set(toolchainVersion 14_2_Rel1)
+set(picotoolVersion 2.2.0)
+set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
+if (EXISTS ${picoVscode})
+    include(${picoVscode})
+endif()
+# ====================================================================================
+set(PICO_BOARD pico2 CACHE STRING "Board type")
+
+# Pull in Raspberry Pi Pico SDK (must be before project)
+include(pico_sdk_import.cmake)
+
+project(jpfont_test C CXX ASM)
+
+# Initialise the Raspberry Pi Pico SDK
+pico_sdk_init()
+
+# Add executable. Default name is the project name, version 0.1
+
+add_executable(jpfont_test
+    jpfont_test.cpp
+    pfnfont.cpp
+    )
+
+pico_set_program_name(jpfont_test "jpfont_test")
+pico_set_program_version(jpfont_test "0.1")
+
+# Modify the below lines to enable/disable output over UART/USB
+pico_enable_stdio_uart(jpfont_test 1)
+pico_enable_stdio_usb(jpfont_test 0)
+
+# Add the standard library to the build
+target_link_libraries(jpfont_test
+        pico_stdlib)
+
+# Add the standard include files to the build
+target_include_directories(jpfont_test PRIVATE
+        ${CMAKE_CURRENT_LIST_DIR}
+)
+
+pico_add_extra_outputs(jpfont_test)
+

+ 39 - 0
jpfont_test/README.md

@@ -0,0 +1,39 @@
+# Pinot Font (PFN) フォントライブラリ
+
+書籍に掲載する予定でしたが、時間切れで掲載できなかったコードです。これは日本語フォントファイルである[Pinot Font(PFN)フォーマット](https://zenn.dev/nom/articles/20211016-micropython-code-to-display-utf8-kanji)のフォントを使って日本語文字描画を行うためのライブラリpfnfont.h/pfnfont.cppと、そのテスト用コードで構成されています。
+
+[Pinot Font(PFN)フォーマット](https://zenn.dev/nom/articles/20211016-micropython-code-to-display-utf8-kanji)についてはリンク先に説明がありますが、フォントグリフをUTF-32コードポイント並びに変換した日本語ビットマップフォントです。従来の日本語フォントは文字コードにJISなどが使われていて扱いが少々厄介ですが、PFNフォントは現在主流のUTF-8環境で簡単かつ高速に扱うことができ、Picoシリーズで日本語を表示させるのにも便利です。ご本家はMicroPython向けですが、C++でPFNフォントを扱うライブラリを作成しました。
+
+Picoでデバッグ実行すると、次のようにコンソールに「Picoで日本語表示」という文字列がアスキーアートで表示されます。
+
+![1755325635472](image/README/1755325635472.png)
+
+PFNFontクラスを使うには、何らかの方法でメモリに読み込んだPFNフォントと、コールバック関数が必要です。このサンプルでは`.incbin`を使って14ドットフォントをメモリ中に置いています。
+
+コールバック関数は3つの引数を取り、渡された1文字のフォントグリフを描画する関数を作成します。描画する先は通常はLCDやOLEDになるでしょう。
+
+```c
+void your_callback(font_gryph_t font, int x, int y);
+```
+
+フォントのビットマップは`font_gryph_t font`に次のように格納されています。
+
+```c
+typedef struct __attribute__((packed)) {
+    uint8_t     width;          // フォントの幅
+    uint8_t     height;         // フォントの高さ
+    uint8_t     *bitmap;        // フォントのビットマップ
+} font_gryph_t;
+```
+
+`x`および`y`は必要に応じて利用するディスプレイ上でのフォントの描画座標が格納されています。
+
+```c
+PFNFont *pfn = new PFNFont(フォントデータの先頭アドレス, フォントデータのサイズ, コールバック関数);
+```
+
+次のように`draw_string()`を呼ぶと1文字ごとに(ここまでの例なら)`your_callback()`が呼ばれます。`your_callback()`を利用したい表示デバイスに合わせて記述すれば、どんなディスプレイにも日本語が表示できます。
+
+```c
+pfn->draw_string("Picoで日本語表示!", 0, 0);
+```

BIN
jpfont_test/image/README/1755325635472.png


+ 61 - 0
jpfont_test/jpfont_test.cpp

@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include "pico/stdlib.h"
+#include "pfnfont.h"
+
+__asm(\
+  ".section \".rodata\" \n"
+  ".balign 4\n"
+
+  ".global _pfnfont_data\n"
+  ".global _pfnfont_data_len\n"
+
+  "_pfnfont_data:\n"
+  ".incbin \"shnmk14u.pfn\"\n"
+  ".set _pfnfont_data_len, . - _pfnfont_data\n"
+  ".section \".text\"\n"
+);
+
+extern const uint8_t _pfnfont_data[];
+extern uint32_t _pfnfont_data_len[];
+
+// ターミナルにアスキーアートとしてフォントを描画するコールバック関数
+void draw_font_to_terminal(font_gryph_t *gryph, int x, int y) {
+    printf("--- Glyph at (%d, %d) ---\n", x, y);
+    for (int r = 0; r < gryph->height; r++) {
+        for (int c = 0; c < gryph->width; c++) {
+            // ビットマップからピクセルデータを取得
+            int bit_index = r * gryph->width + c;
+            int byte_index = bit_index / 8;
+            int bit_offset = 7 - (bit_index % 8);
+            if ((gryph->bitmap[byte_index] >> bit_offset) & 1) {
+                printf("#"); // ピクセルがオン
+            } else {
+                printf(" "); // ピクセルがオフ
+            }
+        }
+        printf("\n");
+    }
+    printf("-----------------------\n");
+}
+
+int main()
+{
+    stdio_init_all();
+
+    // 描画コールバック関数を渡してPFNFontオブジェクトを生成
+    PFNFont *pfn = new PFNFont((uint8_t *)_pfnfont_data, (size_t)_pfnfont_data_len, draw_font_to_terminal);
+
+    // フォントが正しく読み込めたかチェック
+    if (pfn->isValid()) {
+        printf("Font loaded successfully.\n");
+        pfn->draw_string("Picoで日本語表示!", 0, 0);
+    } else {
+        printf("Failed to load font.\n");
+    }
+
+    // メモリを解放
+    delete pfn;
+    
+    while (true)
+        tight_loop_contents();
+}

+ 204 - 0
jpfont_test/pfnfont.cpp

@@ -0,0 +1,204 @@
+#include <stdio.h>
+#include <string.h>
+#include "pfnfont.h"
+
+PFNFont::PFNFont(uint8_t *pfn_font, size_t pfnfont_size, void (*draw_font)(font_gryph_t *,int,int))
+{
+    is_valid = false;
+    font_data = pfn_font;
+    font_size = pfnfont_size;
+    draw_font_callback = draw_font;
+    aligned_bitmap_buffer = NULL;
+
+    // ヘッダサイズとシグニチャを検証
+    if (pfnfont_size < sizeof(pfn_font_header_t) || memcmp(font_data, "PINOTFN", 7) != 0) {
+        return; // 不正なフォントデータ
+    }
+
+    // first blockの先頭アドレス
+    first_block = font_data + sizeof(pfn_font_header_t);
+    if (first_block >= font_data + pfnfont_size) return; // データサイズが不正
+
+    // ブロックをたどりフォントビットマップの最大サイズを調べる
+    bitmap_buffer_size = 0;
+    uint8_t *current_block_ptr = first_block;
+    while(current_block_ptr < (font_data + pfnfont_size)) {
+        pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block_ptr;
+        // このブロックのビットマップサイズ(バイト単位)
+        int bitmap_size = (current_block_header->width * current_block_header->height + 7) / 8;
+        bitmap_buffer_size = max(bitmap_buffer_size, bitmap_size);
+
+        // このブロックのデータ部分の合計サイズ
+        int block_data_size = current_block_header->num_glyphs * (bitmap_size + current_block_header->codepoint_size);
+        // 次のブロックへポインタを進める (ヘッダサイズ + データサイズ)
+        current_block_ptr += sizeof(pfn_block_header_t) + block_data_size;
+    }
+    // 4バイトアラインメントの配列としてメモリを確保
+    if (bitmap_buffer_size > 0) {
+        aligned_bitmap_buffer = (uint8_t *)new uint32_t[(bitmap_buffer_size + 3) / 4];
+    } else {
+        aligned_bitmap_buffer = NULL;
+    }
+
+    is_valid = true;
+}
+
+PFNFont::~PFNFont()
+{
+    // 確保したメモリを開放
+    delete[] aligned_bitmap_buffer;
+}
+
+// UTF-8文字列へのポインタを受け取り、1文字デコードしてコードポイントを返す
+uint32_t PFNFont::utf8_to_codepoint(const char **utf8_str)
+{
+    uint32_t codepoint = 0;
+    const uint8_t* s = (const uint8_t*)*utf8_str;
+    
+    if (s[0] < 0x80) { // 1-byte (ASCII)
+        codepoint = s[0];
+        *utf8_str += 1;
+    } else if ((s[0] & 0xE0) == 0xC0) { // 2-byte sequence
+        codepoint = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
+        *utf8_str += 2;
+    } else if ((s[0] & 0xF0) == 0xE0) { // 3-byte sequence
+        codepoint = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F);
+        *utf8_str += 3;
+    } else if ((s[0] & 0xF8) == 0xF0) { // 4-byte sequence
+        codepoint = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F);
+        *utf8_str += 4;
+    } else {
+        // 不正なUTF-8シーケンスの場合は、1バイト進めてエラーを示す
+        codepoint = '?'; 
+        *utf8_str += 1;
+    }
+    return codepoint;
+}
+
+// コードポイントサイズに合わせてgryphのコードポイントを得るユーティリティ関数
+inline uint32_t PFNFont::get_codepoint_from_glyph(uint8_t *glyph_data, uint8_t codepoint_size)
+{
+    uint32_t codepoint = 0;
+
+    if(codepoint_size == 1) {
+        pfn_glyph1_t *glyph = (pfn_glyph1_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+    else if(codepoint_size == 2){
+        pfn_glyph2_t *glyph = (pfn_glyph2_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+    else if(codepoint_size == 4){
+        pfn_glyph4_t *glyph = (pfn_glyph4_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+
+    return codepoint;
+}
+
+// コードポイントがあるブロックを返す
+uint8_t* PFNFont::find_glyph_in_block(uint32_t codepoint)
+{
+    uint8_t *current_block = first_block;
+
+    do {
+        pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block;
+        uint8_t codepoint_size = current_block_header->codepoint_size;
+        uint8_t font_height = current_block_header->height;
+        uint8_t font_width = current_block_header->width;
+        uint16_t num_glyphs = current_block_header->num_glyphs;
+
+        int glyph_size = (font_width * font_height + 7) / 8 + codepoint_size;
+        // ブロックのデータ部分のサイズ
+        int block_data_size = glyph_size * num_glyphs;
+
+        uint8_t *first_glyph = current_block + sizeof(pfn_block_header_t);
+        uint32_t first_codepoint = get_codepoint_from_glyph(first_glyph, codepoint_size);
+        uint8_t *last_glyph = first_glyph + (num_glyphs - 1) * glyph_size;
+        uint32_t last_codepoint = get_codepoint_from_glyph(last_glyph, codepoint_size);
+
+        if(codepoint < first_codepoint || codepoint > last_codepoint) {
+            // 次のブロックへ移動 (ヘッダサイズ + データサイズ)
+            current_block += sizeof(pfn_block_header_t) + block_data_size;
+            if(current_block >= font_data + font_size) {
+                current_block = NULL;
+                break;
+            }
+        }
+        else {
+            break;
+        }
+    } while(true);
+
+    return current_block;
+}
+
+// コードポイントからフォントグリフを得る
+font_gryph_t *PFNFont::get_gryph_data(uint32_t codepoint)
+{
+    uint8_t *block = find_glyph_in_block(codepoint);
+    if (block == NULL) {
+        // 要求されたグリフが見つからない場合、代替文字('?')を探すか、NULLを返す
+        // ここでは'?'が存在すればそれを返し、なければNULLを返す
+        if (codepoint != '?') {
+            return get_gryph_data('?');
+        }
+        return NULL;
+    }
+
+    pfn_block_header_t *block_header = (pfn_block_header_t *)block;
+    uint8_t font_height = block_header->height;
+    uint8_t font_width = block_header->width;
+    uint16_t num_glyphs = block_header->num_glyphs;
+    uint8_t codepoint_size = block_header->codepoint_size;
+    uint8_t *first_glyph = block + sizeof(pfn_block_header_t);  
+    int glyph_size = (font_width * font_height + 7) / 8 + codepoint_size;
+
+    // 二分探索でグリフを検索
+    int low = 0;
+    int high = num_glyphs - 1;
+    uint8_t *found_glyph = NULL;
+
+    while (low <= high) {
+        int mid = low + (high - low) / 2;
+        uint8_t *current_glyph = first_glyph + mid * glyph_size;
+        uint32_t current_codepoint = get_codepoint_from_glyph(current_glyph, codepoint_size);
+
+        if (current_codepoint == codepoint) {
+            found_glyph = current_glyph;
+            break;
+        } else if (current_codepoint < codepoint) {
+            low = mid + 1;
+        } else {
+            high = mid - 1;
+        }
+    }
+
+    if (found_glyph == NULL) {
+        // find_glyph_in_blockが成功していればここには到達しないはずだが、念のため
+        return (codepoint != '?') ? get_gryph_data('?') : NULL;
+    }
+
+    int bitmap_data_size = (font_width * font_height + 7) / 8;
+    gryph_data.width = font_width;
+    gryph_data.height = font_height;
+    // ビットマップデータをアラインされたバッファにコピー
+    memcpy(aligned_bitmap_buffer, found_glyph + codepoint_size, bitmap_data_size);
+    gryph_data.bitmap = aligned_bitmap_buffer;
+
+    return &gryph_data;
+}
+
+// 文字列を描画する
+void PFNFont::draw_string(const char *str, int x, int y)
+{
+    const char *p = str;
+    while(*p != '\0') {
+        uint32_t codepoint = utf8_to_codepoint(&p);
+        font_gryph_t *gryph = get_gryph_data(codepoint);
+        if(gryph != NULL && draw_font_callback != NULL) {
+            draw_font_callback(gryph, x, y);
+            x += gryph->width;
+        }
+    }
+}

+ 79 - 0
jpfont_test/pfnfont.h

@@ -0,0 +1,79 @@
+#ifndef __PFNFONT
+#define __PFNFONT 1
+
+#include <stdint.h>
+
+#ifndef max
+#define max(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+typedef struct __attribute__((packed)) {
+    char    signature[7];       // シグニチャ、文字列PINOTFN固定
+    uint8_t version;            // バージョン(1)
+    char    fontname[8];        // フォント名(文字列shnmk14u)
+} pfn_font_header_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     width;          // フォントの幅
+    uint8_t     height;         // フォントの高さ
+    uint8_t     codepoint_size; // コードポイントのサイズ(1~4)
+    uint8_t     attribute;      // 予約(未使用)
+    uint16_t    num_glyphs;     // このブロックに含まれるグリフの数
+} pfn_block_header_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph1_t;
+
+typedef struct __attribute__((packed)) {
+    uint16_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph2_t;
+
+typedef struct __attribute__((packed)) {
+    uint32_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph4_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     width;          // フォントの幅
+    uint8_t     height;         // フォントの高さ
+    uint8_t     *bitmap;        // フォントのビットマップ
+} font_gryph_t;
+
+
+class PFNFont {
+ private:
+    uint8_t *font_data;     // フォントデータの先頭アドレス
+    size_t font_size;       // フォントデータのサイズ
+    uint8_t *first_block;    // 最初のフォントブロック
+    font_gryph_t gryph_data;        // get_gryph_dataが返すグリフデータ
+    uint8_t *aligned_bitmap_buffer; // グリフのビットマップ用バッファ
+    size_t bitmap_buffer_size;      // ビットマップバッファのサイズ
+    bool is_valid;                  // フォントデータが有効かどうかのフラグ
+
+    // コードポイントを含むフォントブロックを返す
+    uint8_t* find_glyph_in_block(uint32_t codepoint);
+    // グリフのコードポイントを返す
+    inline uint32_t get_codepoint_from_glyph(uint8_t *glyph_data, uint8_t codepoint_size);
+    // 文字列を描画するコールバック関数
+    void (*draw_font_callback)(font_gryph_t *,int x, int y);
+
+ public:
+    // PFNFontコンストラクタ
+    // uint8_t *pfn_font: pfnフォントデータの先頭アドレス
+    // size_t pfnfont_size: pfnフォントデータのサイズ
+    // void *draw_font(font_gryph_t *,int,int): 文字描画のコールバック関数
+    PFNFont(uint8_t *pfn_font, size_t pfnfont_size, void (*draw_font)(font_gryph_t *,int,int));
+    ~PFNFont();
+    // フォントが有効かチェックする
+    bool isValid() { return is_valid; }
+    // UTF-8をUTF-32コードポイントに変換
+    uint32_t utf8_to_codepoint(const char **utf8_str);
+    // コードポイントからフォントグリフのデータを得る
+    font_gryph_t *get_gryph_data(uint32_t codepoint);
+    // 文字列を描画する
+    void draw_string(const char *str, int x, int y);
+};
+#endif

+ 121 - 0
jpfont_test/pico_sdk_import.cmake

@@ -0,0 +1,121 @@
+# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
+
+# This can be dropped into an external project to help locate this SDK
+# It should be include()ed prior to project()
+
+# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
+#
+# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
+# following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
+# disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
+# disclaimer in the documentation and/or other materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
+# derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
+    set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
+    message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
+    set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
+    message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
+    set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
+    message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
+    set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
+    message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
+endif ()
+
+if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
+  set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
+  message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
+endif()
+
+set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
+set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
+set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
+set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
+
+if (NOT PICO_SDK_PATH)
+    if (PICO_SDK_FETCH_FROM_GIT)
+        include(FetchContent)
+        set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
+        if (PICO_SDK_FETCH_FROM_GIT_PATH)
+            get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
+        endif ()
+        FetchContent_Declare(
+                pico_sdk
+                GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+        )
+
+        if (NOT pico_sdk)
+            message("Downloading Raspberry Pi Pico SDK")
+            # GIT_SUBMODULES_RECURSE was added in 3.17
+            if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
+                FetchContent_Populate(
+                        pico_sdk
+                        QUIET
+                        GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                        GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+                        GIT_SUBMODULES_RECURSE FALSE
+
+                        SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
+                        BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
+                        SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
+                )
+            else ()
+                FetchContent_Populate(
+                        pico_sdk
+                        QUIET
+                        GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                        GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+
+                        SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
+                        BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
+                        SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
+                )
+            endif ()
+
+            set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
+        endif ()
+        set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
+    else ()
+        message(FATAL_ERROR
+                "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
+                )
+    endif ()
+endif ()
+
+get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
+if (NOT EXISTS ${PICO_SDK_PATH})
+    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
+endif ()
+
+set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
+if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
+    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
+endif ()
+
+set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
+
+include(${PICO_SDK_INIT_CMAKE_FILE})

BIN
jpfont_test/shnmk12u.pfn


BIN
jpfont_test/shnmk14u.pfn


BIN
jpfont_test/shnmk16u.pfn