Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
35b73f3
add stylename property to freetype font
dr0id May 18, 2023
2c38f4e
font: add missing stubs for stylename
dr0id May 18, 2023
35d6347
font: reformat file
dr0id May 18, 2023
752c0ed
font/freetype: update versionadded in documentation to 2.3
dr0id May 18, 2023
fc98f19
font: rename 'stylename' to 'style_name' attribute
dr0id May 20, 2023
5eab0f6
font: add style names examples in the docs
dr0id May 23, 2023
f2519dd
font: add tests for style_name property
dr0id May 23, 2023
a990018
font: return NULL in ternary operations instead of 0 in style_name an…
dr0id May 23, 2023
213523e
font: fix wrong null variable
dr0id May 24, 2023
bdf8fe6
font: reformat style names examples
dr0id May 25, 2023
3769b65
readme: update font ttf dependency version to 2.0.12
dr0id May 30, 2023
4ea2b81
font: return error if the style_name cannot be retrieved
dr0id May 30, 2023
5f1a8db
font: fix missing return value in adapted name getter
dr0id May 31, 2023
d1c5556
Merge main
dr0id Jun 21, 2023
f36fd8b
Merge remote-tracking branch 'origin/main' into enhancements/font/sty…
dr0id Jul 13, 2023
a122b71
font.rst: change version added to 2.3.1 for style_name
dr0id Jul 13, 2023
c58e415
font.c: remove error checking in the font_getter_style_name method be…
dr0id Jul 13, 2023
68ec611
pygame.font: raise an error if the style_name property is used after …
dr0id Jul 26, 2023
5e80f8b
freetype: raise an error instead of returning repr in get_name and ge…
dr0id Jul 26, 2023
ddfa563
Documentation: update versionadded to 2.3.1 for Freetype.style_name
dr0id Jul 27, 2023
5e02e19
Font and Freetype: return empty string if the style name is not defin…
dr0id Jul 27, 2023
7cb57e8
Merge main
dr0id Jul 27, 2023
4efd781
font test: update to ensure the result is always a string
dr0id Jul 29, 2023
1626918
freetype: undo changes and return the values as before
dr0id Jul 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Dependency versions:
+----------+------------------------+
| SDL_image| >= 2.0.2 |
+----------+------------------------+
| SDL_ttf | >= 2.0.11 |
| SDL_ttf | >= 2.0.12 |
+----------+------------------------+
| SDL_gfx | (Optional, vendored in)|
+----------+------------------------+
Expand Down
3 changes: 2 additions & 1 deletion buildconfig/stubs/pygame/font.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class Font:
underline: bool
strikethrough: bool
align: int
style_name: str
def __init__(self, filename: Optional[FileArg] = None, size: int = 20) -> None: ...
def render(
self,
text: Union[str, bytes, None],
antialias: bool,
color: ColorValue,
bgcolor: Optional[ColorValue] = None,
wraplength: int = 0
wraplength: int = 0,
) -> Surface: ...
def size(self, text: Union[str, bytes]) -> Tuple[int, int]: ...
def set_underline(self, value: bool) -> None: ...
Expand Down
1 change: 1 addition & 0 deletions buildconfig/stubs/pygame/freetype.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Font:
ascender: int
descender: int
style: int
style_name: str
underline: bool
strong: bool
oblique: bool
Expand Down
19 changes: 18 additions & 1 deletion docs/reST/ref/font.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ solves no longer exists, it will likely be removed in the future.

.. ## Font.name ##

.. attribute:: style_name

| :sl:`Gets the font's style_name.`
| :sg:`style_name -> str`

Read only. Returns the font's style name. Style names are arbitrary, can be an empty string.
Here are some examples:

'Black', 'Bold', 'Bold Italic', 'BoldOblique', 'Book', 'BookOblique', 'Condensed', 'Condensed Oblique',
'ExtraLight', 'Italic', 'Light', 'LightOblique', 'Medium', 'MediumOblique', 'Oblique', 'Regular',
'Semibold', 'Semilight', 'Slanted'


