microvg  2.1.0
microvg
microvg_helper.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2022 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 
16 // -----------------------------------------------------------------------------
17 // Includes
18 // -----------------------------------------------------------------------------
19 
20 #include <LLVG_MATRIX_impl.h>
21 #include <freetype/internal/ftobjs.h>
22 
23 #include "microvg_helper.h"
24 #include "microvg_configuration.h"
25 
26 // -----------------------------------------------------------------------------
27 // Configuration Sanity Check
28 // -----------------------------------------------------------------------------
29 
38 #if !defined MICROVG_CONFIGURATION_VERSION
39  #error "Undefined MICROVG_CONFIGURATION_VERSION, it must be defined in microvg_configuration.h"
40 #endif
41 
42 #if defined MICROVG_CONFIGURATION_VERSION && MICROVG_CONFIGURATION_VERSION != 1
43  #error "Version of the configuration file microvg_configuration.h is not compatible with this implementation."
44 #endif
45 
46 #if defined VG_FEATURE_FONT_COMPLEX_LAYOUT
47 #include "hb.h"
48 #include "hb-ft.h"
49 #endif
50 
51 // -----------------------------------------------------------------------------
52 // Defines
53 // -----------------------------------------------------------------------------
54 
55 #define MIN_HIGH_SURROGATE ((unsigned short)0xD800)
56 #define MAX_HIGH_SURROGATE ((unsigned short)0xDBFF)
57 #define MIN_LOW_SURROGATE ((unsigned short)0xDC00)
58 #define MAX_LOW_SURROGATE ((unsigned short)0xDFFF)
59 #define MIN_SUPPLEMENTARY_CODE_POINT 0x010000
60 
61 #define GET_NEXT_CHARACTER(t,l,o) ((o) >= (l) ? (unsigned short)0 : (t)[o])
62 
63 #ifdef VG_FEATURE_FONT_COMPLEX_LAYOUT
64 #define IS_SIMPLE_LAYOUT (!(face->face_flags & FT_FACE_FLAG_COMPLEX_LAYOUT))
65 #else
66 #define IS_SIMPLE_LAYOUT true
67 #endif // VG_FEATURE_FONT_COMPLEX_LAYOUT
68 
69 // -----------------------------------------------------------------------------
70 // Globals
71 // -----------------------------------------------------------------------------
72 
73 static jfloat g_identity_matrix[LLVG_MATRIX_SIZE];
74 
75 static FT_Face face;
76 
77 // Freetype layout variables
78 static unsigned short *current_text;
79 static unsigned int current_length;
80 static int current_offset;
81 static FT_UInt previous_glyph_index; // previous glyph index for kerning
82 
83 #if defined VG_FEATURE_FONT_COMPLEX_LAYOUT
84 // Harfbuzz layout variables
85 static hb_glyph_info_t *glyph_info;
86 static hb_glyph_position_t *glyph_pos;
87 static unsigned int glyph_count;
88 static int current_glyph;
89 static hb_buffer_t *buf;
90 #endif
91 // -----------------------------------------------------------------------------
92 // Public functions
93 // -----------------------------------------------------------------------------
94 
95 // See the header file for the function documentation
96 void MICROVG_HELPER_initialize(void) {
97  LLVG_MATRIX_IMPL_identity(g_identity_matrix);
98 }
99 
100 // See the header file for the function documentation
101 int MICROVG_HELPER_get_utf(unsigned short *textCharRam, int length, int *offset) {
102 
103  unsigned short highPart = GET_NEXT_CHARACTER(textCharRam, length, *offset);
104  int ret;
105 
106  if (((highPart >= MIN_HIGH_SURROGATE) && (highPart <= MAX_HIGH_SURROGATE))
107  && (*offset < (length - 1))) {
108 
109  unsigned short lowPart = GET_NEXT_CHARACTER(textCharRam, length, *(offset) + 1);
110 
111  if ((lowPart >= MIN_LOW_SURROGATE) && (lowPart <= MAX_LOW_SURROGATE)) {
112  *offset += 2;
113 
114  ret = 0;
115  ret += ((int)highPart - (int)MIN_HIGH_SURROGATE);
116  ret <<= (int)10;
117  ret += ((int)lowPart - (int)MIN_LOW_SURROGATE);
118  ret += (int)MIN_SUPPLEMENTARY_CODE_POINT;
119  }
120  }
121  else {
122  *offset += 1;
123 
124  // standard character or missing low part
125  ret = 0x0000FFFF & (int)highPart;
126  }
127 
128  return ret;
129 }
130 
131 
132 // See the header file for the function documentation
133 void MICROVG_HELPER_layout_configure(int faceHandle, unsigned short *text, int length){
134  face = (FT_Face) faceHandle;
135  // For Misra rule 2.7
136  (void)text;
137  (void)length;
138 
139  if(IS_SIMPLE_LAYOUT){
140  // Freetype variables initialisation
141  current_text = text;
142  current_length = length;
143  current_offset = 0;
144  previous_glyph_index = 0;
145  }
146  else {
147 #if defined VG_FEATURE_FONT_COMPLEX_LAYOUT
148  static hb_font_t *hb_font;
149  static jint current_faceHandle = 0;
150  // load font in Harfbuzz only when faceHandle changes
151  if(faceHandle != current_faceHandle){
152  if(0 != current_faceHandle){
153  hb_font_destroy(hb_font);
154  }
155  // FT_Set_Pixel_Sizes() must be called before hb_ft_font_create() see issue M0092MEJAUI-2643
156  FT_Set_Pixel_Sizes (face, 0, face->units_per_EM); /* set character size */
157  hb_font = hb_ft_font_create(face, NULL);
158  current_faceHandle = faceHandle;
159  }
160 
161  buf = hb_buffer_create();
162  hb_buffer_add_utf16(buf, (const uint16_t *)text, length, 0, -1);
163 
164  hb_buffer_guess_segment_properties(buf);
165 
166  hb_shape(hb_font, buf, NULL, 0);
167 
168  glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
169  glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);
170 
171  current_glyph = 0;
172 #endif // VG_FEATURE_FONT_COMPLEX_LAYOUT
173  }
174 }
175 
176 // See the header file for the function documentation
177 bool MICROVG_HELPER_layout_load_glyph(int *glyph_idx, int *x_advance, int *y_advance, int *x_offset, int *y_offset){
178  // Initiate return value with default values
179  *glyph_idx = 0;
180  *x_advance = 0;
181  *y_advance = 0;
182  *x_offset = 0;
183  *y_offset = 0;
184 
185  bool ret = false;
186 
187 
188  if(IS_SIMPLE_LAYOUT){
189  // Freetype layout
190  FT_ULong next_char = MICROVG_HELPER_get_utf(current_text, current_length, &current_offset);
191  if(0 != next_char){
192  FT_UInt glyph_index = FT_Get_Char_Index(face, next_char);
193 
194  int error = FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_SCALE);
195  if(FT_ERR(Ok) != error) {
196  MEJ_LOG_ERROR_MICROVG("Error while loading glyphid %d: 0x%x, refer to fterrdef.h\n",glyph_index, error);
197  }
198 
199  *x_advance = face->glyph->advance.x;
200 
201  // Compute Kerning
202  if (FT_HAS_KERNING(face) && previous_glyph_index && glyph_index){
203  FT_Vector delta;
204  FT_Get_Kerning(face, previous_glyph_index, glyph_index, FT_KERNING_UNSCALED, &delta);
205 
206  *x_offset = delta.x;
207  *x_advance += delta.x;
208  }
209 
210  previous_glyph_index = glyph_index;
211 
212  *glyph_idx = glyph_index;
213 
214  ret = true;
215  }
216  }
217  else {
218 #if defined VG_FEATURE_FONT_COMPLEX_LAYOUT
219  // Harfbuzz layout
220  if(((unsigned int) 0) != glyph_count){
221 
222  *glyph_idx = glyph_info[current_glyph].codepoint;
223  *x_advance = glyph_pos[current_glyph].x_advance/64;
224  *y_advance = glyph_pos[current_glyph].y_advance/64;
225  *x_offset = glyph_pos[current_glyph].x_offset/64;
226  *y_offset = glyph_pos[current_glyph].y_offset/64;
227 
228  glyph_count --;
229  current_glyph ++;
230 
231  // Load glyph
232  int error = FT_Load_Glyph(face, *glyph_idx, FT_LOAD_NO_SCALE);
233  if(FT_ERR(Ok) != error) {
234  MEJ_LOG_ERROR_MICROVG("Error while loading glyphid %d: 0x%x, refer to fterrdef.h\n", *glyph_idx, error);
235  }
236 
237  ret = true;
238  } else {
239  hb_buffer_destroy(buf);
240  ret = false;
241  }
242 #endif // VG_FEATURE_FONT_COMPLEX_LAYOUT
243  }
244 
245  return ret;
246 }
247 
248 // See the header file for the function documentation
249 jfloat* MICROVG_HELPER_check_matrix(jfloat* matrix) {
250  return (NULL == matrix) ? g_identity_matrix : matrix;
251 }
252 
253 // See the header file for the function documentation
254 uint32_t MICROVG_HELPER_apply_alpha(uint32_t color, uint32_t alpha) {
255  uint32_t color_alpha = (((color >> 24) & (uint32_t)0xff) * alpha) / (uint32_t)255;
256  return (color & (uint32_t)0xffffff) | (color_alpha << 24);
257 }
258 
259 // -----------------------------------------------------------------------------
260 // EOF
261 // -----------------------------------------------------------------------------
int MICROVG_HELPER_get_utf(unsigned short *textCharRam, int length, int *offset)
Gets the next UTF character from a text buffer.
MicroEJ MicroVG library low level API: helper to implement library natives methods.
MicroEJ MicroVG library low level API: enable some features according to the hardware capacities...
#define MIN_HIGH_SURROGATE