You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.7 KiB

  1. /*
  2. Copyright 2020 rupa <rupa@lrrr.us> @rupa
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <print.h>
  15. #include "rupa.h"
  16. #if defined(UNICODE_SCRIPT_MODE_ENABLE)
  17. const font_t * translator = NULL;
  18. // https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols
  19. static const font_t *fonts_map[] = {
  20. [F_FRACT] = &(font_t){0x1D56C, 0x1D586, 0x1D7D8}, // fraktur/doublestruck numbers
  21. [F_ITALI] = &(font_t){0x1D468, 0x1D482, 0x1D7CE}, // italic/bold numbers
  22. [F_MONOS] = &(font_t){0x1D670, 0x1D68A, 0x1D7F6}, // monospace
  23. [F_NORML] = &(font_t){0x1D400, 0x1D41A, 0x00030}, // normal!
  24. [F_SANSI] = &(font_t){0x1D63C, 0x1D656, 0x1D7EC}, // sans 1talic/sans bold numbers
  25. [F_SANSN] = &(font_t){0x1D5D4, 0x1D5EE, 0x1D7E2}, // sans normal/sans numbers
  26. [F_SCRPT] = &(font_t){0x1D4D0, 0x1D4EA, 0x1D7CE}, // script/bold numbers
  27. };
  28. /*
  29. font_t doublestruc = {0x1D538, 0x1D552, 0x1D7D8};
  30. uint_32 []snowflakes = {
  31. // doublestruck
  32. 0x1D53B, // C
  33. 0x1D540, // H
  34. 0x1D546, // N
  35. 0x1D548, // P
  36. 0x1D549, // Q
  37. 0x1D54A, // R
  38. 0x1D552, // Z
  39. };
  40. */
  41. const font_t *get_script_mode(void) {
  42. return translator;
  43. }
  44. bool set_script_mode(int fc) {
  45. translator = translator == fonts_map[fc] ? NULL : fonts_map[fc];
  46. dprintf("set_script_mode: %u %b\n", fc, translator != NULL);
  47. return true;
  48. }
  49. // Maps A-Z, a-z, and 0-9 to other unicode ranges. We also map space to EN
  50. // SPACE for some reason :)
  51. uint32_t map_alnum(const font_t *f, bool is_shifted, uint32_t keycode) {
  52. switch (keycode) {
  53. case KC_SPACE:
  54. return (is_shifted ? 0 : 0x2002); // EN SPACE
  55. case KC_0:
  56. return (is_shifted ? 0 : f->zero_glyph);
  57. case KC_A ... KC_Z:
  58. return (is_shifted ? f->upper_alpha : f->lower_alpha) + keycode - KC_A;
  59. case KC_1 ... KC_9:
  60. return (is_shifted ? 0 : f->zero_glyph + keycode - KC_1 + 1);
  61. default:
  62. return 0;
  63. }
  64. }
  65. bool script_mode_translate(bool is_shifted, uint32_t keycode) {
  66. uint32_t translated = map_alnum(translator, is_shifted, keycode);
  67. if (translated == 0) return true;
  68. dprintf("script_mode_translate: %u => %d\n", keycode, translated);
  69. register_unicode(translated);
  70. return false;
  71. }
  72. #endif