From cfc055c978d98906a8090d092d4416cbfbd2bbce Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Mon, 22 Aug 2022 21:24:25 +0200 Subject: Package resources + a metadata file into a single .zip file. --- src/resources/CMakeLists.txt | 29 ++++ src/resources/fonts.json | 62 +++++++ src/resources/fonts/7segment.woff | Bin 0 -> 2124 bytes src/resources/fonts/BebasNeue-Regular.ttf | Bin 0 -> 60576 bytes src/resources/fonts/Teko-Light.ttf | Bin 0 -> 279608 bytes src/resources/fonts/repetitionscrolling.ttf | Bin 0 -> 42872 bytes src/resources/generate-fonts.py | 80 +++++++++ src/resources/generate-img.py | 56 ++++++ src/resources/generate-package.py | 72 ++++++++ src/resources/images.json | 9 + src/resources/images/pine_logo.png | Bin 0 -> 1808 bytes src/resources/images/pine_logo.svg | 253 ++++++++++++++++++++++++++++ src/resources/obsolete_files.json | 6 + 13 files changed, 567 insertions(+) create mode 100644 src/resources/CMakeLists.txt create mode 100644 src/resources/fonts.json create mode 100644 src/resources/fonts/7segment.woff create mode 100644 src/resources/fonts/BebasNeue-Regular.ttf create mode 100644 src/resources/fonts/Teko-Light.ttf create mode 100644 src/resources/fonts/repetitionscrolling.ttf create mode 100755 src/resources/generate-fonts.py create mode 100755 src/resources/generate-img.py create mode 100755 src/resources/generate-package.py create mode 100644 src/resources/images.json create mode 100644 src/resources/images/pine_logo.png create mode 100644 src/resources/images/pine_logo.svg create mode 100644 src/resources/obsolete_files.json (limited to 'src/resources') diff --git a/src/resources/CMakeLists.txt b/src/resources/CMakeLists.txt new file mode 100644 index 00000000..0983aaff --- /dev/null +++ b/src/resources/CMakeLists.txt @@ -0,0 +1,29 @@ + +find_program(LV_FONT_CONV "lv_font_conv" NO_CACHE REQUIRED + HINTS "${CMAKE_SOURCE_DIR}/node_modules/.bin") +message(STATUS "Using ${LV_FONT_CONV} to generate font files") + +find_program(LV_IMG_CONV "lv_img_conv" NO_CACHE REQUIRED + HINTS "${CMAKE_SOURCE_DIR}/node_modules/.bin") +message(STATUS "Using ${LV_IMG_CONV} to generate font files") + +if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12) + # FindPython3 module introduces with CMake 3.12 + # https://cmake.org/cmake/help/latest/module/FindPython3.html + find_package(Python3 REQUIRED) +else() + set(Python3_EXECUTABLE "python") +endif() + +# generate fonts +add_custom_target(GenerateResources + COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/generate-fonts.py --lv-font-conv "${LV_FONT_CONV}" ${CMAKE_CURRENT_SOURCE_DIR}/fonts.json + COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/generate-img.py --lv-img-conv "${LV_IMG_CONV}" ${CMAKE_CURRENT_SOURCE_DIR}/images.json + COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/generate-package.py --config ${CMAKE_CURRENT_SOURCE_DIR}/fonts.json --config ${CMAKE_CURRENT_SOURCE_DIR}/images.json --obsolete obsolete_files.json --output infinitime-resources-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}.zip + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/fonts.json + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/images.json + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + + COMMENT "Generate fonts and images for resource package" +) + diff --git a/src/resources/fonts.json b/src/resources/fonts.json new file mode 100644 index 00000000..a1545e34 --- /dev/null +++ b/src/resources/fonts.json @@ -0,0 +1,62 @@ +{ + "teko" : { + "sources": [ + { + "file": "fonts/Teko-Light.ttf", + "symbols": "0123456789:/amp" + } + ], + "bpp": 1, + "size": 28, + "format": "bin", + "target_path": "/fonts/" + }, + "bebas" : { + "sources": [ + { + "file": "fonts/BebasNeue-Regular.ttf", + "symbols": "0123456789:" + } + ], + "bpp": 1, + "size": 120, + "format": "bin", + "target_path": "/fonts/" + }, + "lv_font_dots_40": { + "sources": [ + { + "file": "fonts/repetitionscrolling.ttf", + "symbols": "0123456789-MONTUEWEDTHUFRISATSUN WK" + } + ], + "bpp": 1, + "size": 40, + "format": "bin", + "target_path": "/fonts/" + }, + "7segments_40" : { + "sources": [ + { + "file": "fonts/7segment.woff", + "symbols": "0123456789: -" + } + ], + "bpp": 1, + "size": 40, + "format": "bin", + "target_path": "/fonts" + }, + "7segments_115" : { + "sources": [ + { + "file": "fonts/7segment.woff", + "symbols": "0123456789: -" + } + ], + "bpp": 1, + "size": 115, + "format": "bin", + "target_path": "/fonts/" + } +} diff --git a/src/resources/fonts/7segment.woff b/src/resources/fonts/7segment.woff new file mode 100644 index 00000000..79ed9249 Binary files /dev/null and b/src/resources/fonts/7segment.woff differ diff --git a/src/resources/fonts/BebasNeue-Regular.ttf b/src/resources/fonts/BebasNeue-Regular.ttf new file mode 100644 index 00000000..76e22b8b Binary files /dev/null and b/src/resources/fonts/BebasNeue-Regular.ttf differ diff --git a/src/resources/fonts/Teko-Light.ttf b/src/resources/fonts/Teko-Light.ttf new file mode 100644 index 00000000..679f0137 Binary files /dev/null and b/src/resources/fonts/Teko-Light.ttf differ diff --git a/src/resources/fonts/repetitionscrolling.ttf b/src/resources/fonts/repetitionscrolling.ttf new file mode 100644 index 00000000..dc124164 Binary files /dev/null and b/src/resources/fonts/repetitionscrolling.ttf differ diff --git a/src/resources/generate-fonts.py b/src/resources/generate-fonts.py new file mode 100755 index 00000000..d6b47ab2 --- /dev/null +++ b/src/resources/generate-fonts.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python + +import io +import sys +import json +import shutil +import typing +import os.path +import argparse +import subprocess + +class Source(object): + def __init__(self, d): + self.file = d['file'] + if not os.path.exists(self.file): + self.file = os.path.join(os.path.dirname(sys.argv[0]), self.file) + self.range = d.get('range') + self.symbols = d.get('symbols') + + +def gen_lvconv_line(lv_font_conv: str, dest: str, size: int, bpp: int, format: str, sources: typing.List[Source], compress:bool=False): + if format != "lvgl" and format != "bin": + format = "lvgl" + + args = [lv_font_conv, '--size', str(size), '--output', dest, '--bpp', str(bpp), '--format', format] + if not compress: + args.append('--no-compress') + for source in sources: + args.extend(['--font', source.file]) + if source.range: + args.extend(['--range', source.range]) + if source.symbols: + args.extend(['--symbols', source.symbols]) + + return args + +def main(): + ap = argparse.ArgumentParser(description='auto generate LVGL font files from fonts') + ap.add_argument('config', type=str, help='config file to use') + ap.add_argument('-f', '--font', type=str, action='append', help='Choose specific fonts to generate (default: all)', default=[]) + ap.add_argument('--lv-font-conv', type=str, help='Path to "lv_font_conf" executable', default="lv_font_conv") + args = ap.parse_args() + + if not shutil.which(args.lv_font_conv): + sys.exit(f"Missing lv_font_conv. Make sure it's findable (in PATH) or specify it manually") + if not os.path.exists(args.config): + sys.exit(f'Error: the config file {args.config} does not exist.') + if not os.access(args.config, os.R_OK): + sys.exit(f'Error: the config file {args.config} is not accessible (permissions?).') + with open(args.config, 'r') as fd: + data = json.load(fd) + + fonts_to_run = set(data.keys()) + + if args.font: + enabled_fonts = set() + for font in args.font: + enabled_fonts.add(font[:-2] if font.endswith('.c') else font) + d = enabled_fonts.difference(fonts_to_run) + if d: + print(f'Warning: requested font{"s" if len(d)>1 else ""} missing: {" ".join(d)}') + fonts_to_run = fonts_to_run.intersection(enabled_fonts) + + for name in fonts_to_run: + font = data[name] + sources = font.pop('sources') + patches = font.pop('patches') if 'patches' in font else [] + font['sources'] = [Source(thing) for thing in sources] + extension = 'c' if font['format'] != 'bin' else 'bin' + font.pop('target_path') + line = gen_lvconv_line(args.lv_font_conv, f'{name}.{extension}', **font) + subprocess.check_call(line) + if patches: + for patch in patches: + subprocess.check_call(['/usr/bin/env', 'patch', name+'.'+extension, patch]) + + + +if __name__ == '__main__': + main() diff --git a/src/resources/generate-img.py b/src/resources/generate-img.py new file mode 100755 index 00000000..cdbfc030 --- /dev/null +++ b/src/resources/generate-img.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +import io +import sys +import json +import shutil +import typing +import os.path +import argparse +import subprocess + +def gen_lvconv_line(lv_img_conv: str, dest: str, color_format: str, output_format: str, binary_format: str, sources: str): + args = [lv_img_conv, sources, '--force', '--output-file', dest, '--color-format', color_format, '--output-format', output_format, '--binary-format', binary_format] + + return args + +def main(): + ap = argparse.ArgumentParser(description='auto generate LVGL font files from fonts') + ap.add_argument('config', type=str, help='config file to use') + ap.add_argument('-i', '--image', type=str, action='append', help='Choose specific images to generate (default: all)', default=[]) + ap.add_argument('--lv-img-conv', type=str, help='Path to "lv_img_conf" executable', default="lv_img_conv") + args = ap.parse_args() + + if not shutil.which(args.lv_img_conv): + sys.exit(f"Missing lv_img_conv. Make sure it's findable (in PATH) or specify it manually") + if not os.path.exists(args.config): + sys.exit(f'Error: the config file {args.config} does not exist.') + if not os.access(args.config, os.R_OK): + sys.exit(f'Error: the config file {args.config} is not accessible (permissions?).') + with open(args.config, 'r') as fd: + data = json.load(fd) + + images_to_run = set(data.keys()) + + if args.image: + enabled_images = set() + for image in args.image: + enabled_images.add(image[:-2] if image.endswith('.c') else image) + d = enabled_images.difference(images_to_run) + if d: + print(f'Warning: requested image{"s" if len(d)>1 else ""} missing: {" ".join(d)}') + images_to_run = images_to_run.intersection(enabled_images) + + for name in images_to_run: + image = data[name] + if not os.path.exists(image['sources']): + image['sources'] = os.path.join(os.path.dirname(sys.argv[0]), image['sources']) + extension = 'bin' + image.pop('target_path') + line = gen_lvconv_line(args.lv_img_conv, f'{name}.{extension}', **image) + subprocess.check_call(line) + + + +if __name__ == '__main__': + main() diff --git a/src/resources/generate-package.py b/src/resources/generate-package.py new file mode 100755 index 00000000..ff02d4fe --- /dev/null +++ b/src/resources/generate-package.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +import io +import sys +import json +import shutil +import typing +import os.path +import argparse +import subprocess +from zipfile import ZipFile + +def main(): + ap = argparse.ArgumentParser(description='auto generate LVGL font files from fonts') + ap.add_argument('--config', '-c', type=str, action='append', help='config file to use') + ap.add_argument('--obsolete', type=str, help='List of obsolete files') + ap.add_argument('--output', type=str, help='output file name') + args = ap.parse_args() + + for config_file in args.config: + if not os.path.exists(config_file): + sys.exit(f'Error: the config file {config_file} does not exist.') + if not os.access(config_file, os.R_OK): + sys.exit(f'Error: the config file {config_file} is not accessible (permissions?).') + + if args.obsolete: + obsolete_file_path = os.path.join(os.path.dirname(sys.argv[0]), args.obsolete) + if not os.path.exists(obsolete_file_path): + sys.exit(f'Error: the "obsolete" file {args.obsolete} does not exist.') + if not os.access(obsolete_file_path, os.R_OK): + sys.exit(f'Error: the "obsolete" file {args.obsolete} is not accessible (permissions?).') + + zf = ZipFile(args.output, mode='w') + resource_files = [] + + for config_file in args.config: + with open(config_file, 'r') as fd: + data = json.load(fd) + + resource_names = set(data.keys()) + for name in resource_names: + resource = data[name] + resource_files.append({ + "filename": name+'.bin', + "path": resource['target_path'] + name+'.bin' + }) + + path = name + '.bin' + if not os.path.exists(path): + path = os.path.join(os.path.dirname(sys.argv[0]), path) + zf.write(path) + + if args.obsolete: + obsolete_file_path = os.path.join(os.path.dirname(sys.argv[0]), args.obsolete) + with open(obsolete_file_path, 'r') as fd: + obsolete_data = json.load(fd) + else: + obsolete_data = {} + output = { + 'resources': resource_files, + 'obsolete_files': obsolete_data + } + + + with open("resources.json", 'w') as fd: + json.dump(output, fd, indent=4) + + zf.write('resources.json') + zf.close() + +if __name__ == '__main__': + main() diff --git a/src/resources/images.json b/src/resources/images.json new file mode 100644 index 00000000..764747ca --- /dev/null +++ b/src/resources/images.json @@ -0,0 +1,9 @@ +{ + "pine_small" : { + "sources": "images/pine_logo.png", + "color_format": "CF_TRUE_COLOR", + "output_format": "bin", + "binary_format": "ARGB8565_RBSWAP", + "target_path": "/images/" + } +} diff --git a/src/resources/images/pine_logo.png b/src/resources/images/pine_logo.png new file mode 100644 index 00000000..aa96be4b Binary files /dev/null and b/src/resources/images/pine_logo.png differ diff --git a/src/resources/images/pine_logo.svg b/src/resources/images/pine_logo.svg new file mode 100644 index 00000000..55f21169 --- /dev/null +++ b/src/resources/images/pine_logo.svg @@ -0,0 +1,253 @@ + + + + + + + + + + + + + + + diff --git a/src/resources/obsolete_files.json b/src/resources/obsolete_files.json new file mode 100644 index 00000000..6109ace7 --- /dev/null +++ b/src/resources/obsolete_files.json @@ -0,0 +1,6 @@ +[ + { + "path": "/example-of-obsolete-file.bin", + "since": "1.11.0" + } +] \ No newline at end of file -- cgit v1.2.3-70-g09d2 From cbe2f08cf7e9cd7bbfe3162e254db9648460ae58 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sat, 3 Sep 2022 20:11:44 +0200 Subject: Fix missing '/' in the path of a font (external resource) --- src/resources/fonts.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/resources') diff --git a/src/resources/fonts.json b/src/resources/fonts.json index a1545e34..55882c3d 100644 --- a/src/resources/fonts.json +++ b/src/resources/fonts.json @@ -45,7 +45,7 @@ "bpp": 1, "size": 40, "format": "bin", - "target_path": "/fonts" + "target_path": "/fonts/" }, "7segments_115" : { "sources": [ -- cgit v1.2.3-70-g09d2 From e2a3d9f0c7a531d129501325e793fdd634babba8 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 11 Sep 2022 12:43:30 +0200 Subject: Small improvement in generate-fonts.py following code review. Add lv_img_conf in Docker image to build resources at build time. --- CMakeLists.txt | 9 +++++++++ docker/Dockerfile | 10 ++++++++++ docker/build.sh | 7 ++++++- docker/post_build.sh.in | 2 ++ src/CMakeLists.txt | 12 +++++++++++- src/resources/generate-fonts.py | 2 +- 6 files changed, 39 insertions(+), 3 deletions(-) (limited to 'src/resources') diff --git a/CMakeLists.txt b/CMakeLists.txt index be8ef41f..1da460c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,10 @@ if(BUILD_DFU) set(BUILD_DFU true) endif() +if(BUILD_RESOURCES) + set(BUILD_RESOURCES true) +endif() + set(TARGET_DEVICE "PINETIME" CACHE STRING "Target device") set_property(CACHE TARGET_DEVICE PROPERTY STRINGS PINETIME MOY-TFK5 MOY-TIN5 MOY-TON5 MOY-UNK) @@ -98,6 +102,11 @@ if(BUILD_DFU) else() message(" * Build DFU (using adafruit-nrfutil) : Disabled") endif() +if(BUILD_RESOURCES) + message(" * Build resources : Enabled") +else() + message(" * Build resources : Disabled") +endif() set(VERSION_EDIT_WARNING "// Do not edit this file, it is automatically generated by CMAKE!") configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/Version.h) diff --git a/docker/Dockerfile b/docker/Dockerfile index f2d187d0..927160db 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -22,6 +22,12 @@ RUN apt-get update -qq \ python3-dev \ git \ apt-utils \ + pkg-config \ + libpixman-1-dev \ + libcairo2-dev \ + libpango-1.0-0 \ + ibpango1.0-dev \ + libpangocairo-1.0-0 \ && curl -sL https://deb.nodesource.com/setup_18.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/cache/apt/* /var/lib/apt/lists/*; @@ -33,6 +39,10 @@ RUN pip3 install -Iv cryptography==3.3 RUN pip3 install cbor RUN npm i lv_font_conv@1.5.2 -g +RUN npm i ts-node@10.9.1 -g +RUN npm i @swc/core -g +RUN npm i lv_img_conv@0.3.0 -g + # build.sh knows how to compile COPY build.sh /opt/ diff --git a/docker/build.sh b/docker/build.sh index 663390c4..58dcccc1 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -11,6 +11,10 @@ export SOURCES_DIR="${SOURCES_DIR:=/sources}" export BUILD_DIR="${BUILD_DIR:=$SOURCES_DIR/build}" export OUTPUT_DIR="${OUTPUT_DIR:=$SOURCES_DIR/build/output}" +# Specify a folder with read/write access to NPM +export NPM_DIR="$BUILD_DIR/npm" +export npm_config_cache="${NPM_DIR}" + export BUILD_TYPE=${BUILD_TYPE:=Release} export GCC_ARM_VER=${GCC_ARM_VER:="10.3-2021.10"} export NRF_SDK_VER=${NRF_SDK_VER:="nRF5_SDK_15.3.0_59ac345"} @@ -62,7 +66,8 @@ CmakeGenerate() { -DUSE_OPENOCD=1 \ -DARM_NONE_EABI_TOOLCHAIN_PATH="$TOOLS_DIR/$GCC_ARM_PATH" \ -DNRF5_SDK_PATH="$TOOLS_DIR/$NRF_SDK_VER" \ - -DBUILD_DFU=1 + -DBUILD_DFU=1 \ + -DBUILD_RESOURCES=1 } CmakeBuild() { diff --git a/docker/post_build.sh.in b/docker/post_build.sh.in index 8c94471a..5d82f3be 100755 --- a/docker/post_build.sh.in +++ b/docker/post_build.sh.in @@ -15,6 +15,8 @@ cp "$BUILD_DIR/src/pinetime-mcuboot-app-dfu-$PROJECT_VERSION.zip" "$OUTPUT_DIR/p cp "$BUILD_DIR/src/pinetime-mcuboot-recovery-loader-image-$PROJECT_VERSION.bin" "$OUTPUT_DIR/pinetime-mcuboot-recovery-loader-image-$PROJECT_VERSION.bin" cp "$BUILD_DIR/src/pinetime-mcuboot-recovery-loader-dfu-$PROJECT_VERSION.zip" "$OUTPUT_DIR/pinetime-mcuboot-recovery-loader-dfu-$PROJECT_VERSION.zip" +cp "$BUILD_DIR/src/resources/infinitime-resources-$PROJECT_VERSION.zip" "$OUTPUT_DIR/infinitime-resources-$PROJECT_VERSION.zip" + mkdir -p "$OUTPUT_DIR/src" cp $BUILD_DIR/src/*.bin "$OUTPUT_DIR/src/" cp $BUILD_DIR/src/*.hex "$OUTPUT_DIR/src/" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f81e7026..92f309a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -940,6 +940,10 @@ add_custom_command(TARGET ${EXECUTABLE_NAME} COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_FILE_NAME}.out "${EXECUTABLE_FILE_NAME}.hex" COMMENT "post build steps for ${EXECUTABLE_FILE_NAME}") +if(BUILD_RESOURCES) + add_dependencies(${EXECUTABLE_NAME} GenerateResources) +endif() + # Build binary intended to be used by bootloader set(EXECUTABLE_MCUBOOT_NAME "pinetime-mcuboot-app") set(EXECUTABLE_MCUBOOT_FILE_NAME ${EXECUTABLE_MCUBOOT_NAME}-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}) @@ -973,6 +977,10 @@ add_custom_command(TARGET ${EXECUTABLE_MCUBOOT_NAME} COMMENT "post build steps for ${EXECUTABLE_MCUBOOT_FILE_NAME}" ) +if(BUILD_RESOURCES) + add_dependencies(${EXECUTABLE_MCUBOOT_NAME} GenerateResources) +endif() + if(BUILD_DFU) add_custom_command(TARGET ${EXECUTABLE_MCUBOOT_NAME} POST_BUILD @@ -1127,7 +1135,9 @@ if(BUILD_DFU) ) endif() -add_subdirectory(resources) +if(BUILD_RESOURCES) + add_subdirectory(resources) +endif() # FLASH diff --git a/src/resources/generate-fonts.py b/src/resources/generate-fonts.py index d6b47ab2..20408166 100755 --- a/src/resources/generate-fonts.py +++ b/src/resources/generate-fonts.py @@ -20,7 +20,7 @@ class Source(object): def gen_lvconv_line(lv_font_conv: str, dest: str, size: int, bpp: int, format: str, sources: typing.List[Source], compress:bool=False): if format != "lvgl" and format != "bin": - format = "lvgl" + format = "bin" if dest.lower().endswith(".bin") else "lvgl" args = [lv_font_conv, '--size', str(size), '--output', dest, '--bpp', str(bpp), '--format', format] if not compress: -- cgit v1.2.3-70-g09d2