1 #ifndef _EVAS_FONT_H
2 #define _EVAS_FONT_H
3 
4 typedef unsigned char         DATA8;
5 typedef unsigned long long    DATA64;
6 
7 #include <ft2build.h>
8 #include FT_FREETYPE_H
9 #include FT_GLYPH_H
10 #include FT_SIZES_H
11 #include FT_MODULE_H
12 
13 #ifndef FT_HAS_COLOR
14 # define FT_HAS_COLOR(face) 0
15 #endif
16 
17 #ifndef FT_LOAD_COLOR
18 # define FT_LOAD_COLOR FT_LOAD_DEFAULT
19 #endif
20 
21 #include <Eina.h>
22 
23 #include "evas_text_utils.h"
24 
25 #ifdef EAPI
26 # undef EAPI
27 #endif
28 
29 #ifdef _WIN32
30 # ifdef EFL_BUILD
31 #  ifdef DLL_EXPORT
32 #   define EAPI __declspec(dllexport)
33 #  else
34 #   define EAPI
35 #  endif
36 # else
37 #  define EAPI __declspec(dllimport)
38 # endif
39 #else
40 # ifdef __GNUC__
41 #  if __GNUC__ >= 4
42 #   define EAPI __attribute__ ((visibility("default")))
43 #  else
44 #   define EAPI
45 #  endif
46 # else
47 #  define EAPI
48 # endif
49 #endif
50 
51 #define LK(x)  Eina_Lock x
52 #define LKI(x) eina_lock_new(&(x))
53 #define LKD(x) eina_lock_free(&(x))
54 #define LKL(x) eina_lock_take(&(x))
55 #define LKT(x) eina_lock_take_try(&(x))
56 #define LKU(x) eina_lock_release(&(x))
57 #define LKDBG(x) eina_lock_debug(&(x))
58 
59 /**
60  * See explanation of variation_sequences at:
61  * https://unicode.org/Public/UCD/latest/ucd/StandardizedVariants.txt
62  * https://unicode.org/reports/tr37/
63  * https://unicode.org/ivd/
64  * https://www.freetype.org/freetype2/docs/reference/ft2-glyph_variants.html
65 */
66 #define VAR_SEQ(x) (GENERIC_VARIATION_SEQUENCES(x) | IDEOGRAPHICS_VARIATION_SEQUENCES(x) | MANGOLIAN_VARIATION_SEQUENCES(x))
67 #define GENERIC_VARIATION_SEQUENCES(x) ((x>=0xFE00 && x<=0xFE0F) ? x : 0)
68 #define IDEOGRAPHICS_VARIATION_SEQUENCES(x) ((x>=0xE0100 && x<=0xE01EF) ? x : 0)
69 #define MANGOLIAN_VARIATION_SEQUENCES(x) ((x>=0x180B && x<=0x180D) ? x : 0)
70 /**
71  * http://unicode.org/emoji/charts/emoji-variants.html
72 */
73 #define  VARIATION_EMOJI_PRESENTATION  0xFE0F
74 #define  VARIATION_TEXT_PRESENTATION   0xFE0E
75 
76 /**
77  * These Options (Flags) are used with evas_common_font_glyph_search function
78  */
79 #define EVAS_FONT_SEARCH_OPTION_NONE            0x0000
80 #define EVAS_FONT_SEARCH_OPTION_SKIP_COLOR      0x0001
81 
82 
83 #define FASH_INT_MAGIC              0x01012345
84 #define FASH_GLYPH_MAGIC            0x02012345
85 
86 enum _Evas_Font_Style
87 {
88    EVAS_FONT_STYLE_SLANT,
89    EVAS_FONT_STYLE_WEIGHT,
90    EVAS_FONT_STYLE_WIDTH
91 };
92 
93 enum _Evas_Font_Slant
94 {
95    EVAS_FONT_SLANT_NORMAL,
96    EVAS_FONT_SLANT_OBLIQUE,
97    EVAS_FONT_SLANT_ITALIC
98 };
99 
100 enum _Evas_Font_Weight
101 {
102    EVAS_FONT_WEIGHT_NORMAL,
103    EVAS_FONT_WEIGHT_THIN,
104    EVAS_FONT_WEIGHT_ULTRALIGHT,
105    EVAS_FONT_WEIGHT_EXTRALIGHT,
106    EVAS_FONT_WEIGHT_LIGHT,
107    EVAS_FONT_WEIGHT_BOOK,
108    EVAS_FONT_WEIGHT_MEDIUM,
109    EVAS_FONT_WEIGHT_SEMIBOLD,
110    EVAS_FONT_WEIGHT_BOLD,
111    EVAS_FONT_WEIGHT_ULTRABOLD,
112    EVAS_FONT_WEIGHT_EXTRABOLD,
113    EVAS_FONT_WEIGHT_BLACK,
114    EVAS_FONT_WEIGHT_EXTRABLACK
115 };
116 
117 enum _Evas_Font_Width
118 {
119    EVAS_FONT_WIDTH_NORMAL,
120    EVAS_FONT_WIDTH_ULTRACONDENSED,
121    EVAS_FONT_WIDTH_EXTRACONDENSED,
122    EVAS_FONT_WIDTH_CONDENSED,
123    EVAS_FONT_WIDTH_SEMICONDENSED,
124    EVAS_FONT_WIDTH_SEMIEXPANDED,
125    EVAS_FONT_WIDTH_EXPANDED,
126    EVAS_FONT_WIDTH_EXTRAEXPANDED,
127    EVAS_FONT_WIDTH_ULTRAEXPANDED
128 };
129 
130 enum _Evas_Font_Spacing
131 {
132    EVAS_FONT_SPACING_PROPORTIONAL,
133    EVAS_FONT_SPACING_DUAL,
134    EVAS_FONT_SPACING_MONO,
135    EVAS_FONT_SPACING_CHARCELL
136 };
137 
138 typedef enum _Evas_Font_Style               Evas_Font_Style;
139 typedef enum _Evas_Font_Slant               Evas_Font_Slant;
140 typedef enum _Evas_Font_Weight              Evas_Font_Weight;
141 typedef enum _Evas_Font_Width               Evas_Font_Width;
142 typedef enum _Evas_Font_Spacing             Evas_Font_Spacing;
143 
144 typedef struct _Evas_Font_Dir               Evas_Font_Dir;
145 typedef struct _Evas_Font                   Evas_Font;
146 typedef struct _Evas_Font_Alias             Evas_Font_Alias;
147 typedef struct _Evas_Font_Description       Evas_Font_Description;
148 
149 
150 typedef struct _RGBA_Font             RGBA_Font;
151 typedef struct _RGBA_Font_Int         RGBA_Font_Int;
152 typedef struct _RGBA_Font_Source      RGBA_Font_Source;
153 typedef struct _RGBA_Font_Glyph       RGBA_Font_Glyph;
154 typedef struct _RGBA_Font_Glyph_Out   RGBA_Font_Glyph_Out;
155 
156 typedef struct _Fash_Item_variation_Index_Item  Fash_Item_variation_Index_Item;
157 typedef struct _Fash_Item_variation_List        Fash_Item_variation_List;
158 typedef struct _Fash_Item_Index_Map_Variations  Fash_Item_Index_Map_Variations;
159 
160 typedef struct _Fash_Item_Index_Map Fash_Item_Index_Map;
161 typedef struct _Fash_Int_Map        Fash_Int_Map;
162 typedef struct _Fash_Int_Map2       Fash_Int_Map2;
163 typedef struct _Fash_Int            Fash_Int;
164 
165 
166 struct _Fash_Item_Index_Map
167 {
168    RGBA_Font_Int *fint;
169    int            index;
170 };
171 struct _Fash_Item_variation_Index_Item
172 {
173    Fash_Item_Index_Map  item;
174    Eina_Unicode         variation_sequence;
175 };
176 struct _Fash_Item_variation_List
177 {
178    Fash_Item_variation_Index_Item  *list;
179    size_t                           length;
180    size_t                           capacity;
181 };
182 struct _Fash_Item_Index_Map_Variations
183 {
184    Fash_Item_Index_Map         item;
185    Fash_Item_variation_List   *variations;
186 };
187 struct _Fash_Int_Map
188 {
189   Fash_Item_Index_Map_Variations items[256];
190 };
191 struct _Fash_Int_Map2
192 {
193    Fash_Int_Map *bucket[256];
194 };
195 struct _Fash_Int
196 {
197    unsigned int MAGIC;
198    Fash_Int_Map2 *bucket[256];
199    void (*freeme) (Fash_Int *fash);
200 };
201 
202 typedef struct _Fash_Glyph_Map  Fash_Glyph_Map;
203 typedef struct _Fash_Glyph_Map2 Fash_Glyph_Map2;
204 typedef struct _Fash_Glyph      Fash_Glyph;
205 struct _Fash_Glyph_Map
206 {
207    RGBA_Font_Glyph *item[256];
208 };
209 struct _Fash_Glyph_Map2
210 {
211    Fash_Glyph_Map *bucket[256];
212 };
213 struct _Fash_Glyph
214 {
215    unsigned int MAGIC;
216    Fash_Glyph_Map2 *bucket[256];
217    void (*freeme) (Fash_Glyph *fash);
218 };
219 
220 
221 typedef enum _Font_Hint_Flags
222 {
223    FONT_NO_HINT,
224    FONT_AUTO_HINT,
225    FONT_BYTECODE_HINT
226 } Font_Hint_Flags;
227 
228 typedef enum _Font_Rend_Flags
229 {
230    FONT_REND_REGULAR   = 0,
231    FONT_REND_SLANT     = (1 << 0),
232    FONT_REND_WEIGHT    = (1 << 1),
233 } Font_Rend_Flags;
234 
235 struct _RGBA_Font
236 {
237    Eina_List       *fonts;
238    Fash_Int        *fash;
239    Font_Hint_Flags  hinting;
240    int              references;
241    LK(lock);
242    unsigned char    sizeok : 1;
243 };
244 
245 
246 struct _Evas_Font_Dir
247 {
248    Eina_Hash *lookup;
249    Eina_List *fonts;
250    Eina_List *aliases;
251    DATA64     dir_mod_time;
252    DATA64     fonts_dir_mod_time;
253    DATA64     fonts_alias_mod_time;
254 };
255 
256 struct _Evas_Font
257 {
258    struct {
259       const char *prop[14];
260    } x;
261    struct {
262       const char *name;
263    } simple;
264    const char *path;
265    char     type;
266 };
267 
268 struct _Evas_Font_Alias
269 {
270    const char *alias;
271    Evas_Font  *fn;
272 };
273 
274 struct _Evas_Font_Description
275 {
276    int ref;
277    Eina_Stringshare *name;
278    Eina_Stringshare *fallbacks;
279    Eina_Stringshare *lang;
280    Eina_Stringshare *style;
281 
282    Evas_Font_Slant slant;
283    Evas_Font_Weight weight;
284    Evas_Font_Width width;
285    Evas_Font_Spacing spacing;
286 
287    Eina_Bool is_new : 1;
288 };
289 
290 struct _RGBA_Font_Int
291 {
292    EINA_INLIST;
293    RGBA_Font_Source *src;
294    Eina_Hash        *kerning;
295    Fash_Glyph       *fash;
296    unsigned int      size;
297    double            scale_factor;
298    Eina_Bool         is_resized;
299    int               real_size;
300    int               max_h;
301    int               references;
302    int               usage;
303    struct {
304       FT_Size       size;
305 #ifdef USE_HARFBUZZ
306       void         *hb_font;
307 #endif
308    } ft;
309    LK(ft_mutex);
310    Font_Hint_Flags  hinting;
311    Font_Rend_Flags  wanted_rend; /* The wanted rendering style */
312    Font_Rend_Flags  runtime_rend; /* The rendering we need to do on runtime
313                                      in order to comply with the wanted_rend. */
314 
315    Eina_List       *task;
316 
317    int              generation;
318 
319    Efl_Text_Font_Bitmap_Scalable bitmap_scalable;
320 
321    unsigned char    sizeok : 1;
322    unsigned char    inuse : 1;
323 };
324 
325 struct _RGBA_Font_Source
326 {
327    const char       *name;
328    const char       *file;
329    void             *data;
330    unsigned int      current_size;
331    int               data_size;
332    int               references;
333    struct {
334       int            orig_upem;
335       FT_Face        face;
336    } ft;
337 };
338 
339 /*
340  * laziness wins for now. The parts used from the freetpye struct are
341  * kept intact to avoid changing the code using it until we know exactly
342  * what needs to be changed
343  */
344 struct _RGBA_Font_Glyph_Out
345 {
346    unsigned char *rle;
347    struct {
348       unsigned char *buffer;
349       unsigned short rows;
350       unsigned short width;
351       unsigned short pitch;
352       unsigned short rle_alloc : 1;
353       unsigned short no_free_glout : 1;
354    } bitmap;
355    int rle_size;
356 };
357 
358 struct _RGBA_Font_Glyph
359 {
360    FT_UInt         index;
361    Evas_Coord      width;
362    Evas_Coord      x_bear;
363    Evas_Coord      y_bear;
364    FT_Glyph        glyph;
365    FT_Vector       advance;
366    RGBA_Font_Glyph_Out *glyph_out;
367    void           *col_dat;
368    /* this is a problem - only 1 engine at a time can extend such a font... grrr */
369    void           *ext_dat;
370    void           (*ext_dat_free) (void *ext_dat);
371    RGBA_Font_Int   *fi;
372 };
373 
374 
375 /* The tangent of the slant angle we do on runtime. */
376 #define _EVAS_FONT_SLANT_TAN 0.221694663
377 
378 /* main */
379 
380 EAPI void              evas_common_font_init                 (void);
381 EAPI void              evas_common_font_shutdown             (void);
382 EAPI void              evas_common_font_font_all_unload      (void);
383 
384 EAPI int               evas_common_font_ascent_get           (RGBA_Font *fn);
385 EAPI int               evas_common_font_descent_get          (RGBA_Font *fn);
386 EAPI int               evas_common_font_max_ascent_get       (RGBA_Font *fn);
387 EAPI int               evas_common_font_max_descent_get      (RGBA_Font *fn);
388 EAPI int               evas_common_font_instance_ascent_get           (RGBA_Font_Int *fi);
389 EAPI int               evas_common_font_instance_descent_get          (RGBA_Font_Int *fi);
390 EAPI int               evas_common_font_instance_max_ascent_get       (RGBA_Font_Int *fi);
391 EAPI int               evas_common_font_instance_max_descent_get      (RGBA_Font_Int *fi);
392 EAPI int               evas_common_font_instance_underline_position_get       (RGBA_Font_Int *fi);
393 EAPI int               evas_common_font_instance_underline_thickness_get      (RGBA_Font_Int *fi);
394 EAPI int               evas_common_font_get_line_advance     (RGBA_Font *fn);
395 void *evas_common_font_freetype_face_get(RGBA_Font *font); /* XXX: Not EAPI on purpose. Not ment to be used in modules. */
396 
397 EAPI RGBA_Font_Glyph  *evas_common_font_int_cache_glyph_get  (RGBA_Font_Int *fi, FT_UInt index);
398 EAPI Eina_Bool         evas_common_font_int_cache_glyph_render(RGBA_Font_Glyph *fg);
399 EAPI FT_UInt           evas_common_get_char_index            (RGBA_Font_Int* fi, Eina_Unicode gl, Eina_Unicode variation_sequence);
400 
401 /* load */
402 EAPI void              evas_common_font_dpi_set              (int dpi_h, int dpi_v);
403 EAPI RGBA_Font_Source *evas_common_font_source_memory_load   (const char *name, const void *data, int data_size);
404 EAPI RGBA_Font_Source *evas_common_font_source_load          (const char *name);
405 EAPI int               evas_common_font_source_load_complete (RGBA_Font_Source *fs);
406 EAPI RGBA_Font_Source *evas_common_font_source_find          (const char *name);
407 EAPI void              evas_common_font_source_free          (RGBA_Font_Source *fs);
408 EAPI void              evas_common_font_size_use             (RGBA_Font *fn);
409 EAPI RGBA_Font_Int    *evas_common_font_int_load             (const char *name, int size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
410 EAPI RGBA_Font_Int    *evas_common_font_int_load_init        (RGBA_Font_Int *fn);
411 EAPI RGBA_Font_Int    *evas_common_font_int_load_complete    (RGBA_Font_Int *fi);
412 EAPI RGBA_Font        *evas_common_font_memory_load          (const char *source, const char *name, int size, const void *data, int data_size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
413 EAPI RGBA_Font        *evas_common_font_load                 (const char *name, int size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
414 EAPI RGBA_Font        *evas_common_font_add                  (RGBA_Font *fn, const char *name, int size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
415 EAPI RGBA_Font        *evas_common_font_memory_add           (RGBA_Font *fn, const char *source, const char *name, int size, const void *data, int data_size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
416 EAPI void              evas_common_font_free                 (RGBA_Font *fn);
417 EAPI void              evas_common_font_int_unref            (RGBA_Font_Int *fi);
418 EAPI void              evas_common_font_hinting_set          (RGBA_Font *fn, Font_Hint_Flags hinting);
419 EAPI Eina_Bool         evas_common_hinting_available         (Font_Hint_Flags hinting);
420 EAPI RGBA_Font        *evas_common_font_memory_hinting_load  (const char *source, const char *name, int size, const void *data, int data_size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
421 EAPI RGBA_Font        *evas_common_font_hinting_load         (const char *name, int size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
422 EAPI RGBA_Font        *evas_common_font_hinting_add          (RGBA_Font *fn, const char *name, int size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
423 EAPI RGBA_Font        *evas_common_font_memory_hinting_add   (RGBA_Font *fn, const char *source, const char *name, int size, const void *data, int data_size, Font_Hint_Flags hinting, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
424 EAPI void              evas_common_font_int_modify_cache_by  (RGBA_Font_Int *fi, int dir);
425 EAPI int               evas_common_font_cache_get            (void);
426 EAPI void              evas_common_font_cache_set            (int size);
427 EAPI void              evas_common_font_flush                (void);
428 EAPI void              evas_common_font_flush_last           (void);
429 EAPI RGBA_Font_Int    *evas_common_font_int_find             (const char *name, int size, Font_Rend_Flags wanted_rend, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
430 EAPI void              evas_common_font_all_clear            (void);
431 EAPI void              evas_common_font_ext_clear            (void);
432 
433 /* query */
434 
435 EAPI int               evas_common_font_query_kerning        (RGBA_Font_Int* fi, FT_UInt left, FT_UInt right, int* kerning);
436 EAPI void              evas_common_font_query_size           (RGBA_Font *fn, const Evas_Text_Props *intl_props, int *w, int *h);
437 EAPI int               evas_common_font_query_inset          (RGBA_Font *fn, const Evas_Text_Props *text_props);
438 EAPI int               evas_common_font_query_right_inset    (RGBA_Font *fn, const Evas_Text_Props *text_props);
439 EAPI void              evas_common_font_query_advance        (RGBA_Font *fn, const Evas_Text_Props *intl_props, int *h_adv, int *v_adv);
440 EAPI int               evas_common_font_query_char_coords    (RGBA_Font *fn, const Evas_Text_Props *intl_props, int pos, int *cx, int *cy, int *cw, int *ch);
441 EAPI int               evas_common_font_query_pen_coords     (RGBA_Font *fn, const Evas_Text_Props *intl_props, int pos, int *cpen_x, int *cy, int *cadv, int *ch);
442 EAPI int               evas_common_font_query_char_at_coords (RGBA_Font *fn, const Evas_Text_Props *intl_props, int x, int y, int *cx, int *cy, int *cw, int *ch);
443 EAPI int               evas_common_font_query_last_up_to_pos (RGBA_Font *fn, const Evas_Text_Props *intl_props, int x, int y, int width_offset);
444 EAPI int               evas_common_font_query_run_font_end_get(RGBA_Font *fn, RGBA_Font_Int **script_fi, RGBA_Font_Int **cur_fi, Evas_Script_Type script, const Eina_Unicode *text, int run_len);
445 EAPI void              evas_common_font_ascent_descent_get(RGBA_Font *fn, const Evas_Text_Props *text_props, int *ascent, int *descent);
446 
447 EAPI void             *evas_common_font_glyph_compress(void *data, int num_grays, int pixel_mode, int pitch_data, int w, int h, int *size_ret);
448 EAPI DATA8            *evas_common_font_glyph_uncompress(RGBA_Font_Glyph *fg, int *wret, int *hret);
449 EAPI int               evas_common_font_glyph_search         (RGBA_Font *fn, RGBA_Font_Int **fi_ret, Eina_Unicode gl, Eina_Unicode variation_sequence, uint32_t evas_font_search_options);
450 
451 void evas_common_font_load_init(void);
452 void evas_common_font_load_shutdown(void);
453 
454 void evas_font_dir_cache_free(void);
455 const char *evas_font_dir_cache_find(char *dir, char *font);
456 Eina_List *evas_font_dir_available_list(const Eina_List *font_paths);
457 void evas_font_dir_available_list_free(Eina_List *available);
458 void evas_font_free(void *font);
459 void evas_fonts_zero_free();
460 void evas_fonts_zero_pressure();
461 void evas_font_name_parse(Evas_Font_Description *fdesc, const char *name);
462 unsigned int evas_font_style_find(const char *start, const char *end, Evas_Font_Style style);
463 const char *evas_font_style_find_str(int type, Evas_Font_Style style);
464 Evas_Font_Description *evas_font_desc_new(void);
465 Evas_Font_Description *evas_font_desc_dup(const Evas_Font_Description *fdesc);
466 void evas_font_desc_unref(Evas_Font_Description *fdesc);
467 int evas_font_desc_cmp(const Evas_Font_Description *a, const Evas_Font_Description *b);
468 Evas_Font_Description *evas_font_desc_ref(Evas_Font_Description *fdesc);
469 const char *evas_font_lang_normalize(const char *lang);
470 void * evas_font_load(const Eina_List *font_paths, int hinting, Evas_Font_Description *fdesc, const char *source, Evas_Font_Size size, Efl_Text_Font_Bitmap_Scalable bitmap_scalable);
471 void evas_font_load_hinting_set(void *font, int hinting);
472 
473 #undef EAPI
474 #define EAPI
475 
476 #endif /* _EVAS_FONT_H */
477