1 /* Text alignment and justification algorithm. Supports left, right, center
2  * alignment and justify. Supports tab stops and kerning.
3  */
4 
5 #ifndef _MF_JUSTIFY_H_
6 #define _MF_JUSTIFY_H_
7 
8 #include "mf_rlefont.h"
9 #include <stdbool.h>
10 
11 enum mf_align_t
12 {
13     MF_ALIGN_LEFT = 0,
14     MF_ALIGN_CENTER,
15     MF_ALIGN_RIGHT
16 };
17 
18 /* Callback for rendering a single character.
19  * x0:          Left edge of the target position of character.
20  * y0:          Upper edge of the target position of character.
21  * character:   Character to render.
22  * state:       Free state variable for use by the callback.
23  * Returns the width of the character.
24  */
25 typedef uint8_t (*mf_character_callback_t) (int16_t x0, int16_t y0,
26                                             mf_char character, void *state);
27 
28 /* Get width of a string in pixels.
29  *
30  * font:   Pointer to the font definition.
31  * text:   Pointer to start of the text to measure.
32  * count:  Number of characters on the line or 0 to read until end of string.
33  * kern:   True to consider kerning (slower).
34  */
35 MF_EXTERN int16_t mf_get_string_width(const struct mf_font_s *font,
36                                       mf_str text, uint16_t count, bool kern);
37 
38 /* Render a single line of aligned text.
39  *
40  * font:     Pointer to the font definition.
41  * x0:       Depending on aligned, either left, center or right edge of target.
42  * y0:       Upper edge of the target area.
43  * align:    Type of alignment.
44  * text:     Pointer to start of the text to render.
45  * count:    Number of characters on the line or 0 to read until end of string.
46  * callback: Callback to call for each character.
47  * state:    Free variable for use in the callback.
48  */
49 MF_EXTERN void mf_render_aligned(const struct mf_font_s *font,
50                                  int16_t x0, int16_t y0,
51                                  enum mf_align_t align,
52                                  mf_str text, uint16_t count,
53                                  mf_character_callback_t callback,
54                                  void *state);
55 
56 /* Render a single line of justified text.
57  *
58  * font:     Pointer to the font definition.
59  * x0:       Left edge of the target area.
60  * y0:       Upper edge of the target area.
61  * width:    Width of the target area.
62  * text:     Pointer to start of the text to render.
63  * count:    Number of characters on the line or 0 to read until end of string.
64  * callback: Callback to call for each character.
65  * state:    Free variable for use in the callback.
66  */
67 MF_EXTERN void mf_render_justified(const struct mf_font_s *font,
68                                    int16_t x0, int16_t y0, int16_t width,
69                                    mf_str text, uint16_t count,
70                                    mf_character_callback_t callback,
71                                    void *state);
72 
73 
74 #endif
75