initial version of custom fonts
This commit is contained in:
@@ -135,3 +135,56 @@ ruby -rdigest -e 'puts [
|
||||
"./notosans_8_regular.h",
|
||||
].map{|f| Digest::SHA256.hexdigest(File.read(f)).to_i(16) }.sum % (2 ** 32) - (2 ** 31)'
|
||||
))"
|
||||
|
||||
# Custom font sizes (must match convert-builtin-fonts.sh)
|
||||
CUSTOM_FONT_SIZES=(12 14 16 18)
|
||||
|
||||
# Generate font IDs for all custom fonts
|
||||
echo ""
|
||||
echo "// Custom font IDs"
|
||||
|
||||
CUSTOM_DIR="./custom"
|
||||
declare -a CUSTOM_FONT_NAMES=()
|
||||
declare -a CUSTOM_FONT_UPPERCASE=()
|
||||
|
||||
if [ -d "$CUSTOM_DIR" ]; then
|
||||
for font_dir in "$CUSTOM_DIR"/*/; do
|
||||
if [ -d "$font_dir" ]; then
|
||||
font_folder_name=$(basename "$font_dir")
|
||||
font_name_lower=$(echo "$font_folder_name" | tr '[:upper:]' '[:lower:]')
|
||||
font_name_upper=$(echo "$font_folder_name" | tr '[:lower:]' '[:upper:]')
|
||||
|
||||
# Check if font headers exist (Regular is required)
|
||||
if [ -f "./custom/${font_name_lower}_12_regular.h" ]; then
|
||||
CUSTOM_FONT_NAMES+=("$font_name_lower")
|
||||
CUSTOM_FONT_UPPERCASE+=("$font_name_upper")
|
||||
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
font_id=$(ruby -rdigest -e "puts [
|
||||
\"./custom/${font_name_lower}_${size}_regular.h\",
|
||||
\"./custom/${font_name_lower}_${size}_bold.h\",
|
||||
\"./custom/${font_name_lower}_${size}_bolditalic.h\",
|
||||
\"./custom/${font_name_lower}_${size}_italic.h\",
|
||||
].map{|f| Digest::SHA256.hexdigest(File.read(f)).to_i(16) }.sum % (2 ** 32) - (2 ** 31)")
|
||||
echo "#define ${font_name_upper}_${size}_FONT_ID ($font_id)"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Generate CUSTOM_FONT_IDS lookup array
|
||||
echo ""
|
||||
echo "// Custom font ID lookup array: CUSTOM_FONT_IDS[fontIndex][sizeIndex]"
|
||||
echo "// Size indices: 0=12pt, 1=14pt, 2=16pt, 3=18pt"
|
||||
|
||||
if [ ${#CUSTOM_FONT_NAMES[@]} -gt 0 ]; then
|
||||
echo "static const int CUSTOM_FONT_IDS[][4] = {"
|
||||
for i in "${!CUSTOM_FONT_UPPERCASE[@]}"; do
|
||||
font_upper="${CUSTOM_FONT_UPPERCASE[$i]}"
|
||||
echo " {${font_upper}_12_FONT_ID, ${font_upper}_14_FONT_ID, ${font_upper}_16_FONT_ID, ${font_upper}_18_FONT_ID},"
|
||||
done
|
||||
echo "};"
|
||||
else
|
||||
echo "static const int CUSTOM_FONT_IDS[][4] = {};"
|
||||
fi
|
||||
|
||||
@@ -9,6 +9,9 @@ BOOKERLY_FONT_SIZES=(12 14 16 18)
|
||||
NOTOSANS_FONT_SIZES=(12 14 16 18)
|
||||
OPENDYSLEXIC_FONT_SIZES=(8 10 12 14)
|
||||
|
||||
# Custom font sizes - modify this array to change sizes for user-provided fonts
|
||||
CUSTOM_FONT_SIZES=(12 14 16 18)
|
||||
|
||||
for size in ${BOOKERLY_FONT_SIZES[@]}; do
|
||||
for style in ${READER_FONT_STYLES[@]}; do
|
||||
font_name="bookerly_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
||||
@@ -53,3 +56,209 @@ for size in ${UI_FONT_SIZES[@]}; do
|
||||
done
|
||||
|
||||
python fontconvert.py notosans_8_regular 8 ../builtinFonts/source/NotoSans/NotoSans-Regular.ttf > ../builtinFonts/notosans_8_regular.h
|
||||
|
||||
# ============================================================================
|
||||
# Custom Fonts Processing
|
||||
# ============================================================================
|
||||
# Process all custom fonts in the custom/ folder
|
||||
# Each subfolder should contain TTF files named: FontName-Regular.ttf, FontName-Bold.ttf, etc.
|
||||
|
||||
CUSTOM_DIR="../builtinFonts/custom"
|
||||
CUSTOM_HEADER="../builtinFonts/custom/customFonts.h"
|
||||
|
||||
# Collect custom font names
|
||||
declare -a CUSTOM_FONT_NAMES=()
|
||||
declare -a CUSTOM_FONT_LOWERCASE=()
|
||||
|
||||
if [ -d "$CUSTOM_DIR" ]; then
|
||||
for font_dir in "$CUSTOM_DIR"/*/; do
|
||||
if [ -d "$font_dir" ]; then
|
||||
# Get the font folder name (e.g., "FernMicro")
|
||||
font_folder_name=$(basename "$font_dir")
|
||||
font_name_lower=$(echo "$font_folder_name" | tr '[:upper:]' '[:lower:]')
|
||||
|
||||
# Check if Regular font exists (required)
|
||||
regular_font=$(find "$font_dir" -maxdepth 1 -iname "*-Regular.ttf" -o -iname "*-Regular.otf" 2>/dev/null | head -1)
|
||||
if [ -z "$regular_font" ]; then
|
||||
echo "Warning: Skipping $font_folder_name - no Regular font found"
|
||||
continue
|
||||
fi
|
||||
|
||||
CUSTOM_FONT_NAMES+=("$font_folder_name")
|
||||
CUSTOM_FONT_LOWERCASE+=("$font_name_lower")
|
||||
|
||||
echo "Processing custom font: $font_folder_name"
|
||||
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
for style in ${READER_FONT_STYLES[@]}; do
|
||||
style_lower=$(echo $style | tr '[:upper:]' '[:lower:]')
|
||||
output_name="${font_name_lower}_${size}_${style_lower}"
|
||||
output_path="../builtinFonts/custom/${output_name}.h"
|
||||
|
||||
# Find the font file for this style (try TTF then OTF)
|
||||
font_file=$(find "$font_dir" -maxdepth 1 -iname "*-${style}.ttf" -o -iname "*-${style}.otf" 2>/dev/null | head -1)
|
||||
|
||||
if [ -n "$font_file" ]; then
|
||||
python fontconvert.py "$output_name" "$size" "$font_file" --2bit > "$output_path"
|
||||
echo "Generated $output_path"
|
||||
else
|
||||
# If style not found, use Regular as fallback
|
||||
echo "Note: $font_folder_name-${style} not found, using Regular"
|
||||
python fontconvert.py "$output_name" "$size" "$regular_font" --2bit > "$output_path"
|
||||
echo "Generated $output_path (fallback from Regular)"
|
||||
fi
|
||||
done
|
||||
done
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Generate customFonts.h registry (header with extern declarations)
|
||||
echo "Generating customFonts.h registry..."
|
||||
|
||||
CUSTOM_CPP="../../../src/customFonts.cpp"
|
||||
|
||||
cat > "$CUSTOM_HEADER" << 'HEADER_START'
|
||||
/**
|
||||
* Generated by convert-builtin-fonts.sh
|
||||
* Registry of available custom fonts
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <EpdFont.h>
|
||||
#include <EpdFontFamily.h>
|
||||
|
||||
class GfxRenderer;
|
||||
|
||||
HEADER_START
|
||||
|
||||
# Write the count
|
||||
echo "#define CUSTOM_FONT_COUNT ${#CUSTOM_FONT_NAMES[@]}" >> "$CUSTOM_HEADER"
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
|
||||
# Write font names array
|
||||
if [ ${#CUSTOM_FONT_NAMES[@]} -gt 0 ]; then
|
||||
echo "static const char* CUSTOM_FONT_NAMES[] = {" >> "$CUSTOM_HEADER"
|
||||
for i in "${!CUSTOM_FONT_NAMES[@]}"; do
|
||||
if [ $i -lt $((${#CUSTOM_FONT_NAMES[@]} - 1)) ]; then
|
||||
echo " \"${CUSTOM_FONT_NAMES[$i]}\"," >> "$CUSTOM_HEADER"
|
||||
else
|
||||
echo " \"${CUSTOM_FONT_NAMES[$i]}\"" >> "$CUSTOM_HEADER"
|
||||
fi
|
||||
done
|
||||
echo "};" >> "$CUSTOM_HEADER"
|
||||
else
|
||||
echo "static const char* CUSTOM_FONT_NAMES[] = {};" >> "$CUSTOM_HEADER"
|
||||
fi
|
||||
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
|
||||
# Include all generated headers in the header file
|
||||
echo "// Include all custom font headers" >> "$CUSTOM_HEADER"
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
for style in ${READER_FONT_STYLES[@]}; do
|
||||
style_lower=$(echo $style | tr '[:upper:]' '[:lower:]')
|
||||
echo "#include <builtinFonts/custom/${font_name_lower}_${size}_${style_lower}.h>" >> "$CUSTOM_HEADER"
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
|
||||
# Generate extern declarations for EpdFont and EpdFontFamily in header
|
||||
if [ ${#CUSTOM_FONT_NAMES[@]} -gt 0 ]; then
|
||||
echo "// Extern EpdFont declarations for custom fonts" >> "$CUSTOM_HEADER"
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
for style in ${READER_FONT_STYLES[@]}; do
|
||||
var_name="${font_name_lower}${size}${style}Font"
|
||||
echo "extern EpdFont ${var_name};" >> "$CUSTOM_HEADER"
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
echo "// Extern EpdFontFamily declarations for custom fonts" >> "$CUSTOM_HEADER"
|
||||
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
family_name="${font_name_lower}${size}FontFamily"
|
||||
echo "extern EpdFontFamily ${family_name};" >> "$CUSTOM_HEADER"
|
||||
done
|
||||
done
|
||||
fi
|
||||
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
|
||||
# Function declaration in header
|
||||
echo "// Function to register all custom fonts with the renderer" >> "$CUSTOM_HEADER"
|
||||
echo "void registerCustomFonts(GfxRenderer& renderer);" >> "$CUSTOM_HEADER"
|
||||
echo "" >> "$CUSTOM_HEADER"
|
||||
|
||||
# Generate the .cpp file with actual definitions
|
||||
cat > "$CUSTOM_CPP" << 'CPP_START'
|
||||
/**
|
||||
* Generated by convert-builtin-fonts.sh
|
||||
* Custom font definitions
|
||||
*/
|
||||
#include <builtinFonts/custom/customFonts.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include "fontIds.h"
|
||||
|
||||
CPP_START
|
||||
|
||||
# Generate EpdFont definitions in .cpp
|
||||
if [ ${#CUSTOM_FONT_NAMES[@]} -gt 0 ]; then
|
||||
echo "// EpdFont definitions for custom fonts" >> "$CUSTOM_CPP"
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
for style in ${READER_FONT_STYLES[@]}; do
|
||||
style_lower=$(echo $style | tr '[:upper:]' '[:lower:]')
|
||||
var_name="${font_name_lower}${size}${style}Font"
|
||||
data_name="${font_name_lower}_${size}_${style_lower}"
|
||||
echo "EpdFont ${var_name}(&${data_name});" >> "$CUSTOM_CPP"
|
||||
done
|
||||
done
|
||||
done
|
||||
|
||||
echo "" >> "$CUSTOM_CPP"
|
||||
echo "// EpdFontFamily definitions for custom fonts" >> "$CUSTOM_CPP"
|
||||
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
family_name="${font_name_lower}${size}FontFamily"
|
||||
regular="${font_name_lower}${size}RegularFont"
|
||||
bold="${font_name_lower}${size}BoldFont"
|
||||
italic="${font_name_lower}${size}ItalicFont"
|
||||
bolditalic="${font_name_lower}${size}BoldItalicFont"
|
||||
echo "EpdFontFamily ${family_name}(&${regular}, &${bold}, &${italic}, &${bolditalic});" >> "$CUSTOM_CPP"
|
||||
done
|
||||
done
|
||||
fi
|
||||
|
||||
echo "" >> "$CUSTOM_CPP"
|
||||
|
||||
# Generate registerCustomFonts function in .cpp
|
||||
echo "void registerCustomFonts(GfxRenderer& renderer) {" >> "$CUSTOM_CPP"
|
||||
|
||||
if [ ${#CUSTOM_FONT_NAMES[@]} -gt 0 ]; then
|
||||
echo "#if CUSTOM_FONT_COUNT > 0" >> "$CUSTOM_CPP"
|
||||
for font_name_lower in "${CUSTOM_FONT_LOWERCASE[@]}"; do
|
||||
font_name_upper=$(echo "$font_name_lower" | tr '[:lower:]' '[:upper:]')
|
||||
for size in ${CUSTOM_FONT_SIZES[@]}; do
|
||||
family_name="${font_name_lower}${size}FontFamily"
|
||||
echo " renderer.insertFont(${font_name_upper}_${size}_FONT_ID, ${family_name});" >> "$CUSTOM_CPP"
|
||||
done
|
||||
done
|
||||
echo "#else" >> "$CUSTOM_CPP"
|
||||
echo " (void)renderer; // Suppress unused parameter warning" >> "$CUSTOM_CPP"
|
||||
echo "#endif" >> "$CUSTOM_CPP"
|
||||
else
|
||||
echo " (void)renderer; // Suppress unused parameter warning" >> "$CUSTOM_CPP"
|
||||
fi
|
||||
|
||||
echo "}" >> "$CUSTOM_CPP"
|
||||
echo "" >> "$CUSTOM_CPP"
|
||||
|
||||
echo "Generated customFonts.h and customFonts.cpp with ${#CUSTOM_FONT_NAMES[@]} custom font(s)"
|
||||
|
||||
Reference in New Issue
Block a user