Skip to content

Commit

Permalink
screen: Introduce CJK font
Browse files Browse the repository at this point in the history
  • Loading branch information
xingrz committed Feb 18, 2023
1 parent 14ffcfa commit 9c1cbf8
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ jobs:
- name: Set up build tools
run: |
apt-get update
apt-get install -y python3-pip protobuf-compiler
apt-get install -y python3-pip protobuf-compiler curl
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt-get install -y nodejs
pip install -U fonttools
npm install -g lv_font_conv
- name: Cache west modules
uses: actions/[email protected]
Expand Down
6 changes: 5 additions & 1 deletion config/boards/arm/hw75_dynamic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ HW-75 Dynamic

```sh
sudo apt-get update
sudo apt-get install -y python3-pip protobuf-compiler
sudo apt-get install -y python3-pip protobuf-compiler curl
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo pip install -U fonttools
sudo npm install -g lv_font_conv
```

### 编译
Expand Down
13 changes: 11 additions & 2 deletions config/boards/arm/hw75_dynamic/app/screen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ zephyr_library_include_directories(${ZEPHYR_LVGL_MODULE_DIR})
zephyr_library_include_directories(${ZEPHYR_BASE}/lib/gui/lvgl)
zephyr_library_include_directories(${APPLICATION_SOURCE_DIR}/include)

include(${ZMK_CONFIG}/cmake/ui_strings.cmake)

generate_ui_strings(strings.csv
OUTPUT_DEFS STRINGS_SRC
OUTPUT_TEXT STRINGS_TXT
)

zephyr_library_sources(${STRINGS_SRC})
zephyr_library_include_directories(${CMAKE_CURRENT_BINARY_DIR})

zephyr_library_sources(layer_status.c)
zephyr_library_sources(status_screen.c)

zephyr_library_sources(fonts/icons_19.c)
zephyr_library_compile_options(-DLV_LVGL_H_INCLUDE_SIMPLE)
add_subdirectory(fonts)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

add_subdirectory(zfull_9)

