microvg  7.0.1
microvg
ui_font_drawing_vg.c
1 /*
2  * C
3  *
4  * Copyright 2024-2025 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 
8 /*
9  * @file
10  * @brief Redirection of all MicroVG library drawing functions. This file is required
11  * (must be compiled in the C project) but should not be modified (except when the VEE
12  * port uses more than 3 destination formats, see below).
13  *
14  * This file follows strictly the same concept as ui_drawing.c but for MicroVG
15  * library.
16  *
17  * @author MicroEJ Developer Team
18  * @version 7.0.1
19  * @see ui_drawing.c
20  */
21 
22 // --------------------------------------------------------------------------------
23 // Includes
24 // --------------------------------------------------------------------------------
25 
26 #include <math.h>
27 
28 #include <LLVG_FONT_impl.h>
29 #include <LLVG_MATRIX_impl.h>
30 
31 #include "ui_font_drawing.h"
32 #include "vg_drawing.h"
33 #include "vg_freetype.h"
34 
35 // --------------------------------------------------------------------------------
36 // Defines
37 // --------------------------------------------------------------------------------
38 
39 /*
40  * @brief Redirects all ui_font_drawing.h functions to this file
41  */
42 #define UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX GET_CUSTOM_FONT_FUNCTIONS_SUFFIX(UI_FONT_FORMAT_VG)
43 #define UI_FONT_DRAWING_VG_stringWidth CONCAT(UI_FONT_DRAWING_stringWidth_custom, UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
44 #define UI_FONT_DRAWING_VG_initializeRenderableStringSNIContext CONCAT( \
45  UI_FONT_DRAWING_initializeRenderableStringSNIContext_custom, UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
46 #define UI_FONT_DRAWING_VG_drawString CONCAT(UI_FONT_DRAWING_drawString_custom, UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
47 #define UI_FONT_DRAWING_VG_drawRenderableString CONCAT(UI_FONT_DRAWING_drawRenderableString_custom, \
48  UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
49 #define UI_FONT_DRAWING_VG_drawScaledStringBilinear CONCAT(UI_FONT_DRAWING_drawScaledStringBilinear_custom, \
50  UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
51 #define UI_FONT_DRAWING_VG_drawScaledRenderableStringBilinear CONCAT( \
52  UI_FONT_DRAWING_drawScaledRenderableStringBilinear_custom, UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
53 #define UI_FONT_DRAWING_VG_drawCharWithRotationBilinear CONCAT(UI_FONT_DRAWING_drawCharWithRotationBilinear_custom, \
54  UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
55 #define UI_FONT_DRAWING_VG_drawCharWithRotationNearestNeighbor CONCAT( \
56  UI_FONT_DRAWING_drawCharWithRotationNearestNeighbor_custom, UI_FONT_DRAWING_VG_FUNCTIONS_SUFFIX)
57 
58 // --------------------------------------------------------------------------------
59 // Typedefs
60 // --------------------------------------------------------------------------------
61 
67 typedef struct {
71  MICROUI_Font ui_font;
72 
77 
83 
84 // --------------------------------------------------------------------------------
85 // ui_font_drawing.h functions
86 // --------------------------------------------------------------------------------
87 
88 // See the header file for the function documentation
89 jint UI_FONT_DRAWING_VG_stringWidth(jchar *chars, jint length, MICROUI_Font *font) {
90 #if defined VG_FEATURE_FONT && \
91  (defined VG_FEATURE_FONT_FREETYPE_VECTOR || defined VG_FEATURE_FONT_FREETYPE_BITMAP) && \
92  (VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_VECTOR || VG_FEATURE_FONT == VG_FEATURE_FONT_FREETYPE_BITMAP)
93  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
94  return (int)ceil(VG_FREETYPE_string_width(chars, length, vg_font->vg_font_handle, vg_font->vg_font_size,
95  LLVG_FONT_DEFAULT_LETTER_SPACING));
96 #else
97  (void)chars;
98  (void)length;
99  (void)font;
100  return 0;
101 #endif
102 }
103 
104 // See the header file for the function documentation
105 DRAWING_Status UI_FONT_DRAWING_VG_drawString(MICROUI_GraphicsContext *gc, jchar *chars, jint length, MICROUI_Font *font,
106  jint x, jint y) {
107  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
108  jfloat translated_matrix[LLVG_MATRIX_SIZE];
109  LLVG_MATRIX_IMPL_setTranslate(translated_matrix, x, y);
110  return VG_DRAWING_drawString(gc, chars, length, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix,
111  0xff /* fully opaque */, LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
112 }
113 
114 // See the header file for the function documentation
115 DRAWING_Status UI_FONT_DRAWING_VG_drawScaledStringBilinear(MICROUI_GraphicsContext *gc, jchar *chars, jint length,
116  MICROUI_Font *font, jint x, jint y, jfloat xRatio,
117  jfloat yRatio) {
118  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
119  jfloat translated_matrix[LLVG_MATRIX_SIZE];
120  LLVG_MATRIX_IMPL_setScale(translated_matrix, xRatio, yRatio);
121  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, x, y);
122  return VG_DRAWING_drawString(gc, chars, length, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix,
123  0xff /* fully opaque */, LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
124 }
125 
126 // See the header file for the function documentation
127 DRAWING_Status UI_FONT_DRAWING_VG_drawCharWithRotationBilinear(MICROUI_GraphicsContext *gc, jchar c, MICROUI_Font *font,
128  jint x, jint y, jint xRotation, jint yRotation,
129  jfloat angle, jint alpha) {
130  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
131  jfloat translated_matrix[LLVG_MATRIX_SIZE];
132 
133  // retrieve the non-translated rotation center
134  jint relative_x_rotation = xRotation - x;
135  jint relative_y_rotation = yRotation - y;
136 
137  // rotate around the rotation center
138  LLVG_MATRIX_IMPL_setTranslate(translated_matrix, -relative_x_rotation, -relative_y_rotation);
139  LLVG_MATRIX_IMPL_postRotate(translated_matrix, angle);
140  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, relative_x_rotation, relative_y_rotation);
141 
142  // translate to the destination point
143  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, x, y);
144 
145  return VG_DRAWING_drawString(gc, &c, 1, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix, alpha,
146  LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
147 }
148 
149 // See the header file for the function documentation
150 DRAWING_Status UI_FONT_DRAWING_VG_drawCharWithRotationNearestNeighbor(MICROUI_GraphicsContext *gc, jchar c,
151  MICROUI_Font *font, jint x, jint y,
152  jint xRotation, jint yRotation, jfloat angle,
153  jint alpha) {
154  return UI_FONT_DRAWING_VG_drawCharWithRotationBilinear(gc, c, font, x, y, xRotation, yRotation, angle, alpha);
155 }
156 
157 // -----------------------------------------------------------------------------
158 // EOF
159 // -----------------------------------------------------------------------------
Represents a MicroUI Font extended by MicroVG.
MICROUI_Font ui_font
The MicroUI Font header.
jint vg_font_size
The size to apply on the MicroVG Font.
jint vg_font_handle
The identifier of the MicroVG Font.
MicroEJ MicroVG library low level API: implementation over FreeType.