ui_ssfn.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * ui_ssfn.h
  3. * https://gitlab.com/bztsrc/smgui
  4. *
  5. * Copyright (C) 2024 bzt (bztsrc@gitlab), MIT license
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
  20. * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  22. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * @brief Scalable Screen font driver for SMGUI
  25. */
  26. #ifndef UI_SSFN_H
  27. #define UI_SSFN_H 1
  28. #ifndef UI_FONT
  29. #define UI_FONT 1
  30. #else
  31. #error "An UI font implementation has already been included"
  32. #endif
  33. #define SSFN_IMPLEMENTATION
  34. #include <stdint.h>
  35. #include <ssfn.h>
  36. #ifndef UI_H
  37. #include <ui.h>
  38. #undef UI_H
  39. #endif
  40. #define UI_SETFONTHOOK ui_ssfn_bbox,ui_ssfn_draw
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /**
  45. * Measure string and return width, height, left margin, and baseline (top)
  46. */
  47. int ui_ssfn_bbox(void *fnt, char *str, char *end, int *w, int *h, int *l, int *t)
  48. {
  49. char c;
  50. if(str && end) { c = *end; *end = 0; }
  51. ssfn_bbox((ssfn_t*)fnt, str, w, h, l, t);
  52. if(str && end) *end = c;
  53. return 0;
  54. }
  55. /**
  56. * Draw an UTF-8 string to the given pixel buffer
  57. */
  58. int ui_ssfn_draw(void *fnt, char *str, char *end, uint8_t *dst, uint32_t color, int x, int y, int l, int t, int p,
  59. int cx0, int cy0, int cx1, int cy1)
  60. {
  61. ssfn_t *ssfn = (ssfn_t*)fnt;
  62. ssfn_buf_t buf = { 0 };
  63. int ret;
  64. if(!dst || !str || cx1 < 1 || cy1 < 1 || p < 4) return -1;
  65. if(x >= cx1 || y >= cy1) return 0;
  66. buf.ptr = (uint8_t*)dst + cy0 * p + (cx0 << 2);
  67. buf.w = cx1 - cx0;
  68. buf.h = cy1 - cy0;
  69. buf.p = p;
  70. buf.x = l + (x - cx0);
  71. buf.y = t + (y - cy0);
  72. buf.fg = color;
  73. while((!end || str < end) && (ret = ssfn_render(ssfn, &buf, str)) > 0)
  74. str += ret;
  75. return 0;
  76. }
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif /* UI_SSFN_H */