microvg  3.0.1
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 = path->data_offset + 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->data_offset = header_size;
174  path->format = MICROVG_PATH_get_path_encoder_format();
175  }
176  else {
177  // the given byte array is too small
178  ret = header_size;
179  }
180 
181  return ret;
182 }
183 
184 // See the header file for the function documentation
185 jint LLVG_PATH_IMPL_appendPathCommand1(jbyte* jpath, jint length, jint cmd, jfloat x, jfloat y) {
186 
188  jint ret = LLVG_SUCCESS;
189 
190  int32_t index = _extend_path(path, length, cmd, 2);
191  if (index > 0) {
192  (void)MICROVG_PATH_append_path_command1((jbyte*)path, (uint32_t)index, cmd, x, y);
193  }
194  else {
195  // too small buffer, ret is the required extra size * -1
196  ret = -index;
197  }
198 
199  return ret;
200 }
201 
202 // See the header file for the function documentation
203 jint LLVG_PATH_IMPL_appendPathCommand2(jbyte* jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
204  jfloat y2) {
205 
207  jint ret = LLVG_SUCCESS;
208 
209  if (LLVG_PATH_CMD_CLOSE == cmd) {
210  // parameters are path's bounds
211  ret = _close_path(path, length, x1, y1, x2, y2);
212  }
213  else {
214  int32_t index = _extend_path(path, length, cmd, 4);
215  if (index > 0) {
216  (void)MICROVG_PATH_append_path_command2((jbyte*)path, (uint32_t)index, cmd, x1, y1, x2, y2);
217  }
218  else {
219  // too small buffer, ret is the required extra size * -1
220  ret = -index;
221  }
222  }
223 
224  return ret;
225 }
226 
227 // See the header file for the function documentation
228 jint LLVG_PATH_IMPL_appendPathCommand3(jbyte* jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
229  jfloat y2, jfloat x3, jfloat y3) {
230 
232  jint ret = LLVG_SUCCESS;
233 
234  int32_t index = _extend_path(path, length, cmd, 6);
235  if (index > 0) {
236  (void)MICROVG_PATH_append_path_command3((jbyte*)path, (uint32_t)index, cmd, x1, y1, x2, y2, x3, y3);
237  }
238  else {
239  // too small buffer, ret is the required extra size * -1
240  ret = -index;
241  }
242 
243  return ret;
244 }
245 
246 // See the header file for the function documentation
247 void LLVG_PATH_IMPL_reopenPath(jbyte* jpath) {
249  path->data_size -= MICROVG_PATH_get_path_command_size(LLVG_PATH_CMD_CLOSE, 0);
250 }
251 
252 // -----------------------------------------------------------------------------
253 // EOF
254 // -----------------------------------------------------------------------------
255 
256 #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...