microvg  5.0.0
microvg
LLVG_PATH_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2022-2023 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 
21 #include "microvg_configuration.h"
22 
23 #ifdef VG_FEATURE_PATH
24 
25 // -----------------------------------------------------------------------------
26 // Includes
27 // -----------------------------------------------------------------------------
28 
29 #include <math.h>
30 #include <string.h>
31 
32 #include <LLVG_PATH_impl.h>
33 
34 #include "microvg_path.h"
35 #include "microvg_helper.h"
36 #include "bsp_util.h"
37 
38 // -----------------------------------------------------------------------------
39 // Private functions
40 // -----------------------------------------------------------------------------
41 
42 /*
43  * @brief Extends the path to be able to store the command and its parameters.
44  *
45  * @return the offset in path buffer where the command will be stored. If the path
46  * buffer is not large enough to contain the requested command, returns a negative
47  * number corresponding to size the buffer must be enlarged for this command.
48  */
49 static int32_t _extend_path(MICROVG_PATH_HEADER_t* path, jint length, jint cmd, uint32_t nb_fields) {
50  uint32_t index = MICROVG_PATH_get_path_header_size() + path->data_size;
51  uint32_t extra_size = MICROVG_PATH_get_path_command_size(cmd, nb_fields);
52  int32_t ret;
53 
54  if (length >= (index + extra_size)) {
55  path->data_size += extra_size;
56 
57  // return next free space (return a positive value)
58  ret = index;
59  }
60  else {
61  // too small buffer, ret is the required extra size
62  // (return a negative value)
63  ret = -extra_size;
64  }
65 
66  return ret;
67 }
68 
69 static int32_t _close_path(MICROVG_PATH_HEADER_t* path, jint length, jfloat x1, jfloat y1, jfloat x2, jfloat y2) {
70  int32_t index = _extend_path(path, length, LLVG_PATH_CMD_CLOSE, 0);
71  int32_t ret = LLVG_SUCCESS;
72  if (index > 0) {
73  // finalizes the path by storing the path's bounds
74  path->bounds_xmin = x1;
75  path->bounds_xmax = x2;
76  path->bounds_ymin = y1;
77  path->bounds_ymax = y2;
78  (void)MICROVG_PATH_append_path_command0((jbyte*)path, (uint32_t)index, LLVG_PATH_CMD_CLOSE);
79  }
80  else {
81  // too small buffer, ret is the required extra size * -1
82  ret = -index;
83  }
84  return ret;
85 }
86 
87 // -----------------------------------------------------------------------------
88 // Specific path format functions [optional]: weak functions
89 // -----------------------------------------------------------------------------
90 
91 // See the header file for the function documentation
92 BSP_DECLARE_WEAK_FCNT void MICROVG_PATH_initialize(void) {
93  // nothing to do
94 }
95 
96 // See the header file for the function documentation
97 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_get_path_header_size(void) {
98  return sizeof(MICROVG_PATH_HEADER_t);
99 }
100 
101 // See the header file for the function documentation
102 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_get_path_command_size(jint command, uint32_t nbParams) {
103  (void)command;
104  return (nbParams + (uint32_t)1 /* command */) * sizeof(uint32_t);
105 }
106 
107 // See the header file for the function documentation
108 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_append_path_command0(jbyte* path, uint32_t offset, jint cmd) {
109  uint32_t* data = (uint32_t*)(path + offset);
110  *data = MICROVG_PATH_convert_path_command(cmd);
111  return sizeof(uint32_t);
112 }
113 
114 // See the header file for the function documentation
115 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_append_path_command1(jbyte* path, uint32_t offset, jint cmd, jfloat x, jfloat y) {
116  uint32_t* data = (uint32_t*)(path + offset);
117  *data = MICROVG_PATH_convert_path_command(cmd);
118  ++data;
119  *data = JFLOAT_TO_UINT32_t(x);
120  ++data;
121  *data = JFLOAT_TO_UINT32_t(y);
122  return (uint32_t)3 * sizeof(uint32_t);
123 }
124 
125 // See the header file for the function documentation
126 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_append_path_command2(jbyte* path, uint32_t offset, jint cmd, jfloat x1, jfloat y1, jfloat x2, jfloat y2) {
127  uint32_t* data = (uint32_t*)(path + offset);
128  *data = MICROVG_PATH_convert_path_command(cmd);
129  ++data;
130  *data = JFLOAT_TO_UINT32_t(x1);
131  ++data;
132  *data = JFLOAT_TO_UINT32_t(y1);
133  ++data;
134  *data = JFLOAT_TO_UINT32_t(x2);
135  ++data;
136  *data = JFLOAT_TO_UINT32_t(y2);
137  return (uint32_t)5 * sizeof(uint32_t);
138 }
139 
140 // See the header file for the function documentation
141 BSP_DECLARE_WEAK_FCNT uint32_t MICROVG_PATH_append_path_command3(jbyte* path, uint32_t offset, jint cmd, jfloat x1, jfloat y1, jfloat x2, jfloat y2,
142  jfloat x3, jfloat y3) {
143  uint32_t* data = (uint32_t*)(path + offset);
144  *data = MICROVG_PATH_convert_path_command(cmd);
145  ++data;
146  *data = JFLOAT_TO_UINT32_t(x1);
147  ++data;
148  *data = JFLOAT_TO_UINT32_t(y1);
149  ++data;
150  *data = JFLOAT_TO_UINT32_t(x2);
151  ++data;
152  *data = JFLOAT_TO_UINT32_t(y2);
153  ++data;
154  *data = JFLOAT_TO_UINT32_t(x3);
155  ++data;
156  *data = JFLOAT_TO_UINT32_t(y3);
157  return (uint32_t)7 * sizeof(uint32_t);
158 }
159 
160 // -----------------------------------------------------------------------------
161 // LLVG_PATH_impl.h functions
162 // -----------------------------------------------------------------------------
163 
164 // See the header file for the function documentation
165 jint LLVG_PATH_IMPL_initializePath(jbyte* jpath, jint length) {
166 
168  uint32_t header_size = MICROVG_PATH_get_path_header_size();
169  jint ret = LLVG_SUCCESS;
170 
171  if (length >= header_size) {
172  path->data_size = 0;
173  path->format = MICROVG_PATH_get_path_encoder_format();
174  }
175  else {
176  // the given byte array is too small
177  ret = header_size;
178  }
179 
180  return ret;
181 }
182 
183 // See the header file for the function documentation
184 jint LLVG_PATH_IMPL_appendPathCommand1(jbyte* jpath, jint length, jint cmd, jfloat x, jfloat y) {
185 
187  jint ret = LLVG_SUCCESS;
188 
189  int32_t index = _extend_path(path, length, cmd, 2);
190  if (index > 0) {
191  (void)MICROVG_PATH_append_path_command1((jbyte*)path, (uint32_t)index, cmd, x, y);
192  }
193  else {
194  // too small buffer, ret is the required extra size * -1
195  ret = -index;
196  }
197 
198  return ret;
199 }
200 
201 // See the header file for the function documentation
202 jint LLVG_PATH_IMPL_appendPathCommand2(jbyte* jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
203  jfloat y2) {
204 
206  jint ret = LLVG_SUCCESS;
207 
208  if (LLVG_PATH_CMD_CLOSE == cmd) {
209  // parameters are path's bounds
210  ret = _close_path(path, length, x1, y1, x2, y2);
211  }
212  else {
213  int32_t index = _extend_path(path, length, cmd, 4);
214  if (index > 0) {
215  (void)MICROVG_PATH_append_path_command2((jbyte*)path, (uint32_t)index, cmd, x1, y1, x2, y2);
216  }
217  else {
218  // too small buffer, ret is the required extra size * -1
219  ret = -index;
220  }
221  }
222 
223  return ret;
224 }
225 
226 // See the header file for the function documentation
227 jint LLVG_PATH_IMPL_appendPathCommand3(jbyte* jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
228  jfloat y2, jfloat x3, jfloat y3) {
229 
231  jint ret = LLVG_SUCCESS;
232 
233  int32_t index = _extend_path(path, length, cmd, 6);
234  if (index > 0) {
235  (void)MICROVG_PATH_append_path_command3((jbyte*)path, (uint32_t)index, cmd, x1, y1, x2, y2, x3, y3);
236  }
237  else {
238  // too small buffer, ret is the required extra size * -1
239  ret = -index;
240  }
241 
242  return ret;
243 }
244 
245 // See the header file for the function documentation
246 void LLVG_PATH_IMPL_reopenPath(jbyte* jpath) {
248  path->data_size -= MICROVG_PATH_get_path_command_size(LLVG_PATH_CMD_CLOSE, 0);
249 }
250 
251 // -----------------------------------------------------------------------------
252 // EOF
253 // -----------------------------------------------------------------------------
254 
255 #endif // VG_FEATURE_PATH
MicroEJ MicroVG library low level API: helper to implement library natives methods.
MicroEJ MicroVG library low level API: implementation of Path.
MicroEJ MicroVG library low level API: enable some features according to the hardware capacities...