.. versionadded:: 2.3.1

.. ## Font.style_name ##

.. attribute:: italic

| :sl:`Gets or sets whether the font should be rendered in (faked) italics.`
Expand Down Expand Up @@ -263,7 +280,7 @@ solves no longer exists, it will likely be removed in the future.

.. attribute:: align

| :sl:`Set how rendered text is aligned when given a wrap length`
| :sl:`Set how rendered text is aligned when given a wrap length.`
| :sg:`align -> int`

Can be set to `pygame.FONT_LEFT`, `pygame.FONT_RIGHT`, or
Expand Down
16 changes: 16 additions & 0 deletions docs/reST/ref/freetype.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ loaded. This module must be imported explicitly to be used. ::
Read only. Returns the real (long) name of the font, as
recorded in the font file.

.. attribute:: style_name

| :sl:`Gets the font's style_name.`
| :sg:`style_name -> str`

Read only. Returns the font's style name. Style names are arbitrary, can be an empty string.
Here are some examples:

'Black', 'Bold', 'Bold Italic', 'BoldOblique', 'Book', 'BookOblique', 'Condensed', 'Condensed Oblique',
'ExtraLight', 'Italic', 'Light', 'LightOblique', 'Medium', 'MediumOblique', 'Oblique', 'Regular',
'Semibold', 'Semilight', 'Slanted'

.. versionadded:: 2.3.1

.. ## Font.style_name ##

.. attribute:: path

