microvg  7.0.0
microvg
ui_font_drawing_vg.c
1 /*
2  * C
3  *
4  * Copyright 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 
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.0
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  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
91  return (int)ceil(VG_FREETYPE_string_width(chars, length, vg_font->vg_font_handle, vg_font->vg_font_size,
92  LLVG_FONT_DEFAULT_LETTER_SPACING));
93 }
94 
95 // See the header file for the function documentation
96 DRAWING_Status UI_FONT_DRAWING_VG_drawString(MICROUI_GraphicsContext *gc, jchar *chars, jint length, MICROUI_Font *font,
97  jint x, jint y) {
98  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
99  jfloat translated_matrix[LLVG_MATRIX_SIZE];
100  LLVG_MATRIX_IMPL_setTranslate(translated_matrix, x, y);
101  return VG_DRAWING_drawString(gc, chars, length, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix,
102  0xff /* fully opaque */, LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
103 }
104 
105 // See the header file for the function documentation
106 DRAWING_Status UI_FONT_DRAWING_VG_drawScaledStringBilinear(MICROUI_GraphicsContext *gc, jchar *chars, jint length,
107  MICROUI_Font *font, jint x, jint y, jfloat xRatio,
108  jfloat yRatio) {
109  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
110  jfloat translated_matrix[LLVG_MATRIX_SIZE];
111  LLVG_MATRIX_IMPL_setScale(translated_matrix, xRatio, yRatio);
112  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, x, y);
113  return VG_DRAWING_drawString(gc, chars, length, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix,
114  0xff /* fully opaque */, LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
115 }
116 
117 // See the header file for the function documentation
118 DRAWING_Status UI_FONT_DRAWING_VG_drawCharWithRotationBilinear(MICROUI_GraphicsContext *gc, jchar c, MICROUI_Font *font,
119  jint x, jint y, jint xRotation, jint yRotation,
120  jfloat angle, jint alpha) {
121  MICROUI_FontVG *vg_font = (MICROUI_FontVG *)font;
122  jfloat translated_matrix[LLVG_MATRIX_SIZE];
123 
124  // retrieve the non-translated rotation center
125  jint relative_x_rotation = xRotation - x;
126  jint relative_y_rotation = yRotation - y;
127 
128  // rotate around the rotation center
129  LLVG_MATRIX_IMPL_setTranslate(translated_matrix, -relative_x_rotation, -relative_y_rotation);
130  LLVG_MATRIX_IMPL_postRotate(translated_matrix, angle);
131  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, relative_x_rotation, relative_y_rotation);
132 
133  // translate to the destination point
134  LLVG_MATRIX_IMPL_postTranslate(translated_matrix, x, y);
135 
136  return VG_DRAWING_drawString(gc, &c, 1, vg_font->vg_font_handle, vg_font->vg_font_size, translated_matrix, alpha,
137  LLVG_BLEND_SRC_OVER, LLVG_FONT_DEFAULT_LETTER_SPACING);
138 }
139 
140 // See the header file for the function documentation
141 DRAWING_Status UI_FONT_DRAWING_VG_drawCharWithRotationNearestNeighbor(MICROUI_GraphicsContext *gc, jchar c,
142  MICROUI_Font *font, jint x, jint y,
143  jint xRotation, jint yRotation, jfloat angle,
144  jint alpha) {
145  return UI_FONT_DRAWING_VG_drawCharWithRotationBilinear(gc, c, font, x, y, xRotation, yRotation, angle, alpha);
146 }
147 
148 // -----------------------------------------------------------------------------
149 // EOF
150 // -----------------------------------------------------------------------------
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.