Skip to content

Commit fc29c93

Browse files
committed
Move font rendering implementation into a separate method
1 parent 0a617b9 commit fc29c93

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

src_c/font.c

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
#include "structmember.h"
3939

4040
#define RAISE_TEXT_TYPE_ERROR() \
41-
RAISE(PyExc_TypeError, "text must be a unicode or bytes");
41+
RAISE(PyExc_TypeError, "text must be a unicode or bytes")
4242

4343
#define RAISE_FONT_QUIT_ERROR_RETURN(r) \
4444
RAISERETURN(pgExc_SDLError, \
45-
"Invalid font (font module quit since font created)", r)
45+
"Invalid font (font module quit since font created)", r);
4646

4747
#define RAISE_FONT_QUIT_ERROR() \
4848
RAISE(pgExc_SDLError, \
@@ -538,38 +538,19 @@ font_set_strikethrough(PyObject *self, PyObject *arg)
538538
Py_RETURN_NONE;
539539
}
540540

541-
static PyObject *
542-
font_render(PyObject *self, PyObject *args, PyObject *kwds)
541+
static SDL_Surface *
542+
_create_font_surface(TTF_Font *font, PyObject *text, int antialias,
543+
PyObject *fg_rgba_obj, PyObject *bg_rgba_obj,
544+
int wraplength)
543545
{
544-
if (!PgFont_GenerationCheck(self)) {
545-
return RAISE_FONT_QUIT_ERROR();
546-
}
547-
548-
TTF_Font *font = PyFont_AsFont(self);
549-
int antialias;
550-
PyObject *text, *final;
551-
PyObject *fg_rgba_obj, *bg_rgba_obj = Py_None;
552-
Uint8 rgba[] = {0, 0, 0, 0};
553546
SDL_Surface *surf;
547+
Uint8 rgba[] = {0, 0, 0, 0};
554548
const char *astring = "";
555-
int wraplength = 0;
556-
557-
if (!PgFont_GenerationCheck(self)) {
558-
return RAISE_FONT_QUIT_ERROR()
559-
}
560-
561-
static char *kwlist[] = {"text", "antialias", "color",
562-
"bgcolor", "wraplength", NULL};
563-
564-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OpO|Oi", kwlist, &text,
565-
&antialias, &fg_rgba_obj, &bg_rgba_obj,
566-
&wraplength)) {
567-
return NULL;
568-
}
569549

550+
// 글꼴 생성
570551
if (!pg_RGBAFromObjEx(fg_rgba_obj, rgba, PG_COLOR_HANDLE_ALL)) {
571552
/* Exception already set for us */
572-
return NULL;
553+
return (SDL_Surface *)(NULL);
573554
}
574555

575556
SDL_Color foreg = {rgba[0], rgba[1], rgba[2], SDL_ALPHA_OPAQUE};
@@ -579,37 +560,37 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
579560
if (bg_rgba_obj != Py_None) {
580561
if (!pg_RGBAFromObjEx(bg_rgba_obj, rgba, PG_COLOR_HANDLE_ALL)) {
581562
/* Exception already set for us */
582-
return NULL;
563+
return (SDL_Surface *)(NULL);
583564
}
584565
backg = (SDL_Color){rgba[0], rgba[1], rgba[2], SDL_ALPHA_OPAQUE};
585566
}
586567

587568
if (!PyUnicode_Check(text) && !PyBytes_Check(text) && text != Py_None) {
588-
return RAISE_TEXT_TYPE_ERROR();
569+
return (SDL_Surface *)(RAISE_TEXT_TYPE_ERROR());
589570
}
590571

591572
if (wraplength < 0) {
592-
return RAISE(PyExc_ValueError,
593-
"wraplength parameter must be positive");
573+
return (SDL_Surface *)(RAISE(PyExc_ValueError,
574+
"wraplength parameter must be positive"));
594575
}
595576

596577
if (PyUnicode_Check(text)) {
597578
Py_ssize_t _size = -1;
598579
astring = PyUnicode_AsUTF8AndSize(text, &_size);
599580
if (astring == NULL) { /* exception already set */
600-
return NULL;
581+
return (SDL_Surface *)(NULL);
601582
}
602583
if (strlen(astring) != (size_t)_size) {
603-
return RAISE(PyExc_ValueError,
604-
"A null character was found in the text");
584+
return (SDL_Surface *)(RAISE(
585+
PyExc_ValueError, "A null character was found in the text"));
605586
}
606587
}
607588

608589
else if (PyBytes_Check(text)) {
609590
/* Bytes_AsStringAndSize with NULL arg for length emits
610591
ValueError if internal NULL bytes are present */
611592
if (PyBytes_AsStringAndSize(text, (char **)&astring, NULL) == -1) {
612-
return NULL; /* exception already set */
593+
return (SDL_Surface *)(NULL); /* exception already set */
613594
}
614595
}
615596

@@ -656,9 +637,42 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
656637
}
657638

658639
if (surf == NULL) {
659-
return RAISE(pgExc_SDLError, TTF_GetError());
640+
return (SDL_Surface *)(RAISE(pgExc_SDLError, TTF_GetError()));
660641
}
661642

643+
return surf;
644+
}
645+
646+
static PyObject *
647+
font_render(PyObject *self, PyObject *args, PyObject *kwds)
648+
{
649+
if (!PgFont_GenerationCheck(self)) {
650+
return RAISE_FONT_QUIT_ERROR();
651+
}
652+
653+
int antialias;
654+
PyObject *text, *final;
655+
PyObject *fg_rgba_obj, *bg_rgba_obj = Py_None;
656+
SDL_Surface *surf;
657+
int wraplength = 0;
658+
TTF_Font *font = PyFont_AsFont(self);
659+
660+
if (!PgFont_GenerationCheck(self)) {
661+
return RAISE_FONT_QUIT_ERROR()
662+
}
663+
664+
static char *kwlist[] = {"text", "antialias", "color",
665+
"bgcolor", "wraplength", NULL};
666+
667+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OpO|Oi", kwlist, &text,
668+
&antialias, &fg_rgba_obj, &bg_rgba_obj,
669+
&wraplength)) {
670+
return NULL;
671+
}
672+
673+
surf = _create_font_surface(font, text, antialias, fg_rgba_obj,
674+
bg_rgba_obj, wraplength);
675+
662676
final = (PyObject *)pgSurface_New(surf);
663677
if (final == NULL) {
664678
SDL_FreeSurface(surf);

0 commit comments

Comments
 (0)