microvg  6.0.1
microvg
LLVG_FONT_impl_freetype.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2020-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
15 #include "vg_configuration.h"
16 
17 #if defined VG_FEATURE_FONT && \
18  (defined VG_FEATURE_FONT_FREETYPE_VECTOR || defined VG_FEATURE_FONT_FREETYPE_BITMAP) && \
19  (VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_VECTOR || VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_BITMAP)
20 
21 // -----------------------------------------------------------------------------
22 // Includes
23 // -----------------------------------------------------------------------------
24 
25 #include <math.h>
26 #include <string.h>
27 #include <aftypes.h>
28 
29 #include <LLVG_impl.h>
30 #include <LLVG_FONT_impl.h>
31 
32 #if defined(VG_FEATURE_FONT_EXTERNAL)
33 #include <LLEXT_RES_impl.h>
34 #include <freetype/internal/ftmemory.h>
35 #endif
36 
37 #include "vg_freetype.h"
38 #include "vg_helper.h"
39 #include "vg_trace.h"
40 #include "ui_util.h"
41 
42 // -----------------------------------------------------------------------------
43 // Macros and Defines
44 // -----------------------------------------------------------------------------
45 
46 /*
47  * @brief Computes the scale to apply to the font.
48  */
49 #define GET_SCALE(s, f) ((s) / (f)->units_per_EM)
50 
51 /*
52  * @brief Macro to add a FONT event and its type.
53  */
54 #define LOG_MICROVG_FONT_START(fn) LOG_MICROVG_START(LOG_MICROVG_FONT_ID, CONCAT_DEFINES(LOG_MICROVG_FONT_, fn))
55 #define LOG_MICROVG_FONT_END(fn) LOG_MICROVG_END(LOG_MICROVG_FONT_ID, CONCAT_DEFINES(LOG_MICROVG_FONT_, fn))
56 
57 // -----------------------------------------------------------------------------
58 // Types
59 // -----------------------------------------------------------------------------
60 
61 /*
62  * @brief Structure to load a resource by calling SNIX_get_resource()
63  */
64 typedef struct {
65  void *data;
66  uint32_t size;
67 } SNIX_resource;
68 
69 // -----------------------------------------------------------------------------
70 // Extern functions
71 // -----------------------------------------------------------------------------
72 
73 /*
74  * @brief SNIX_get_resource() available since MicroEJ Architecture version 7.13
75  */
76 extern int32_t SNIX_get_resource(jchar *path, SNIX_resource *resource);
77 
78 // -----------------------------------------------------------------------------
79 // Internal function definitions
80 // -----------------------------------------------------------------------------
81 
82 /*
83  * @brief Disposes Freetype font when the associated Java object VectorFont is
84  * garbaged collected.
85  */
86 static void _dispose_registered_font(void *faceHandle);
87 
88 /*
89  * @brief Opens a font that has been loaded into memory.
90  *
91  * @see FT_New_Memory_Face in freetype.h
92  *
93  * @param[in] face: a handle to a new face object.
94  * @param[in] data: a pointer to the beginning of the font data.
95  * @param[in] length: the size of the memory chunk used by the font data.
96  *
97  * @return FreeType error code.
98  */
99 static FT_Error __load_memory_font(FT_Face *face, void *data, int length);
100 
101 /*
102  * @brief Opens a font that has been compiled with the application.
103  *
104  * @see FT_New_Memory_Face in freetype.h
105  *
106  * @param[in] face: a handle to a new face object.
107  * @param[in] font_name: a path to the font file.
108  *
109  * @return FreeType error code.
110  */
111 static FT_Error __load_internal_font(FT_Face *face, jchar *font_name);
112 
113 /*
114  * @brief Gets the description of the given native resource: a vector font.
115  * @see SNI_getDescriptionFunction
116  */
117 static void __register_font_description(void *resource, char *buffer, uint32_t bufferLength);
118 
119 #if defined(VG_FEATURE_FONT_EXTERNAL)
120 
121 /*
122  * @brief Opens a font that has not been compiled with the application.
123  *
124  * @see FT_Open_Face in freetype.h
125  *
126  * @param[in] face: a handle to a new face object.
127  * @param[in] font_name: a path to the font file.
128  *
129  * @return FreeType error code.
130  */
131 static FT_Error __load_external_font(FT_Face *face, jchar *font_name);
132 
133 /*
134  * @brief Reads a chunk from an external resource.
135  *
136  * @see FT_Stream_IoFunc in ftsystem.h
137  *
138  * @param[in] stream: a handle to the source stream.
139  * @param[in] offset: the offset of read in stream.
140  * @param[in] buffer: the address of the read buffer.
141  * @param[in] count: the number of bytes to read from the stream.
142  *
143  * @return the number of bytes effectively read by the stream.
144  */
145 static unsigned long __read_external_resource(FT_Stream stream, unsigned long offset, unsigned char *buffer,
146  unsigned long count);
147 
148 /*
149  * @brief Closes a given input stream.
150  *
151  * @see FT_Stream_IoFunc in ftsystem.h
152  *
153  * @param[in] stream: a handle to the target stream.
154  */
155 static void __close_external_resource(FT_Stream stream);
156 
157 #endif // VG_FEATURE_FONT_EXTERNAL
158 
159 // -----------------------------------------------------------------------------
160 // Global Variables
161 // -----------------------------------------------------------------------------
162 
163 FT_Library library;
164 FT_Renderer renderer;
165 
166 #if defined VG_FEATURE_FONT && defined VG_FEATURE_FONT_FREETYPE_VECTOR && \
167  (VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_VECTOR)
168 const char *renderer_name = "microvg";
169 #endif
170 #if defined VG_FEATURE_FONT && defined VG_FEATURE_FONT_FREETYPE_BITMAP && \
171  (VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_BITMAP)
172 const char *renderer_name = "smooth";
173 #endif
174 
175 // -----------------------------------------------------------------------------
176 // vg_freetype.h functions
177 // -----------------------------------------------------------------------------
178 
179 // See the header file for the function documentation
180 void VG_FREETYPE_initialize(void) {
181  FT_Error error = FT_Init_FreeType(&library);
182  if (FT_ERR(Ok) == error) {
183  renderer = FT_RENDERER(FT_Get_Module(library, renderer_name));
184  if (0 != renderer) {
185  MEJ_LOG_INFO_MICROVG("Freetype renderer: %s\n", FT_MODULE_CLASS(renderer)->module_name);
186  } else {
187  MEJ_LOG_ERROR_MICROVG("No renderer found with name %s\n", renderer_name);
188  }
189  } else {
190  MEJ_LOG_ERROR_MICROVG("Internal freetype error initializing library, ID = %d\n", error);
191  }
192 }
193 
194 // -----------------------------------------------------------------------------
195 // LLVG_FONT_impl.h functions
196 // -----------------------------------------------------------------------------
197 
198 // See the header file for the function documentation
199 jint LLVG_FONT_IMPL_load_font(jchar *font_name, jboolean complex_layout) {
200  LOG_MICROVG_FONT_START(load);
201 
202  FT_Face face = 0;
203  jint ret = LLVG_FONT_UNLOADED;
204 
205  // Check that complex layouter is available for complex text layout
206  if (complex_layout && !LLVG_FONT_IMPL_has_complex_layouter()) {
207  ret = LLVG_FONT_UNLOADED;
208  } else {
209  // try to load an internal resource
210  FT_Error error = __load_internal_font(&face, font_name);
211 
212 #if defined(VG_FEATURE_FONT_EXTERNAL)
213  if (FT_ERR(Cannot_Open_Resource) == error) {
214  // try to load an external resource
215  error = __load_external_font(&face, font_name);
216  }
217 #endif // VG_FEATURE_FONT_EXTERNAL
218 
219  if (FT_ERR(Ok) == error) {
220  // Use Unicode
221  FT_Select_Charmap(face, ft_encoding_unicode);
222  MEJ_LOG_INFO_MICROVG("Freetype font loaded: %s\n", (const char *)font_name);
223 
224  SNI_registerResource((void *)face, (SNI_closeFunction) & _dispose_registered_font,
225  &__register_font_description);
226 
227 #if defined(VG_FEATURE_FONT_COMPLEX_LAYOUT)
228  if (JTRUE == complex_layout) {
229  face->face_flags |= FT_FACE_FLAG_COMPLEX_LAYOUT;
230  }
231 #endif // VG_FEATURE_FONT_COMPLEX_LAYOUT
232 
233  ret = (jint)face;
234  } else if (FT_ERR(Cannot_Open_Resource) != error) {
235  MEJ_LOG_ERROR_MICROVG("An error occurred during font loading: 0x%x, refer to fterrdef.h\n", error);
236  } else {
237  MEJ_LOG_ERROR_MICROVG("Resource not found: %s\n", (const char *)font_name);
238  }
239  }
240 
241  LOG_MICROVG_FONT_END(load);
242  return ret;
243 }
244 
245 // See the header file for the function documentation
246 jfloat LLVG_FONT_IMPL_string_width(jchar *text, jint faceHandle, jfloat size, jfloat letterSpacing) {
247  LOG_MICROVG_FONT_START(stringWidth);
248  jfloat ret;
249 
250  if (LLVG_FONT_UNLOADED == faceHandle) {
251  ret = (jfloat)LLVG_RESOURCE_CLOSED;
252  } else {
253  FT_Face face = (FT_Face)faceHandle;
254  float scaled_width = 0.f;
255 
256  if (size >= 0) {
257  float scale = GET_SCALE(size, face);
258 
259  int nb_chars = 0;
260  long unscaled_width = 0;
261 
262  // Layout variables
263  int glyph_index; // current glyph index
264  int previous_glyph_index = 0; // previous glyph index for kerning
265 
266  int advance_x;
267  int previous_advance_x;
268  int previous_offset_x;
269  int advance_y;
270  int offset_x;
271  int offset_y;
272 
273  int length = (int)SNI_getArrayLength(text);
274  VG_HELPER_layout_configure(faceHandle, text, length);
275 
276  while (VG_HELPER_layout_load_glyph(&glyph_index, &advance_x, &advance_y, &offset_x, &offset_y)) {
277  // At that point the current glyph has been loaded by Freetype
278  if (0 == previous_glyph_index) {
279  // first glyph: remove the first blank line
280  if (0 != face->glyph->metrics.width) {
281  unscaled_width -= face->glyph->metrics.horiBearingX;
282  } else {
283  unscaled_width -= face->glyph->advance.x;
284  }
285  }
286 
287  unscaled_width += advance_x;
288  previous_glyph_index = glyph_index;
289  nb_chars++;
290  // Last call to VG_HELPER_layout_load_glyph clear advance_x and offset_x.
291  // We need to keep them for last glyph measurement
292  previous_advance_x = advance_x;
293  previous_offset_x = offset_x;
294  }
295 
296  // last glyph: remove the last blank line
297  if (0 != face->glyph->metrics.width) {
298  unscaled_width -= previous_advance_x;
299  unscaled_width += face->glyph->metrics.horiBearingX; // glyph's left blank line
300  unscaled_width += face->glyph->metrics.width; // glyph's width
301  unscaled_width += previous_offset_x; // glyph's offset_x
302  } else {
303  if (0 != unscaled_width) {
304  unscaled_width -= previous_advance_x;
305  }
306  }
307  scaled_width = ((scale * (float)unscaled_width) + (((float)nb_chars - 1) * letterSpacing));
308  }
309 
310  ret = scaled_width;
311  }
312 
313  LOG_MICROVG_FONT_END(stringWidth);
314  return ret;
315 }
316 
317 // See the header file for the function documentation
318 jfloat LLVG_FONT_IMPL_string_height(jchar *text, jint faceHandle, jfloat size) {
319  LOG_MICROVG_FONT_START(stringHeight);
320  jfloat ret;
321 
322  if (LLVG_FONT_UNLOADED == faceHandle) {
323  ret = (jfloat)LLVG_RESOURCE_CLOSED;
324  } else {
325  FT_Face face = (FT_Face)faceHandle;
326  float scaled_height = 0.f;
327 
328  if (size >= 0) {
329  float scale = GET_SCALE(size, face);
330 
331  FT_Pos horiBearingYTop = 0;
332  FT_Pos horiBearingYBottom = 0;
333 
334  // Layout variables
335  int glyph_index; // current glyph index
336  int previous_glyph_index = 0; // previous glyph index for kerning
337 
338  int advance_x;
339  int advance_y;
340  int offset_x;
341  int offset_y;
342 
343  int length = (int)SNI_getArrayLength(text);
344  VG_HELPER_layout_configure(faceHandle, text, length);
345 
346  while (VG_HELPER_layout_load_glyph(&glyph_index, &advance_x, &advance_y, &offset_x, &offset_y)) {
347  // At that point the current glyph has been loaded by Freetype
348 
349  FT_Pos yBottom = face->glyph->metrics.horiBearingY - face->glyph->metrics.height;
350  horiBearingYBottom = (0 == previous_glyph_index) ? yBottom : MIN(yBottom, horiBearingYBottom);
351  horiBearingYTop = MAX(face->glyph->metrics.horiBearingY, horiBearingYTop);
352 
353  previous_glyph_index = glyph_index;
354  }
355 
356  scaled_height = scale * (float)(horiBearingYTop - horiBearingYBottom);
357  }
358 
359  ret = scaled_height;
360  }
361 
362  LOG_MICROVG_FONT_END(stringHeight);
363  return ret;
364 }
365 
366 // See the header file for the function documentation
367 jfloat LLVG_FONT_IMPL_get_baseline_position(jint faceHandle, jfloat size) {
368  LOG_MICROVG_FONT_START(baseline);
369  jfloat ret;
370 
371  if (LLVG_FONT_UNLOADED == faceHandle) {
372  ret = (jfloat)LLVG_RESOURCE_CLOSED;
373  } else {
374  FT_Face face = (FT_Face)faceHandle;
375  float advance_y = 0.f;
376 
377  if (size >= 0) {
378  float scale = GET_SCALE(size, face);
379  advance_y = (face->ascender * scale);
380  }
381  ret = advance_y;
382  }
383 
384  LOG_MICROVG_FONT_END(baseline);
385  return ret;
386 }
387 
388 // See the header file for the function documentation
389 jfloat LLVG_FONT_IMPL_get_height(jint faceHandle, jfloat size) {
390  LOG_MICROVG_FONT_START(height);
391  jfloat ret;
392 
393  if (LLVG_FONT_UNLOADED == faceHandle) {
394  ret = (jfloat)LLVG_RESOURCE_CLOSED;
395  } else {
396  FT_Face face = (FT_Face)faceHandle;
397  float scale = GET_SCALE(size, face);
398  ret = face->height * scale;
399  }
400 
401  LOG_MICROVG_FONT_END(height);
402  return ret;
403 }
404 
405 // See the header file for the function documentation
406 void LLVG_FONT_IMPL_dispose(jint faceHandle) {
407  _dispose_registered_font((void *)faceHandle);
408 }
409 
410 // See the header file for the function documentation
411 bool LLVG_FONT_IMPL_has_complex_layouter(void) {
412 #ifdef VG_FEATURE_FONT_COMPLEX_LAYOUT
413  return true;
414 #else
415  return false;
416 #endif
417 }
418 
419 // -----------------------------------------------------------------------------
420 // Internal functions
421 // -----------------------------------------------------------------------------
422 
423 static void _dispose_registered_font(void *faceHandle) {
424  FT_Face face = (FT_Face)faceHandle;
425 
426  // unregister the resource since the VEE does not need to call it anymore
427  SNI_unregisterResource((void *)face, (SNI_closeFunction) & _dispose_registered_font);
428 
429 #if defined(VG_FEATURE_FONT_EXTERNAL)
430  // FT_Done_Face() sets the stream to NULL: have to save it to close the
431  // external resource
432  FT_Stream stream = face->stream;
433 #endif // VG_FEATURE_FONT_EXTERNAL
434 
435  FT_Done_Face(face);
436 
437 #if defined(VG_FEATURE_FONT_EXTERNAL)
438  // frees the stream when the font is external (Freetype doesn't recommend
439  // to read the flag FT_FACE_FLAG_EXTERNAL_STREAM)
440  if (&__close_external_resource == stream->close) {
441  ft_mem_free(library->memory, stream);
442  }
443 #endif // VG_FEATURE_FONT_EXTERNAL
444 }
445 
446 static FT_Error __load_memory_font(FT_Face *face, void *data, int length) {
447  return FT_New_Memory_Face(library, data, length, 0, face);
448 }
449 
450 static FT_Error __load_internal_font(FT_Face *face, jchar *font_name) {
451  SNIX_resource font_resource;
452  FT_Error error;
453 
454  if (0 == SNIX_get_resource(font_name, &font_resource)) {
455  error = __load_memory_font(face, font_resource.data, font_resource.size);
456  } else {
457  error = FT_ERR(Cannot_Open_Resource);
458  }
459  return error;
460 }
461 
462 #if defined(VG_FEATURE_FONT_EXTERNAL)
463 
464 static FT_Error __load_external_font(FT_Face *face, jchar *font_name) {
465  FT_Error error;
466 
467  // try to load an external resource (respect LLEXT_RES_open path naming convention)
468  char const *ext_path = &(((char const *)(font_name))[1]) /* first '/' */;
469  RES_ID resource_id = LLEXT_RES_open(ext_path);
470 
471  if (0 <= resource_id) {
472  int32_t resource_size = LLEXT_RES_available(resource_id);
473 
474  // check if the external resource is byte addressable
475  int32_t base_address = LLEXT_RES_getBaseAddress(resource_id);
476 
477  if (-1 != base_address) {
478  // load the font as "memory" font
479  // cppcheck-suppress [misra-c2012-11.6] base_address is an address
480  error = __load_memory_font(face, (void *)base_address, resource_size);
481 
482  // we can close the resource because we know its address
483  LLEXT_RES_close(resource_id);
484  } else {
485  // load the font as external font
486 
487  FT_Open_Args args;
488  args.flags = FT_OPEN_STREAM;
489  args.driver = NULL;
490  args.stream = (FT_StreamRec *)ft_mem_alloc(library->memory, sizeof(FT_StreamRec), &error);
491 
492  if (FT_ERR(Ok) == error) {
493  args.stream->base = NULL;
494  args.stream->size = resource_size;
495  args.stream->pos = 0;
496  args.stream->descriptor.value = resource_id;
497  // args.stream->pathname = NULL; // not used
498  args.stream->read = &__read_external_resource;
499  args.stream->close = &__close_external_resource;
500 
501  error = FT_Open_Face(library, &args, 0, face);
502  } else {
503  error = FT_ERR(Out_Of_Memory);
504  }
505  }
506  } else {
507  error = FT_ERR(Cannot_Open_Resource);
508  }
509 
510  return error;
511 }
512 
513 static unsigned long __read_external_resource(FT_Stream stream, unsigned long offset, unsigned char *buffer,
514  unsigned long count) {
515  int32_t size = count;
516  RES_ID resource_id = (RES_ID)(stream->descriptor.value);
517  LLEXT_RES_seek(resource_id, offset);
518  LLEXT_RES_read(resource_id, buffer, &size);
519  return size;
520 }
521 
522 static void __close_external_resource(FT_Stream stream) {
523  RES_ID resource_id = (RES_ID)(stream->descriptor.value);
524  LLEXT_RES_close(resource_id);
525 }
526 
527 #endif // VG_FEATURE_FONT_EXTERNAL
528 
529 static void __register_font_description(void *resource, char *buffer, uint32_t bufferLength) {
530  (void)resource;
531  const char descEF[] = "Vector Font";
532  if (bufferLength >= sizeof(descEF)) {
533  memcpy(buffer, descEF, sizeof(descEF));
534  }
535 }
536 
537 // cppcheck-suppress [misra-c2012-3.2]
538 #endif \
539  // defined VG_FEATURE_FONT && \
540  // (defined VG_FEATURE_FONT_FREETYPE_VECTOR || defined VG_FEATURE_FONT_FREETYPE_BITMAP) && \
541  // (VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_VECTOR || VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_BITMAP)
542 // -----------------------------------------------------------------------------
543 // EOF
544 // -----------------------------------------------------------------------------
MicroEJ MicroVG library low level API: enable some features according to the hardware capacities.
MicroEJ MicroVG library low level API: implementation over FreeType.
MicroEJ MicroVG library low level API: helper to implement library natives methods.
#define FT_FACE_FLAG_COMPLEX_LAYOUT
Freetype supplementary flag for complex layout Uses a free bit in freetype face flags to convey the c...
Definition: vg_helper.h:66