-
Notifications
You must be signed in to change notification settings - Fork 5
/
minidraw.h
11511 lines (9503 loc) · 394 KB
/
minidraw.h
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
2D graphics library. Choice of public domain or MIT-0. See license statements at the end of this file.
minidraw - v0.x.x - 2019-xx-xx
David Reid - [email protected]
*/
/* WORK IN PROGRESS */
#ifndef MINIDRAW_H
#define MINIDRAW_H
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4324) /* structure was padded due to alignment specifier */
#pragma warning(disable:4214) /* nonstandard extension used: bit field types other than int */
#endif
/* Platform/backend detection. */
#ifdef _WIN32
#define MD_WIN32
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP || WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
#define MD_WIN32_UWP
#else
#define MD_WIN32_DESKTOP
#endif
#else
#define MD_POSIX
#include <pthread.h> /* Unfortunate #include, but needed for pthread_t, pthread_mutex_t and pthread_cond_t types. */
#ifdef __unix__
#define MD_UNIX
#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
#define MD_BSD
#endif
#endif
#ifdef __linux__
#define MD_LINUX
#endif
#ifdef __APPLE__
#define MD_APPLE
#endif
#endif
#include <stddef.h> /* For size_t. */
/* Sized types. Prefer built-in types. Fall back to stdint. */
#ifdef _MSC_VER
#if defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wlanguage-extension-token"
#pragma GCC diagnostic ignored "-Wc++11-long-long"
#pragma GCC diagnostic ignored "-Wlong-long"
#endif
typedef signed __int8 md_int8;
typedef unsigned __int8 md_uint8;
typedef signed __int16 md_int16;
typedef unsigned __int16 md_uint16;
typedef signed __int32 md_int32;
typedef unsigned __int32 md_uint32;
typedef signed __int64 md_int64;
typedef unsigned __int64 md_uint64;
#if defined(__clang__)
#pragma GCC diagnostic pop
#endif
#else
#define MD_HAS_STDINT
#include <stdint.h>
typedef int8_t md_int8;
typedef uint8_t md_uint8;
typedef int16_t md_int16;
typedef uint16_t md_uint16;
typedef int32_t md_int32;
typedef uint32_t md_uint32;
typedef int64_t md_int64;
typedef uint64_t md_uint64;
#endif
#ifdef MD_HAS_STDINT
typedef uintptr_t md_uintptr;
#else
#if defined(_WIN32)
#if defined(_WIN64)
typedef md_uint64 md_uintptr;
#else
typedef md_uint32 md_uintptr;
#endif
#elif defined(__GNUC__)
#if defined(__LP64__)
typedef md_uint64 md_uintptr;
#else
typedef md_uint32 md_uintptr;
#endif
#else
typedef md_uint64 md_uintptr; /* Fallback. */
#endif
#endif
typedef md_uint8 md_bool8;
typedef md_uint32 md_bool32;
#define MD_TRUE 1
#define MD_FALSE 0
typedef void* md_handle;
typedef void* md_ptr;
typedef void (* md_proc)(void);
#if defined(_MSC_VER) && !defined(_WCHAR_T_DEFINED)
typedef md_uint16 wchar_t;
#endif
typedef char md_utf8;
typedef md_uint16 md_utf16;
typedef md_uint32 md_utf32;
/* Define NULL for some compilers. */
#ifndef NULL
#define NULL 0
#endif
#if defined(SIZE_MAX)
#define MD_SIZE_MAX SIZE_MAX
#else
#define MD_SIZE_MAX 0xFFFFFFFF /* When SIZE_MAX is not defined by the standard library just default to the maximum 32-bit unsigned integer. */
#endif
#define MD_PI 3.14159265358979323846
#define MD_PIF 3.14159265358979323846f
#define MD_DEGREES(radians) ((double)((radians) * 57.29577951308232087685))
#define MD_DEGREESF(radians) ( (float)((radians) * 57.29577951308232087685f))
#define MD_RADIANS(degrees) ((double)((degrees) * 0.01745329251994329577))
#define MD_RADIANSF(degrees) ( (float)((degrees) * 0.01745329251994329577f))
#ifdef _MSC_VER
#define MD_INLINE __forceinline
#else
#ifdef __GNUC__
#define MD_INLINE __inline__ __attribute__((always_inline))
#else
#define MD_INLINE
#endif
#endif
#define MD_PRIVATE
/* Backend Support */
#if defined(MD_WIN32)
#define MD_SUPPORT_GDI
#define MD_SUPPORT_DIRECT2D
#endif
#if defined(MD_APPLE)
#define MD_SUPPORT_COREGRAPHICS
#endif
#if defined(MD_UNIX) || defined(MD_LINUX)
#define MD_SUPPORT_CAIRO
#define MD_SUPPORT_XFT
#endif
/* Backends */
#if !defined(MD_NO_GDI) && defined(MD_SUPPORT_GDI)
#define MD_ENABLE_GDI
#endif
#if !defined(MD_NO_DIRECT2D) && defined(MD_SUPPORT_DIRECT2D)
#define MD_ENABLE_DIRECT2D
#endif
#if !defined(MD_NO_COREGRAPHICS) && defined(MD_SUPPORT_COREGRAPHICS)
#define MD_ENABLE_COREGRAPHICS
#endif
#if !defined(MD_NO_CAIRO) && defined(MD_SUPPORT_CAIRO)
#define MD_ENABLE_CAIRO
#endif
#if !defined(MD_NO_XFT) && defined(MD_SUPPORT_XFT)
#define MD_ENABLE_XFT
#endif
/* Result Codes */
typedef int md_result;
#define MD_SUCCESS 0
/* General errors. */
#define MD_ERROR -1 /* A generic error. */
#define MD_INVALID_ARGS -2
#define MD_INVALID_OPERATION -3
#define MD_OUT_OF_MEMORY -4
#define MD_OUT_OF_RANGE -5
#define MD_TOO_BIG -6
#define MD_ACCESS_DENIED -7
#define MD_DOES_NOT_EXIST -8
#define MD_ALREADY_EXISTS -9
#define MD_TOO_MANY_OPEN_FILES -10
#define MD_INVALID_FILE -11
#define MD_FAILED_TO_OPEN_FILE -12
#define MD_FAILED_TO_READ_FILE -13
#define MD_FAILED_TO_WRITE_FILE -14
#define MD_END_OF_FILE -15
#define MD_NO_SPACE -16
#define MD_NEGATIVE_SEEK -17
#define MD_PATH_TOO_LONG -18
#define MD_NAME_TOO_LONG -19
#define MD_MEMORY_ALREADY_MAPPED -20
#define MD_TIMEOUT -21
#define MD_CANCELLED -22
#define MD_INVALID_BOM -200
#define MD_INVALID_CODE_POINT -201
/* Forward Declarations */
typedef struct md_api md_api;
typedef struct md_api_config md_api_config;
typedef struct md_font md_font;
typedef struct md_font_config md_font_config;
typedef struct md_brush md_brush;
typedef struct md_brush_config md_brush_config;
typedef struct md_gc md_gc;
typedef struct md_gc_config md_gc_config;
/* Enumerators */
typedef enum
{
md_backend_gdi, /* Typography via Uniscribe */
/*md_backend_direct2d,*/ /* Typography via DirectWrite */
/*md_backend_coregraphics,*/ /* Typography via Core Text */
md_backend_cairo, /* Typography via Pango */
/*md_backend_xft*/ /* Typography via minidraw */
md_backend_custom
} md_backend;
typedef enum
{
md_font_weight_medium = 0,
md_font_weight_thin,
md_font_weight_extra_light,
md_font_weight_light,
md_font_weight_semi_light,
md_font_weight_book,
md_font_weight_semi_bold,
md_font_weight_bold,
md_font_weight_extra_bold,
md_font_weight_heavy,
md_font_weight_extra_heavy,
md_font_weight_normal = md_font_weight_medium,
md_font_weight_default = md_font_weight_medium
} md_font_weight;
typedef enum
{
md_font_slant_none = 0,
md_font_slant_italic,
md_font_slant_oblique
} md_font_slant;
typedef enum
{
md_line_cap_flat = 0,
md_line_cap_round = 1,
md_line_cap_square = 2
} md_line_cap;
typedef enum
{
md_line_join_miter = 0,
md_line_join_round = 1,
md_line_join_bevel = 2
} md_line_join;
typedef enum
{
md_brush_type_solid = 0, /* RGB or RGBA */
md_brush_type_linear = 1, /* Source is a linear gradient. */
md_brush_type_radial = 2, /* Source is a radial gradient. */
md_brush_type_gc = 3 /* Source is a graphics context. */
} md_brush_type;
typedef enum
{
md_blend_op_src = 0, /* Default. Draws the source over the top of the destination with no blending. */
md_blend_op_src_over = 1 /* Standard alpha blending. */
} md_blend_op;
typedef enum
{
md_antialias_mode_default = 0, /* Let the backend decide, but prefer anti-aliasing if available. Will be the same as md_antialias_none on GDI since GDI does not support anti-aliasing. */
md_antialias_mode_none = 1, /* Anti-aliasing will be disabled. Useful for straight-edge primitives like un-rotated rectangles or where performance is a concern. */
md_antialias_mode_gray = 2, /* Standard grayscale anti-aliasing. */
md_antialias_mode_subpixel = 3 /* ClearType RGB style anti-aliasing. Falls back to md_antialias_mode_gray if unavailable. */
} md_antialias_mode;
typedef enum
{
md_fill_mode_winding = 0, /* Default. */
md_fill_mode_evenodd
} md_fill_mode;
typedef enum
{
md_stretch_filter_nearest = 0, /* Default. */
md_stretch_filter_linear = 1
} md_stretch_filter;
typedef enum
{
md_format_unknown = 0,
md_format_rgba,
md_format_rgb,
md_format_bgra, /* Optimal format for GDI. */
md_format_bgr,
md_format_argb /* Optimal format for Cairo. */
} md_format;
typedef enum
{
md_alignment_left,
md_alignment_right,
md_alignment_top,
md_alignment_bottom,
md_alignment_center,
} md_alignment;
/* Structures */
#if defined(MD_SUPPORT_GDI)
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic" /* type of bit-field is a GCC extension */
#endif
typedef struct
{
md_uint16 uJustification : 4;
md_uint16 fClusterStart : 1;
md_uint16 fDiacritic : 1;
md_uint16 fZeroWidth : 1;
md_uint16 fReserved : 1;
md_uint16 fShapeReserved : 8;
} MD_SCRIPT_VISATTR;
typedef struct
{
md_uint16 uBidiLevel : 5;
md_uint16 fOverrideDirection : 1;
md_uint16 fInhibitSymSwap : 1;
md_uint16 fCharShape : 1;
md_uint16 fDigitSubstitute : 1;
md_uint16 fInhibitLigate : 1;
md_uint16 fDisplayZWG : 1;
md_uint16 fArabicNumContext : 1;
md_uint16 fGcpClusters : 1;
md_uint16 fReserved : 1;
md_uint16 fEngineReserved : 2;
} MD_SCRIPT_STATE;
typedef struct
{
md_uint16 eScript : 10;
md_uint16 fRTL : 1;
md_uint16 fLayoutRTL : 1;
md_uint16 fLinkBefore : 1;
md_uint16 fLinkAfter : 1;
md_uint16 fLogicalOrder : 1;
md_uint16 fNoGlyphIndex : 1;
MD_SCRIPT_STATE s;
} MD_SCRIPT_ANALYSIS;
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
typedef struct
{
int iCharPos;
MD_SCRIPT_ANALYSIS a;
} MD_SCRIPT_ITEM;
#endif
typedef struct
{
md_uint8 r;
md_uint8 g;
md_uint8 b;
md_uint8 a;
} md_color;
typedef struct
{
md_int32 left;
md_int32 top;
md_int32 right;
md_int32 bottom;
} md_rect;
typedef struct
{
float m00; /* Rotation cosine. Horizontal scale. */
float m01; /* Rotation sine. */
float m10; /* Rotation negative sine. */
float m11; /* Rotation cosine. Vertical scale. */
float dx; /* X translation. */
float dy; /* Y translation. */
} md_matrix;
typedef struct
{
md_uint32 index;
md_int32 advance;
union
{
#if defined(MD_SUPPORT_GDI)
struct
{
MD_SCRIPT_VISATTR sv; /* Passed around to ScriptShape() and ScriptPlace(). */
long offsetX; /* For filling in the GOFFSET parameter of ScriptTextOut(). */
long offsetY; /* For filling in the GOFFSET parameter of ScriptTextOut(). */
} gdi;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
md_int32 width; /* From PangoGlyphGeometry. */
md_int32 offsetX; /* From PangoGlyphGeometry. */
md_int32 offsetY; /* From PangoGlyphGeometry. */
md_uint32 isClusterStart : 1; /* From PangoGlyphVisAttr */
} cairo;
#endif
md_uint32 _unused;
} backend;
} md_glyph;
typedef struct
{
md_int32 lPadding;
md_int32 rPadding;
md_int32 sizeX;
md_int32 sizeY;
} md_text_metrics;
typedef struct
{
md_int32 ascent;
md_int32 descent;
} md_font_metrics;
typedef struct
{
md_int32 sizeX;
md_int32 sizeY;
md_int32 bearingX;
md_int32 bearingY;
md_int32 advanceX;
md_int32 advanceY;
} md_glyph_metrics;
typedef struct
{
md_int32 boundsX;
md_int32 boundsY;
md_int32 boundsSizeX;
md_int32 boundsSizeY;
md_int32 textOffsetX;
md_int32 textOffsetY;
md_rect padding;
md_alignment alignmentX;
md_alignment alignmentY;
md_int32 borderWidth; /* When positive, draws the border on the inside of the bounds. When negative draws it on the outside. */
md_color borderColor;
md_int32 tabWidthInPixels; /* When 0, falls back to tabSizeInSpaces. */
md_int32 tabWidthInSpaces; /* Used when tabSizeInPixels is 0. */
md_bool32 fillBackground : 1;
md_bool32 singleLine : 1;
} md_text_layout;
typedef struct
{
size_t offset; /* Offset in code units (bytes for UTF-8, shorts for UTF-16, integers for UTF-32). */
size_t length; /* Length in code units. */
/* Backend-specific data. */
union
{
#if defined(MD_SUPPORT_GDI)
struct
{
MD_SCRIPT_ANALYSIS sa; /* Passed around to Script*() APIs. */
/*HFONT*/ md_handle hFont; /* The font to use when drawing this item. */
/*SCRIPT_CACHE*/ md_ptr sc; /* The SCRIPT_CACHE object passed around to ScriptShape(), ScriptPlace() and ScriptTextOut(). */
} gdi;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*PangoItem**/ md_ptr pPangoItem;
} cairo;
#endif
md_uint32 _unused;
} backend;
} md_item;
typedef struct
{
md_api* pAPI;
union
{
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*GList**/ md_ptr pPangoItems;
} cairo;
#endif
int _unused;
} backend;
} md_itemize_state;
typedef void (* md_uninit_proc) (md_api* pAPI);
typedef md_result (* md_itemize_utf8_proc) (md_font* pFont, const md_utf8* pTextUTF8, size_t textLength, md_item* pItems, md_uint32* pItemCount, md_itemize_state* pItemizeState);
typedef md_result (* itemize_utf16_proc) (md_font* pFont, const md_utf16* pTextUTF16, size_t textLength, md_item* pItems, md_uint32* pItemCount, md_itemize_state* pItemizeState);
typedef md_result (* itemize_utf32_proc) (md_font* pFont, const md_utf32* pTextUTF32, size_t textLength, md_item* pItems, md_uint32* pItemCount, md_itemize_state* pItemizeState);
typedef void (* free_itemize_state_proc) (md_itemize_state* pItemizeState);
typedef md_result (* shape_utf8_proc) (md_font* pFont, md_item* pItem, const md_utf8* pTextUTF8, size_t textLength, md_glyph* pGlyphs, size_t* pGlyphCount, size_t* pClusters, md_text_metrics* pRunMetrics);
typedef md_result (* shape_utf16_proc) (md_font* pFont, md_item* pItem, const md_utf16* pTextUTF16, size_t textLength, md_glyph* pGlyphs, size_t* pGlyphCount, size_t* pClusters, md_text_metrics* pRunMetrics);
typedef md_result (* shape_utf32_proc) (md_font* pFont, md_item* pItem, const md_utf32* pTextUTF32, size_t textLength, md_glyph* pGlyphs, size_t* pGlyphCount, size_t* pClusters, md_text_metrics* pRunMetrics);
typedef md_result (* font_init_proc) (md_api* pAPI, const md_font_config* pConfig, md_font* pFont);
typedef void (* font_uninit_proc) (md_font* pFont);
typedef md_result (* font_get_glyph_metrics_proc) (md_font* pFont, const md_glyph* pGlyphs, size_t glyphCount, md_glyph_metrics* pGlyphMetrics);
typedef md_result (* font_get_glyph_metrics_by_index_proc)(md_font* pFont, const md_uint32* pGlyphIndices, size_t glyphCount, md_glyph_metrics* pGlyphMetrics);
typedef md_result (* brush_init_proc) (md_api* pAPI, const md_brush_config* pConfig, md_brush* pBrush);
typedef void (* brush_uninit_proc) (md_brush* pBrush);
typedef void (* brush_set_origin_proc) (md_brush* pBrush, md_int32 x, md_int32 y);
typedef md_result (* gc_init_proc) (md_api* pAPI, const md_gc_config* pConfig, md_gc* pGC);
typedef void (* gc_uninit_proc) (md_gc* pGC);
typedef md_result (* gc_get_image_data_proc) (md_gc* pGC, md_format outputFormat, void* pImageData);
typedef md_result (* gc_get_size_proc) (md_gc* pGC, md_uint32* pSizeX, md_uint32* pSizeY);
typedef md_result (* gc_save_proc) (md_gc* pGC);
typedef md_result (* gc_restore_proc) (md_gc* pGC);
typedef void (* gc_set_matrix_proc) (md_gc* pGC, const md_matrix* pMatrix);
typedef void (* gc_get_matrix_proc) (md_gc* pGC, md_matrix* pMatrix);
typedef void (* gc_set_matrix_identity_proc) (md_gc* pGC);
typedef void (* gc_transform_proc) (md_gc* pGC, const md_matrix* pMatrix);
typedef void (* gc_translate_proc) (md_gc* pGC, md_int32 offsetX, md_int32 offsetY);
typedef void (* gc_rotate_proc) (md_gc* pGC, float rotationInRadians);
typedef void (* gc_scale_proc) (md_gc* pGC, float scaleX, float scaleY);
typedef void (* gc_set_line_width_proc) (md_gc* pGC, md_int32 width);
typedef void (* gc_set_line_cap_proc) (md_gc* pGC, md_line_cap cap);
typedef void (* gc_set_line_join_proc) (md_gc* pGC, md_line_join join);
typedef void (* gc_set_miter_limit_proc) (md_gc* pGC, float limit);
typedef void (* gc_set_line_dash_proc) (md_gc* pGC, const float* dashes, md_uint32 count);
typedef void (* gc_set_line_brush_proc) (md_gc* pGC, md_brush* pBrush);
typedef void (* gc_set_line_brush_solid_proc) (md_gc* pGC, md_color color);
typedef void (* gc_set_line_brush_gc_proc) (md_gc* pGC, md_gc* pSrcGC);
typedef void (* gc_set_fill_brush_proc) (md_gc* pGC, md_brush* pBrush);
typedef void (* gc_set_fill_brush_solid_proc) (md_gc* pGC, md_color color);
typedef void (* gc_set_fill_brush_gc_proc) (md_gc* pGC, md_gc* pSrcGC);
typedef void (* gc_set_text_fg_color_proc) (md_gc* pGC, md_color fgColor);
typedef void (* gc_set_text_bg_color_proc) (md_gc* pGC, md_color bgColor);
typedef md_color (* gc_get_text_fg_color_proc) (md_gc* pGC);
typedef md_color (* gc_get_text_bg_color_proc) (md_gc* pGC);
typedef void (* gc_set_blend_op_proc) (md_gc* pGC, md_blend_op op);
typedef void (* gc_set_antialias_mode_proc) (md_gc* pGC, md_antialias_mode mode);
typedef void (* gc_set_fill_mode_proc) (md_gc* pGC, md_fill_mode mode);
typedef void (* gc_set_stretch_filter_proc) (md_gc* pGC, md_stretch_filter filter);
typedef void (* gc_move_to_proc) (md_gc* pGC, md_int32 x, md_int32 y);
typedef void (* gc_line_to_proc) (md_gc* pGC, md_int32 x, md_int32 y);
typedef void (* gc_rectangle_proc) (md_gc* pGC, md_int32 left, md_int32 top, md_int32 right, md_int32 bottom);
typedef void (* gc_arc_proc) (md_gc* pGC, md_int32 x, md_int32 y, md_int32 radius, float angle1InRadians, float angle2InRadians);
typedef void (* gc_curve_to_proc) (md_gc* pGC, md_int32 x1, md_int32 y1, md_int32 x2, md_int32 y2, md_int32 x3, md_int32 y3);
typedef void (* gc_close_path_proc) (md_gc* pGC);
typedef void (* gc_clip_proc) (md_gc* pGC);
typedef void (* gc_reset_clip_proc) (md_gc* pGC);
typedef md_bool32 (* gc_is_point_inside_clip_proc) (md_gc* pGC, md_int32 x, md_int32 y);
typedef void (* gc_fill_proc) (md_gc* pGC);
typedef void (* gc_stroke_proc) (md_gc* pGC);
typedef void (* gc_fill_and_stroke_proc) (md_gc* pGC);
typedef void (* gc_draw_gc_proc) (md_gc* pGC, md_gc* pSrcGC, md_int32 srcX, md_int32 srcY);
typedef void (* gc_draw_glyphs_proc) (md_gc* pGC, const md_item* pItem, const md_glyph* pGlyphs, size_t glyphCount, md_int32 x, md_int32 y);
typedef void (* gc_clear_proc) (md_gc* pGC, md_color color);
typedef struct
{
md_uninit_proc uninit;
md_itemize_utf8_proc itemizeUTF8;
itemize_utf16_proc itemizeUTF16;
itemize_utf32_proc itemizeUTF32;
free_itemize_state_proc freeItemizeState;
shape_utf8_proc shapeUTF8;
shape_utf16_proc shapeUTF16;
shape_utf32_proc shapeUTF32;
font_init_proc fontInit;
font_uninit_proc fontUninit;
font_get_glyph_metrics_proc fontGetGlyphMetrics;
font_get_glyph_metrics_by_index_proc fontGetGlyphMetricsByIndex;
brush_init_proc brushInit;
brush_uninit_proc brushUninit;
brush_set_origin_proc brushSetOrigin;
gc_init_proc gcInit;
gc_uninit_proc gcUninit;
gc_get_image_data_proc gcGetImageData;
gc_get_size_proc gcGetSize;
gc_save_proc gcSave;
gc_restore_proc gcRestore;
gc_set_matrix_proc gcSetMatrix;
gc_get_matrix_proc gcGetMatrix;
gc_set_matrix_identity_proc gcSetMatrixIdentity;
gc_transform_proc gcTransform;
gc_translate_proc gcTranslate;
gc_rotate_proc gcRotate;
gc_scale_proc gcScale;
gc_set_line_width_proc gcSetLineWidth;
gc_set_line_cap_proc gcSetLineCap;
gc_set_line_join_proc gcSetLineJoin;
gc_set_miter_limit_proc gcSetMiterLimit;
gc_set_line_dash_proc gcSetLineDash;
gc_set_line_brush_proc gcSetLineBrush;
gc_set_line_brush_solid_proc gcSetLineBrushSolid;
gc_set_line_brush_gc_proc gcSetLineBrushGC;
gc_set_fill_brush_proc gcSetFillBrush;
gc_set_fill_brush_solid_proc gcSetFillBrushSolid;
gc_set_fill_brush_gc_proc gcSetFillBrushGC;
gc_set_text_fg_color_proc gcSetTextFGColor;
gc_set_text_bg_color_proc gcSetTextBGColor;
gc_get_text_fg_color_proc gcGetTextFGColor;
gc_get_text_bg_color_proc gcGetTextBGColor;
gc_set_blend_op_proc gcSetBlendOp;
gc_set_antialias_mode_proc gcSetAntialiasMode;
gc_set_fill_mode_proc gcSetFillMode;
gc_set_stretch_filter_proc gcSetStretchFilter;
gc_move_to_proc gcMoveTo;
gc_line_to_proc gcLineTo;
gc_rectangle_proc gcRectangle;
gc_arc_proc gcArc;
gc_curve_to_proc gcCurveTo;
gc_close_path_proc gcClosePath;
gc_clip_proc gcClip;
gc_reset_clip_proc gcResetClip;
gc_is_point_inside_clip_proc gcIsPointInsideClip;
gc_fill_proc gcFill;
gc_stroke_proc gcStroke;
gc_fill_and_stroke_proc gcFillAndStroke;
gc_draw_gc_proc gcDrawGC;
gc_draw_glyphs_proc gcDrawGlyphs;
gc_clear_proc gcClear;
} md_api_procs;
struct md_api_config
{
md_backend backend;
void* pUserData;
struct
{
/*HDC*/ md_handle hDC; /* Optional pre-created global device context. */
} gdi;
struct
{
/*PangoContext**/ md_ptr pPangoContext; /* Optional application-defined PangoContext. When set to null, the pango context is set to pango_font_map_create_context(pango_cairo_font_map_get_default()) */
} cairo;
struct
{
md_api_procs procs;
} custom;
};
struct md_api
{
md_backend backend;
void* pUserData;
md_api_procs procs;
#if defined(MD_WIN32)
struct
{
int _unused;
} win32;
#endif
#if defined(MD_APPLE)
struct
{
int _unused;
} apple;
#endif
#if defined(MD_SUPPORT_GDI)
struct
{
/*HDC*/ md_handle hGlobalDC; /* Global DC for font creation and glyph shaping and placement. */
/*HBRUSH*/ md_handle hStockSolidFillBrush; /* The GDI stock brush to use for solid brushes. */
/* usp10.dll */
md_handle hUsp10DLL;
md_proc ScriptItemize;
md_proc ScriptShape;
md_proc ScriptPlace;
md_proc ScriptTextOut;
md_proc ScriptFreeCache;
/* gdi32.dll */
md_handle hGdi32DLL;
/* msimg32.dll */
md_handle hMsimg32DLL;
md_proc AlphaBlend;
md_bool32 ownsGlobalDC : 1;
} gdi;
#endif
#if defined(MD_SUPPORT_DIRECT2D)
struct
{
int _unused;
} direct2d;
#endif
#if defined(MD_SUPPORT_COREGRAPHICS)
struct
{
int _unused;
} coregraphics;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*PangoContext**/ md_ptr pPangoContext; /* Created with the PangoFontMap retrieved with pango_cairo_font_map_get_default(). */
md_bool32 ownsPangoContext : 1;
} cairo;
#endif
#if defined(MD_SUPPORT_XFT)
struct
{
int _unused;
} xft;
#endif
struct
{
int _unused;
} custom;
};
struct md_font_config
{
const char* family;
md_uint32 sizeInPixels; /* If set to 0, sizeInPoints will be used instead. */
md_uint32 sizeInPoints; /* If set to 0, sizeInPixels will be used instead. */
md_font_weight weight;
md_font_slant slant;
md_antialias_mode antialiasMode; /* Preferred anti-aliasing to use with this font. This is just a hint. */
void* pUserData;
struct
{
/*PangoFontMap**/ md_ptr pPangoFontMap; /* Custom application-defined PangoFontMap to use when creating the font. */
/*PangoContext**/ md_ptr pPangoContext; /* Custom application-defined PangoContext to use when creating the font. */
} cairo;
};
struct md_font
{
md_api* pAPI; /* The md_api object that was used to initialize the font. */
void* pUserData;
md_font_metrics metrics;
#if defined(MD_SUPPORT_GDI)
struct
{
/*HFONT*/ md_handle hFont;
/*SCRIPT_CACHE*/ md_ptr sc; /* The SCRIPT_CACHE object passed around to ScriptShape(), ScriptPlace() and ScriptTextOut(). */
} gdi;
#endif
#if defined(MD_SUPPORT_DIRECT2D)
struct
{
int _unused;
} direct2d;
#endif
#if defined(MD_SUPPORT_COREGRAPHICS)
struct
{
int _unused;
} coregraphics;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*PangoFont**/ md_ptr pPangoFont;
/*PangoFontDescription**/ md_ptr pPangoFontDesc;
/*PangoAttrList**/ md_ptr pPangoAttrList; /* Passed into pango_itemize(). */
} cairo;
#endif
#if defined(MD_SUPPORT_XFT)
struct
{
int _unused;
} xft;
#endif
};
typedef struct
{
float offset; /* Between 0 and 1. */
md_color color;
} md_color_stop;
struct md_brush_config
{
md_brush_type type;
void* pUserData;
md_color_stop* pColorStops; /* Only used with linear and radial brushes. */
md_uint32 colorStopCount;
struct
{
md_color color;
} solid;
struct
{
md_int32 x0;
md_int32 y0;
md_int32 x1;
md_int32 x2;
} linear;
struct
{
md_int32 cx0; /* Center of the start circle. */
md_int32 cy0;
md_int32 radius0; /* Radius of the start circle. */
md_int32 cx1; /* Center of the end circle. */
md_int32 cy1;
md_int32 radius1; /* Radius of the end circle. */
} radial;
struct
{
md_gc* pGC;
} gc;
};
struct md_brush
{
md_api* pAPI;
void* pUserData;
md_brush_config config;
#if defined(MD_SUPPORT_GDI)
struct
{
/*HBRUSH*/ md_handle hBrush;
/*HBITMAP*/ md_handle hBitmap; /* Only used for complex brushes. Owned by the md_brush object and deleted in md_brush_uninit(). */
md_int32 originX;
md_int32 originY;
md_bool32 ownsBitmap : 1; /* When set to true, hBitmap will be destroyed in md_brush_uninit(). */
} gdi;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*cairo_pattern_t**/ md_ptr pCairoPattern;
/*cairo_surface_t**/ md_ptr pCairoSurface; /* Only used when the source is a cairo surface of a transient GC. */
} cairo;
#endif
};
struct md_gc_config
{
md_uint32 sizeX; /* You should set this for Cairo even when passing in a pre-existing cairo_t object. */
md_uint32 sizeY; /* You should set this for Cairo even when passing in a pre-existing cairo_t object. */
md_uint32 stride; /* Stride in pixels. Only used when pInitialImageData is not null. */
md_format format; /* The format of the data contained in pInitialImageData (if any) and the preferred internal format. Cannot be md_format_unkonwn if pInitialImageData is not null. */
const void* pInitialImageData; /* Can be null in which case the initial contents are undefined. */
void* pUserData;
#if defined(MD_SUPPORT_GDI)
struct
{
/*HDC*/ md_handle hDC; /* Existing HDC to use as the rendering target. Set this for transient context's such as those used in BeginPaint()/EndPaint() pairs. */
} gdi;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*cairo_t**/ md_ptr pCairoContext;
} cairo;
#endif
};
#if defined(MD_SUPPORT_GDI)
typedef struct
{
/*HPEN*/ md_handle hPen; /* Lazily initialized. Set to NULL to indicate the case when a new pen handle needs to be created. */
md_int32 lineWidth;
md_line_cap lineCap;
md_line_join lineJoin;
md_uint32 dashCount;
float dashes[16];
md_blend_op blendOp;
md_color textFGColor;
md_color textBGColor;
md_brush_config lineStockBrush; /* For use with solid and GC brushes. Only used when pUserLineBrush is NULL. */
md_brush* pUserLineBrush; /* For use with user-defined brushes. */
md_brush transientFillBrush; /* For use with solid and GC brushes. Only used when pUserFillBrush is NULL. */
md_brush* pUserFillBrush; /* For use with user-defined brushes. */
md_bool32 hasTransientFillBrush : 1;
md_bool32 hasPathBegun : 1; /* Determines whether or not BeginPath has been called. */
} md_gc_state_gdi;
#endif
#if defined(MD_SUPPORT_CAIRO)
typedef struct
{
md_stretch_filter stretchFilter;
md_color textFGColor;
md_color textBGColor;
md_brush* pFillBrush;
md_brush* pLineBrush;
md_brush_config transientFillBrush;
md_brush_config transientLineBrush;
md_bool32 hasTransientFillBrush : 1;
md_bool32 hasTransientLineBrush : 1;
} md_gc_state_cairo;
#endif
struct md_gc
{
md_api* pAPI; /* The md_api object that was used to initialize the graphics context. */
void* pUserData;
md_format format;
md_bool32 isTransient : 1;
#if defined(MD_SUPPORT_GDI)
struct
{
/*HDC*/ md_handle hDC;
/*HBITMAP*/ md_handle hBitmap; /* The HBITMAP object that will be used as the target for graphics output. */
void* pBitmapData; /* A pointer to the raw bitmap data. Owned by hBitmap. Freed by GDI when hBitmap is deleted. */
md_uint32 bitmapSizeX;
md_uint32 bitmapSizeY;
md_gc_state_gdi* pState; /* Heap allocated via realloc() for now. May change to a per-API allocation scheme. */
md_uint32 stateCap; /* The capacity of pState. */
md_uint32 stateCount; /* The number of valid items in pState. */
} gdi;
#endif
#if defined(MD_SUPPORT_DIRECT2D)
struct
{
int _unused;
} direct2d;
#endif
#if defined(MD_SUPPORT_COREGRAPHICS)
struct
{
int _unused;
} coregraphics;
#endif
#if defined(MD_SUPPORT_CAIRO)
struct
{
/*cairo_t**/ md_ptr pCairoContext;
/*cairo_surface_t**/ md_ptr pCairoSurface;
void* pCairoSurfaceData;
md_uint32 cairoSurfaceSizeX;
md_uint32 cairoSurfaceSizeY;
md_gc_state_cairo* pState; /* Heap allocated via realloc() for now. May change to a per-API allocation scheme. */
md_uint32 stateCap; /* The capacity of pState. */
md_uint32 stateCount; /* The number of valid items in pState. */
} cairo;
#endif
#if defined(MD_SUPPORT_XFT)
struct
{
int _unused;
} xft;
#endif
};
/**************************************************************************************************************************************************************
Unicode
**************************************************************************************************************************************************************/
#define MD_UNICODE_MIN_CODE_POINT 0x000000
#define MD_UNICODE_MAX_CODE_POINT 0x10FFFF
#define MD_UNICODE_REPLACEMENT_CHARACTER 0x00FFFD
#define MD_UNICODE_REPLACEMENT_CHARACTER_LENGTH_UTF8 3
#define MD_UNICODE_REPLACEMENT_CHARACTER_LENGTH_UTF16 1
#define MD_UNICODE_REPLACEMENT_CHARACTER_LENGTH_UTF32 1
#define MD_FORBID_BOM (1 << 1)
#define MD_ERROR_ON_INVALID_CODE_POINT (1 << 2)
/*
Checks if the given UTF-16 byte order mark indicates little endian.
*/
MD_INLINE md_bool32 md_is_utf16_bom_le(const md_uint8 bom[2])
{
/* RFC 2781 - Section 3.2 */
return bom[0] == 0xFF && bom[1] == 0xFE;
}
/*
Checks if the given UTF-16 byte order mark indicates big endian.
*/
MD_INLINE md_bool32 md_is_utf16_bom_be(const md_uint8 bom[2])
{
/* RFC 2781 - Section 3.2 */
return bom[0] == 0xFE && bom[1] == 0xFF;