zephyr_library_sources(icons_19.c)
zephyr_compile_options(-DLV_LVGL_H_INCLUDE_SIMPLE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

zephyr_library()

set(FONT_SRC Zfull-GB.ttf)

set(FONT_NAME zfull)
set(FONT_SIZE 9)

include(${ZMK_CONFIG}/cmake/font_subset.cmake)
font_subset(FONT_SUBSET_TTF ${FONT_SRC} ${STRINGS_TXT})

include(${ZMK_CONFIG}/cmake/lv_font_conv.cmake)
lv_font_conv(FONT_C_SRCS ${FONT_SUBSET_TTF}
NAME ${FONT_NAME}
SIZE ${FONT_SIZE}
BPP 1
NO_PREFILTER
NO_KERNING
)

zephyr_library_sources(${FONT_C_SRCS})
zephyr_library_include_directories(${ZEPHYR_LVGL_MODULE_DIR})
zephyr_library_include_directories(${ZEPHYR_BASE}/lib/gui/lvgl)
Binary file not shown.
2 changes: 2 additions & 0 deletions config/boards/arm/hw75_dynamic/app/screen/layer_status.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#define LAYER_LABEL(node) COND_CODE_0(DT_NODE_HAS_PROP(node, label), (NULL), (DT_LABEL(node))),

LV_FONT_DECLARE(icons_19);
LV_FONT_DECLARE(zfull_9);

#define UI_SYMBOL_VOLUME "\xEF\x80\xA8"
#define UI_SYMBOL_BRIGHTNESS "\xEF\x86\x85"
Expand Down Expand Up @@ -93,6 +94,7 @@ int layer_status_init(lv_obj_t *parent, lv_group_t *group)
lv_style_init(&st_name);
lv_style_set_pad_ver(&st_name, LV_STATE_DEFAULT, 4);
lv_style_set_pad_hor(&st_name, LV_STATE_DEFAULT, 0);
lv_style_set_text_font(&st_name, LV_STATE_DEFAULT, &zfull_9);

lv_style_init(&st_list);
lv_style_set_radius(&st_list, LV_STATE_DEFAULT, 0);
Expand Down
3 changes: 3 additions & 0 deletions config/boards/arm/hw75_dynamic/app/screen/strings.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
STRING_LAYER_VOL,音量
STRING_LAYER_BRI,亮度
STRING_LAYER_SCR,滚动
29 changes: 29 additions & 0 deletions config/cmake/font_subset.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

function(FONT_SUBSET OUT SRC TEXT)
file(REAL_PATH ${SRC} FONT_FILE)
file(REAL_PATH ${TEXT} TEXT_FILE)

get_filename_component(FONT_NAME ${SRC} NAME_WE)
get_filename_component(FONT_EXT ${SRC} EXT)

set(OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${FONT_NAME}_subset${FONT_EXT})
set(${OUT} ${OUTPUT_FILE} PARENT_SCOPE)

add_custom_command(
OUTPUT ${OUTPUT_FILE}
COMMAND pyftsubset
ARGS ${FONT_FILE}
--text-file=${TEXT_FILE}
--output-file=${OUTPUT_FILE}
DEPENDS ${FONT_FILE}
${TEXT_FILE}
COMMENT "Generating subset font: ${FONT_NAME}_subset${FONT_EXT}"
)

set_source_files_properties(
${OUTPUT_FILE}
PROPERTIES GENERATED TRUE
)
endfunction()
59 changes: 59 additions & 0 deletions config/cmake/lv_font_conv.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

function(LV_FONT_CONV OUT SRC)
set(options NO_COMPRESS NO_PREFILTER NO_KERNING)
set(one_value_keywords NAME SIZE BPP)
set(multi_value_keywords RANGE)
cmake_parse_arguments(LV_FONT_CONV
"${options}"
"${one_value_keywords}"
"${multi_value_keywords}"
${ARGN}
)

file(REAL_PATH ${SRC} FONT_SRC)

set(FONT_OUT ${CMAKE_CURRENT_BINARY_DIR}/${LV_FONT_CONV_NAME}_${LV_FONT_CONV_SIZE}.c)
set(${OUT} ${FONT_OUT} PARENT_SCOPE)

set(LV_FONT_CONV_OPTS)

if(LV_FONT_CONV_NO_COMPRESS)
list(APPEND LV_FONT_CONV_OPTS "--no-compress")
endif()

if(LV_FONT_CONV_NO_PREFILTER)
list(APPEND LV_FONT_CONV_OPTS "--no-prefilter")
endif()

if(LV_FONT_CONV_NO_KERNING)
list(APPEND LV_FONT_CONV_OPTS "--no-kerning")
endif()

if (LV_FONT_CONV_RANGE)
string(JOIN "," LV_FONT_CONV_RANGE ${LV_FONT_CONV_RANGE})
else()
set(LV_FONT_CONV_RANGE "0x0000-0xFFFF")
endif()

add_custom_command(
OUTPUT ${FONT_OUT}
COMMAND lv_font_conv
ARGS --size ${FONT_SIZE}
--output "${FONT_OUT}"
--bpp ${LV_FONT_CONV_BPP}
--format lvgl
--font "${FONT_SRC}"
--range ${LV_FONT_CONV_RANGE}
${LV_FONT_CONV_OPTS}
DEPENDS ${FONT_SRC}
${LV_FONT_CONV_RANGE_FILE}
COMMENT "Generating font in LVGL format: ${FONT_NAME}_${FONT_SIZE}.c"
)

set_source_files_properties(
${FONT_OUT}
PROPERTIES GENERATED TRUE
)
endfunction()
40 changes: 40 additions & 0 deletions config/cmake/ui_strings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2022-2023 XiNGRZ
# SPDX-License-Identifier: MIT

function(GENERATE_UI_STRINGS INPUT)
set(options)
set(one_value_keywords OUTPUT_DEFS OUTPUT_TEXT)
set(multi_value_keywords)
cmake_parse_arguments(UI_STRINGS
"${options}"
"${one_value_keywords}"
"${multi_value_keywords}"
${ARGN}
)

file(REAL_PATH ${INPUT} INPUT)

get_filename_component(NAME ${INPUT} NAME_WE)

set(DEFS_FILE ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h)
set(TEXT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.txt)

add_custom_command(
OUTPUT ${DEFS_FILE} ${TEXT_FILE}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ui_strings.py
ARGS --input ${INPUT}
--output-defs ${DEFS_FILE}
--output-text ${TEXT_FILE}
DEPENDS ${INPUT}
COMMENT "Generating UI strings: ${NAME}.h, ${NAME}.txt"
)

set_source_files_properties(
${DEFS_FILE}
${TEXT_FILE}
PROPERTIES GENERATED TRUE
)

set(${UI_STRINGS_OUTPUT_DEFS} ${DEFS_FILE} PARENT_SCOPE)
set(${UI_STRINGS_OUTPUT_TEXT} ${TEXT_FILE} PARENT_SCOPE)
endfunction()
20 changes: 20 additions & 0 deletions config/cmake/ui_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import argparse
import csv

def main():
parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, required=True)
parser.add_argument('--output-defs', type=str, required=True)
parser.add_argument('--output-text', type=str, required=True)
args = parser.parse_args()

with open(args.input, 'r') as f_in, open(args.output_defs, 'w') as f_od, open(args.output_text, 'w') as f_ot:
reader = csv.reader(f_in)
for row in reader:
key, value = row
text = value.strip().replace('\\n', '')
f_od.write(f'#define {key} "{value}"\n')
f_ot.write(f'{text}\n')

if __name__ == '__main__':
main()
6 changes: 3 additions & 3 deletions config/hw75_dynamic.keymap
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
keymap {
compatible = "zmk,keymap";
layer_0 {
label = "VOL";
label = "音量";
bindings = <&lvkp LV_KEY_PREV &lvkp LV_KEY_NEXT>;
sensor-bindings = <&inc_dec_kp C_VOL_DN C_VOL_UP>;
};

layer_1 {
label = "BRI";
label = "亮度";
bindings = <&trans &trans>;
sensor-bindings = <&inc_dec_kp C_BRI_DEC C_BRI_INC>;
};

layer_2 {
label = "SCR";
label = "滚动";
bindings = <&trans &trans>;
sensor-bindings = <&mw MW_UP(1) MW_DN(1)>;
};
Expand Down

0 comments on commit 9c1cbf8

Please sign in to comment.