@@ -135,9 +135,14 @@ static PyObject *
135
135
fontmodule_init (PyObject * self , PyObject * _null )
136
136
{
137
137
if (!font_initialized ) {
138
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
139
+ if (!TTF_Init ())
140
+ #else
138
141
if (TTF_Init ())
142
+ #endif
143
+ {
139
144
return RAISE (pgExc_SDLError , SDL_GetError ());
140
-
145
+ }
141
146
font_initialized = 1 ;
142
147
}
143
148
Py_RETURN_NONE ;
@@ -169,7 +174,11 @@ font_get_height(PyObject *self, PyObject *_null)
169
174
}
170
175
171
176
TTF_Font * font = PyFont_AsFont (self );
177
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
178
+ return PyLong_FromLong (TTF_GetFontHeight (font ));
179
+ #else
172
180
return PyLong_FromLong (TTF_FontHeight (font ));
181
+ #endif
173
182
}
174
183
175
184
static PyObject *
@@ -180,7 +189,11 @@ font_get_descent(PyObject *self, PyObject *_null)
180
189
}
181
190
182
191
TTF_Font * font = PyFont_AsFont (self );
192
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
193
+ return PyLong_FromLong (TTF_GetFontDescent (font ));
194
+ #else
183
195
return PyLong_FromLong (TTF_FontDescent (font ));
196
+ #endif
184
197
}
185
198
186
199
static PyObject *
@@ -191,7 +204,11 @@ font_get_ascent(PyObject *self, PyObject *_null)
191
204
}
192
205
193
206
TTF_Font * font = PyFont_AsFont (self );
207
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
208
+ return PyLong_FromLong (TTF_GetFontAscent (font ));
209
+ #else
194
210
return PyLong_FromLong (TTF_FontAscent (font ));
211
+ #endif
195
212
}
196
213
197
214
static PyObject *
@@ -202,7 +219,11 @@ font_get_linesize(PyObject *self, PyObject *_null)
202
219
}
203
220
204
221
TTF_Font * font = PyFont_AsFont (self );
222
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
223
+ return PyLong_FromLong (TTF_GetFontLineSkip (font ));
224
+ #else
205
225
return PyLong_FromLong (TTF_FontLineSkip (font ));
226
+ #endif
206
227
}
207
228
208
229
static PyObject *
@@ -453,7 +474,10 @@ font_getter_align(PyObject *self, void *closure)
453
474
return RAISE_FONT_QUIT_ERROR ();
454
475
}
455
476
456
- #if SDL_TTF_VERSION_ATLEAST (2 , 20 , 0 )
477
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
478
+ TTF_Font * font = PyFont_AsFont (self );
479
+ return PyLong_FromLong (TTF_GetFontWrapAlignment (font ));
480
+ #elif SDL_TTF_VERSION_ATLEAST (2 , 20 , 0 )
457
481
TTF_Font * font = PyFont_AsFont (self );
458
482
return PyLong_FromLong (TTF_GetFontWrappedAlign (font ));
459
483
#else
@@ -492,7 +516,11 @@ font_setter_align(PyObject *self, PyObject *value, void *closure)
492
516
return -1 ;
493
517
}
494
518
519
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
520
+ TTF_SetFontWrapAlignment (font , val );
521
+ #else
495
522
TTF_SetFontWrappedAlign (font , val );
523
+ #endif
496
524
return 0 ;
497
525
#else
498
526
PyErr_SetString (pgExc_SDLError ,
@@ -611,28 +639,41 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
611
639
length string */
612
640
613
641
if (strlen (astring ) == 0 ) { /* special 0 string case */
642
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
643
+ int height = TTF_GetFontHeight (font );
644
+ #else
614
645
int height = TTF_FontHeight (font );
646
+ #endif
615
647
surf = PG_CreateSurface (0 , height , SDL_PIXELFORMAT_XRGB8888 );
616
648
}
617
649
else { /* normal case */
618
650
if (antialias && bg_rgba_obj == Py_None ) {
619
- #if SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
651
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
652
+ surf = TTF_RenderText_Blended_Wrapped (font , astring , 0 , foreg ,
653
+ wraplength );
654
+ #elif SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
620
655
surf = TTF_RenderUTF8_Blended_Wrapped (font , astring , foreg ,
621
656
wraplength );
622
657
#else
623
658
surf = TTF_RenderUTF8_Blended (font , astring , foreg );
624
659
#endif
625
660
}
626
661
else if (antialias ) {
627
- #if SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
662
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
663
+ surf = TTF_RenderText_Shaded_Wrapped (font , astring , 0 , foreg ,
664
+ backg , wraplength );
665
+ #elif SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
628
666
surf = TTF_RenderUTF8_Shaded_Wrapped (font , astring , foreg , backg ,
629
667
wraplength );
630
668
#else
631
669
surf = TTF_RenderUTF8_Shaded (font , astring , foreg , backg );
632
670
#endif
633
671
}
634
672
else {
635
- #if SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
673
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
674
+ surf = TTF_RenderText_Solid_Wrapped (font , astring , 0 , foreg ,
675
+ wraplength );
676
+ #elif SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
636
677
surf =
637
678
TTF_RenderUTF8_Solid_Wrapped (font , astring , foreg , wraplength );
638
679
#else
@@ -642,9 +683,16 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
642
683
resolve to Render_Solid, that needs to be explicitly handled. */
643
684
if (surf != NULL && bg_rgba_obj != Py_None ) {
644
685
SDL_SetColorKey (surf , 0 , 0 );
645
- surf -> format -> palette -> colors [0 ].r = backg .r ;
646
- surf -> format -> palette -> colors [0 ].g = backg .g ;
647
- surf -> format -> palette -> colors [0 ].b = backg .b ;
686
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
687
+ SDL_Palette * palette = SDL_GetSurfacePalette (surf );
688
+ #else
689
+ SDL_Palette * palette = surf -> format -> palette ;
690
+ #endif
691
+ if (palette ) {
692
+ palette -> colors [0 ].r = backg .r ;
693
+ palette -> colors [0 ].g = backg .g ;
694
+ palette -> colors [0 ].b = backg .b ;
695
+ }
648
696
}
649
697
}
650
698
}
@@ -683,15 +731,24 @@ font_size(PyObject *self, PyObject *text)
683
731
return NULL ;
684
732
}
685
733
string = PyBytes_AS_STRING (bytes );
734
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
735
+ ecode = TTF_GetStringSize (font , string , 0 , & w , & h ) ? 0 : -1 ;
736
+ #else
686
737
ecode = TTF_SizeUTF8 (font , string , & w , & h );
738
+ #endif
687
739
Py_DECREF (bytes );
688
740
if (ecode ) {
689
741
return RAISE (pgExc_SDLError , TTF_GetError ());
690
742
}
691
743
}
692
744
else if (PyBytes_Check (text )) {
693
745
string = PyBytes_AS_STRING (text );
694
- if (TTF_SizeText (font , string , & w , & h )) {
746
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
747
+ if (!TTF_GetStringSize (font , string , 0 , & w , & h ))
748
+ #else
749
+ if (TTF_SizeText (font , string , & w , & h ))
750
+ #endif
751
+ {
695
752
return RAISE (pgExc_SDLError , TTF_GetError ());
696
753
}
697
754
}
@@ -737,7 +794,13 @@ font_setter_point_size(PyFontObject *self, PyObject *value, void *closure)
737
794
return -1 ;
738
795
}
739
796
740
- if (TTF_SetFontSize (font , val ) == -1 ) {
797
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
798
+ /* TODO: can consider supporting float in python API */
799
+ if (!TTF_SetFontSize (font , (float )val ))
800
+ #else
801
+ if (TTF_SetFontSize (font , val ) == -1 )
802
+ #endif
803
+ {
741
804
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
742
805
return -1 ;
743
806
}
@@ -786,7 +849,13 @@ font_set_ptsize(PyObject *self, PyObject *arg)
786
849
"point_size cannot be equal to, or less than 0" );
787
850
}
788
851
789
- if (TTF_SetFontSize (font , val ) == -1 ) {
852
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
853
+ /* TODO: can consider supporting float in python API */
854
+ if (!TTF_SetFontSize (font , (float )val ))
855
+ #else
856
+ if (TTF_SetFontSize (font , val ) == -1 )
857
+ #endif
858
+ {
790
859
return RAISE (pgExc_SDLError , SDL_GetError ());
791
860
}
792
861
((PyFontObject * )self )-> ptsize = val ;
@@ -806,7 +875,11 @@ font_getter_name(PyObject *self, void *closure)
806
875
}
807
876
808
877
TTF_Font * font = PyFont_AsFont (self );
878
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
879
+ const char * font_name = TTF_GetFontFamilyName (font );
880
+ #else
809
881
const char * font_name = TTF_FontFaceFamilyName (font );
882
+ #endif
810
883
811
884
return PyUnicode_FromString (font_name ? font_name : "" );
812
885
}
@@ -819,7 +892,11 @@ font_getter_style_name(PyObject *self, void *closure)
819
892
}
820
893
821
894
TTF_Font * font = PyFont_AsFont (self );
895
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
896
+ const char * font_style_name = TTF_GetFontStyleName (font );
897
+ #else
822
898
const char * font_style_name = TTF_FontFaceStyleName (font );
899
+ #endif
823
900
return PyUnicode_FromString (font_style_name ? font_style_name : "" );
824
901
}
825
902
@@ -882,9 +959,16 @@ font_metrics(PyObject *self, PyObject *textobj)
882
959
* TTF_GlyphMetrics() seems to return a value for any character,
883
960
* using the default invalid character, if the char is not found.
884
961
*/
962
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
963
+ if (!surrogate && /* conditional and */
964
+ TTF_GetGlyphMetrics (font , (Uint16 )ch , & minx , & maxx , & miny , & maxy ,
965
+ & advance ))
966
+ #else
885
967
if (!surrogate && /* conditional and */
886
968
!TTF_GlyphMetrics (font , (Uint16 )ch , & minx , & maxx , & miny , & maxy ,
887
- & advance )) {
969
+ & advance ))
970
+ #endif
971
+ {
888
972
listitem =
889
973
Py_BuildValue ("(iiiii)" , minx , maxx , miny , maxy , advance );
890
974
if (!listitem ) {
@@ -912,6 +996,14 @@ font_metrics(PyObject *self, PyObject *textobj)
912
996
return list ;
913
997
}
914
998
999
+ /* This is taken from the harfbuzz header file. It converts script name in the
1000
+ * format expected by sdl2 (a 4 char string) to the format expected by sdl3
1001
+ * (a single uint32 tag) */
1002
+ #define HB_TAG (c1 , c2 , c3 , c4 ) \
1003
+ ((Uint32)((((uint32_t)(c1) & 0xFF) << 24) | \
1004
+ (((uint32_t)(c2) & 0xFF) << 16) | \
1005
+ (((uint32_t)(c3) & 0xFF) << 8) | ((uint32_t)(c4) & 0xFF)))
1006
+
915
1007
static PyObject *
916
1008
font_set_script (PyObject * self , PyObject * arg )
917
1009
{
@@ -935,7 +1027,13 @@ font_set_script(PyObject *self, PyObject *arg)
935
1027
"script code must be exactly 4 characters" );
936
1028
}
937
1029
938
- if (TTF_SetFontScriptName (font , script_code ) < 0 ) {
1030
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
1031
+ if (!TTF_SetFontScript (font , HB_TAG (script_code [0 ], script_code [1 ],
1032
+ script_code [2 ], script_code [3 ])))
1033
+ #else
1034
+ if (TTF_SetFontScriptName (font , script_code ) < 0 )
1035
+ #endif
1036
+ {
939
1037
return RAISE (pgExc_SDLError , SDL_GetError ());
940
1038
}
941
1039
#else
@@ -1013,8 +1111,12 @@ font_set_direction(PyObject *self, PyObject *arg, PyObject *kwarg)
1013
1111
" to https://github.com/pygame-community/pygame-ce" );
1014
1112
}
1015
1113
}
1016
-
1017
- if (TTF_SetFontDirection (font , dir )) {
1114
+ #if SDL_TTF_VERSION_ATLEAST (3 , 0 , 0 )
1115
+ if (!TTF_SetFontDirection (font , dir ))
1116
+ #else
1117
+ if (TTF_SetFontDirection (font , dir ))
1118
+ #endif
1119
+ {
1018
1120
return RAISE (pgExc_SDLError , SDL_GetError ());
1019
1121
}
1020
1122
@@ -1183,7 +1285,8 @@ font_init(PyFontObject *self, PyObject *args, PyObject *kwds)
1183
1285
1184
1286
Py_BEGIN_ALLOW_THREADS ;
1185
1287
#if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1186
- font = TTF_OpenFontIO (rw , 1 , fontsize );
1288
+ /* TODO: can consider supporting float in python API */
1289
+ font = TTF_OpenFontIO (rw , 1 , (float )fontsize );
1187
1290
#else
1188
1291
font = TTF_OpenFontRW (rw , 1 , fontsize );
1189
1292
#endif
0 commit comments