| :sl:`Font file path`
Expand Down
2 changes: 1 addition & 1 deletion docs/readmes/README.fr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Versions des dépendances:
+----------+------------------------+
| SDL_image| >= 2.0.2 |
+----------+------------------------+
| SDL_ttf | >= 2.0.11 |
| SDL_ttf | >= 2.0.12 |
+----------+------------------------+
| SDL_gfx | (Optional, vendored in)|
+----------+------------------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/readmes/README.zh-cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pygame显然依赖于SDL和Python。此外pygame还嵌入了几个较小的库
+----------+------------------------+
| SDL_image| >= 2.0.2 |
+----------+------------------------+
| SDL_ttf | >= 2.0.11 |
| SDL_ttf | >= 2.0.12 |
+----------+------------------------+
| SDL_gfx | (可选,内置) |
+----------+------------------------+
Expand Down
22 changes: 20 additions & 2 deletions src_c/_freetype.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ _ftfont_setstyle(pgFontObject *, PyObject *, void *);
static PyObject *
_ftfont_getname(pgFontObject *, void *);
static PyObject *
_ftfont_getstylename(pgFontObject *, void *);
static PyObject *
_ftfont_getpath(pgFontObject *, void *);
static PyObject *
_ftfont_getscalable(pgFontObject *, void *);
Expand Down Expand Up @@ -550,6 +552,8 @@ static PyGetSetDef _ftfont_getsets[] = {
{"descender", (getter)_ftfont_getfontmetric, 0,
DOC_FREETYPE_FONT_DESCENDER, (void *)_PGFT_Font_GetDescender},
{"name", (getter)_ftfont_getname, 0, DOC_FREETYPE_FONT_NAME, 0},
{"style_name", (getter)_ftfont_getstylename, 0,
DOC_FREETYPE_FONT_STYLENAME, 0},
{"path", (getter)_ftfont_getpath, 0, DOC_FREETYPE_FONT_PATH, 0},
{"scalable", (getter)_ftfont_getscalable, 0, DOC_FREETYPE_FONT_SCALABLE,
0},
Expand Down Expand Up @@ -1095,9 +1099,23 @@ _ftfont_getname(pgFontObject *self, void *closure)
{
if (pgFont_IS_ALIVE(self)) {
const char *name = _PGFT_Font_GetName(self->freetype, self);
return name ? PyUnicode_FromString(name) : 0;
return PyUnicode_FromString(name ? name : "");
}
return PyObject_Repr((PyObject *)self);

PyErr_SetString(PyExc_AttributeError, "<uninitialized Font object>");
return NULL;
}

static PyObject *
_ftfont_getstylename(pgFontObject *self, void *closure)
{
if (pgFont_IS_ALIVE(self)) {
const char *stylename = _PGFT_Font_GetStyleName(self->freetype, self);
return PyUnicode_FromString(stylename ? stylename : "");
}

PyErr_SetString(PyExc_AttributeError, "<uninitialized Font object>");
return NULL;
}

static PyObject *
Expand Down
3 changes: 2 additions & 1 deletion src_c/doc/font_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
#define DOC_FONT_FONT "Font(filename=None, size=20) -> Font\nFont(filename, size) -> Font\nFont(pathlib.Path, size) -> Font\nFont(object, size) -> Font\ncreate a new Font object from a file"
#define DOC_FONT_FONT_BOLD "bold -> bool\nGets or sets whether the font should be rendered in (faked) bold."
#define DOC_FONT_FONT_NAME "name -> str\nGets the font's name."
#define DOC_FONT_FONT_STYLENAME "style_name -> str\nGets the font's style_name."
#define DOC_FONT_FONT_ITALIC "italic -> bool\nGets or sets whether the font should be rendered in (faked) italics."
#define DOC_FONT_FONT_UNDERLINE "underline -> bool\nGets or sets whether the font should be rendered with an underline."
#define DOC_FONT_FONT_STRIKETHROUGH "strikethrough -> bool\nGets or sets whether the font should be rendered with a strikethrough."
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length"
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length."
#define DOC_FONT_FONT_RENDER "render(text, antialias, color, bgcolor=None, wraplength=0) -> Surface\ndraw text on a new Surface"
#define DOC_FONT_FONT_SIZE "size(text) -> (width, height)\ndetermine the amount of space needed to render text"
#define DOC_FONT_FONT_SETUNDERLINE "set_underline(bool) -> None\ncontrol if text is rendered with an underline"
Expand Down
1 change: 1 addition & 0 deletions src_c/doc/freetype_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define DOC_FREETYPE_GETDEFAULTFONT "get_default_font() -> string\nGet the filename of the default font"
#define DOC_FREETYPE_FONT "Font(file, size=0, font_index=0, resolution=0, ucs4=False) -> Font\nFont(pathlib.Path) -> Font\nCreate a new Font instance from a supported font file."
#define DOC_FREETYPE_FONT_NAME "name -> string\nProper font name."
#define DOC_FREETYPE_FONT_STYLENAME "style_name -> str\nGets the font's style_name."
#define DOC_FREETYPE_FONT_PATH "path -> unicode\nFont file path"
#define DOC_FREETYPE_FONT_SIZE "size -> float\nsize -> (float, float)\nThe default point size used in rendering"
#define DOC_FREETYPE_FONT_GETRECT "get_rect(text, style=STYLE_DEFAULT, rotation=0, size=0) -> rect\nReturn the size and offset of rendered text"
Expand Down
34 changes: 26 additions & 8 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
#define RAISE_TEXT_TYPE_ERROR() \
RAISE(PyExc_TypeError, "text must be a unicode or bytes");

#define RAISE_FONT_QUIT_ERROR() \
RAISE(pgExc_SDLError, \
"Invalid font (font module quit since font created)");

/* For filtering out UCS-4 and larger characters when Python is
* built with Py_UNICODE_WIDE.
*/
Expand Down Expand Up @@ -491,8 +495,7 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds)
int wraplength = 0;

if (!PgFont_GenerationCheck(self)) {
return RAISE(pgExc_SDLError,
"Invalid font (font module quit since font created)");
return RAISE_FONT_QUIT_ERROR()
}

