-
Notifications
You must be signed in to change notification settings - Fork 0
/
freetype2-demos-4-svg-hooks-skia.patch
937 lines (886 loc) · 30.1 KB
/
freetype2-demos-4-svg-hooks-skia.patch
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
From 3997b5b1c075685ea3180f10d5cfde63632a9a0c Mon Sep 17 00:00:00 2001
From: Hin-Tak Leung <htl10@users.sourceforge.net>
Date: Mon, 6 May 2024 02:18:18 +0100
Subject: [PATCH] 4-svg-hooks-skia
Signed-off-by: Hin-Tak Leung <htl10@users.sourceforge.net>
---
Makefile | 32 +++-
meson.build | 2 +
src/ftcommon.c | 32 ++++
src/ftcommon.h | 7 +
src/ftgrid.c | 7 +-
src/ftview.c | 8 +-
src/rsvg-port.c | 62 +++++++-
src/rsvg-port.h | 19 +--
src/skia-port.c | 391 ++++++++++++++++++++++++++++++++++++++++++++++++
src/skia-port.h | 58 +++++++
10 files changed, 594 insertions(+), 24 deletions(-)
create mode 100644 src/skia-port.c
create mode 100644 src/skia-port.h
diff --git a/Makefile b/Makefile
index cded506..dc2e2ce 100644
--- a/Makefile
+++ b/Makefile
@@ -148,7 +148,10 @@ else
COMPILE = $(CC) $(ANSIFLAGS) \
$(INCLUDES:%=$I%) \
$(CFLAGS) \
- $(FT_DEMO_CFLAGS)
+ $(FT_DEMO_CFLAGS) -I$(TOP_DIR_2)/../svg-native-viewer/svgnative/include/ \
+ -I$(TOP_DIR_2)/../skia/include/core/ \
+ -I$(TOP_DIR_2)/../skia/ -DHAVE_SKIA
+
# Enable C99 for gcc to avoid warnings.
# Note that clang++ aborts with an error if we use `-std=C99',
@@ -186,8 +189,12 @@ else
# `FT_DEMO_LDFLAGS` has been set in `unix-cc.mk`, too.
override CC = $(CCraw)
LINK_CMD = $(LIBTOOL) --mode=link $(CC) \
+ -L$(TOP_DIR_2)/../svg-native-viewer/svgnative/build/linux/ -lSVGNativeViewerLib -lexpat \
+ -L$(TOP_DIR_2)/../skia/out/Shared/ -lsvg -lskia -lskshaper -lskunicode_icu -lskunicode_core -lharfbuzz -lGL -lfontconfig -ljpeg \
$(subst /,$(COMPILER_SEP),$(LDFLAGS))
LINK_LIBS = $(subst /,$(COMPILER_SEP),$(FTLIB) $(EFENCE)) \
+ -L$(TOP_DIR_2)/../svg-native-viewer/svgnative/build/linux/ -lSVGNativeViewerLib -lexpat \
+ -L$(TOP_DIR_2)/../skia/out/Shared/ -lsvg -lskia -lskshaper -lskunicode_icu -lskunicode_core -lharfbuzz -lGL -lfontconfig -ljpeg \
$(FT_DEMO_LDFLAGS)
else
LINK_CMD = $(CC) $(subst /,$(COMPILER_SEP),$(LDFLAGS))
@@ -469,9 +476,30 @@ else
$(OBJ_DIR_2)/rsvg-port.$(SO): $(SRC_DIR)/rsvg-port.c $(SRC_DIR)/rsvg-port.h
$(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+ $(OBJ_DIR_2)/skia-port.$(SO): $(SRC_DIR)/skia-port.c $(SRC_DIR)/skia-port.h
+ $(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+ $(OBJ_DIR_2)/rsvg-port-alt.$(SO): $(SRC_DIR)/rsvg-port.c $(SRC_DIR)/rsvg-port.h
+ $(COMPILE) -DUSE_ADOBE=1 $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+ $(OBJ_DIR_2)/skia-port-alt.$(SO): $(SRC_DIR)/skia-port.c $(SRC_DIR)/skia-port.h
+ $(COMPILE) -DUSE_ADOBE=1 $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+ $(OBJ_DIR_2)/skia-colrv1-stub.$(SO): $(SRC_DIR)/skia-colrv1-stub.cpp $(SRC_DIR)/skia-colrv1-stub.h
+ $(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+ $(OBJ_DIR_2)/skia-src-ports-SkFontHost_FreeType_colrv1.$(SO): $(SRC_DIR)/skia-src-ports-SkFontHost_FreeType_colrv1.cpp
+ $(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+
+ $(OBJ_DIR_2)/skia-src-ports-SkFontHost_FreeType_common_colrv1.$(SO): $(SRC_DIR)/skia-src-ports-SkFontHost_FreeType_common_colrv1.cpp
+ $(COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)
+
FTCOMMON_OBJ := $(OBJ_DIR_2)/ftcommon.$(SO) \
$(OBJ_DIR_2)/ftpngout.$(SO) \
- $(OBJ_DIR_2)/rsvg-port.$(SO)
+ $(OBJ_DIR_2)/rsvg-port.$(SO) \
+ $(OBJ_DIR_2)/skia-port.$(SO) \
+ $(OBJ_DIR_2)/rsvg-port-alt.$(SO) \
+ $(OBJ_DIR_2)/skia-port-alt.$(SO)
####################################################################
diff --git a/meson.build b/meson.build
index 95fd322..600cf4f 100644
--- a/meson.build
+++ b/meson.build
@@ -83,6 +83,8 @@ ftcommon_lib = static_library('ftcommon',
'src/ftpngout.c',
'src/rsvg-port.c',
'src/rsvg-port.h',
+ 'src/skia-port.c',
+ 'src/skia-port.h',
],
c_args: ftcommon_lib_c_args,
dependencies: [libpng_dep, librsvg_dep, libfreetype2_dep],
diff --git a/src/ftcommon.c b/src/ftcommon.c
index dd33006..23e2a0b 100644
--- a/src/ftcommon.c
+++ b/src/ftcommon.c
@@ -29,6 +29,7 @@
#include "strbuf.h"
#include "ftcommon.h"
#include "rsvg-port.h"
+#include "skia-port.h"
#include <stdio.h>
#include <stdlib.h>
@@ -395,6 +396,8 @@
handle->use_sbits_cache = 1;
+ handle->use_skia = 0;
+
/* string_init */
memset( handle->string, 0, sizeof ( TGlyph ) * MAX_GLYPHS );
handle->string_length = 0;
@@ -444,6 +447,35 @@
}
+ void
+ FTDemo_Set_SVG_Hooks( FTDemo_Handle* handle )
+ {
+ /* The use of an external SVG rendering library is optional. */
+ switch ( handle->use_skia )
+ {
+ case 1:
+ (void)FT_Property_Set( handle->library,
+ "ot-svg", "svg-hooks", &skia_hooks );
+ break;
+
+ case 2:
+ (void)FT_Property_Set( handle->library,
+ "ot-svg", "svg-hooks", &skia_hooks2 );
+ break;
+
+ case 3:
+ (void)FT_Property_Set( handle->library,
+ "ot-svg", "svg-hooks", &rsvg_hooks2 );
+ break;
+
+ default:
+ (void)FT_Property_Set( handle->library,
+ "ot-svg", "svg-hooks", &rsvg_hooks );
+ break;
+ }
+ }
+
+
void
FTDemo_Version( FTDemo_Handle* handle,
FT_String str[64] )
diff --git a/src/ftcommon.h b/src/ftcommon.h
index f8c371b..8e29100 100644
--- a/src/ftcommon.h
+++ b/src/ftcommon.h
@@ -223,6 +223,8 @@
int lcd_mode; /* mono, aa, light, vrgb, ... */
int preload; /* force font file preloading */
+ int use_skia; /* use_skia for render hooks */
+
/* don't touch the following fields! */
/* used for string rendering */
@@ -244,6 +246,11 @@
FTDemo_Done( FTDemo_Handle* handle );
+ /* set svg hooks */
+ void
+ FTDemo_Set_SVG_Hooks( FTDemo_Handle* handle );
+
+
/* append version information */
void
FTDemo_Version( FTDemo_Handle* handle,
diff --git a/src/ftgrid.c b/src/ftgrid.c
index 3f992ca..d2c670d 100644
--- a/src/ftgrid.c
+++ b/src/ftgrid.c
@@ -1716,7 +1716,7 @@
while ( 1 )
{
- option = getopt( *argc, *argv, "a:d:e:f:k:nr:v" );
+ option = getopt( *argc, *argv, "a:d:e:f:k:nr:s:v" );
if ( option == -1 )
break;
@@ -1774,6 +1774,11 @@
usage( execname );
break;
+ case 's':
+ handle->use_skia = atoi( optarg );
+ FTDemo_Set_SVG_Hooks( handle );
+ break;
+
case 'v':
{
FT_String str[64] = "ftgrid (FreeType) ";
diff --git a/src/ftview.c b/src/ftview.c
index c3482e2..92b66e1 100644
--- a/src/ftview.c
+++ b/src/ftview.c
@@ -1710,7 +1710,7 @@
while ( 1 )
{
- option = getopt( *argc, *argv, "d:e:f:k:L:l:m:pr:v" );
+ option = getopt( *argc, *argv, "d:e:f:k:L:l:m:pr:s:v" );
if ( option == -1 )
break;
@@ -1799,6 +1799,12 @@
usage( execname );
break;
+ case 's':
+ handle->use_skia = atoi( optarg );
+ FTDemo_Set_SVG_Hooks( handle );
+ break;
+
+
case 'v':
{
FT_String str[64] = "ftview (FreeType) ";
diff --git a/src/rsvg-port.c b/src/rsvg-port.c
index 4e39d0f..ce3e7d3 100644
--- a/src/rsvg-port.c
+++ b/src/rsvg-port.c
@@ -22,7 +22,12 @@
#ifdef HAVE_LIBRSVG
#include <cairo.h>
+#ifdef USE_ADOBE
+#include "svgnative/SVGDocument.h"
+#include "svgnative/ports/cairo/CairoSVGRenderer.h"
+#else
#include <librsvg/rsvg.h>
+#endif
#include <stdlib.h>
#include <math.h>
@@ -30,6 +35,7 @@
#include <freetype/ftbbox.h>
#include "rsvg-port.h"
+#include <memory>
/*
@@ -39,12 +45,17 @@
* useful to cache some of the results obtained by one hook function that
* the other one might use.
*/
- FT_Error
+ static FT_Error
rsvg_port_init( FT_Pointer *state )
{
/* allocate the memory upon initialization */
*state = malloc( sizeof( Rsvg_Port_StateRec ) ); /* XXX error handling */
+#ifdef USE_ADOBE
+ fprintf( stderr, "Using Adobe Cairo\n" );
+#else
+ fprintf( stderr, "Using Librsvg Cairo\n" );
+#endif
return FT_Err_Ok;
}
@@ -52,7 +63,7 @@
/*
* Deallocate the state structure.
*/
- void
+ static void
rsvg_port_free( FT_Pointer *state )
{
free( *state );
@@ -65,7 +76,7 @@
* simply use the recording surface by playing it back against the
* surface.
*/
- FT_Error
+ static FT_Error
rsvg_port_render( FT_GlyphSlot slot,
FT_Pointer *_state )
{
@@ -145,7 +156,7 @@
* necessary for appropriate memory allocation, as well as ultimately
* compositing the glyph later on by client applications.
*/
- FT_Error
+ static FT_Error
rsvg_port_preset_slot( FT_GlyphSlot slot,
FT_Bool cache,
FT_Pointer *_state )
@@ -160,6 +171,7 @@
FT_UShort end_glyph_id = document->end_glyph_id;
FT_UShort start_glyph_id = document->start_glyph_id;
+#ifndef USE_ADOBE /* Rsvg */
/* Librsvg variables. */
GError *gerror = NULL;
gboolean ret;
@@ -173,6 +185,7 @@
RsvgLength out_height;
RsvgRectangle out_viewbox;
RsvgDimensionData dimension_svg;
+#endif
cairo_t *rec_cr;
cairo_matrix_t transform_matrix;
@@ -207,15 +220,23 @@
state = &state_dummy;
/* Form an `RsvgHandle` by loading the SVG document. */
+#ifndef USE_ADOBE /* Rsvg */
handle = rsvg_handle_new_from_data( document->svg_document,
document->svg_document_length,
&gerror );
+#else
+ auto renderer = std::make_shared<SVGNative::CairoSVGRenderer>();
+ char *s = strndup((char *)document->svg_document,
+ document->svg_document_length); // careful here, svg documents could have NULLs - see svg-native-viewer#164
+ auto handle = std::unique_ptr<SVGNative::SVGDocument>(SVGNative::SVGDocument::CreateSVGDocument(s, renderer));
+#endif
if ( handle == NULL )
{
error = FT_Err_Invalid_SVG_Document;
goto CleanLibrsvg;
}
+#ifndef USE_ADOBE /* Rsvg */
/* Get attributes like `viewBox` and `width`/`height`. */
rsvg_handle_get_intrinsic_dimensions( handle,
&out_has_width,
@@ -274,6 +295,10 @@
/* Scale factors from SVG coordinates to the needed output size. */
x_svg_to_out = (double)metrics.x_ppem / dimension_svg.width;
y_svg_to_out = (double)metrics.y_ppem / dimension_svg.height;
+#else
+ x_svg_to_out = (double)metrics.x_ppem / units_per_EM;
+ y_svg_to_out = (double)metrics.y_ppem / units_per_EM;
+#endif
/*
* Create a cairo recording surface. This is done for two reasons.
@@ -302,9 +327,18 @@
yy = (double)document->transform.yy / ( 1 << 16 );
x0 = (double)document->delta.x / 64 *
+#ifndef USE_ADOBE /* Rsvg */
dimension_svg.width / metrics.x_ppem;
+#else
+ handle->Width() / metrics.x_ppem;
+#endif
y0 = -(double)document->delta.y / 64 *
+#ifndef USE_ADOBE /* Rsvg */
dimension_svg.height / metrics.y_ppem;
+#else
+ handle->Height() / metrics.y_ppem;
+#endif
+
/* Cairo stores both transformation and translation in one matrix. */
transform_matrix.xx = xx;
@@ -320,21 +354,34 @@
/* Set up a transformation matrix. */
cairo_transform( rec_cr, &transform_matrix );
+#ifdef USE_ADOBE
+ renderer->SetCairo(rec_cr);
+#endif
/* If the document contains only one glyph, `start_glyph_id` and */
/* `end_glyph_id` have the same value. Otherwise `end_glyph_id` */
/* is larger. */
if ( start_glyph_id < end_glyph_id )
{
/* Render only the element with its ID equal to `glyph<ID>`. */
+#ifndef USE_ADOBE /* Rsvg */
sprintf( str, "#glyph%u", slot->glyph_index );
id = str;
+#else
+ sprintf( str, "glyph%u", slot->glyph_index );
+ id = str;
+ handle->Render(id, handle->Width(), handle->Height());
+#endif
}
else
{
+#ifdef USE_ADOBE
+ handle->Render(handle->Width(), handle->Height());
+#endif
/* NULL = Render the whole document */
id = NULL;
}
+#ifndef USE_ADOBE /* Rsvg */
#if LIBRSVG_CHECK_VERSION( 2, 52, 0 )
{
RsvgRectangle viewport =
@@ -361,6 +408,7 @@
error = FT_Err_Invalid_SVG_Document;
goto CleanCairo;
}
+#endif
/* Get the bounding box of the drawing. */
cairo_recording_surface_ink_extents( state->rec_surface, &x, &y,
@@ -428,13 +476,19 @@
CleanLibrsvg:
/* Destroy the handle. */
+#ifndef USE_ADOBE /* Rsvg */
g_object_unref( handle );
+#endif
return error;
}
+#ifdef USE_ADOBE
+ SVG_RendererHooks rsvg_hooks2 = {
+#else
SVG_RendererHooks rsvg_hooks = {
+#endif
(SVG_Lib_Init_Func)rsvg_port_init,
(SVG_Lib_Free_Func)rsvg_port_free,
(SVG_Lib_Render_Func)rsvg_port_render,
diff --git a/src/rsvg-port.h b/src/rsvg-port.h
index b9164b3..d256e96 100644
--- a/src/rsvg-port.h
+++ b/src/rsvg-port.h
@@ -25,7 +25,9 @@
#ifdef HAVE_LIBRSVG
#include <cairo.h>
+#ifndef USE_ADOBE /* Rsvg */
#include <librsvg/rsvg.h>
+#endif
#include <freetype/freetype.h>
@@ -45,25 +47,10 @@
typedef struct Rsvg_Port_StateRec_* Rsvg_Port_State;
-
- FT_Error
- rsvg_port_init( FT_Pointer *state );
-
- void
- rsvg_port_free( FT_Pointer *state );
-
- FT_Error
- rsvg_port_render( FT_GlyphSlot slot,
- FT_Pointer *state );
-
- FT_Error
- rsvg_port_preset_slot( FT_GlyphSlot slot,
- FT_Bool cache,
- FT_Pointer *state );
-
#endif /* HAVE_LIBRSVG */
+ extern SVG_RendererHooks rsvg_hooks2;
extern SVG_RendererHooks rsvg_hooks;
#endif /* RSVG_PORT_H */
diff --git a/src/skia-port.c b/src/skia-port.c
new file mode 100644
index 0000000..33e782e
--- /dev/null
+++ b/src/skia-port.c
@@ -0,0 +1,391 @@
+/****************************************************************************
+ *
+ * skia-port.c
+ *
+ * Skia-based hook functions for OT-SVG rendering in FreeType
+ * (implementation).
+ *
+ * Copyright (C) 2022-2023 by
+ * Hin-Tak Leung, based on rsvg-port.c
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+/*
+ * Main reference, this landed in Skia m103:
+ * https://github.com/google/skia/commit/9cbadcd9280dc139af2f4d41d25a6c9a750e0302.patch
+
+ * From 9cbadcd9280dc139af2f4d41d25a6c9a750e0302 Mon Sep 17 00:00:00 2001
+ * From: Ben Wagner <bungeman@google.com>
+ * Date: Wed, 20 Apr 2022 17:52:50 -0400
+ * Subject: [PATCH] Add optional OT-SVG support to FreeType
+
+ * In particular,
+ src/ports/SkFontHost_FreeType_common.cpp:
+ SkScalerContext_FreeType_Base::drawSVGGlyph()
+ src/ports/SkFontHost_FreeType.cpp:
+ SkScalerContext_FreeType::generateMetrics()
+*/
+#include <ft2build.h>
+#include <freetype/otsvg.h>
+
+#ifdef HAVE_SKIA
+
+#ifdef USE_ADOBE
+#include "svgnative/SVGDocument.h"
+#include "svgnative/ports/skia/SkiaSVGRenderer.h"
+#else
+#include "modules/svg/include/SkSVGDOM.h"
+#include "modules/svg/include/SkSVGNode.h"
+#include "modules/svg/include/SkSVGRenderContext.h" // SkSVGPresentationContext
+#endif
+#include "include/core/SkBBHFactory.h"
+#include "include/core/SkBitmap.h"
+#include "include/core/SkCanvas.h"
+#include "include/core/SkMatrix.h"
+#include "include/core/SkStream.h"
+#include "include/private/base/SkFixed.h" // SkFixedToFloat(x)
+#include <stdlib.h>
+#include <math.h>
+
+#include <freetype/freetype.h>
+#include <freetype/ftbbox.h>
+
+#include "skia-port.h"
+#include <memory>
+
+ /*
+ * The init hook is called when the first OT-SVG glyph is rendered. All
+ * we do is to allocate an internal state structure and set the pointer in
+ * `library->svg_renderer_state`. This state structure becomes very
+ * useful to cache some of the results obtained by one hook function that
+ * the other one might use.
+ */
+ static FT_Error
+ skia_port_init( FT_Pointer *state )
+ {
+ /* allocate the memory upon initialization */
+ *state = calloc( sizeof( Skia_Port_StateRec ), 1 ); /* XXX error handling */
+ // Skia pointers are reference-counted, so
+ // malloc seems to be buggy and calloc is needed here.
+ // Unlike rsvg.
+
+#ifdef USE_ADOBE
+ fprintf( stderr, "Using Adobe Skia\n" );
+#else
+ fprintf( stderr, "Using Skia m103+\n" );
+#endif
+ return FT_Err_Ok;
+ }
+
+
+ /*
+ * Deallocate the state structure.
+ */
+ static void
+ skia_port_free( FT_Pointer *state )
+ {
+ free( *state );
+ }
+
+
+ /*
+ * The render hook. The job of this hook is to simply render the glyph in
+ * the buffer that has been allocated on the FreeType side. Here we
+ * simply use the recording surface by playing it back against the
+ * surface.
+ */
+ static FT_Error
+ skia_port_render( FT_GlyphSlot slot,
+ FT_Pointer *_state )
+ {
+ FT_Error error = FT_Err_Ok;
+
+ Skia_Port_State state;
+
+ state = *(Skia_Port_State*)_state;
+
+ /* Create a SkBitmap to store the rendered image. However, */
+ /* don't allocate memory; instead use the space already provided in */
+ /* `slot->bitmap.buffer`. */
+ SkBitmap dstBitmap;
+ dstBitmap.setInfo(SkImageInfo::Make(slot->bitmap.width, slot->bitmap.rows,
+ kBGRA_8888_SkColorType, // Not kN32 - FT_Bitmap are platform-neutral, FT_PIXEL_MODE_BGRA blue channel comes first in memory.
+ kPremul_SkAlphaType), // FT_PIXEL_MODE_BGRA is pre-multiplied and in the sRGB colorspace
+ slot->bitmap.pitch);
+ dstBitmap.setPixels(slot->bitmap.buffer);
+
+ SkCanvas canvas(dstBitmap);
+
+ canvas.clear(SK_ColorTRANSPARENT);
+
+ /* Set a translate transform that translates the points in such a way */
+ /* that we get a tight rendering with least redundant white spac. */
+ canvas.translate( -state->x, -state->y ); /* floating values in skia */
+
+ /* Replay from state->picture. This saves us from parsing the */
+ /* document again and redoing what was already done in the preset */
+ /* hook. */
+ canvas.drawPicture( state->picture );
+
+ slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA;
+ slot->bitmap.num_grays = 256;
+ slot->format = FT_GLYPH_FORMAT_BITMAP;
+
+ /* Clean up everything. */
+ state->picture.reset();
+
+ return error;
+ }
+
+
+ /*
+ * This hook is called at two different locations. Firstly, it is called
+ * when presetting the glyphslot when `FT_Load_Glyph` is called.
+ * Secondly, it is called right before the render hook is called. When
+ * `cache` is false, it is the former, when `cache` is true, it is the
+ * latter.
+ *
+ * The job of this function is to preset the slot setting the width,
+ * height, pitch, `bitmap.left`, and `bitmap.top`. These are all
+ * necessary for appropriate memory allocation, as well as ultimately
+ * compositing the glyph later on by client applications.
+ */
+ static FT_Error
+ skia_port_preset_slot( FT_GlyphSlot slot,
+ FT_Bool cache,
+ FT_Pointer *_state )
+ {
+ /* FreeType variables. */
+ FT_Error error = FT_Err_Ok;
+
+ FT_SVG_Document document = (FT_SVG_Document)slot->other;
+
+ FT_UShort units_per_EM = document->units_per_EM;
+ FT_UShort end_glyph_id = document->end_glyph_id;
+ FT_UShort start_glyph_id = document->start_glyph_id;
+
+ SkCanvas *recordingCanvas;
+ SkMatrix m;
+
+ /* Rendering port's state. */
+ Skia_Port_State state;
+ Skia_Port_StateRec state_dummy;
+
+ /* General variables. */
+ double x, y;
+ double width, height;
+
+ float metrics_width, metrics_height;
+ float horiBearingX, horiBearingY;
+ float vertBearingX, vertBearingY;
+ float tmpf;
+
+ /* If `cache` is `TRUE` we store calculations in the actual port */
+ /* state variable, otherwise we just create a dummy variable and */
+ /* store there. This saves us from too many 'if' statements. */
+ if ( cache )
+ state = *(Skia_Port_State*)_state;
+ else
+ state = &state_dummy;
+
+ /* Form an `sk_sp<SkSVGDOM>` by loading the SVG document. */
+#ifdef USE_ADOBE
+ auto renderer = std::make_shared<SVGNative::SkiaSVGRenderer>();
+ char *s = strndup((char *)document->svg_document,
+ document->svg_document_length); // careful here, svg documents could have NULLs - see svg-native-viewer#164
+ auto svg = std::unique_ptr<SVGNative::SVGDocument>(SVGNative::SVGDocument::CreateSVGDocument(s, renderer));
+#else
+ SkMemoryStream svgmem(document->svg_document,
+ document->svg_document_length, false /*not copying */);
+ sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromStream(svgmem);
+
+ //svg->getRoot()->intrinsicSize();
+ if (svg->containerSize().isEmpty()) {
+ SkSize size = SkSize::Make(units_per_EM, units_per_EM);
+ svg->setContainerSize(size);
+ }
+#endif
+ // Do we care about the viewBox attribute? It is auto I think, anyway.
+
+ /*
+ * Create a SkPictureRecorder. This is done for two reasons.
+ * Firstly, it is required to get the bounding box of the final drawing
+ * so we can use an appropriate translate transform to get a tight
+ * rendering. Secondly, if `cache` is true, we can save this surface
+ * and later replay it against an image surface for the final rendering.
+ * This saves us from loading and parsing the document again.
+ */
+ SkPictureRecorder recorder;
+
+ SkRect infiniteRect = SkRect::MakeLTRB(-SK_ScalarInfinity, -SK_ScalarInfinity,
+ SK_ScalarInfinity, SK_ScalarInfinity);
+ sk_sp<SkBBoxHierarchy> bboxh = SkRTreeFactory()();
+
+ recordingCanvas = recorder.beginRecording(infiniteRect, bboxh);
+
+ /*
+ * Borrow heavily from:
+ * src/ports/SkFontHost_FreeType_common.cpp:
+ * SkScalerContext_FreeType_Base::drawSVGGlyph()
+ */
+ SkASSERT(slot->format == FT_GLYPH_FORMAT_SVG);
+
+ /*
+ * We need to take into account any transformations applied. The end
+ * user who applied the transformation doesn't know the internal details
+ * of the SVG document. Thus, we expect that the end user should just
+ * write the transformation as if the glyph is a traditional one. We
+ * then do some maths on this to get the equivalent transformation in
+ * SVG coordinates.
+ */
+ FT_Matrix ftMatrix = document->transform;
+ FT_Vector ftOffset = document->delta;
+
+ /* Skia stores both transformation and translation in one matrix. */
+ m.setAll(
+ SkFixedToFloat(ftMatrix.xx), -SkFixedToFloat(ftMatrix.xy), SkFixedToFloat(ftOffset.x),
+ -SkFixedToFloat(ftMatrix.yx), SkFixedToFloat(ftMatrix.yy), -SkFixedToFloat(ftOffset.y),
+ 0 , 0 , 1 );
+
+ /* Set up a scale transformation to scale up the document to the */
+ /* required output size. */
+ m.postScale(SkFixedToFloat(document->metrics.x_scale) / 64.0f,
+ SkFixedToFloat(document->metrics.y_scale) / 64.0f);
+
+ /* Set up a transformation matrix. */
+ recordingCanvas->concat(m);
+
+#ifdef USE_ADOBE
+ renderer->SetSkCanvas(recordingCanvas);
+#endif
+ /* If the document contains only one glyph, `start_glyph_id` and */
+ /* `end_glyph_id` have the same value. Otherwise `end_glyph_id` */
+ /* is larger. */
+ if ( start_glyph_id < end_glyph_id )
+ {
+#ifndef USE_ADOBE /* Skia svg */
+ SkSVGPresentationContext pctx;
+#endif
+ char id[32];
+ /* Render only the element with its ID equal to `glyph<ID>`. */
+ sprintf( id, "glyph%u", slot->glyph_index );
+
+ /*
+ * Unlike Rsvg, Skia's renderNode() takes an extra
+ * SkSVGPresentationContext argument, which sets foreground
+ * colors, palettes, etc, and does not take NULL to render
+ * whole. In the case of OT-SVG, there is no extra
+ * Context, so leaving it as default is fine.
+ */
+#ifdef USE_ADOBE
+ svg->Render(id, svg->Width(), svg->Height());
+#else
+ svg->renderNode(recordingCanvas, pctx, id);
+#endif
+ }
+ else
+ {
+ /* Render the whole document */
+#ifdef USE_ADOBE
+ svg->Render(svg->Width(), svg->Height());
+#else
+ svg->render(recordingCanvas);
+#endif
+ }
+
+ /* Get the bounding box of the drawing. */
+ state->picture = recorder.finishRecordingAsPicture();
+ SkRect bounds = state->picture->cullRect();
+ SkASSERT(bounds.isFinite());
+
+ width = ceil(bounds.right()) - floor(bounds.left()); //bounds.width(); // fRight minus fLeft
+ height = ceil(bounds.bottom()) - floor(bounds.top()); //bounds.height(); // fBottom minus fTop
+ x = floor(bounds.left());
+ y = floor(bounds.top());
+
+ /* We store the bounding box's `x` and `y` values so that the render */
+ /* hook can apply a translation to get a tight rendering. */
+ state->x = x; /* truncation is consistent in being floor() for negative values, to match cast to (FT_Int) below. */
+ state->y = y;
+ /* rsvg actually always returns floats in integral values, but floor() x,y and ceil height/width */
+ /* we don't want to copy the floor behavior, actually */
+
+ /* Preset the values. */
+ slot->bitmap_left = (FT_Int) state->x;
+ slot->bitmap_top = (FT_Int)-state->y;
+
+ slot->bitmap.rows = height ;
+ slot->bitmap.width = width ;
+
+ slot->bitmap.pitch = (int)slot->bitmap.width * 4;
+
+ slot->bitmap.pixel_mode = FT_PIXEL_MODE_BGRA;
+
+ /* Compute all the bearings and set them correctly. The outline is */
+ /* scaled already, we just need to use the bounding box. */
+ metrics_width = (float)width;
+ metrics_height = (float)height;
+
+ horiBearingX = (float) state->x;
+ horiBearingY = (float)-state->y;
+
+ vertBearingX = slot->metrics.horiBearingX / 64.0f -
+ slot->metrics.horiAdvance / 64.0f / 2;
+ vertBearingY = ( slot->metrics.vertAdvance / 64.0f -
+ slot->metrics.height / 64.0f ) / 2; /* XXX parentheses correct? */
+
+ /* Do conversion in two steps to avoid 'bad function cast' warning. */
+ tmpf = roundf( metrics_width * 64 );
+ slot->metrics.width = (FT_Pos)tmpf;
+ tmpf = roundf( metrics_height * 64 );
+ slot->metrics.height = (FT_Pos)tmpf;
+
+ slot->metrics.horiBearingX = (FT_Pos)( horiBearingX * 64 ); /* XXX rounding? */
+ slot->metrics.horiBearingY = (FT_Pos)( horiBearingY * 64 );
+ slot->metrics.vertBearingX = (FT_Pos)( vertBearingX * 64 );
+ slot->metrics.vertBearingY = (FT_Pos)( vertBearingY * 64 );
+
+ if ( slot->metrics.vertAdvance == 0 )
+ slot->metrics.vertAdvance = (FT_Pos)( metrics_height * 1.2f * 64 );
+
+ /* If a render call is to follow, just destroy the canvas for the */
+ /* SkPictureRecorder since no more drawing will be done on it. */
+ /* However, keep the Picture itself for use by the render hook. */
+ // TRUE defined in /usr/include/glib-2.0/glib/gmacros.h
+ // TRUE/FALSE not in c++, defined in include/freetype/internal/ftobjs.h (not used)
+ // but defined indirectly in librsvg. Its header to refer to it (from glib).
+ if ( !cache )
+ {
+ /* We don't have to do this; just being pedantic */
+ state->picture.reset();
+ }
+
+ return error;
+ }
+
+
+#ifdef USE_ADOBE
+ SVG_RendererHooks skia_hooks2 = {
+#else
+ SVG_RendererHooks skia_hooks = {
+#endif
+ (SVG_Lib_Init_Func)skia_port_init,
+ (SVG_Lib_Free_Func)skia_port_free,
+ (SVG_Lib_Render_Func)skia_port_render,
+ (SVG_Lib_Preset_Slot_Func)skia_port_preset_slot
+ };
+
+#else /* !HAVE_SKIA */
+
+ SVG_RendererHooks skia_hooks = { NULL, NULL, NULL, NULL };
+
+#endif /* !HAVE_SKIA */
+
+
+/* End */
diff --git a/src/skia-port.h b/src/skia-port.h
new file mode 100644
index 0000000..7de56c6
--- /dev/null
+++ b/src/skia-port.h
@@ -0,0 +1,58 @@
+/****************************************************************************
+ *
+ * skia-port.h
+ *
+ * Skia based hook functions for OT-SVG rendering in FreeType
+ * (headers).
+ *
+ * Copyright (C) 2022-2023 by
+ * Hin-Tak Leung, based on rsvg-port.h
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+#ifndef SKIA_PORT_H
+#define SKIA_PORT_H
+
+#include <ft2build.h>
+#include <freetype/otsvg.h>
+
+#ifdef HAVE_SKIA
+
+#include "include/core/SkPictureRecorder.h"
+#include "include/core/SkPicture.h"
+#include <freetype/freetype.h>
+
+
+ /*
+ * Different hook functions can access persisting data by creating a state
+ * structure and putting its address in `library->svg_renderer_state`.
+ * Functions can then store and retrieve data from this structure.
+ */
+ typedef struct Skia_Port_StateRec_
+ {
+ sk_sp<SkPicture> picture; /* sk_sp is short for "Shared pointer class to wrap classes that support a ref()/unref() interface."!*/
+
+ double x;
+ double y;
+
+ } Skia_Port_StateRec;
+
+ typedef struct Skia_Port_StateRec_* Skia_Port_State;
+
+
+#endif /* HAVE_SKAI */
+
+
+ extern SVG_RendererHooks skia_hooks2;
+ extern SVG_RendererHooks skia_hooks;
+
+#endif /* SKIA_PORT_H */
+
+
+/* End */
--
2.45.0