Debian Codegraph @

pencil/source/draw/draw_svg_path_data.h

raw
1 /* File: draw_svg_path_data.h; Copyright and License: see below */
2
3 #ifndef DRAW_SVG_PATH_DATA_H
4 #define DRAW_SVG_PATH_DATA_H
5
6 /* public file for the doxygen documentation: */
7 /*!
8 * \file
9 * \brief Converts svg path data commands to a path in cairo drawing context
10 */
11
12 #include "pencil_size.h"
13 #include "geometry/geometry_h_align.h"
14 #include "geometry/geometry_v_align.h"
15 #include "geometry/geometry_rectangle.h"
16 #include "geometry/geometry_dimensions.h"
17 #include "entity/data_classifier_type.h"
18 #include "set/data_profile_part.h"
19 #include "utf8stringbuf/utf8stringviewtokenizer.h"
20 #include "u8/u8_error.h"
21 #include "u8/u8_error_info.h"
22 #include <cairo.h>
23 #include <stdint.h>
24
25 /*!
26 * \brief attributes of the draw svg path data functions
27 */
28 struct draw_svg_path_data_struct {
29 int dummy; /*!< This object is a collection of stateless drawing functions */
30 };
31
32 typedef struct draw_svg_path_data_struct draw_svg_path_data_t;
33
34 /*!
35 * \brief initializes the draw_svg_path_data_t
36 *
37 * \param this_ pointer to own object attributes
38 */
39 static inline void draw_svg_path_data_init( draw_svg_path_data_t *this_ );
40
41 /*!
42 * \brief destroys the draw_svg_path_data_t
43 *
44 * \param this_ pointer to own object attributes
45 */
46 static inline void draw_svg_path_data_destroy( draw_svg_path_data_t *this_ );
47
48 /*!
49 * \brief parses the svg path data drawing commands, determines the svg view bounds
50 *
51 * \param this_ pointer to own object attributes
52 * \param tok_iterator token iterator of drawing directives. The first token shall be the first draw command.
53 * double quotes or end-of-stream end the processing, the double quotes token is already consumed.
54 * the token iterator must be in mode UTF8STRINGVIEWTOKENMODE_FLOAT_ONLY.
55 * \param[out] out_view_rect bounding rectangle of the svg drawing_directives
56 * \param[out] out_err_info pointer to an error_info_t data struct;
57 * in case of U8_ERROR_PARSER_STRUCTURE, it provides an error description when returning
58 * \return U8_ERROR_NONE if the image was drawn,
59 * U8_ERROR_NOT_FOUND if no image was found,
60 * U8_ERROR_PARSER_STRUCTURE if expected tokens in the input image were missing or in wrong order
61 */
62 static inline u8_error_t draw_svg_path_data_parse_bounds ( const draw_svg_path_data_t *this_,
63 utf8stringviewtokenizer_t *tok_iterator,
64 geometry_rectangle_t *out_view_rect,
65 u8_error_info_t *out_err_info
66 );
67
68 /*!
69 * \brief draws the svg path data drawing commands into the target_bounds rect
70 *
71 * \param this_ pointer to own object attributes
72 * \param tok_iterator token iterator of drawing directives. The first token shall be the first draw command.
73 * double quotes or end-of-stream end the processing, the double quotes token is already consumed.
74 * the token iterator must be in mode UTF8STRINGVIEWTOKENMODE_FLOAT_ONLY.
75 * \param[in] in_view_rect bounding rectangle of the svg drawing_directives; this is needed as input.
76 * \param[out] out_err_info pointer to an error_info_t data struct;
77 * in case of U8_ERROR_PARSER_STRUCTURE, it provides an error description when returning
78 * \param target_bounds bounding rectangle of the cairo drawing directives, to which io_view_rect is scaled to.
79 * \param cr a cairo drawing context
80 * \return U8_ERROR_NONE if the image was drawn,
81 * U8_ERROR_NOT_FOUND if no image was found,
82 * U8_ERROR_PARSER_STRUCTURE if expected tokens in the input image were missing or in wrong order
83 */
84 static inline u8_error_t draw_svg_path_data_draw ( const draw_svg_path_data_t *this_,
85 utf8stringviewtokenizer_t *tok_iterator,
86 const geometry_rectangle_t *in_view_rect,
87 u8_error_info_t *out_err_info,
88 const geometry_rectangle_t *target_bounds,
89 cairo_t *cr
90 );
91
92 /*!
93 * \brief parses the svg path data drawing commands and/or draws these into the target_bounds rect
94 *
95 * \param this_ pointer to own object attributes
96 * \param draw false if only the view_rect shall be determined, true if the drawing_directives shall be drawn
97 * \param tok_iterator token iterator of drawing directives. The first token shall be the first draw command.
98 * double quotes or end-of-stream end the processing, the double quotes token is already consumed.
99 * the token iterator must be in mode UTF8STRINGVIEWTOKENMODE_FLOAT_ONLY.
100 * \param[in,out] io_view_rect bounding rectangle of the svg drawing_directives;
101 * in all cases this is provided as output,
102 * in case of draw==true this is needed as input,
103 * in case of draw==false this is written as output.
104 * \param[out] out_err_info pointer to an error_info_t data struct;
105 * in case of U8_ERROR_PARSER_STRUCTURE, it provides an error description when returning
106 * \param target_bounds bounding rectangle of the cairo drawing directives, to which io_view_rect is scaled to.
107 * \param cr a cairo drawing context or NULL in case draw==false
108 * \return U8_ERROR_NONE if the image was drawn,
109 * U8_ERROR_NOT_FOUND if no image was found,
110 * U8_ERROR_PARSER_STRUCTURE if expected tokens in the input image were missing or in wrong order
111 */
112 u8_error_t draw_svg_path_data_private_parse ( const draw_svg_path_data_t *this_,
113 bool draw,
114 utf8stringviewtokenizer_t *tok_iterator,
115 geometry_rectangle_t *io_view_rect,
116 u8_error_info_t *out_err_info,
117 const geometry_rectangle_t *target_bounds,
118 cairo_t *cr
119 );
120
121 /*!
122 * \brief determines the angle between two 2-dim vectors.
123 *
124 * \param this_ pointer to own object attributes
125 * \param u_x x value of the u vector
126 * \param u_y y value of the u vector
127 * \param v_x x value of the v vector
128 * \param v_y y value of the v vector
129 * \return angle in rad, range -pi..pi
130 */
131 static inline double draw_svg_path_data_private_get_angle ( const draw_svg_path_data_t *this_,
132 double u_x,
133 double u_y,
134 double v_x,
135 double v_y
136 );
137
138 /*!
139 * \brief determines the center, start and delta angles of the arc given the endpoints.
140 *
141 * This function is an implementation of the proposed conversion rules in
142 * https://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
143 *
144 * \param this_ pointer to own object attributes
145 * \param start_x abscissa of the starting point
146 * \param start_y ordinate of the starting point
147 * \param end_x abscissa of the end point
148 * \param end_y ordinate of the end point
149 * \param large_arc true if the arc is spanning more than 180 degree / 1*pi
150 * \param sweep_positive_dir true if the arc is traversed in positive-angle direction
151 * \param r_x major ellipsis radius
152 * \param r_y minor ellipsis radius
153 * \param phi angle between major ellipsis radius and x-axis (unit: rad)
154 * \param[out] out_center_x abscissa of the arc-center point
155 * \param[out] out_center_y ordinate of the arc-center point
156 * \param[out] out_start_angle angle of the starting point (unit: rad)
157 * \param[out] out_delta_angle delta angle from the start to the end point (unit: rad)
158 * \return U8_ERROR_NONE if the provided values are a valid arc,
159 * U8_ERROR_VALUE_OUT_OF_RANGE if provided values are not valid, e.g. start and end points are equal.
160 * U8_ERROR_EDGE_CASE_PARAM if the arc degrades to a straight line from start to end
161 */
162 static inline u8_error_t draw_svg_path_data_private_get_arc_center ( const draw_svg_path_data_t *this_,
163 double start_x,
164 double start_y,
165 double end_x,
166 double end_y,
167 bool large_arc,
168 bool sweep_positive_dir,
169 double r_x,
170 double r_y,
171 double phi,
172 double *out_center_x,
173 double *out_center_y,
174 double *out_start_angle,
175 double *out_delta_angle
176 );
177
178 #include "draw_svg_path_data.inl"
179
180 #endif /* DRAW_SVG_PATH_DATA_H */
181
182
183 /*
184 Copyright 2023-2026 Andreas Warnke
185
186 Licensed under the Apache License, Version 2.0 (the "License");
187 you may not use this file except in compliance with the License.
188 You may obtain a copy of the License at
189
190 http://www.apache.org/licenses/LICENSE-2.0
191
192 Unless required by applicable law or agreed to in writing, software
193 distributed under the License is distributed on an "AS IS" BASIS,
194 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
195 See the License for the specific language governing permissions and
196 limitations under the License.
197 */