sciter/capi/
scgraphics.rs

1//! Sciter's platform independent graphics interface.
2
3#![allow(non_camel_case_types, non_snake_case)]
4#![allow(dead_code)]
5
6use capi::scdom::HELEMENT;
7use capi::sctypes::{BOOL, LPCBYTE, LPCWSTR, LPVOID, UINT};
8use capi::scvalue::VALUE;
9
10MAKE_HANDLE!(#[doc = "Graphics native handle."] HGFX, _HGFX);
11MAKE_HANDLE!(#[doc = "Image native handle."] HIMG, _HIMG);
12MAKE_HANDLE!(#[doc = "Path native handle."] HPATH, _HPATH);
13MAKE_HANDLE!(#[doc = "Text native handle."] HTEXT, _HTEXT);
14
15pub type SC_REAL = f32;
16pub type SC_POS = SC_REAL;
17pub type SC_DIM = SC_REAL;
18pub type SC_ANGLE = SC_REAL;
19
20pub type SC_COLOR = u32;
21
22
23#[repr(C)]
24#[derive(Debug, PartialEq)]
25/// Type of the result value for Sciter Graphics functions.
26pub enum GRAPHIN_RESULT {
27	/// E.g. not enough memory.
28  PANIC = -1,
29  /// Success.
30  OK = 0,
31  /// Bad parameter.
32  BAD_PARAM = 1,
33  /// Operation failed, e.g. `restore()` without `save()`.
34  FAILURE = 2,
35  /// Platform does not support the requested feature.
36  NOTSUPPORTED = 3,
37}
38
39impl std::error::Error for GRAPHIN_RESULT {}
40
41impl std::fmt::Display for GRAPHIN_RESULT {
42	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43		write!(f, "{:?}", self)
44	}
45}
46
47
48/// Path drawing mode.
49#[repr(C)]
50#[derive(Debug, PartialEq)]
51pub enum DRAW_PATH {
52  /// Draw without outline line.
53  FILL_ONLY = 1,
54  /// Draw outline without fill.
55  STROKE_ONLY = 2,
56  /// Draw outlined and filled path.
57  FILL_AND_STROKE = 3,
58}
59
60/// Line drawing join mode.
61#[repr(C)]
62#[derive(Debug, PartialEq)]
63pub enum LINE_JOIN {
64  /// Specifies a mitered join. This produces a sharp corner or a clipped corner,
65  /// depending on whether the length of the miter exceeds the miter limit (`10.0`).
66  MITER = 0,
67  /// Specifies a circular join. This produces a smooth, circular arc between the lines.
68  ROUND = 1,
69  /// Specifies a beveled join. This produces a diagonal corner.
70  BEVEL = 2,
71  /// Specifies a mitered join. This produces a sharp corner or a beveled corner,
72  /// depending on whether the length of the miter exceeds the miter limit (`10.0`).
73  MITER_OR_BEVEL = 3,
74}
75
76/// Line drawing cap mode.
77#[repr(C)]
78#[derive(Debug, PartialEq)]
79pub enum LINE_CAP {
80  /// The ends of lines are squared off at the endpoints.
81  BUTT = 0,
82  /// The ends of lines are squared off by adding a box with an equal width
83  /// and half the height of the line's thickness.
84  SQUARE = 1,
85  /// The ends of lines are rounded.
86  ROUND = 2,
87}
88
89#[repr(C)]
90#[derive(Debug, PartialEq)]
91pub enum IMAGE_ENCODING {
92  RAW, // [a,b,g,r,a,b,g,r,...] vector
93  PNG,
94  JPG,
95  WEBP,
96}
97
98#[repr(C)]
99#[derive(Debug)]
100pub struct SC_COLOR_STOP {
101  pub color: SC_COLOR,
102  pub offset: f32,
103}
104
105
106#[repr(C)]
107#[allow(missing_docs)]
108pub struct SciterGraphicsAPI {
109  // image primitives
110  pub imageCreate: extern "system" fn(poutImg: &mut HIMG, width: UINT, height: UINT, withAlpha: BOOL) -> GRAPHIN_RESULT,
111
112  // construct image from B[n+0],G[n+1],R[n+2],A[n+3] data.
113  // Size of pixmap data is pixmapWidth*pixmapHeight*4
114  pub imageCreateFromPixmap:
115    extern "system" fn(poutImg: &mut HIMG, pixmapWidth: UINT, pixmapHeight: UINT, withAlpha: BOOL, pixmap: LPCBYTE) -> GRAPHIN_RESULT,
116
117  pub imageAddRef: extern "system" fn(himg: HIMG) -> GRAPHIN_RESULT,
118
119  pub imageRelease: extern "system" fn(himg: HIMG) -> GRAPHIN_RESULT,
120
121  pub imageGetInfo: extern "system" fn(himg: HIMG, width: &mut UINT, height: &mut UINT, usesAlpha: &mut BOOL) -> GRAPHIN_RESULT,
122
123  pub imageClear: extern "system" fn(himg: HIMG, byColor: SC_COLOR) -> GRAPHIN_RESULT,
124
125  pub imageLoad: extern "system" fn(bytes: LPCBYTE, num_bytes: UINT, pout_img: &mut HIMG) -> GRAPHIN_RESULT, // load png/jpeg/etc. image from stream of bytes
126
127  pub imageSave: extern "system" fn(himg: HIMG, pfn: ImageWriteFunction, prm: LPVOID, encoding: IMAGE_ENCODING, quality: UINT) -> GRAPHIN_RESULT,
128
129  // SECTION: graphics primitives and drawing operations
130
131  // create SC_COLOR value
132  pub RGBA: extern "system" fn(red: UINT, green: UINT, blue: UINT, alpha: UINT) -> SC_COLOR,
133
134  pub gCreate: extern "system" fn(img: HIMG, pout_gfx: &mut HGFX) -> GRAPHIN_RESULT,
135
136  pub gAddRef: extern "system" fn(gfx: HGFX) -> GRAPHIN_RESULT,
137
138  pub gRelease: extern "system" fn(gfx: HGFX) -> GRAPHIN_RESULT,
139
140  // Draws line from x1,y1 to x2,y2 using current lineColor and lineGradient.
141  pub gLine: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS) -> GRAPHIN_RESULT,
142
143  // Draws rectangle using current lineColor/lineGradient and fillColor/fillGradient with (optional) rounded corners.
144  pub gRectangle: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS) -> GRAPHIN_RESULT,
145
146  // Draws rounded rectangle using current lineColor/lineGradient and fillColor/fillGradient with (optional) rounded corners.
147  pub gRoundedRectangle: extern "system" fn(
148    hgfx: HGFX,
149    x1: SC_POS,
150    y1: SC_POS,
151    x2: SC_POS,
152    y2: SC_POS,
153    radii8: *const SC_DIM,
154  ) -> GRAPHIN_RESULT,
155
156  // Draws circle or ellipse using current lineColor/lineGradient and fillColor/fillGradient.
157  pub gEllipse: extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM) -> GRAPHIN_RESULT,
158
159  // Draws closed arc using current lineColor/lineGradient and fillColor/fillGradient.
160  pub gArc:
161    extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_POS, ry: SC_POS, start: SC_ANGLE, sweep: SC_ANGLE) -> GRAPHIN_RESULT,
162
163  // Draws star.
164  pub gStar: extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, r1: SC_DIM, r2: SC_DIM, start: SC_ANGLE, rays: UINT) -> GRAPHIN_RESULT,
165
166  // Closed polygon.
167  pub gPolygon: extern "system" fn(hgfx: HGFX, xy: *const SC_POS, num_points: UINT) -> GRAPHIN_RESULT,
168
169  // Polyline.
170  pub gPolyline: extern "system" fn(hgfx: HGFX, xy: *const SC_POS, num_points: UINT) -> GRAPHIN_RESULT,
171
172  // SECTION: Path operations
173  pub pathCreate: extern "system" fn(path: &mut HPATH) -> GRAPHIN_RESULT,
174
175  pub pathAddRef: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,
176
177  pub pathRelease: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,
178
179  pub pathMoveTo: extern "system" fn(path: HPATH, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,
180
181  pub pathLineTo: extern "system" fn(path: HPATH, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,
182
183  pub pathArcTo: extern "system" fn(
184    path: HPATH,
185    x: SC_POS,
186    y: SC_POS,
187    angle: SC_ANGLE,
188    rx: SC_DIM,
189    ry: SC_DIM,
190    is_large_arc: BOOL,
191    clockwise: BOOL,
192    relative: BOOL,
193  ) -> GRAPHIN_RESULT,
194
195  pub pathQuadraticCurveTo: extern "system" fn(path: HPATH, xc: SC_POS, yc: SC_POS, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,
196
197  pub pathBezierCurveTo:
198    extern "system" fn(path: HPATH, xc1: SC_POS, yc1: SC_POS, xc2: SC_POS, yc2: SC_POS, x: SC_POS, y: SC_POS, relative: BOOL)
199      -> GRAPHIN_RESULT,
200
201  pub pathClosePath: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,
202
203  pub gDrawPath: extern "system" fn(hgfx: HGFX, path: HPATH, dpm: DRAW_PATH) -> GRAPHIN_RESULT,
204
205  // end of path opearations
206
207// SECTION: affine tranformations:
208  pub gRotate: extern "system" fn(hgfx: HGFX, radians: SC_ANGLE, cx: Option<&SC_POS>, cy: Option<&SC_POS>) -> GRAPHIN_RESULT,
209
210  pub gTranslate: extern "system" fn(hgfx: HGFX, cx: SC_POS, cy: SC_POS) -> GRAPHIN_RESULT,
211
212  pub gScale: extern "system" fn(hgfx: HGFX, x: SC_DIM, y: SC_DIM) -> GRAPHIN_RESULT,
213
214  pub gSkew: extern "system" fn(hgfx: HGFX, dx: SC_DIM, dy: SC_DIM) -> GRAPHIN_RESULT,
215
216  // all above in one shot
217  pub gTransform:
218    extern "system" fn(hgfx: HGFX, m11: SC_POS, m12: SC_POS, m21: SC_POS, m22: SC_POS, dx: SC_POS, dy: SC_POS) -> GRAPHIN_RESULT,
219
220  // end of affine tranformations.
221
222// SECTION: state save/restore
223  pub gStateSave: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,
224
225  pub gStateRestore: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,
226
227  // end of state save/restore
228
229// SECTION: drawing attributes
230
231  // set line width for subsequent drawings.
232  pub gLineWidth: extern "system" fn(hgfx: HGFX, width: SC_DIM) -> GRAPHIN_RESULT,
233
234  pub gLineJoin: extern "system" fn(hgfx: HGFX, join_type: LINE_JOIN) -> GRAPHIN_RESULT,
235
236  pub gLineCap: extern "system" fn(hgfx: HGFX, cap_type: LINE_CAP) -> GRAPHIN_RESULT,
237
238  // SC_COLOR for solid lines/strokes
239  pub gLineColor: extern "system" fn(hgfx: HGFX, color: SC_COLOR) -> GRAPHIN_RESULT,
240
241  // SC_COLOR for solid fills
242  pub gFillColor: extern "system" fn(hgfx: HGFX, color: SC_COLOR) -> GRAPHIN_RESULT,
243
244  // setup parameters of linear gradient of lines.
245  pub gLineGradientLinear:
246    extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, stops: *const SC_COLOR_STOP, nstops: UINT)
247      -> GRAPHIN_RESULT,
248
249  // setup parameters of linear gradient of fills.
250  pub gFillGradientLinear:
251    extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, stops: *const SC_COLOR_STOP, nstops: UINT)
252      -> GRAPHIN_RESULT,
253
254  // setup parameters of line gradient radial fills.
255  pub gLineGradientRadial:
256    extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM, stops: *const SC_COLOR_STOP, nstops: UINT)
257      -> GRAPHIN_RESULT,
258
259  // setup parameters of gradient radial fills.
260  pub gFillGradientRadial:
261    extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM, stops: *const SC_COLOR_STOP, nstops: UINT)
262      -> GRAPHIN_RESULT,
263
264  pub gFillMode: extern "system" fn(hgfx: HGFX, even_odd: BOOL) -> GRAPHIN_RESULT,
265
266  // SECTION: text
267
268  // create text layout for host element
269  pub textCreateForElement: extern "system" fn(ptext: &mut HTEXT, text: LPCWSTR, textLength: UINT, he: HELEMENT, classNameOrNull: LPCWSTR) -> GRAPHIN_RESULT,
270
271  // create text layout using explicit style declaration
272  pub textCreateForElementAndStyle:
273    extern "system" fn(ptext: &mut HTEXT, text: LPCWSTR, textLength: UINT, he: HELEMENT, style: LPCWSTR, styleLength: UINT) -> GRAPHIN_RESULT,
274
275  // since 4.1.10
276  pub textAddRef: extern "system" fn(text: HTEXT) -> GRAPHIN_RESULT,
277
278  // since 4.1.10
279  pub textRelease: extern "system" fn(text: HTEXT) -> GRAPHIN_RESULT,
280
281  pub textGetMetrics: extern "system" fn(
282    text: HTEXT,
283    minWidth: &mut SC_DIM,
284    maxWidth: &mut SC_DIM,
285    height: &mut SC_DIM,
286    ascent: &mut SC_DIM,
287    descent: &mut SC_DIM,
288    nLines: &mut UINT,
289  ) -> GRAPHIN_RESULT,
290
291  pub textSetBox: extern "system" fn(text: HTEXT, width: SC_DIM, height: SC_DIM) -> GRAPHIN_RESULT,
292
293  // draw text with position (1..9 on MUMPAD) at px,py
294  // Ex: gDrawText(100,100,5) will draw text box with its center at 100,100 px
295  pub gDrawText: extern "system" fn(hgfx: HGFX, text: HTEXT, px: SC_POS, py: SC_POS, position: UINT) -> GRAPHIN_RESULT,
296
297  // SECTION: image rendering
298
299  // draws img onto the graphics surface with current transformation applied (scale, rotation).
300  #[allow(clippy::type_complexity)]
301  pub gDrawImage: extern "system" fn(
302    hgfx: HGFX,
303    himg: HIMG,
304    x: SC_POS,
305    y: SC_POS,
306    w: Option<&SC_DIM>,
307    h: Option<&SC_DIM>,
308    ix: Option<&UINT>,
309    iy: Option<&UINT>,
310    iw: Option<&UINT>,
311    ih: Option<&UINT>,
312    opacity: Option<&f32>,
313  ) -> GRAPHIN_RESULT,
314
315  // SECTION: coordinate space
316  pub gWorldToScreen: extern "system" fn(hgfx: HGFX, inout_x: &mut SC_POS, inout_y: &mut SC_POS) -> GRAPHIN_RESULT,
317
318  pub gScreenToWorld: extern "system" fn(hgfx: HGFX, inout_x: &mut SC_POS, inout_y: &mut SC_POS) -> GRAPHIN_RESULT,
319
320  // SECTION: clipping
321  pub gPushClipBox: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, opacity: f32) -> GRAPHIN_RESULT,
322
323  pub gPushClipPath: extern "system" fn(hgfx: HGFX, hpath: HPATH, opacity: f32) -> GRAPHIN_RESULT,
324
325  // pop clip layer previously set by gPushClipBox or gPushClipPath
326  pub gPopClip: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,
327
328  // image painter
329  pub imagePaint: extern "system" fn(himg: HIMG, pPainter: ImagePaintFunction, prm: LPVOID) -> GRAPHIN_RESULT, // paint on image using graphics
330
331  // VALUE interface
332  pub vWrapGfx: extern "system" fn(hgfx: HGFX, toValue: *mut VALUE) -> GRAPHIN_RESULT,
333
334  pub vWrapImage: extern "system" fn(himg: HIMG, toValue: *mut VALUE) -> GRAPHIN_RESULT,
335
336  pub vWrapPath: extern "system" fn(hpath: HPATH, toValue: *mut VALUE) -> GRAPHIN_RESULT,
337
338  pub vWrapText: extern "system" fn(htext: HTEXT, toValue: *mut VALUE) -> GRAPHIN_RESULT,
339
340  pub vUnWrapGfx: extern "system" fn(fromValue: *const VALUE, phgfx: &mut HGFX) -> GRAPHIN_RESULT,
341
342  pub vUnWrapImage: extern "system" fn(fromValue: *const VALUE, phimg: &mut HIMG) -> GRAPHIN_RESULT,
343
344  pub vUnWrapPath: extern "system" fn(fromValue: *const VALUE, phpath: &mut HPATH) -> GRAPHIN_RESULT,
345
346  pub vUnWrapText: extern "system" fn(fromValue: *const VALUE, phtext: &mut HTEXT) -> GRAPHIN_RESULT,
347
348	// since 4.4.3.20
349  pub gFlush: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,
350}
351
352pub type ImageWriteFunction = extern "system" fn(prm: LPVOID, data: LPCBYTE, data_length: UINT);
353pub type ImagePaintFunction = extern "system" fn(prm: LPVOID, hgfx: HGFX, width: UINT, height: UINT);