1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup blf
22  */
23 
24 #pragma once
25 
26 #include "BLI_compiler_attrs.h"
27 #include "BLI_sys_types.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /* enable this only if needed (unused circa 2016) */
34 #define BLF_BLUR_ENABLE 0
35 
36 struct ColorManagedDisplay;
37 struct ResultBLF;
38 struct rctf;
39 struct rcti;
40 
41 int BLF_init(void);
42 void BLF_exit(void);
43 void BLF_default_dpi(int dpi);
44 void BLF_default_set(int fontid);
45 int BLF_default(void); /* get default font ID so we can pass it to other functions */
46 
47 void BLF_cache_clear(void);
48 
49 /* Loads a font, or returns an already loaded font and increments its reference count. */
50 int BLF_load(const char *name) ATTR_NONNULL();
51 int BLF_load_mem(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL();
52 
53 int BLF_load_unique(const char *name) ATTR_NONNULL();
54 int BLF_load_mem_unique(const char *name, const unsigned char *mem, int mem_size) ATTR_NONNULL();
55 
56 void BLF_unload(const char *name) ATTR_NONNULL();
57 void BLF_unload_id(int fontid);
58 
59 /* Check if font supports a particular glyph. */
60 bool BLF_has_glyph(int fontid, unsigned int unicode);
61 
62 /* Attach a file with metrics information from memory. */
63 void BLF_metrics_attach(int fontid, unsigned char *mem, int mem_size);
64 
65 void BLF_aspect(int fontid, float x, float y, float z);
66 void BLF_position(int fontid, float x, float y, float z);
67 void BLF_size(int fontid, int size, int dpi);
68 
69 /* goal: small but useful color API */
70 void BLF_color4ubv(int fontid, const unsigned char rgba[4]);
71 void BLF_color3ubv(int fontid, const unsigned char rgb[3]);
72 void BLF_color3ubv_alpha(int fontid, const unsigned char rgb[3], unsigned char alpha);
73 void BLF_color4ub(
74     int fontid, unsigned char r, unsigned char g, unsigned char b, unsigned char alpha);
75 void BLF_color3ub(int fontid, unsigned char r, unsigned char g, unsigned char b);
76 void BLF_color4f(int fontid, float r, float g, float b, float a);
77 void BLF_color4fv(int fontid, const float rgba[4]);
78 void BLF_color3f(int fontid, float r, float g, float b);
79 void BLF_color3fv_alpha(int fontid, const float rgb[3], float alpha);
80 /* also available: UI_FontThemeColor(fontid, colorid) */
81 
82 /* Set a 4x4 matrix to be multiplied before draw the text.
83  * Remember that you need call BLF_enable(BLF_MATRIX)
84  * to enable this.
85  *
86  * The order of the matrix is like GL:
87  *
88  *  | m[0]  m[4]  m[8]  m[12] |
89  *  | m[1]  m[5]  m[9]  m[13] |
90  *  | m[2]  m[6]  m[10] m[14] |
91  *  | m[3]  m[7]  m[11] m[15] |
92  */
93 void BLF_matrix(int fontid, const float m[16]);
94 
95 /* Batch drawcalls together as long as
96  * the modelview matrix and the font remain unchanged. */
97 void BLF_batch_draw_begin(void);
98 void BLF_batch_draw_flush(void);
99 void BLF_batch_draw_end(void);
100 
101 /* Draw the string using the default font, size and dpi. */
102 void BLF_draw_default(float x, float y, float z, const char *str, size_t len) ATTR_NONNULL();
103 void BLF_draw_default_ascii(float x, float y, float z, const char *str, size_t len) ATTR_NONNULL();
104 
105 /* Set size and DPI, and return default font ID. */
106 int BLF_set_default(void);
107 
108 /* Draw the string using the current font. */
109 void BLF_draw_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info)
110     ATTR_NONNULL(2);
111 void BLF_draw(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
112 void BLF_draw_ascii_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info)
113     ATTR_NONNULL(2);
114 void BLF_draw_ascii(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
115 int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth) ATTR_NONNULL(2);
116 
117 typedef bool (*BLF_GlyphBoundsFn)(const char *str,
118                                   const size_t str_step_ofs,
119                                   const struct rcti *glyph_step_bounds,
120                                   const int glyph_advance_x,
121                                   const struct rctf *glyph_bounds,
122                                   const int glyph_bearing[2],
123                                   void *user_data);
124 
125 void BLF_boundbox_foreach_glyph_ex(int fontid,
126                                    const char *str,
127                                    size_t len,
128                                    BLF_GlyphBoundsFn user_fn,
129                                    void *user_data,
130                                    struct ResultBLF *r_info) ATTR_NONNULL(2);
131 void BLF_boundbox_foreach_glyph(int fontid,
132                                 const char *str,
133                                 size_t len,
134                                 BLF_GlyphBoundsFn user_fn,
135                                 void *user_data) ATTR_NONNULL(2);
136 
137 /* Get the string byte offset that fits within a given width */
138 size_t BLF_width_to_strlen(int fontid, const char *str, size_t len, float width, float *r_width)
139     ATTR_NONNULL(2);
140 /* Same as BLF_width_to_strlen but search from the string end */
141 size_t BLF_width_to_rstrlen(int fontid, const char *str, size_t len, float width, float *r_width)
142     ATTR_NONNULL(2);
143 
144 /* This function return the bounding box of the string
145  * and are not multiplied by the aspect.
146  */
147 void BLF_boundbox_ex(int fontid,
148                      const char *str,
149                      size_t len,
150                      struct rctf *box,
151                      struct ResultBLF *r_info) ATTR_NONNULL(2);
152 void BLF_boundbox(int fontid, const char *str, size_t len, struct rctf *box) ATTR_NONNULL();
153 
154 /* The next both function return the width and height
155  * of the string, using the current font and both value
156  * are multiplied by the aspect of the font.
157  */
158 float BLF_width_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info)
159     ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
160 float BLF_width(int fontid, const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
161 float BLF_height_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info)
162     ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2);
163 float BLF_height(int fontid, const char *str, size_t len) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
164 
165 /* Return dimensions of the font without any sample text. */
166 int BLF_height_max(int fontid) ATTR_WARN_UNUSED_RESULT;
167 float BLF_width_max(int fontid) ATTR_WARN_UNUSED_RESULT;
168 float BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT;
169 float BLF_ascender(int fontid) ATTR_WARN_UNUSED_RESULT;
170 
171 /* The following function return the width and height of the string, but
172  * just in one call, so avoid extra freetype2 stuff.
173  */
174 void BLF_width_and_height(int fontid, const char *str, size_t len, float *r_width, float *r_height)
175     ATTR_NONNULL();
176 
177 /* For fixed width fonts only, returns the width of a
178  * character.
179  */
180 float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT;
181 
182 /* By default, rotation and clipping are disable and
183  * have to be enable/disable using BLF_enable/disable.
184  */
185 void BLF_rotation(int fontid, float angle);
186 void BLF_clipping(int fontid, float xmin, float ymin, float xmax, float ymax);
187 void BLF_wordwrap(int fontid, int wrap_width);
188 
189 #if BLF_BLUR_ENABLE
190 void BLF_blur(int fontid, int size);
191 #endif
192 
193 void BLF_enable(int fontid, int option);
194 void BLF_disable(int fontid, int option);
195 
196 /* Shadow options, level is the blur level, can be 3, 5 or 0 and
197  * the other argument are the rgba color.
198  * Take care that shadow need to be enable using BLF_enable!!!
199  */
200 void BLF_shadow(int fontid, int level, const float rgba[4]) ATTR_NONNULL(3);
201 
202 /* Set the offset for shadow text, this is the current cursor
203  * position plus this offset, don't need call BLF_position before
204  * this function, the current position is calculate only on
205  * BLF_draw, so it's safe call this whenever you like.
206  */
207 void BLF_shadow_offset(int fontid, int x, int y);
208 
209 /* Set the buffer, size and number of channels to draw, one thing to take care is call
210  * this function with NULL pointer when we finish, for example:
211  *
212  *     BLF_buffer(my_fbuf, my_cbuf, 100, 100, 4, true, NULL);
213  *
214  *     ... set color, position and draw ...
215  *
216  *     BLF_buffer(NULL, NULL, NULL, 0, 0, false, NULL);
217  */
218 void BLF_buffer(int fontid,
219                 float *fbuf,
220                 unsigned char *cbuf,
221                 int w,
222                 int h,
223                 int nch,
224                 struct ColorManagedDisplay *display);
225 
226 /* Set the color to be used for text. */
227 void BLF_buffer_col(int fontid, const float rgba[4]) ATTR_NONNULL(2);
228 
229 /* Draw the string into the buffer, this function draw in both buffer,
230  * float and unsigned char _BUT_ it's not necessary set both buffer, NULL is valid here.
231  */
232 void BLF_draw_buffer_ex(int fontid, const char *str, size_t len, struct ResultBLF *r_info)
233     ATTR_NONNULL(2);
234 void BLF_draw_buffer(int fontid, const char *str, size_t len) ATTR_NONNULL(2);
235 
236 /* Add a path to the font dir paths. */
237 void BLF_dir_add(const char *path) ATTR_NONNULL();
238 
239 /* Remove a path from the font dir paths. */
240 void BLF_dir_rem(const char *path) ATTR_NONNULL();
241 
242 /* Return an array with all the font dir (this can be used for filesel) */
243 char **BLF_dir_get(int *ndir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
244 
245 /* Free the data return by BLF_dir_get. */
246 void BLF_dir_free(char **dirs, int count) ATTR_NONNULL();
247 
248 /* blf_thumbs.c */
249 void BLF_thumb_preview(const char *filename,
250                        const char **draw_str,
251                        const char **i18n_draw_str,
252                        const unsigned char draw_str_lines,
253                        const float font_color[4],
254                        const int font_size,
255                        unsigned char *buf,
256                        int w,
257                        int h,
258                        int channels) ATTR_NONNULL();
259 
260 /* blf_font_default.c */
261 int BLF_load_default(const bool unique);
262 int BLF_load_mono_default(const bool unique);
263 
264 #ifdef DEBUG
265 void BLF_state_print(int fontid);
266 #endif
267 
268 /* font->flags. */
269 #define BLF_ROTATION (1 << 0)
270 #define BLF_CLIPPING (1 << 1)
271 #define BLF_SHADOW (1 << 2)
272 #define BLF_KERNING_DEFAULT (1 << 3)
273 #define BLF_MATRIX (1 << 4)
274 #define BLF_ASPECT (1 << 5)
275 #define BLF_WORD_WRAP (1 << 6)
276 #define BLF_MONOCHROME (1 << 7) /* no-AA */
277 #define BLF_HINTING_NONE (1 << 8)
278 #define BLF_HINTING_SLIGHT (1 << 9)
279 #define BLF_HINTING_FULL (1 << 10)
280 #define BLF_BOLD (1 << 11)
281 #define BLF_ITALIC (1 << 12)
282 
283 #define BLF_DRAW_STR_DUMMY_MAX 1024
284 
285 /* XXX, bad design */
286 extern int blf_mono_font;
287 extern int blf_mono_font_render; /* don't mess drawing with render threads. */
288 
289 /**
290  * Result of drawing/evaluating the string
291  */
292 struct ResultBLF {
293   /**
294    * Number of lines drawn when #BLF_WORD_WRAP is enabled (both wrapped and `\n` newline).
295    */
296   int lines;
297   /**
298    * The 'cursor' position on completion (ignoring character boundbox).
299    */
300   int width;
301 };
302 
303 #ifdef __cplusplus
304 }
305 #endif
306