1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Sciter's platform independent graphics interface.

#![allow(non_camel_case_types, non_snake_case)]
#![allow(dead_code)]

use capi::scdom::HELEMENT;
use capi::sctypes::{BOOL, LPCBYTE, LPCWSTR, LPVOID, LPWSTR, UINT};
use capi::scvalue::VALUE;

MAKE_HANDLE!(#[doc = "Graphics native handle."] HGFX, _HGFX);
MAKE_HANDLE!(#[doc = "Image native handle."] HIMG, _HIMG);
MAKE_HANDLE!(#[doc = "Path native handle."] HPATH, _HPATH);
MAKE_HANDLE!(#[doc = "Text native handle."] HTEXT, _HTEXT);

pub type SC_REAL = f32;
pub type SC_POS = SC_REAL;
pub type SC_DIM = SC_REAL;
pub type SC_ANGLE = SC_REAL;

pub type SC_COLOR = u32;


#[repr(C)]
#[derive(Debug, PartialEq)]
/// Type of the result value for Sciter Graphics functions.
pub enum GRAPHIN_RESULT {
	/// E.g. not enough memory.
  PANIC = -1,
  /// Success.
  OK = 0,
  /// Bad parameter.
  BAD_PARAM = 1,
  /// Operation failed, e.g. `restore()` without `save()`.
  FAILURE = 2,
  /// Platform does not support the requested feature.
  NOTSUPPORTED = 3,
}

/// Path drawing mode.
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum DRAW_PATH {
  /// Draw without outline line.
  FILL_ONLY = 1,
  /// Draw outline without fill.
  STROKE_ONLY = 2,
  /// Draw outlined and filled path.
  FILL_AND_STROKE = 3,
}

/// Line drawing join mode.
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum LINE_JOIN {
  /// Specifies a mitered join. This produces a sharp corner or a clipped corner,
  /// depending on whether the length of the miter exceeds the miter limit (`10.0`).
  MITER = 0,
  /// Specifies a circular join. This produces a smooth, circular arc between the lines.
  ROUND = 1,
  /// Specifies a beveled join. This produces a diagonal corner.
  BEVEL = 2,
  /// Specifies a mitered join. This produces a sharp corner or a beveled corner,
  /// depending on whether the length of the miter exceeds the miter limit (`10.0`).
  MITER_OR_BEVEL = 3,
}

/// Line drawing cap mode.
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum LINE_CAP {
  /// The ends of lines are squared off at the endpoints.
  BUTT = 0,
  /// The ends of lines are squared off by adding a box with an equal width
  /// and half the height of the line's thickness.
  SQUARE = 1,
  /// The ends of lines are rounded.
  ROUND = 2,
}

#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum TEXT_ALIGNMENT {
  DEFAULT,
  START,
  END,
  CENTER,
}

#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum TEXT_DIRECTION {
  DEFAULT,
  LTR,
  RTL,
  TTB,
}

#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum IMAGE_ENCODING {
  RAW, // [a,b,g,r,a,b,g,r,...] vector
  PNG,
  JPG,
  WEBP,
}

#[repr(C)]
#[derive(Debug)]
pub struct SC_COLOR_STOP {
  pub color: SC_COLOR,
  pub offset: f32,
}

#[repr(C)]
#[derive(Debug)]
pub struct TEXT_FORMAT {
  pub fontFamily: LPWSTR,
  pub fontWeight: UINT,
  pub fontItalic: BOOL, // 100...900, 400 - normal, 700 - bold
  pub fontSize: f32,    // dips
  pub lineHeight: f32,  // dips
  pub textDirection: TEXT_DIRECTION,
  pub textAlignment: TEXT_ALIGNMENT,
  pub lineAlignment: TEXT_ALIGNMENT, // horizontal alignment
  pub localeName: LPWSTR,                   // a.k.a. vertical alignment for roman writing systems
}

#[repr(C)]
#[allow(missing_docs)]
pub struct SciterGraphicsAPI {
  // image primitives
  pub imageCreate: extern "system" fn(poutImg: &mut HIMG, width: UINT, height: UINT, withAlpha: BOOL) -> GRAPHIN_RESULT,

  // construct image from B[n+0],G[n+1],R[n+2],A[n+3] data.
  // Size of pixmap data is pixmapWidth*pixmapHeight*4
  pub imageCreateFromPixmap:
    extern "system" fn(poutImg: &mut HIMG, pixmapWidth: UINT, pixmapHeight: UINT, withAlpha: BOOL, pixmap: LPCBYTE) -> GRAPHIN_RESULT,

  pub imageAddRef: extern "system" fn(himg: HIMG) -> GRAPHIN_RESULT,

  pub imageRelease: extern "system" fn(himg: HIMG) -> GRAPHIN_RESULT,

  pub imageGetInfo: extern "system" fn(himg: HIMG, width: &mut UINT, height: &mut UINT, usesAlpha: &mut BOOL) -> GRAPHIN_RESULT,

  pub imageClear: extern "system" fn(himg: HIMG, byColor: SC_COLOR) -> GRAPHIN_RESULT,

  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

  pub imageSave: extern "system" fn(himg: HIMG, pfn: ImageWriteFunction, prm: LPVOID, encoding: IMAGE_ENCODING, quality: UINT) -> GRAPHIN_RESULT,

  // SECTION: graphics primitives and drawing operations

  // create SC_COLOR value
  pub RGBA: extern "system" fn(red: UINT, green: UINT, blue: UINT, alpha: UINT) -> SC_COLOR,

  pub gCreate: extern "system" fn(img: HIMG, pout_gfx: &mut HGFX) -> GRAPHIN_RESULT,

  pub gAddRef: extern "system" fn(gfx: HGFX) -> GRAPHIN_RESULT,

  pub gRelease: extern "system" fn(gfx: HGFX) -> GRAPHIN_RESULT,

  // Draws line from x1,y1 to x2,y2 using current lineColor and lineGradient.
  pub gLine: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS) -> GRAPHIN_RESULT,

  // Draws rectangle using current lineColor/lineGradient and fillColor/fillGradient with (optional) rounded corners.
  pub gRectangle: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS) -> GRAPHIN_RESULT,

  // Draws rounded rectangle using current lineColor/lineGradient and fillColor/fillGradient with (optional) rounded corners.
  pub gRoundedRectangle: extern "system" fn(
    hgfx: HGFX,
    x1: SC_POS,
    y1: SC_POS,
    x2: SC_POS,
    y2: SC_POS,
    radii8: *const SC_DIM,
  ) -> GRAPHIN_RESULT,

  // Draws circle or ellipse using current lineColor/lineGradient and fillColor/fillGradient.
  pub gEllipse: extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM) -> GRAPHIN_RESULT,

  // Draws closed arc using current lineColor/lineGradient and fillColor/fillGradient.
  pub gArc:
    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,

  // Draws star.
  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,

  // Closed polygon.
  pub gPolygon: extern "system" fn(hgfx: HGFX, xy: *const SC_POS, num_points: UINT) -> GRAPHIN_RESULT,

  // Polyline.
  pub gPolyline: extern "system" fn(hgfx: HGFX, xy: *const SC_POS, num_points: UINT) -> GRAPHIN_RESULT,

  // SECTION: Path operations
  pub pathCreate: extern "system" fn(path: &mut HPATH) -> GRAPHIN_RESULT,

  pub pathAddRef: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,

  pub pathRelease: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,

  pub pathMoveTo: extern "system" fn(path: HPATH, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,

  pub pathLineTo: extern "system" fn(path: HPATH, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,

  pub pathArcTo: extern "system" fn(
    path: HPATH,
    x: SC_POS,
    y: SC_POS,
    angle: SC_ANGLE,
    rx: SC_DIM,
    ry: SC_DIM,
    is_large_arc: BOOL,
    clockwise: BOOL,
    relative: BOOL,
  ) -> GRAPHIN_RESULT,

  pub pathQuadraticCurveTo: extern "system" fn(path: HPATH, xc: SC_POS, yc: SC_POS, x: SC_POS, y: SC_POS, relative: BOOL) -> GRAPHIN_RESULT,

  pub pathBezierCurveTo:
    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)
      -> GRAPHIN_RESULT,

  pub pathClosePath: extern "system" fn(path: HPATH) -> GRAPHIN_RESULT,

  pub gDrawPath: extern "system" fn(hgfx: HGFX, path: HPATH, dpm: DRAW_PATH) -> GRAPHIN_RESULT,

  // end of path opearations

// SECTION: affine tranformations:
  pub gRotate: extern "system" fn(hgfx: HGFX, radians: SC_ANGLE, cx: Option<&SC_POS>, cy: Option<&SC_POS>) -> GRAPHIN_RESULT,

  pub gTranslate: extern "system" fn(hgfx: HGFX, cx: SC_POS, cy: SC_POS) -> GRAPHIN_RESULT,

  pub gScale: extern "system" fn(hgfx: HGFX, x: SC_DIM, y: SC_DIM) -> GRAPHIN_RESULT,

  pub gSkew: extern "system" fn(hgfx: HGFX, dx: SC_DIM, dy: SC_DIM) -> GRAPHIN_RESULT,

  // all above in one shot
  pub gTransform:
    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,

  // end of affine tranformations.

// SECTION: state save/restore
  pub gStateSave: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,

  pub gStateRestore: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,

  // end of state save/restore

// SECTION: drawing attributes

  // set line width for subsequent drawings.
  pub gLineWidth: extern "system" fn(hgfx: HGFX, width: SC_DIM) -> GRAPHIN_RESULT,

  pub gLineJoin: extern "system" fn(hgfx: HGFX, join_type: LINE_JOIN) -> GRAPHIN_RESULT,

  pub gLineCap: extern "system" fn(hgfx: HGFX, cap_type: LINE_CAP) -> GRAPHIN_RESULT,

  // SC_COLOR for solid lines/strokes
  pub gLineColor: extern "system" fn(hgfx: HGFX, color: SC_COLOR) -> GRAPHIN_RESULT,

  // SC_COLOR for solid fills
  pub gFillColor: extern "system" fn(hgfx: HGFX, color: SC_COLOR) -> GRAPHIN_RESULT,

  // setup parameters of linear gradient of lines.
  pub gLineGradientLinear:
    extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, stops: *const SC_COLOR_STOP, nstops: UINT)
      -> GRAPHIN_RESULT,

  // setup parameters of linear gradient of fills.
  pub gFillGradientLinear:
    extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, stops: *const SC_COLOR_STOP, nstops: UINT)
      -> GRAPHIN_RESULT,

  // setup parameters of line gradient radial fills.
  pub gLineGradientRadial:
    extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM, stops: *const SC_COLOR_STOP, nstops: UINT)
      -> GRAPHIN_RESULT,

  // setup parameters of gradient radial fills.
  pub gFillGradientRadial:
    extern "system" fn(hgfx: HGFX, x: SC_POS, y: SC_POS, rx: SC_DIM, ry: SC_DIM, stops: *const SC_COLOR_STOP, nstops: UINT)
      -> GRAPHIN_RESULT,

  pub gFillMode: extern "system" fn(hgfx: HGFX, even_odd: BOOL) -> GRAPHIN_RESULT,

  // SECTION: text

  // create text layout using element's styles
  pub textCreateForElement: extern "system" fn(ptext: &mut HTEXT, text: LPCWSTR, textLength: UINT, he: HELEMENT) -> GRAPHIN_RESULT,

  // create text layout using explicit format declaration
  pub textCreate:
    extern "system" fn(ptext: &mut HTEXT, text: LPCWSTR, textLength: UINT, format: *const TEXT_FORMAT) -> GRAPHIN_RESULT,

  pub textGetMetrics: extern "system" fn(
    text: HTEXT,
    minWidth: &mut SC_DIM,
    maxWidth: &mut SC_DIM,
    height: &mut SC_DIM,
    ascent: &mut SC_DIM,
    descent: &mut SC_DIM,
    nLines: &mut UINT,
  ) -> GRAPHIN_RESULT,

  pub textSetBox: extern "system" fn(text: HTEXT, width: SC_DIM, height: SC_DIM) -> GRAPHIN_RESULT,

  // draw text with position (1..9 on MUMPAD) at px,py
  // Ex: gDrawText( 100,100,5) will draw text box with its center at 100,100 px
  pub gDrawText: extern "system" fn(hgfx: HGFX, text: HTEXT, px: SC_POS, py: SC_POS, position: UINT) -> GRAPHIN_RESULT,

  // SECTION: image rendering

  // draws img onto the graphics surface with current transformation applied (scale, rotation).
  #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))]
  pub gDrawImage: extern "system" fn(
    hgfx: HGFX,
    himg: HIMG,
    x: SC_POS,
    y: SC_POS,
    w: Option<&SC_DIM>,
    h: Option<&SC_DIM>,
    ix: Option<&UINT>,
    iy: Option<&UINT>,
    iw: Option<&UINT>,
    ih: Option<&UINT>,
    opacity: Option<&f32>,
  ) -> GRAPHIN_RESULT,

  // SECTION: coordinate space
  pub gWorldToScreen: extern "system" fn(hgfx: HGFX, inout_x: &mut SC_POS, inout_y: &mut SC_POS) -> GRAPHIN_RESULT,

  pub gScreenToWorld: extern "system" fn(hgfx: HGFX, inout_x: &mut SC_POS, inout_y: &mut SC_POS) -> GRAPHIN_RESULT,

  // SECTION: clipping
  pub gPushClipBox: extern "system" fn(hgfx: HGFX, x1: SC_POS, y1: SC_POS, x2: SC_POS, y2: SC_POS, opacity: f32) -> GRAPHIN_RESULT,

  pub gPushClipPath: extern "system" fn(hgfx: HGFX, hpath: HPATH, opacity: f32) -> GRAPHIN_RESULT,

  // pop clip layer previously set by gPushClipBox or gPushClipPath
  pub gPopClip: extern "system" fn(hgfx: HGFX) -> GRAPHIN_RESULT,

  // image painter
  pub imagePaint: extern "system" fn(himg: HIMG, pPainter: ImagePaintFunction, prm: LPVOID) -> GRAPHIN_RESULT, // paint on image using graphics

  // VALUE interface
  pub vWrapGfx: extern "system" fn(hgfx: HGFX, toValue: *mut VALUE) -> GRAPHIN_RESULT,

  pub vWrapImage: extern "system" fn(himg: HIMG, toValue: *mut VALUE) -> GRAPHIN_RESULT,

  pub vWrapPath: extern "system" fn(hpath: HPATH, toValue: *mut VALUE) -> GRAPHIN_RESULT,

  pub vWrapText: extern "system" fn(htext: HTEXT, toValue: *mut VALUE) -> GRAPHIN_RESULT,

  pub vUnWrapGfx: extern "system" fn(fromValue: *const VALUE, phgfx: &mut HGFX) -> GRAPHIN_RESULT,

  pub vUnWrapImage: extern "system" fn(fromValue: *const VALUE, phimg: &mut HIMG) -> GRAPHIN_RESULT,

  pub vUnWrapPath: extern "system" fn(fromValue: *const VALUE, phpath: &mut HPATH) -> GRAPHIN_RESULT,

  pub vUnWrapText: extern "system" fn(fromValue: *const VALUE, phtext: &mut HTEXT) -> GRAPHIN_RESULT,
}

pub type ImageWriteFunction = extern "system" fn(prm: LPVOID, data: LPCBYTE, data_length: UINT);
pub type ImagePaintFunction = extern "system" fn(prm: LPVOID, hgfx: HGFX, width: UINT, height: UINT);