text.h (4511B)
1 /* 2 * Copyright (c) 2021-2024 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #ifndef LTK_TEXT_H 18 #define LTK_TEXT_H 19 20 #include <stddef.h> 21 #include <stdint.h> 22 #include "color.h" 23 #include "graphics.h" 24 #include "rect.h" 25 26 typedef struct ltk_text_line ltk_text_line; 27 typedef struct ltk_text_context ltk_text_context; 28 29 ltk_text_context *ltk_text_context_create(ltk_renderdata *data); 30 void ltk_text_context_destroy(ltk_text_context *ctx); 31 32 /* FIXME: allow to give length of text */ 33 /* FIXME: font string format is currently defined by implementation, maybe standardize that? */ 34 ltk_text_line *ltk_text_line_create(ltk_text_context *ctx, const char *font, int font_size, char *text, int take_over_text, int width); 35 ltk_text_line *ltk_text_line_create_const_text(ltk_text_context *ctx, const char *font, int font_size, const char *text, int width); 36 void ltk_text_line_set_font(ltk_text_line *tl, const char *font, int font_size); 37 void ltk_text_line_set_font_size(ltk_text_line *tl, int font_size); 38 void ltk_text_line_set_width(ltk_text_line *tl, int width); 39 void ltk_text_line_get_size(ltk_text_line *tl, int *w, int *h); 40 void ltk_text_line_destroy(ltk_text_line *tl); 41 /* FIXME: length of text */ 42 void ltk_text_line_set_text(ltk_text_line *line, char *text, int take_over_text); 43 void ltk_text_line_set_const_text(ltk_text_line *line, const char *text); 44 const char *ltk_text_line_get_text(ltk_text_line *line); 45 46 /* Draw the entire line to a surface. */ 47 /* FIXME: Some widgets rely on this to not fail when negative coordinates are given or 48 the text goes outside of the surface boundaries - in the stb backend, this is taken 49 into account and the pango-xft backend doesn't *seem* to have any problems with it, 50 but I don't know if that's guaranteed. */ 51 void ltk_text_line_draw(ltk_text_line *tl, ltk_surface *s, ltk_color *color, int x, int y); 52 53 /* Draw a line onto a surface at position x,y and clipped to 'clip'. 54 Note that 'clip' is relative to the origin of the given surface, not 'x' and 'y'. */ 55 void ltk_text_line_draw_clipped(ltk_text_line *tl, ltk_surface *s, ltk_color *color, int x, int y, ltk_rect clip); 56 57 void ltk_text_line_clear_attrs(ltk_text_line *tl); 58 void ltk_text_line_add_attr_bg(ltk_text_line *tl, size_t start, size_t end, ltk_color *color); 59 void ltk_text_line_add_attr_fg(ltk_text_line *tl, size_t start, size_t end, ltk_color *color); 60 61 typedef enum { 62 LTK_TEXT_LTR, 63 LTK_TEXT_RTL, 64 } ltk_text_direction; 65 66 /* FIXME: support for strong and weak cursor */ 67 /* FIXME: better interface that doesn't just return ltr on error (e.g. invalid index) */ 68 ltk_text_direction ltk_text_line_get_byte_direction(ltk_text_line *tl, size_t byte); 69 ltk_text_direction ltk_text_line_get_softline_direction(ltk_text_line *tl, size_t line); 70 size_t ltk_text_line_get_num_softlines(ltk_text_line *tl); 71 size_t ltk_text_line_xy_to_pos(ltk_text_line *tl, int x, int y, int snap_nearest); 72 void ltk_text_line_pos_to_rect(ltk_text_line *tl, size_t pos, int *x_ret, int *y_ret, int *w_ret, int *h_ret); 73 size_t ltk_text_line_x_softline_to_pos(ltk_text_line *tl, int x, size_t softline, int snap_nearest); 74 void ltk_text_line_pos_to_x_softline(ltk_text_line *tl, size_t pos, int middle, int *x_ret, size_t *softline_ret); 75 size_t ltk_text_line_move_cursor_visually(ltk_text_line *tl, size_t pos, int movement, size_t *prev_index_ret); 76 77 /* FIXME: two versions: bigline and normal line with text stored in memory but no bidi etc. */ 78 /* need to set endline separator so it can return when end of line reached! */ 79 /* FIXME: does this even make sense? */ 80 void ltk_text_bline_create( 81 size_t (*iter_cur)(void *data), 82 void (*iter_next)(void *data, char **text_ret, size_t *len_ret), 83 void (*iter_prev)(void *data, size_t maxlen, char **text_ret, size_t *len_ret), 84 void *data 85 ); 86 87 #endif /* LTK_TEXT_H */