@@ -540,28 +540,11 @@ font_set_strikethrough(PyObject *self, PyObject *arg)
540
540
541
541
static int
542
542
_create_font_surface (TTF_Font * font , PyObject * text , int antialias ,
543
- PyObject * fg_rgba_obj , PyObject * bg_rgba_obj ,
543
+ SDL_Color foreg , SDL_Color backg , int draw_backg ,
544
544
int wraplength , SDL_Surface * * dst_surf )
545
545
{
546
- Uint8 rgba [] = {0 , 0 , 0 , 0 };
547
546
const char * astring = "" ;
548
547
549
- // 글꼴 생성
550
- if (!pg_RGBAFromObjEx (fg_rgba_obj , rgba , PG_COLOR_HANDLE_ALL )) {
551
- return 0 ; // exception already set
552
- }
553
-
554
- SDL_Color foreg = {rgba [0 ], rgba [1 ], rgba [2 ], SDL_ALPHA_OPAQUE };
555
- /* might be overridden right below, with an explicit background color */
556
- SDL_Color backg = {0 , 0 , 0 , SDL_ALPHA_OPAQUE };
557
-
558
- if (bg_rgba_obj != Py_None ) {
559
- if (!pg_RGBAFromObjEx (bg_rgba_obj , rgba , PG_COLOR_HANDLE_ALL )) {
560
- return 0 ; // exception already set.
561
- }
562
- backg = (SDL_Color ){rgba [0 ], rgba [1 ], rgba [2 ], SDL_ALPHA_OPAQUE };
563
- }
564
-
565
548
if (!PyUnicode_Check (text ) && !PyBytes_Check (text ) && text != Py_None ) {
566
549
PyErr_Format (PyExc_TypeError , "text must be a unicode or bytes" );
567
550
return 0 ;
@@ -602,7 +585,7 @@ _create_font_surface(TTF_Font *font, PyObject *text, int antialias,
602
585
* dst_surf = PG_CreateSurface (0 , height , PG_PIXELFORMAT_XRGB8888 );
603
586
}
604
587
else { /* normal case */
605
- if (antialias && bg_rgba_obj == Py_None ) {
588
+ if (antialias && ! draw_backg ) {
606
589
#if SDL_TTF_VERSION_ATLEAST (2 , 0 , 18 )
607
590
* dst_surf = TTF_RenderUTF8_Blended_Wrapped (font , astring , foreg ,
608
591
wraplength );
@@ -627,7 +610,7 @@ _create_font_surface(TTF_Font *font, PyObject *text, int antialias,
627
610
#endif
628
611
/* If an explicit background was provided and the rendering options
629
612
resolve to Render_Solid, that needs to be explicitly handled. */
630
- if (* dst_surf != NULL && bg_rgba_obj != Py_None ) {
613
+ if (* dst_surf != NULL && draw_backg ) {
631
614
SDL_SetColorKey (* dst_surf , 0 , 0 );
632
615
(* dst_surf )-> format -> palette -> colors [0 ].r = backg .r ;
633
616
(* dst_surf )-> format -> palette -> colors [0 ].g = backg .g ;
@@ -655,8 +638,12 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
655
638
PyObject * text , * final ;
656
639
PyObject * fg_rgba_obj , * bg_rgba_obj = Py_None ;
657
640
SDL_Surface * surf = NULL ;
641
+ SDL_Surface * outline_surf = NULL ;
658
642
int wraplength = 0 ;
659
643
TTF_Font * font = PyFont_AsFont (self );
644
+ int outline_size = ((PyFontObject * )self )-> outline_size ;
645
+ SDL_Color outline_color = ((PyFontObject * )self )-> outline_color ;
646
+ Uint8 rgba [] = {0 , 0 , 0 , 0 };
660
647
661
648
if (!PgFont_GenerationCheck (self )) {
662
649
return RAISE_FONT_QUIT_ERROR ()
@@ -671,14 +658,74 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
671
658
return NULL ;
672
659
}
673
660
674
- if (!_create_font_surface (font , text , antialias , fg_rgba_obj , bg_rgba_obj ,
661
+ // 글꼴 생성
662
+ if (!pg_RGBAFromObjEx (fg_rgba_obj , rgba , PG_COLOR_HANDLE_ALL )) {
663
+ return 0 ; // exception already set
664
+ }
665
+
666
+ SDL_Color foreg = {rgba [0 ], rgba [1 ], rgba [2 ], SDL_ALPHA_OPAQUE };
667
+ SDL_Color backg = {0 , 0 , 0 , SDL_ALPHA_OPAQUE };
668
+ int draw_backg = 1 ;
669
+ /* might be overridden right below, with an explicit background color */
670
+
671
+ if (bg_rgba_obj != Py_None ) {
672
+ if (!pg_RGBAFromObjEx (bg_rgba_obj , rgba , PG_COLOR_HANDLE_ALL )) {
673
+ return 0 ; // exception already set.
674
+ }
675
+ backg = (SDL_Color ){rgba [0 ], rgba [1 ], rgba [2 ], SDL_ALPHA_OPAQUE };
676
+ }
677
+ else {
678
+ draw_backg = 0 ;
679
+ }
680
+
681
+ if (!_create_font_surface (font , text , antialias , foreg , backg , draw_backg ,
675
682
wraplength , & surf )) {
676
683
return NULL ;
677
684
}
678
685
679
- final = (PyObject * )pgSurface_New (surf );
686
+ SDL_Surface * filled_with_outline_surf = NULL ;
687
+
688
+ if (outline_size > 0 ) {
689
+ TTF_SetFontOutline (font , outline_size );
690
+
691
+ if (!_create_font_surface (font , text , antialias , outline_color , backg ,
692
+ draw_backg , wraplength , & outline_surf )) {
693
+ return NULL ;
694
+ }
695
+
696
+ TTF_SetFontOutline (font , 0 );
697
+
698
+ filled_with_outline_surf = PG_CreateSurface (
699
+ outline_surf -> w , outline_surf -> h , SDL_PIXELFORMAT_RGBA32 );
700
+
701
+ // `surf` and `outline_surf` are **NOT** the same size.
702
+ SDL_Rect outlinerect ;
703
+ outlinerect .x = filled_with_outline_surf -> w / 2 - outline_surf -> w / 2 ;
704
+ outlinerect .y = filled_with_outline_surf -> h / 2 - outline_surf -> h / 2 ;
705
+ outlinerect .w = outline_surf -> w ;
706
+ outlinerect .h = outline_surf -> h ;
707
+ SDL_Rect fillrect ;
708
+ fillrect .x = filled_with_outline_surf -> w / 2 - surf -> w / 2 ;
709
+ fillrect .y = filled_with_outline_surf -> h / 2 - surf -> h / 2 ;
710
+ fillrect .w = surf -> w ;
711
+ fillrect .h = surf -> h ;
712
+ SDL_BlitSurface (surf , NULL , filled_with_outline_surf , & fillrect );
713
+ SDL_BlitSurface (outline_surf , NULL , filled_with_outline_surf ,
714
+ & outlinerect );
715
+
716
+ final = (PyObject * )pgSurface_New (filled_with_outline_surf );
717
+ }
718
+ else {
719
+ final = (PyObject * )pgSurface_New (surf );
720
+ }
721
+
680
722
if (final == NULL ) {
681
723
SDL_FreeSurface (surf );
724
+ if (outline_surf != NULL )
725
+ SDL_FreeSurface (outline_surf );
726
+
727
+ if (filled_with_outline_surf != NULL )
728
+ SDL_FreeSurface (filled_with_outline_surf );
682
729
}
683
730
return final ;
684
731
}
@@ -1309,6 +1356,9 @@ font_init(PyFontObject *self, PyObject *args, PyObject *kwds)
1309
1356
Py_DECREF (obj );
1310
1357
self -> font = font ;
1311
1358
self -> ptsize = fontsize ;
1359
+ self -> outline_size = 0 ;
1360
+ SDL_Color init_color = {0 , 0 , 0 , SDL_ALPHA_OPAQUE };
1361
+ self -> outline_color = init_color ;
1312
1362
self -> ttf_init_generation = current_ttf_generation ;
1313
1363
1314
1364
return 0 ;
0 commit comments