123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /*
- * ui_ssfn.h
- * https://gitlab.com/bztsrc/smgui
- *
- * Copyright (C) 2024 bzt (bztsrc@gitlab), MIT license
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to
- * deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- * sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
- * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
- * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * @brief Scalable Screen font driver for SMGUI
- */
- #ifndef UI_SSFN_H
- #define UI_SSFN_H 1
- #ifndef UI_FONT
- #define UI_FONT 1
- #else
- #error "An UI font implementation has already been included"
- #endif
- #define SSFN_IMPLEMENTATION
- #include <stdint.h>
- #include <ssfn.h>
- #ifndef UI_H
- #include <ui.h>
- #undef UI_H
- #endif
- #define UI_SETFONTHOOK ui_ssfn_bbox,ui_ssfn_draw
- #ifdef __cplusplus
- extern "C" {
- #endif
- /**
- * Measure string and return width, height, left margin, and baseline (top)
- */
- int ui_ssfn_bbox(void *fnt, char *str, char *end, int *w, int *h, int *l, int *t)
- {
- char c;
- if(str && end) { c = *end; *end = 0; }
- ssfn_bbox((ssfn_t*)fnt, str, w, h, l, t);
- if(str && end) *end = c;
- return 0;
- }
- /**
- * Draw an UTF-8 string to the given pixel buffer
- */
- 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,
- int cx0, int cy0, int cx1, int cy1)
- {
- ssfn_t *ssfn = (ssfn_t*)fnt;
- ssfn_buf_t buf = { 0 };
- int ret;
- if(!dst || !str || cx1 < 1 || cy1 < 1 || p < 4) return -1;
- if(x >= cx1 || y >= cy1) return 0;
- buf.ptr = (uint8_t*)dst + cy0 * p + (cx0 << 2);
- buf.w = cx1 - cx0;
- buf.h = cy1 - cy0;
- buf.p = p;
- buf.x = l + (x - cx0);
- buf.y = t + (y - cy0);
- buf.fg = color;
- while((!end || str < end) && (ret = ssfn_render(ssfn, &buf, str)) > 0)
- str += ret;
- return 0;
- }
- #ifdef __cplusplus
- }
- #endif
- #endif /* UI_SSFN_H */
|