Mirai's Miscellaneous Misadventures

M42 / include / mimimi / text.h

// license: AGPLv3 or later
// copyright 2023 zamfofex

#ifndef MIMIMI_TEXT_H
#define MIMIMI_TEXT_H

struct mimimi_allocator;
struct mimimi_font;
struct mimimi_image;

// Note: Pointers to 'char' are in the execution encoding.
// Note: Pointers to 'unsigned char' are in UTF-8.
// Note: 'unsigned long int' values represent code points.
// Note: All counts regard bytes.
// Note: Where relevant and otherwise unspecified, the default direction will be LTR.
// Note: The word "grapheme" refers to default extended grapheme clusters.

// Converts from the execution encoding to UTF-8.
// Note: Skips characters outside the basic execution character set (as defined by C99, C11, C17, and C23).
// Note: The returned value needs to be dealocated appropriately.
unsigned char *mimimi_utf8(char *text, struct mimimi_allocator *allocator);

// Returns the value of the first Unicode code point in the text.
unsigned long int mimimi_code_point(unsigned char *text);

// Counts the number of bytes in a type of text segment.
// Note: If the string starts at a delimiter, these return zero.
int mimimi_count_code_point(unsigned char *text);
int mimimi_count_grapheme(unsigned char *text);
int mimimi_count_word(unsigned char *text);
int mimimi_count_paragraph(unsigned char *text);
int mimimi_count_text(unsigned char *text);

// Skips a type of text segment and its delimiter (if any).
// Note: If the string starts at a delimiter, these skip to the start of the next segment of their type.
unsigned char *mimimi_skip_code_point(unsigned char *text);
unsigned char *mimimi_skip_grapheme(unsigned char *text);
unsigned char *mimimi_skip_word(unsigned char *text);
unsigned char *mimimi_skip_paragraph(unsigned char *text);

int mimimi_count_line(unsigned char *text, int width, struct mimimi_font *font);
unsigned char *mimimi_skip_line(unsigned char *text, int width, struct mimimi_font *font);
int mimimi_measure_line(unsigned char *paragraph, unsigned char *text, int count, struct mimimi_font *font);

int mimimi_draw_segment(struct mimimi_image *image, int x, int y, unsigned char *paragraph, unsigned char *line, int line_count, unsigned char *text, int count, struct mimimi_font *font, unsigned char color);
int mimimi_draw_line(struct mimimi_image *image, int x, int y, unsigned char *paragraph, unsigned char *text, int count, struct mimimi_font *font, unsigned char color);
int mimimi_draw_text(struct mimimi_image *image, int x, int y, int width, int height, unsigned char *text, struct mimimi_font *font, unsigned char color);

#endif