Skip to content

Commit cd2142a

Browse files
committed
Modify surface through reference
1 parent fc29c93 commit cd2142a

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

src_c/font.c

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -538,19 +538,17 @@ font_set_strikethrough(PyObject *self, PyObject *arg)
538538
Py_RETURN_NONE;
539539
}
540540

541-
static SDL_Surface *
541+
static int
542542
_create_font_surface(TTF_Font *font, PyObject *text, int antialias,
543543
PyObject *fg_rgba_obj, PyObject *bg_rgba_obj,
544-
int wraplength)
544+
int wraplength, SDL_Surface **dst_surf)
545545
{
546-
SDL_Surface *surf;
547546
Uint8 rgba[] = {0, 0, 0, 0};
548547
const char *astring = "";
549548

550549
// 글꼴 생성
551550
if (!pg_RGBAFromObjEx(fg_rgba_obj, rgba, PG_COLOR_HANDLE_ALL)) {
552-
/* Exception already set for us */
553-
return (SDL_Surface *)(NULL);
551+
return 0; // exception already set
554552
}
555553

556554
SDL_Color foreg = {rgba[0], rgba[1], rgba[2], SDL_ALPHA_OPAQUE};
@@ -559,38 +557,40 @@ _create_font_surface(TTF_Font *font, PyObject *text, int antialias,
559557

560558
if (bg_rgba_obj != Py_None) {
561559
if (!pg_RGBAFromObjEx(bg_rgba_obj, rgba, PG_COLOR_HANDLE_ALL)) {
562-
/* Exception already set for us */
563-
return (SDL_Surface *)(NULL);
560+
return 0; // exception already set.
564561
}
565562
backg = (SDL_Color){rgba[0], rgba[1], rgba[2], SDL_ALPHA_OPAQUE};
566563
}
567564

568565
if (!PyUnicode_Check(text) && !PyBytes_Check(text) && text != Py_None) {
569-
return (SDL_Surface *)(RAISE_TEXT_TYPE_ERROR());
566+
PyErr_Format(PyExc_TypeError, "text must be a unicode or bytes");
567+
return 0;
570568
}
571569

572570
if (wraplength < 0) {
573-
return (SDL_Surface *)(RAISE(PyExc_ValueError,
574-
"wraplength parameter must be positive"));
571+
PyErr_Format(PyExc_ValueError,
572+
"The wraplength parameter must be positive.");
573+
return 0;
575574
}
576575

577576
if (PyUnicode_Check(text)) {
578577
Py_ssize_t _size = -1;
579578
astring = PyUnicode_AsUTF8AndSize(text, &_size);
580-
if (astring == NULL) { /* exception already set */
581-
return (SDL_Surface *)(NULL);
579+
if (astring == NULL) {
580+
return 0; // exception already set.
582581
}
583582
if (strlen(astring) != (size_t)_size) {
584-
return (SDL_Surface *)(RAISE(
585-
PyExc_ValueError, "A null character was found in the text"));
583+
PyErr_Format(PyExc_ValueError,
584+
"A null character was found in the text.");
585+
return 0;
586586
}
587587
}
588588

589589
else if (PyBytes_Check(text)) {
590590
/* Bytes_AsStringAndSize with NULL arg for length emits
591591
ValueError if internal NULL bytes are present */
592592
if (PyBytes_AsStringAndSize(text, (char **)&astring, NULL) == -1) {
593-
return (SDL_Surface *)(NULL); /* exception already set */
593+
return 0; // exception already set.
594594
}
595595
}
596596

@@ -599,48 +599,49 @@ _create_font_surface(TTF_Font *font, PyObject *text, int antialias,
599599

600600
if (strlen(astring) == 0) { /* special 0 string case */
601601
int height = TTF_FontHeight(font);
602-
surf = PG_CreateSurface(0, height, PG_PIXELFORMAT_XRGB8888);
602+
*dst_surf = PG_CreateSurface(0, height, PG_PIXELFORMAT_XRGB8888);
603603
}
604604
else { /* normal case */
605605
if (antialias && bg_rgba_obj == Py_None) {
606606
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
607-
surf = TTF_RenderUTF8_Blended_Wrapped(font, astring, foreg,
608-
wraplength);
607+
*dst_surf = TTF_RenderUTF8_Blended_Wrapped(font, astring, foreg,
608+
wraplength);
609609
#else
610-
surf = TTF_RenderUTF8_Blended(font, astring, foreg);
610+
*dst_surf = TTF_RenderUTF8_Blended(font, astring, foreg);
611611
#endif
612612
}
613613
else if (antialias) {
614614
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
615-
surf = TTF_RenderUTF8_Shaded_Wrapped(font, astring, foreg, backg,
616-
wraplength);
615+
*dst_surf = TTF_RenderUTF8_Shaded_Wrapped(font, astring, foreg,
616+
backg, wraplength);
617617
#else
618-
surf = TTF_RenderUTF8_Shaded(font, astring, foreg, backg);
618+
*dst_surf = TTF_RenderUTF8_Shaded(font, astring, foreg, backg);
619619
#endif
620620
}
621621
else {
622622
#if SDL_TTF_VERSION_ATLEAST(2, 0, 18)
623-
surf =
623+
*dst_surf =
624624
TTF_RenderUTF8_Solid_Wrapped(font, astring, foreg, wraplength);
625625
#else
626-
surf = TTF_RenderUTF8_Solid(font, astring, foreg);
626+
*dst_surf = TTF_RenderUTF8_Solid(font, astring, foreg);
627627
#endif
628628
/* If an explicit background was provided and the rendering options
629629
resolve to Render_Solid, that needs to be explicitly handled. */
630-
if (surf != NULL && bg_rgba_obj != Py_None) {
631-
SDL_SetColorKey(surf, 0, 0);
632-
surf->format->palette->colors[0].r = backg.r;
633-
surf->format->palette->colors[0].g = backg.g;
634-
surf->format->palette->colors[0].b = backg.b;
630+
if (*dst_surf != NULL && bg_rgba_obj != Py_None) {
631+
SDL_SetColorKey(*dst_surf, 0, 0);
632+
(*dst_surf)->format->palette->colors[0].r = backg.r;
633+
(*dst_surf)->format->palette->colors[0].g = backg.g;
634+
(*dst_surf)->format->palette->colors[0].b = backg.b;
635635
}
636636
}
637637
}
638638

639-
if (surf == NULL) {
640-
return (SDL_Surface *)(RAISE(pgExc_SDLError, TTF_GetError()));
639+
if (*dst_surf == NULL) {
640+
PyErr_Format(pgExc_SDLError, TTF_GetError());
641+
return 0;
641642
}
642643

643-
return surf;
644+
return 1;
644645
}
645646

646647
static PyObject *
@@ -653,7 +654,7 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
653654
int antialias;
654655
PyObject *text, *final;
655656
PyObject *fg_rgba_obj, *bg_rgba_obj = Py_None;
656-
SDL_Surface *surf;
657+
SDL_Surface *surf = NULL;
657658
int wraplength = 0;
658659
TTF_Font *font = PyFont_AsFont(self);
659660

@@ -670,8 +671,10 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
670671
return NULL;
671672
}
672673

673-
surf = _create_font_surface(font, text, antialias, fg_rgba_obj,
674-
bg_rgba_obj, wraplength);
674+
if (!_create_font_surface(font, text, antialias, fg_rgba_obj, bg_rgba_obj,
675+
wraplength, &surf)) {
676+
return NULL;
677+
}
675678

676679
final = (PyObject *)pgSurface_New(surf);
677680
if (final == NULL) {

0 commit comments

Comments
 (0)