textregion.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * TODO: This is a work in progress
  7. *
  8. * Port of the original ToaruOS Python text_region library to C.
  9. *
  10. * Allows for the display of rich text with multiple varied formats,
  11. * as well as carat positioning, reflow, links, images, and so on.
  12. */
  13. #include <stdio.h>
  14. #include <toaru/textregion.h>
  15. #include <toaru/sdf.h>
  16. int tr_font_get_width(struct TR_Font * font, char * string) {
  17. return draw_sdf_string_width(string, font->size, font->typeface);
  18. }
  19. int tr_font_write(struct TR_Font * font, gfx_context_t * ctx, int x, int y, char * string) {
  20. return draw_sdf_string(ctx, x, y, string, font->size, font->color, font->typeface);
  21. }
  22. void tr_textunit_set_tag_group(struct TR_TextUnit * self, list_t * tag_group) {
  23. if (!self->tag_group) {
  24. self->tag_group = tag_group;
  25. list_insert(tag_group, self);
  26. } else {
  27. /* Already in a tag group, this is wrong */
  28. }
  29. }
  30. void tr_textunit_set_font(struct TR_TextUnit * self, struct TR_Font * font) {
  31. self->font = font;
  32. self->width = tr_font_get_width(font, self->string);
  33. }
  34. void tr_textunit_set_extra(struct TR_TextUnit * self, char * key, void * data) {
  35. if (!self->extra) {
  36. self->extra = hashmap_create(10);
  37. }
  38. hashmap_set(self->extra, key, data);
  39. }
  40. void tr_textregion_set_alignment(struct TR_TextRegion * self, int align) {
  41. self->align = align;
  42. }
  43. void tr_textregion_set_valignment(struct TR_TextRegion * self, int align) {
  44. self->valign = align;
  45. }
  46. void tr_textregion_set_max_lines(struct TR_TextRegion * self, int max_lines) {
  47. self->max_lines = max_lines;
  48. tr_textregion_reflow(self);
  49. }
  50. int tr_textregion_get_visible_lines(struct TR_TextRegion * self) {
  51. return self->height / self->line_height;
  52. }
  53. void tr_textregion_reflow(struct TR_TextRegion * self) {
  54. if (self->lines) {
  55. fprintf(stderr, "Need to clean out lines\n");
  56. #if 0
  57. list_destroy(self->lines);
  58. list_free(self->lines);
  59. free(self->lines);
  60. #endif
  61. }
  62. #if 0
  63. self->lines = list_create();
  64. int current_width = 0;
  65. list_t * current_units = list_create();
  66. struct TR_TextUnit * leftover = NULL;
  67. int i = 0;
  68. while (i < self->text_units
  69. #endif
  70. }