static char *kwlist[] = {"text", "antialias", "color",
Expand Down Expand Up @@ -620,8 +623,7 @@ font_size(PyObject *self, PyObject *text)
const char *string;

if (!PgFont_GenerationCheck(self)) {
return RAISE(pgExc_SDLError,
"Invalid font (font module quit since font created)");
return RAISE_FONT_QUIT_ERROR();
}

if (PyUnicode_Check(text)) {
Expand Down Expand Up @@ -653,10 +655,26 @@ font_size(PyObject *self, PyObject *text)
static PyObject *
font_getter_name(PyObject *self, void *closure)
{
if (!PgFont_GenerationCheck(self)) {
return RAISE_FONT_QUIT_ERROR();
}

TTF_Font *font = PyFont_AsFont(self);
const char *font_name = TTF_FontFaceFamilyName(font);

return PyUnicode_FromString(font_name);
return PyUnicode_FromString(font_name ? font_name : "");
}

static PyObject *
font_getter_style_name(PyObject *self, void *closure)
{
if (!PgFont_GenerationCheck(self)) {
return RAISE_FONT_QUIT_ERROR();
}

TTF_Font *font = PyFont_AsFont(self);
const char *font_style_name = TTF_FontFaceStyleName(font);
return PyUnicode_FromString(font_style_name ? font_style_name : "");
}

static PyObject *
Expand All @@ -677,10 +695,8 @@ font_metrics(PyObject *self, PyObject *textobj)
Uint16 ch;
PyObject *temp;
int surrogate;

if (!PgFont_GenerationCheck(self)) {
return RAISE(pgExc_SDLError,
"Invalid font (font module quit since font created)");
return RAISE_FONT_QUIT_ERROR();
}

if (PyUnicode_Check(textobj)) {
Expand Down Expand Up @@ -864,6 +880,8 @@ font_set_direction(PyObject *self, PyObject *arg, PyObject *kwarg)
*/
static PyGetSetDef font_getsets[] = {
{"name", (getter)font_getter_name, NULL, DOC_FONT_FONT_NAME, NULL},
{"style_name", (getter)font_getter_style_name, NULL,
DOC_FONT_FONT_STYLENAME, NULL},
{"bold", (getter)font_getter_bold, (setter)font_setter_bold,
DOC_FONT_FONT_BOLD, NULL},
{"italic", (getter)font_getter_italic, (setter)font_setter_italic,
Expand Down
13 changes: 13 additions & 0 deletions src_c/freetype/ft_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ _PGFT_Font_GetName(FreeTypeInstance *ft, pgFontObject *fontobj)
return font->family_name ? font->family_name : "";
}

const char *
_PGFT_Font_GetStyleName(FreeTypeInstance *ft, pgFontObject *fontobj)
{
FT_Face font;
font = _PGFT_GetFont(ft, fontobj);

if (!font) {
PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft));
return 0;
}
return font->style_name ? font->style_name : "";
}

/* All the font metric functions raise an exception and return 0 on an error.
* It is up to the caller to check PyErr_Occurred for a 0 return value.
*/
Expand Down
2 changes: 2 additions & 0 deletions src_c/freetype/ft_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ _PGFT_Font_GetAvailableSize(FreeTypeInstance *, pgFontObject *, long, long *,
long *, long *, double *, double *);
const char *
_PGFT_Font_GetName(FreeTypeInstance *, pgFontObject *);
const char *
_PGFT_Font_GetStyleName(FreeTypeInstance *, pgFontObject *);
int
_PGFT_TryLoadFont_Filename(FreeTypeInstance *, pgFontObject *, const char *,
long);
Expand Down
13 changes: 13 additions & 0 deletions test/font_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,19 @@ def test_set_name():

self.assertRaises(AttributeError, test_set_name)

def test_font_style_name(self):
f = pygame_font.Font(None, 20)
self.assertNotEqual(f.style_name, "")
self.assertNotEqual(f.style_name, None)

def test_font_style_name_is_readonly(self):
f = pygame_font.Font(None, 20)

def _set_style_name():
f.style_name = "Say my other name."

self.assertRaises(AttributeError, _set_style_name)

def test_font_file_not_found(self):
# A per BUG reported by Bo Jangeborg on pygame-user mailing list,
# http://www.mail-archive.com/pygame-users@seul.org/msg11675.html
Expand Down