FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
skia-python/src/skia/Font.cpp at main · skia-python/skia-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
skia-python
/
skia-python
Public
Notifications
You must be signed in to change notification settings
Fork
52
Star
322
Code
Issues
44
Pull requests
6
Discussions
Actions
Projects
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
skia-python
/
src
/
skia
/
Font.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1743 lines (1560 loc) · 66.5 KB
Breadcrumbs
skia-python
/
src
/
skia
/
Font.cpp
Copy path
File metadata and controls
1743 lines (1560 loc) · 66.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
common.h
"
#
include
<
include/core/SkFontMetrics.h
>
#
include
<
include/ports/SkFontMgr_directory.h
>
#
include
<
include/ports/SkFontMgr_empty.h
>
#
include
<
pybind11/stl.h
>
#
include
<
pybind11/stl_bind.h
>
#
include
<
pybind11/iostream.h
>
#
ifdef
__linux__
#
include
<
freetype/freetype.h
>
#
include
<
freetype/ftcolor.h
>
#
include
<
src/ports/SkFontHost_FreeType_common.h
>
#
endif
#
ifdef
__APPLE__
#
include
"
include/ports/SkFontMgr_mac_ct.h
"
#
endif
#
ifdef
__linux__
#
include
"
include/ports/SkFontMgr_fontconfig.h
"
#
include
"
include/ports/SkFontScanner_FreeType.h
"
#
endif
#
ifdef
_WIN32
#
include
"
include/ports/SkTypeface_win.h
"
#
endif
#
include
<
mutex
>
namespace
{
bool
g_factory_called =
false
;
}
//
namespace
static
sk_sp<SkFontMgr>
fontmgr_factory
() {
#
if
defined(__APPLE__)
return
SkFontMgr_New_CoreText
(
nullptr
);
#
elif
defined(__linux__)
return
SkFontMgr_New_FontConfig
(
nullptr
,
SkFontScanner_Make_FreeType
());
#
elif
defined(_WIN32)
return
SkFontMgr_New_DirectWrite
();
#
else
return
SkFontMgr_New_Custom_Empty
();
/*
last resort: SkFontMgr::RefEmpty();
*/
#
endif
}
sk_sp<SkFontMgr>
SkFontMgr_RefDefault
() {
static
std::once_flag flag;
static
sk_sp<SkFontMgr> mgr;
std::call_once
(flag, [] {
mgr =
fontmgr_factory
();
g_factory_called =
true
;
});
return
mgr;
}
using
Axis = SkFontParameters::Variation::Axis;
using
Coordinate = SkFontArguments::VariationPosition::Coordinate;
PYBIND11_MAKE_OPAQUE
(std::vector<Coordinate>);
namespace
{
void
SetVariationPositionCoordinates
(
SkFontArguments::VariationPosition& vp,
const
std::vector<Coordinate>& coords) {
vp.
coordinates
= &coords[
0
];
vp.
coordinateCount
= coords.
size
();
}
py::tuple
SkFontStyleSet_getStyle
(SkFontStyleSet* self,
int
index) {
SkFontStyle style;
SkString name;
if
(index <
0
|| index >= self->
count
())
throw
py::index_error
();
self->
getStyle
(index, &style, &name);
return
py::make_tuple
(style,
py::str
(name.
c_str
(), name.
size
()));
}
py::str
SkFontMgr_getFamilyName
(
const
SkFontMgr& fontmgr,
int
index) {
SkString familyName;
if
(index <
0
|| index >= fontmgr.
countFamilies
())
throw
py::index_error
();
fontmgr.
getFamilyName
(index, &familyName);
return
py::str
(familyName.
c_str
(), familyName.
size
());
}
sk_sp<SkTypeface>
SkTypeface_MakeFromName
(
py::object familyName,
const
SkFontStyle* fontStyle) {
return
SkFontMgr_RefDefault
()->
legacyMakeTypeface
(
(familyName.
is_none
()) ?
nullptr
: familyName.
cast
<std::string>().
c_str
(),
(fontStyle) ? *fontStyle :
SkFontStyle
());
}
}
//
namespace
namespace
{
/*
class OneFontStyleSet and OneFontMgr verbatim from
skia's chrome/m128:example/external_client/src/shape_text.cpp
*/
class
OneFontStyleSet
:
public
SkFontStyleSet
{
public:
explicit
OneFontStyleSet
(sk_sp<SkTypeface> face) : face_(face) {}
protected:
int
count
()
override
{
return
1
; }
void
getStyle
(
int
, SkFontStyle* out_style, SkString*)
override
{
*out_style =
SkFontStyle
();
}
sk_sp<SkTypeface>
createTypeface
(
int
index)
override
{
return
face_; }
sk_sp<SkTypeface>
matchStyle
(
const
SkFontStyle&)
override
{
return
face_; }
private:
sk_sp<SkTypeface> face_;
};
class
OneFontMgr
:
public
SkFontMgr
{
public:
explicit
OneFontMgr
(sk_sp<SkTypeface> face)
: face_(face), style_set_(sk_make_sp<OneFontStyleSet>(face)) {}
protected:
int
onCountFamilies
()
const
override
{
return
1
; }
void
onGetFamilyName
(
int
index, SkString* familyName)
const
override
{
*familyName =
SkString
(
"
the-only-font-I-have
"
);
}
sk_sp<SkFontStyleSet>
onCreateStyleSet
(
int
index)
const
override
{
return
style_set_;
}
sk_sp<SkFontStyleSet>
onMatchFamily
(
const
char
[])
const
override
{
return
style_set_;
}
sk_sp<SkTypeface>
onMatchFamilyStyle
(
const
char
[],
const
SkFontStyle&)
const
override
{
return
face_;
}
sk_sp<SkTypeface>
onMatchFamilyStyleCharacter
(
const
char
familyName[],
const
SkFontStyle& style,
const
char
* bcp47[],
int
bcp47Count, SkUnichar character)
const
override
{
return
face_;
}
sk_sp<SkTypeface>
onLegacyMakeTypeface
(
const
char
[],
SkFontStyle)
const
override
{
return
face_;
}
sk_sp<SkTypeface>
onMakeFromData
(sk_sp<SkData>,
int
)
const
override
{
std::abort
();
return
nullptr
;
}
sk_sp<SkTypeface>
onMakeFromStreamIndex
(std::unique_ptr<SkStreamAsset>,
int
)
const
override
{
std::abort
();
return
nullptr
;
}
sk_sp<SkTypeface>
onMakeFromStreamArgs
(
std::unique_ptr<SkStreamAsset>,
const
SkFontArguments&)
const
override
{
std::abort
();
return
nullptr
;
}
sk_sp<SkTypeface>
onMakeFromFile
(
const
char
[],
int
)
const
override
{
std::abort
();
return
nullptr
;
}
private:
sk_sp<SkTypeface> face_;
sk_sp<SkFontStyleSet> style_set_;
};
/*
Adapted from skia's chrome/m128:example/external_client/src/shape_text.cpp
*/
/*
Forward declaration
*/
sk_sp<SkFontMgr>
OneFontMgr_New_Custom_Empty
(sk_sp<SkData> font_data);
sk_sp<SkFontMgr>
OneFontMgr_New_Custom_Empty
(
char
* argv1) {
SkFILEStream
input
(argv1);
if
(!input.
isValid
()) {
printf
(
"
Cannot open input file %s
\n
"
, argv1);
return
nullptr
;
}
sk_sp<SkData> font_data =
SkData::MakeFromStream
(&input, input.
getLength
());
return
OneFontMgr_New_Custom_Empty
(font_data);
}
sk_sp<SkFontMgr>
OneFontMgr_New_Custom_Empty
(sk_sp<SkData> font_data) {
sk_sp<SkFontMgr> mgr =
SkFontMgr_New_Custom_Empty
();
sk_sp<SkTypeface> face = mgr->
makeFromData
(font_data);
if
(!face) {
printf
(
"
input font stream was not parsable by Freetype
\n
"
);
return
nullptr
;
}
return
sk_make_sp<OneFontMgr>(face);
}
}
//
namespace
void
initFont
(py::
module
&m) {
//
FontStyle
py::class_<SkFontStyle>
fontstyle
(m,
"
FontStyle
"
);
py::enum_<SkFontStyle::Weight>(fontstyle,
"
Weight
"
,
py::arithmetic
())
.
value
(
"
kInvisible_Weight
"
, SkFontStyle::Weight::
kInvisible_Weight
)
.
value
(
"
kThin_Weight
"
, SkFontStyle::Weight::
kThin_Weight
)
.
value
(
"
kExtraLight_Weight
"
, SkFontStyle::Weight::
kExtraLight_Weight
)
.
value
(
"
kLight_Weight
"
, SkFontStyle::Weight::
kLight_Weight
)
.
value
(
"
kNormal_Weight
"
, SkFontStyle::Weight::
kNormal_Weight
)
.
value
(
"
kMedium_Weight
"
, SkFontStyle::Weight::
kMedium_Weight
)
.
value
(
"
kSemiBold_Weight
"
, SkFontStyle::Weight::
kSemiBold_Weight
)
.
value
(
"
kBold_Weight
"
, SkFontStyle::Weight::
kBold_Weight
)
.
value
(
"
kExtraBold_Weight
"
, SkFontStyle::Weight::
kExtraBold_Weight
)
.
value
(
"
kBlack_Weight
"
, SkFontStyle::Weight::
kBlack_Weight
)
.
value
(
"
kExtraBlack_Weight
"
, SkFontStyle::Weight::
kExtraBlack_Weight
)
.
export_values
();
py::enum_<SkFontStyle::Width>(fontstyle,
"
Width
"
,
py::arithmetic
())
.
value
(
"
kUltraCondensed_Width
"
, SkFontStyle::Width::
kUltraCondensed_Width
)
.
value
(
"
kExtraCondensed_Width
"
, SkFontStyle::Width::
kExtraCondensed_Width
)
.
value
(
"
kCondensed_Width
"
, SkFontStyle::Width::
kCondensed_Width
)
.
value
(
"
kSemiCondensed_Width
"
, SkFontStyle::Width::
kSemiCondensed_Width
)
.
value
(
"
kNormal_Width
"
, SkFontStyle::Width::
kNormal_Width
)
.
value
(
"
kSemiExpanded_Width
"
, SkFontStyle::Width::
kSemiExpanded_Width
)
.
value
(
"
kExpanded_Width
"
, SkFontStyle::Width::
kExpanded_Width
)
.
value
(
"
kExtraExpanded_Width
"
, SkFontStyle::Width::
kExtraExpanded_Width
)
.
value
(
"
kUltraExpanded_Width
"
, SkFontStyle::Width::
kUltraExpanded_Width
)
.
export_values
();
py::enum_<SkFontStyle::Slant>(fontstyle,
"
Slant
"
)
.
value
(
"
kUpright_Slant
"
, SkFontStyle::Slant::
kUpright_Slant
)
.
value
(
"
kItalic_Slant
"
, SkFontStyle::Slant::
kItalic_Slant
)
.
value
(
"
kOblique_Slant
"
, SkFontStyle::Slant::
kOblique_Slant
)
.
export_values
();
fontstyle
.
def
(py::init<
int
,
int
, SkFontStyle::Slant>(),
py::arg
(
"
weight
"
),
py::arg
(
"
width
"
),
py::arg
(
"
slant
"
))
.
def
(py::init<>())
.
def
(
"
__repr__
"
,
[] (
const
SkFontStyle& self) {
return
py::str
(
"
FontStyle({}, {}, {})
"
).
format
(
self.
weight
(), self.
width
(), self.
slant
());
})
.
def
(
"
weight
"
, &SkFontStyle::weight)
.
def
(
"
width
"
, &SkFontStyle::width)
.
def
(
"
slant
"
, &SkFontStyle::slant)
.
def_static
(
"
Normal
"
, &SkFontStyle::Normal)
.
def_static
(
"
Bold
"
, &SkFontStyle::Bold)
.
def_static
(
"
Italic
"
, &SkFontStyle::Italic)
.
def_static
(
"
BoldItalic
"
, &SkFontStyle::BoldItalic)
;
//
Typeface
py::class_<SkFontParameters>
fontparameters
(m,
"
FontParameters
"
);
py::class_<SkFontParameters::Variation>
variation
(
fontparameters,
"
Variation
"
,
R"docstring(
Parameters in a variation font axis.
)docstring"
);
py::class_<Axis>(variation,
"
Axis
"
)
.
def
(py::init<>())
.
def
(
"
__repr__
"
,
[] (
const
Axis& self) {
return
py::str
(
"
Axis(tag={:x}, min={}, def={}, max={})
"
).
format
(
self.
tag
, self.
min
, self.
def
, self.
max
);
})
.
def_readwrite
(
"
tag
"
, &Axis::tag,
R"docstring(
Four character identifier of the font axis (weight, width, slant,
italic...).
)docstring"
)
.
def_readwrite
(
"
min
"
, &Axis::min,
R"docstring(
Minimum value supported by this axis.
)docstring"
)
.
def_readwrite
(
"
def
"
, &Axis::def,
R"docstring(
Default value set by this axis.
)docstring"
)
.
def_readwrite
(
"
max
"
, &Axis::max,
R"docstring(
Maximum value supported by this axis. The maximum can equal the minimum.
)docstring"
)
.
def
(
"
isHidden
"
, &Axis::isHidden,
R"docstring(
Return whether this axis is recommended to be remain hidden in user
interfaces.
)docstring"
)
.
def
(
"
setHidden
"
, &Axis::setHidden,
R"docstring(
Set this axis to be remain hidden in user interfaces.
)docstring"
,
py::arg
(
"
hidden
"
))
;
py::class_<SkFontArguments>
fontarguments
(m,
"
FontArguments
"
,
R"docstring(
Represents a set of actual arguments for a font.
)docstring"
);
py::class_<SkFontArguments::VariationPosition>
variationposition
(
fontarguments,
"
VariationPosition
"
,
R"docstring(
Container of coordinates.
)docstring"
);
py::class_<Coordinate>(
variationposition,
"
Coordinate
"
)
.
def
(
py::init
(
[] (SkFourByteTag axis,
float
value) {
return
Coordinate
({axis, value});
}),
py::arg
(
"
axis
"
),
py::arg
(
"
value
"
))
.
def
(
"
__repr__
"
,
[] (
const
Coordinate& self) {
return
py::str
(
"
Coordinate(axis={:x}, value={})
"
).
format
(
self.
axis
, self.
value
);
})
.
def_readwrite
(
"
axis
"
, &Coordinate::axis)
.
def_readwrite
(
"
value
"
, &Coordinate::value)
;
py::bind_vector<std::vector<Coordinate>>(variationposition,
"
Coordinates
"
);
variationposition
.
def
(
py::init
(
[] (
const
std::vector<Coordinate>& coordinates) {
SkFontArguments::VariationPosition vp;
SetVariationPositionCoordinates
(vp, coordinates);
return
vp;
}),
py::arg
(
"
coordinates
"
))
.
def_property
(
"
coordinates
"
,
[] (
const
SkFontArguments::VariationPosition& vp) {
return
std::vector<Coordinate>(
vp.
coordinates
, vp.
coordinates
+ vp.
coordinateCount
);
},
&SetVariationPositionCoordinates)
.
def_readonly
(
"
coordinateCount
"
,
&SkFontArguments::VariationPosition::coordinateCount)
;
fontarguments
.
def
(py::init<>())
.
def
(
"
setCollectionIndex
"
, &SkFontArguments::setCollectionIndex,
R"docstring(
Specify the index of the desired font.
Font formats like ttc, dfont, cff, cid, pfr, t42, t1, and fon may
actually be indexed collections of fonts.
)docstring"
,
py::arg
(
"
collectionIndex
"
))
.
def
(
"
setVariationDesignPosition
"
,
&SkFontArguments::setVariationDesignPosition,
R"docstring(
Specify a position in the variation design space.
Any axis not specified will use the default value. Any specified axis
not actually present in the font will be ignored.
:param position: not copied. The value must remain valid for life of
:py:class:`FontArguments`.
)docstring"
,
py::arg
(
"
position
"
))
.
def
(
"
getCollectionIndex
"
, &SkFontArguments::getCollectionIndex)
.
def
(
"
getVariationDesignPosition
"
,
&SkFontArguments::getVariationDesignPosition)
;
py::class_<SkTypeface, sk_sp<SkTypeface>, SkRefCnt>
typeface
(
m,
"
Typeface
"
,
R"docstring(
The :py:class:`Typeface` class specifies the typeface and intrinsic style of
a font.
This is used in the paint, along with optionally algorithmic settings like
textSize, textSkewX, textScaleX, kFakeBoldText_Mask, to specify how text
appears when drawn (and measured).
Typeface objects are immutable, and so they can be shared between threads.
Example::
typeface = skia.Typeface()
typeface = skia.Typeface('Arial')
typeface = skia.Typeface('Arial', skia.FontStyle.Bold())
)docstring"
);
py::enum_<SkTypeface::SerializeBehavior>(typeface,
"
SerializeBehavior
"
,
R"docstring(
A typeface can serialize just a descriptor (names, etc.), or it can also
include the actual font data (which can be large).
This enum controls how serialize() decides what to serialize.
)docstring"
)
.
value
(
"
kDoIncludeData
"
, SkTypeface::SerializeBehavior::
kDoIncludeData
)
.
value
(
"
kDontIncludeData
"
, SkTypeface::SerializeBehavior::
kDontIncludeData
)
.
value
(
"
kIncludeDataIfLocal
"
,
SkTypeface::SerializeBehavior::
kIncludeDataIfLocal
)
.
export_values
();
typeface
.
def
(
py::init
(
[] (
void
) {
auto
warnings =
pybind11::module::import
(
"
warnings
"
);
auto
builtins =
pybind11::module::import
(
"
builtins
"
);
warnings.
attr
(
"
warn
"
)(
"
\"
Default typeface
\"
is deprecated upstream. Please specify name/file/style choices.
"
,
builtins.
attr
(
"
DeprecationWarning
"
));
return
SkFontMgr_RefDefault
()->
legacyMakeTypeface
(
"
"
,
SkFontStyle
());
}),
R"docstring(
Returns the default normal typeface.
)docstring"
)
.
def
(
py::init
(&SkTypeface_MakeFromName),
R"docstring(
Creates a new reference to the typeface that most closely matches the
requested familyName and fontStyle.
This method allows extended font face specifiers as in the
:py:class:`FontStyle` type. Will never return null.
:param str familyName: May be NULL. The name of the font family.
:param skia.FontStyle fontStyle: The style of the typeface.
:return: reference to the closest-matching typeface.
)docstring"
,
py::arg
(
"
familyName
"
),
py::arg
(
"
fontStyle
"
) =
nullptr
)
.
def
(
"
__repr__
"
,
[] (
const
SkTypeface& self) {
SkString name;
self.
getFamilyName
(&name);
return
py::str
(
"
Typeface('{}', {})
"
).
format
(
name.
c_str
(), self.
fontStyle
());
})
.
def
(
"
fontStyle
"
, &SkTypeface::fontStyle,
R"docstring(
Returns the typeface's intrinsic style attributes.
)docstring"
)
.
def
(
"
isBold
"
, &SkTypeface::isBold,
R"docstring(
Returns true if style() has the kBold bit set.
)docstring"
)
.
def
(
"
isItalic
"
, &SkTypeface::isItalic,
R"docstring(
Returns true if style() has the kItalic bit set.
)docstring"
)
.
def
(
"
isFixedPitch
"
, &SkTypeface::isFixedPitch,
R"docstring(
Returns true if the typeface claims to be fixed-pitch.
)docstring"
)
.
def
(
"
getVariationDesignParameters
"
,
[] (
const
SkTypeface& typeface) {
auto
count = typeface.
getVariationDesignParameters
({
nullptr
,
0
});
if
(count == -
1
)
throw
std::runtime_error
(
"
Failed to get; Likely no parameter
"
);
std::vector<Axis>
params
(count);
if
(count ==
0
)
return
params;
auto
actualCount = typeface.
getVariationDesignParameters
(
{params.
data
(), params.
size
()});
if
(actualCount == -
1
)
throw
std::runtime_error
(
"
Failed to get
"
);
return
params;
},
R"docstring(
Returns the design variation parameters.
:return: List of axes.
:rtype: List[skia.FontParameters.Variation.Axis]
)docstring"
)
.
def
(
"
getVariationDesignPosition
"
,
[] (
const
SkTypeface& typeface) {
auto
count = typeface.
getVariationDesignPosition
({
nullptr
,
0
});
if
(count == -
1
)
throw
std::runtime_error
(
"
Failed to get; Likely no position
"
);
std::vector<Coordinate>
coords
(count);
auto
actualCount = typeface.
getVariationDesignPosition
(
{coords.
data
(), coords.
size
()});
if
(actualCount == -
1
)
throw
std::runtime_error
(
"
Failed to get
"
);
return
coords;
},
R"docstring(
Returns the design variation coordinates.
:param int coordinateCount: the maximum number of entries to get.
:return: list of coordinates
:rtype: List[skia.FontArguments.VariationPosition.Coordinate]
)docstring"
)
.
def
(
"
uniqueID
"
, &SkTypeface::uniqueID,
R"docstring(
Return a 32bit value for this typeface, unique for the underlying font
data.
Will never return 0.
)docstring"
)
.
def
(
"
makeClone
"
, &SkTypeface::makeClone,
R"docstring(
Return a new typeface based on this typeface but parameterized as
specified in the :py:class:`FontArguments`.
If the :py:class:`FontArguments` does not supply an argument for a
parameter in the font then the value from this typeface will be used as
the value for that argument. If the cloned typeface would be exaclty the
same as this typeface then this typeface may be ref'ed and returned. May
return nullptr on failure.
)docstring"
,
py::arg
(
"
fontArguments
"
))
//
.def("serialize", &SkTypeface::serialize)
.
def
(
"
serialize
"
,
py::overload_cast<SkTypeface::SerializeBehavior>(
&SkTypeface::serialize, py::const_),
R"docstring(
Returns a unique signature to a stream, sufficient to reconstruct a
typeface referencing the same font when Deserialize is called.
)docstring"
,
py::arg_v
(
"
behavior
"
, SkTypeface::SerializeBehavior::
kIncludeDataIfLocal
,
"
skia.Typeface.SerializeBehavior.kIncludeDataIfLocal
"
))
.
def
(
"
unicharsToGlyphs
"
,
[] (
const
SkTypeface& typeface,
const
std::vector<SkUnichar>& chars) {
std::vector<SkGlyphID>
glyphs
(chars.
size
());
typeface.
unicharsToGlyphs
({chars.
data
(), chars.
size
()}, {glyphs.
data
(), glyphs.
size
()});
return
glyphs;
},
R"docstring(
Given an array of UTF32 character codes, return their corresponding
glyph IDs.
:param List[int] chars: the array of UTF32 chars.
:return: the corresponding glyph IDs for each character.
)docstring"
,
py::arg
(
"
chars
"
))
.
def
(
"
unicharToGlyph
"
, &SkTypeface::unicharToGlyph,
R"docstring(
Return the glyphID that corresponds to the specified unicode code-point
(in UTF32 encoding).
If the unichar is not supported, returns 0.
This is a short-cut for calling :py:meth:`unicharsToGlyphs`.
)docstring"
,
py::arg
(
"
unichar
"
))
.
def
(
"
countGlyphs
"
, &SkTypeface::countGlyphs,
R"docstring(
Return the number of glyphs in the typeface.
)docstring"
)
.
def
(
"
countTables
"
, &SkTypeface::countTables,
R"docstring(
Return the number of tables in the font.
)docstring"
)
.
def
(
"
getTableTags
"
,
[] (
const
SkTypeface& typeface) {
std::vector<SkFontTableTag>
tags
(typeface.
countTables
());
size_t
size = typeface.
readTableTags
({tags.
data
(), tags.
size
()});
if
(size < tags.
size
())
throw
std::runtime_error
(
"
Failed to get table tags.
"
);
return
tags;
},
R"docstring(
Returns the list of table tags in the font.
)docstring"
)
.
def
(
"
getTableSize
"
, &SkTypeface::getTableSize,
R"docstring(
Given a table tag, return the size of its contents, or 0 if not present.
)docstring"
,
py::arg
(
"
tag
"
))
.
def
(
"
getTableData
"
,
[] (
const
SkTypeface& typeface, SkFontTableTag tag) {
size_t
size = typeface.
getTableSize
(tag);
std::vector<
char
>
data
(size);
auto
written = typeface.
getTableData
(tag,
0
, data.
size
(), &data[
0
]);
if
(written ==
0
&& size >
0
)
throw
std::runtime_error
(
"
Failed to get table data.
"
);
return
py::bytes
(&data[
0
], data.
size
());
},
R"docstring(
Returns the contents of a table.
Note that the contents of the table will be in their native endian order
(which for most truetype tables is big endian). If the table tag is not
found, or there is an error copying the data, then 0 is returned. If
this happens, it is possible that some or all of the memory pointed to
by data may have been written to, even though an error has occured.
:param int tag: The table tag whose contents are to be copied.
:return: table contents
)docstring"
,
py::arg
(
"
tag
"
))
.
def
(
"
copyTableData
"
, &SkTypeface::copyTableData,
R"docstring(
Return an immutable copy of the requested font table, or nullptr if that
table was not found.
This can sometimes be faster than calling :py:meth:`getTableData` twice:
once to find the length, and then again to copy the data.
:param tag: The table tag whose contents are to be copied
:return: an immutable copy of the table's data, or nullptr.
)docstring"
,
py::arg
(
"
tag
"
))
.
def
(
"
getUnitsPerEm
"
, &SkTypeface::getUnitsPerEm,
R"docstring(
Return the units-per-em value for this typeface, or zero if there is an
error.
)docstring"
)
.
def
(
"
getKerningPairAdjustments
"
,
[] (
const
SkTypeface& typeface,
const
std::vector<SkGlyphID>& glyphs) -> py::object {
std::vector<
int32_t
>
adjustments
(glyphs.
size
() -
1
);
auto
result = typeface.
getKerningPairAdjustments
(
{glyphs.
data
(), glyphs.
size
()}, {(glyphs.
size
() >
1
) ? adjustments.
data
() :
nullptr
, adjustments.
size
()});
if
(!result) {
//
Kerning is not supported for this typeface.
return
py::none
();
}
return
py::cast
(adjustments);
},
R"docstring(
Given a run of glyphs, return the associated horizontal adjustments.
Adjustments are in "design units", which are integers relative to the
typeface's units per em (see getUnitsPerEm).
Some typefaces are known to never support kerning. If the typeface does
not support kerning, then this method will return None (no kerning) for
all possible glyph runs.
)docstring"
,
py::arg
(
"
glyphs
"
))
.
def
(
"
getFamilyNames
"
,
[] (
const
SkTypeface& typeface) {
SkTypeface::LocalizedString name;
py::list results;
auto
it = typeface.
createFamilyNameIterator
();
if
(!it)
throw
std::runtime_error
(
"
Null pointer exception
"
);
while
(it->
next
(&name)) {
auto
fString
=
py::str
(
name.
fString
.
c_str
(), name.
fString
.
size
());
auto
fLanguage
=
py::str
(
name.
fLanguage
.
c_str
(), name.
fLanguage
.
size
());
results.
append
(
py::make_tuple
(
fString
,
fLanguage
));
}
it->
unref
();
return
results;
},
R"docstring(
Returns a list of (family name, language) pairs specified by the font.
)docstring"
)
.
def
(
"
getFamilyName
"
,
[] (
const
SkTypeface& typeface) {
SkString name;
typeface.
getFamilyName
(&name);
return
py::str
(name.
c_str
(), name.
size
());
},
R"docstring(
Return the family name for this typeface.
It will always be returned encoded as UTF8, but the language of the name
is whatever the host platform chooses.
)docstring"
)
.
def
(
"
getPostScriptName
"
,
[] (
const
SkTypeface& typeface) {
SkString name;
typeface.
getPostScriptName
(&name);
return
py::str
(name.
c_str
(), name.
size
());
},
R"docstring(
Return the PostScript name for this typeface.
Value may change based on variation parameters.
Returns false if no PostScript name is available.
)docstring"
)
//
.def("openStream", &SkTypeface::openStream,
//
"Return a stream for the contents of the font data, or NULL on "
//
"failure.")
//
.def("makeFontData", &SkTypeface::makeFontData,
//
R"docstring(
//
Return the font data, or nullptr on failure.
//
)docstring")
//
.def("createScalerContext", &SkTypeface::createScalerContext,
//
"Return a scalercontext for the given descriptor.")
.
def
(
"
getBounds
"
, &SkTypeface::getBounds,
R"docstring(
Return a rectangle (scaled to 1-pt) that represents the union of the
bounds of all of the glyphs, but each one positioned at (0,).
This may be conservatively large, and will not take into account any
hinting or other size-specific adjustments.
)docstring"
)
//
.def("filterRec", &SkTypeface::filterRec)
//
.def("getFontDescriptor", &SkTypeface::getFontDescriptor)
/*
.def_static("UniqueID", &SkTypeface::UniqueID,
R"docstring(
Return the uniqueID for the specified typeface.
If the face is null, resolve it to the default font and return its
uniqueID. Will never return 0.
)docstring",
py::arg("typeface"))
*/
.
def_static
(
"
Equal
"
, &SkTypeface::Equal,
R"docstring(
Returns true if the two typefaces reference the same underlying font,
handling either being null (treating null as the default font)
)docstring"
,
py::arg
(
"
self
"
),
py::arg
(
"
other
"
))
.
def
(
"
__eq__
"
, &SkTypeface::Equal,
py::is_operator
())
.
def_static
(
"
MakeDefault
"
,
[] (
void
) {
auto
warnings =
pybind11::module::import
(
"
warnings
"
);
auto
builtins =
pybind11::module::import
(
"
builtins
"
);
warnings.
attr
(
"
warn
"
)(
"
\"
Default typeface
\"
is deprecated upstream. Please specify name/file/style choices.
"
,
builtins.
attr
(
"
DeprecationWarning
"
));
return
SkFontMgr_RefDefault
()->
legacyMakeTypeface
(
"
"
,
SkFontStyle
());
},
R"docstring(
Returns the default normal typeface, which is never nullptr.
)docstring"
)
.
def_static
(
"
MakeEmpty
"
, &SkTypeface::MakeEmpty,
R"docstring(
Returns a non-null typeface which contains no glyphs.
)docstring"
)
.
def_static
(
"
MakeFromName
"
, &SkTypeface_MakeFromName,
R"docstring(
Creates a new reference to the typeface that most closely matches the
requested familyName and fontStyle.
This method allows extended font face specifiers as in the
:py:class:`FontStyle` type. Will never return null.
:param str familyName: May be NULL. The name of the font family.
:param skia.FontStyle fontStyle: The style of the typeface.
:return: reference to the closest-matching typeface.
)docstring"
,
py::arg
(
"
familyName
"
),
py::arg
(
"
fontStyle
"
) =
nullptr
)
.
def_static
(
"
MakeFromFile
"
,
[] (
const
std::string& path,
int
index) {
return
SkFontMgr_RefDefault
()->
makeFromFile
(&path[
0
], index);
},
R"docstring(
Return a new typeface given a file.
If the file does not exist, or is not a valid font file, returns
nullptr.
)docstring"
,
py::arg
(
"
path
"
),
py::arg
(
"
index
"
) =
0
)
//
.def_static("MakeFromStream", &SkTypeface::MakeFromStream,
//
"Return a new typeface given a stream.")
.
def_static
(
"
MakeFromData
"
,
[] (sk_sp<SkData> data,
int
index) {
return
SkFontMgr_RefDefault
()->
makeFromData
(data, index);
},
R"docstring(
Return a new typeface given a :py:class:`Data`.
If the data is null, or is not a valid font file, returns nullptr.
Use :py:meth:`MakeDeserialize` for the result of :py:meth:`serialize`.
)docstring"
,
py::arg
(
"
data
"
),
py::arg
(
"
index
"
) =
0
)
//
.def_static("MakeFromFontData", &SkTypeface::MakeFromFontData,
//
"Return a new typeface given font data and configuration.")
.
def_static
(
"
MakeDeserialize
"
,
[] (
const
sk_sp<SkData>& data, sk_sp<SkFontMgr> lastResortMgr) {
SkMemoryStream
stream
(data);
return
SkTypeface::MakeDeserialize
(&stream, lastResortMgr);
},
R"docstring(
Given the data previously written by :py:meth:`serialize`, return a new
instance of a typeface referring to the same font.
If that font is not available, return nullptr.
)docstring"
,
py::arg
(
"
dats
"
),
py::arg
(
"
lastResortMgr
"
))
;
//
FontMgr
py::class_<SkFontStyleSet, sk_sp<SkFontStyleSet>, SkRefCnt>(m,
"
FontStyleSet
"
)
.
def
(
"
__getitem__
"
, &SkFontStyleSet_getStyle,
py::arg
(
"
index
"
))
.
def
(
"
__len__
"
, &SkFontStyleSet::count)
.
def
(
"
__repr__
"
,
[] (SkFontStyleSet& self) {
return
py::str
(
"
FontStyleSet({})
"
).
format
(self.
count
());
})
.
def
(
"
count
"
, &SkFontStyleSet::count)
.
def
(
"
getStyle
"
, &SkFontStyleSet_getStyle,
py::arg
(
"
index
"
))
.
def
(
"
createTypeface
"
, &SkFontStyleSet::createTypeface,
py::arg
(
"
index
"
))
.
def
(
"
matchStyle
"
, &SkFontStyleSet::matchStyle,
py::arg
(
"
pattern
"
))
.
def_static
(
"
CreateEmpty
"
, &SkFontStyleSet::CreateEmpty)
;
py::class_<SkFontMgr, sk_sp<SkFontMgr>, SkRefCnt>(m,
"
FontMgr
"
,
R"docstring(
Font manager that knows system fonts.
Example::
fontmgr = skia.FontMgr()
print(list(fontmgr))
familyName = fontmgr[0]
typeface = fontmgr.matchFamilyStyle(familyName, skia.FontStyle.Normal())
assert typeface is not None
)docstring"
)
.
def
(
py::init
([] () {
return
SkFontMgr_RefDefault
(); }))
.
def_static
(
"
New_Custom_Directory
"
, &SkFontMgr_New_Custom_Directory,
R"docstring(
Create a custom font manager which scans a given directory for font files.
This font manager uses FreeType for rendering.
)docstring"
,
py::arg
(
"
dir
"
))
.
def_static
(
"
New_Custom_Empty
"
, &SkFontMgr_New_Custom_Empty)
.
def_static
(
"
New_Custom_Empty
"
, py::overload_cast<
char
*>(&OneFontMgr_New_Custom_Empty),
py::arg
(
"
filename
"
))
.
def_static
(
"
New_Custom_Empty
"
, py::overload_cast<sk_sp<SkData>>(&OneFontMgr_New_Custom_Empty),
py::arg
(
"
data
"
))
.
def
(
"
__getitem__
"
, &SkFontMgr_getFamilyName,
py::arg
(
"
index
"
))
.
def
(
"
__len__
"
, &SkFontMgr::countFamilies)
.
def
(
"
countFamilies
"
, &SkFontMgr::countFamilies)
.
def
(
"
getFamilyName
"
, &SkFontMgr_getFamilyName,
py::arg
(
"
index
"
))
.
def
(
"
createStyleSet
"
,
[] (
const
SkFontMgr& fontmgr,
int
index) {
return
sk_sp<SkFontStyleSet>(fontmgr.
createStyleSet
(index));
},
py::arg
(
"
index
"
))
.
def
(
"
matchFamily
"
,
[] (
const
SkFontMgr& fontmgr,
const
std::string* familyName) {
return
sk_sp<SkFontStyleSet>(
fontmgr.
matchFamily
(
(familyName) ? familyName->
c_str
() :
nullptr
));
},
R"docstring(
Never returns NULL; will return an empty set if the name is not found.
Passing nullptr as the parameter will return the default system family.
Note that most systems don't have a default system family, so passing
nullptr will often result in the empty set.
It is possible that this will return a style set not accessible from
createStyleSet(int) due to hidden or auto-activated fonts.
)docstring"
,
py::arg
(
"
familyName
"
))
.
def
(
"
matchFamilyStyle
"
,
[] (
const
SkFontMgr& fontmgr,
const
py::object& familyName,
const
SkFontStyle& style) {
return
sk_sp<SkTypeface>(
fontmgr.
matchFamilyStyle
(
(familyName.
is_none
()) ?
nullptr
:
familyName.
cast
<std::string>().
c_str
(), style));
},
R"docstring(
Find the closest matching typeface to the specified familyName and style
and return a ref to it.
Will return nullptr if no 'good' match is found.
Passing nullptr as the parameter for familyName will return the
default system font.
It is possible that this will return a style set not accessible from
createStyleSet(int) or matchFamily(const char[]) due to hidden or
auto-activated fonts.
)docstring"
,
py::arg
(
"
familyName
"
),
py::arg
(
"
style
"
))
.
def
(
"
matchFamilyStyleCharacter
"
,
[] (
const
SkFontMgr& fontmgr,
const
std::string& familyName,
const
SkFontStyle& style,
const
std::vector<std::string>& bcp47,
SkUnichar character) {
std::vector<
const
char
*>
bcp47_
(bcp47.
size
());
std::transform
(bcp47.
begin
(), bcp47.
end
(), bcp47_.
begin
(),
[] (
const
std::string& c) {
return
c.
c_str
(); });
return
sk_sp<SkTypeface>(
fontmgr.
matchFamilyStyleCharacter
(
familyName.
c_str
(), style, &bcp47_[
0
], bcp47_.
size
(),
character));
},
R"docstring(
Use the system fallback to find a typeface for the given character.
Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes, so
it is fine to just pass a ISO 639 here.
Will return NULL if no family can be found for the character in the
system fallback.
Passing nullptr as the parameter for familyName will return the
default system font.
bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
most significant. If no specified bcp47 codes match, any font with the
requested character will be matched.
)docstring"
,
py::arg
(
"
familyName
"
),
py::arg
(
"
style
"
),
py::arg
(
"
bcp47
"
),
py::arg
(
"
character
"
))
.
def
(
"
makeFromData
"
, &SkFontMgr::makeFromData,
R"docstring(
Create a typeface for the specified data and TTC index (pass 0 for none)
or NULL if the data is not recognized.
)docstring"
,
py::arg
(
"
data
"
),
py::arg
(
"
ttcIndex
"
) =
0
)
//
.def("makeFromStream", &SkFontMgr::makeFromStream)
//
.def("makeFromStream", &SkFontMgr::makeFromStream)
//
.def("makeFromFontData", &SkFontMgr::makeFromFontData)
.
def
(
"
makeFromFile
"
,
[] (
const
SkFontMgr& fontmgr,
const
std::string& path,
int
ttcIndex) {
return
fontmgr.
makeFromFile
(path.
c_str
(), ttcIndex);
},
R"docstring(
Create a typeface for the specified fileName and TTC index (pass 0 for
none) or NULL if the file is not found, or its contents are not
recognized.
)docstring"
,
py::arg
(
"
path
"
),
py::arg
(
"
ttcIndex
"
) =
0
)
.
def
(
"
legacyMakeTypeface
"
,
[] (
const
SkFontMgr& fontmgr,
const
std::string& familyName,
const
SkFontStyle& style) {
return
fontmgr.
legacyMakeTypeface
(familyName.
c_str
(), style);
},
py::arg
(
"
familyName
"
),
py::arg
(
"
style
"
))
.
def_static
(
"
RefDefault
"
, &SkFontMgr_RefDefault,
R"docstring(
Return the default fontmgr.
)docstring"
)
;
//
This has the side-effect of letting "skia.FontMgr.OneFontMgr()" work.
m.
attr
(
"
FontMgr
"
).
attr
(
"
OneFontMgr
"
) = m.
attr
(
"
FontMgr
"
).
attr
(
"
New_Custom_Empty
"
);
//
Font
py::enum_<SkFontHinting>(m,
"
FontHinting
"
)
.
value
(
"
kNone
"
, SkFontHinting::
kNone
)
.
value
(
"
kSlight
"
, SkFontHinting::
kSlight
)
.
value
(
"
kNormal
"
, SkFontHinting::
kNormal
)
.
value
(
"
kFull
"
, SkFontHinting::
kFull
)
.
export_values
();
py::enum_<SkTextEncoding>(m,
"
TextEncoding
"
)
.
value
(
"
kUTF8
"
, SkTextEncoding::
kUTF8
)
.
value
(
"
kUTF16
"
, SkTextEncoding::
kUTF16
)
.
value
(
"
kUTF32
"
, SkTextEncoding::
kUTF32
)
.
value
(
"
kGlyphID
"
, SkTextEncoding::
kGlyphID
)
.
export_values
();
py::class_<SkFont>
font
(m,
"
Font
"
,
R"docstring(
:py:class:`Font` controls options applied when drawing and measuring text.
)docstring"
);
py::enum_<SkFont::Edging>(font,
"
Edging
"
,
R"docstring(
Whether edge pixels draw opaque or with partial transparency.
)docstring"
)
.
value
(
"
kAlias
"
, SkFont::Edging::
kAlias
)
.
value
(
"
kAntiAlias
"
, SkFont::Edging::
kAntiAlias
)
.
value
(
"
kSubpixelAntiAlias
"
, SkFont::Edging::
kSubpixelAntiAlias
)
.
export_values
();
font
/*
.def("__repr__",
[] (const SkFont& self) {
return py::str("Font({}, {}, {}, {})").format(
self.getTypefaceOrDefault(), self.getSize(), self.getScaleX(),
self.getSkewX());
})
*/
.
def
(
py::init
(
[] (
void
) {
auto
warnings =
pybind11::module::import
(
"
warnings
"
);
auto
builtins =
pybind11::module::import
(
"
builtins
"
);
warnings.
attr
(
"
warn
"
)(
"
\"
Default font
\"
is deprecated upstream. Please specify name/file/style choices.
"
,
builtins.
attr
(
"
DeprecationWarning
"
));
return
SkFont
(
SkFontMgr_RefDefault
()->
legacyMakeTypeface
(
"
"
,
SkFontStyle
()));
}),
R"docstring(
Constructs :py:class:`Font` with default values.
)docstring"
)
.
def
(
py::init
(
[] (py::object typeface, SkScalar size) {
if
(typeface.
is_none
()) {
auto
warnings =
pybind11::module::import
(
"
warnings
"
);
auto
builtins =
pybind11::module::import
(
"
builtins
"
);
warnings.
attr
(
"
warn
"
)(
"
\"
Default font
\"
is deprecated upstream. Please specify name/file/style choices.
"
,
builtins.
attr
(
"
DeprecationWarning
"
));
return
SkFont
(
SkFontMgr_RefDefault
()->
legacyMakeTypeface
(
"
"
,
SkFontStyle
()), size);
}
else
{
return
SkFont
(typeface.
cast
<sk_sp<SkTypeface>>(), size);
}
}),
R"docstring(
Constructs :py:class:`Font` with default values with
:py:class:`Typeface` and size in points.
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL