uxggenpages.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env bash
  2. #####################################################################
  3. # uxggenpages.sh for u8g
  4. #
  5. # This script will generate u8g c files for specified fonts
  6. #
  7. # Copyright 2015-2018 Yunhui Fu <yhfudev@gmail.com>
  8. # License: BSD
  9. #####################################################################
  10. my_getpath() {
  11. local PARAM_DN="$1"
  12. shift
  13. #readlink -f
  14. local DN="${PARAM_DN}"
  15. local FN=
  16. if [ ! -d "${DN}" ]; then
  17. FN=$(basename "${DN}")
  18. DN=$(dirname "${DN}")
  19. fi
  20. cd "${DN}" > /dev/null 2>&1
  21. DN=$(pwd)
  22. cd - > /dev/null 2>&1
  23. echo -n "${DN}"
  24. [[ -z "$FN" ]] || echo -n "/${FN}"
  25. }
  26. #DN_EXEC=`echo "$0" | ${EXEC_AWK} -F/ '{b=$1; for (i=2; i < NF; i ++) {b=b "/" $(i)}; print b}'`
  27. DN_EXEC=$(dirname $(my_getpath "$0") )
  28. #####################################################################
  29. EXEC_GENPAGES=${DN_EXEC}/genpages/genpages.exe
  30. [ -x "${EXEC_GENPAGES}" ] || { echo "Error: genpages not found!" ; exit 1; }
  31. EXEC_BDF2U8G=${DN_EXEC}/bdf2u8g/bdf2u8g.exe
  32. [ -x "${EXEC_BDF2U8G}" ] || { echo "Error: bdf2u8g not found!" ; exit 1; }
  33. DN_CUR=$(pwd)
  34. DN_DATA=$(pwd)/datatmp
  35. mkdir -p "${DN_DATA}"
  36. #####################################################################
  37. FONTHOME=/usr/share/fonts
  38. FN_FONT_BASE="marlin-6x12-3"
  39. #FN_FONT_BASE=unifont
  40. #FN_FONT_BASE=wenquanyi_12pt
  41. #FN_FONT_BASE=wenquanyi_9pt
  42. FN_FONT="${1:-}"
  43. LANG="$2"
  44. DN_FONT0=`dirname ${FN_FONT}`
  45. DN_FONT="$(my_getpath ${DN_FONT0})"
  46. FN_FONT="$(my_getpath "${DN_FONT}")/"`basename ${FN_FONT}`
  47. [ -z "${FN_FONT}" ] && FN_FONT=${DN_DATA}/../${FN_FONT_BASE}.bdf
  48. [ -f "${FN_FONT}" ] || FN_FONT=${DN_EXEC}/${FN_FONT_BASE}.bdf
  49. [ -f "${FN_FONT}" ] || FN_FONT="$FONTHOME/wenquanyi/${FN_FONT_BASE}.bdf"
  50. [ -f "${FN_FONT}" ] || FN_FONT="$FONTHOME/X11/misc/${FN_FONT_BASE}.bdf"
  51. [ -f "${FN_FONT}" ] || FN_FONT="$FONTHOME/misc/${FN_FONT_BASE}.bdf"
  52. #echo "uxggenpages.sh: FN_FONT=${FN_FONT}"
  53. if [ ! -f "${FN_FONT}" ]; then
  54. FN_FONT_PCF="$FONTHOME/X11/misc/${FN_FONT_BASE}.pcf"
  55. [ -f "${FN_FONT_PCF}" ] || FN_FONT_PCF="$FONTHOME/misc/${FN_FONT_BASE}.pcf"
  56. [ -f "${FN_FONT_PCF}" ] || FN_FONT_PCF="$FONTHOME/wenquanyi/${FN_FONT_BASE}.pcf"
  57. if [ -f "${FN_FONT_PCF}" ]; then
  58. EXEC_PCF2BDF=$(which pcf2bdf)
  59. if [ ! -x "${EXEC_PCF2BDF}" ]; then
  60. echo "Error: not found pcf2bdf!"
  61. echo " Please install pcf2bdf."
  62. exit 1
  63. fi
  64. FN_FONT="./${FN_FONT_BASE}.bdf"
  65. echo ${EXEC_PCF2BDF} -o "${FN_FONT}" "${FN_FONT_PCF}"
  66. ${EXEC_PCF2BDF} -o "${FN_FONT}" "${FN_FONT_PCF}"
  67. fi
  68. fi
  69. [ -f "${FN_FONT}" ] || { echo "Error: can't find font ${FN_FONT}!" ; exit 1; }
  70. #####################################################################
  71. #(cd ${DN_EXEC}; gcc -o genpages genpages.c getline.c)
  72. rm -f tmpa tmpb
  73. touch tmpa tmpb
  74. #rm -f ${DN_EXEC}/fontpage_*.h
  75. rm -f fontpage_*.h
  76. cat << EOF >"proc.awk"
  77. BEGIN {
  78. cur_page=0;
  79. val_begin=0;
  80. val_pre=0;
  81. utf8_pre="";
  82. utf8_begin="";
  83. }{
  84. page=\$1;
  85. val_real=\$2;
  86. utf8=\$3;
  87. # assert (val_real < 128);
  88. val=val_real + 128;
  89. if (cur_page != page) {
  90. if (cur_page != 0) {
  91. if (val_begin != 0) {
  92. print cur_page " " val_begin " " val_pre " " utf8_begin " " utf8_pre;
  93. }
  94. }
  95. cur_page=page;
  96. val_begin=val;
  97. val_pre=val;
  98. utf8_begin=utf8;
  99. utf8_pre=utf8;
  100. } else {
  101. if (val_pre + 1 != val) {
  102. if (cur_page != 0) {
  103. print cur_page " " val_begin " " val_pre " " utf8_begin " " utf8_pre;
  104. }
  105. val_begin=val;
  106. val_pre=val;
  107. utf8_begin=utf8;
  108. utf8_pre=utf8;
  109. } else {
  110. val_pre = val;
  111. utf8_pre=utf8;
  112. }
  113. }
  114. } END {
  115. if (cur_page != 0) {
  116. print cur_page " " val_begin " " val_pre " " utf8_begin " " utf8_pre;
  117. }
  118. }
  119. EOF
  120. AWK=$(which gawk || which awk)
  121. grep -Hrn _UxGT . | grep '"' \
  122. | sed 's/_UxGT("/\n&/g;s/[^\n]*\n_UxGT("\([^"]*\)[^\n]*/\1 /g;s/.$//' \
  123. | ${EXEC_GENPAGES} \
  124. | sort -k 1n -k 2n | uniq \
  125. | "$AWK" -v EXEC_PREFIX=${DN_EXEC} -f "proc.awk" \
  126. | while read PAGE BEGIN END UTF8BEGIN UTF8END; do \
  127. if [ ! -f ${DN_DATA}/fontpage_${PAGE}_${BEGIN}_${END}.h ]; then \
  128. ${EXEC_BDF2U8G} -u ${PAGE} -b ${BEGIN} -e ${END} ${FN_FONT} fontpage_${PAGE}_${BEGIN}_${END} ${DN_DATA}/fontpage_${PAGE}_${BEGIN}_${END}.h > /dev/null 2>&1 ;
  129. fi ; \
  130. grep -A 10000000000 u8g_fntpgm_uint8_t ${DN_DATA}/fontpage_${PAGE}_${BEGIN}_${END}.h >> tmpa ; \
  131. echo " FONTDATA_ITEM(${PAGE}, ${BEGIN}, ${END}, fontpage_${PAGE}_${BEGIN}_${END}), // '${UTF8BEGIN}' -- '${UTF8END}'" >> tmpb ;\
  132. done
  133. TMPA=$(cat tmpa)
  134. TMPB=$(cat tmpb)
  135. EOL=$'\n'
  136. [[ ! "$TMPA" == "" ]] && TMPA="$TMPA$EOL$EOL"
  137. [[ ! "$TMPB" == "" ]] && TMPB="$EOL$TMPB$EOL"
  138. rm -f tmpa tmpb "proc.awk"
  139. cat <<EOF >fontutf8-data.h
  140. /**
  141. * Generated automatically by buildroot/share/fonts/uxggenpages.sh
  142. * Contents will be REPLACED by future processing!
  143. * Use genallfont.sh to generate font data for updated languages.
  144. */
  145. #pragma once
  146. #include "langdata.h"
  147. ${TMPA}static const uxg_fontinfo_t g_fontinfo_${LANG}[] PROGMEM = {${TMPB}};
  148. EOF