Skip to content

Commit 6d0c42e

Browse files
authored
Merge pull request #2175 from dr0id/enhancements/font/stylename
add style_name property to font and freetype font
2 parents e9c4115 + 1626918 commit 6d0c42e

File tree

14 files changed

+117
-15
lines changed

14 files changed

+117
-15
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Dependency versions:
148148
+----------+------------------------+
149149
| SDL_image| >= 2.0.2 |
150150
+----------+------------------------+
151-
| SDL_ttf | >= 2.0.11 |
151+
| SDL_ttf | >= 2.0.12 |
152152
+----------+------------------------+
153153
| SDL_gfx | (Optional, vendored in)|
154154
+----------+------------------------+

buildconfig/stubs/pygame/font.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ class Font:
3434
underline: bool
3535
strikethrough: bool
3636
align: int
37+
style_name: str
3738
def __init__(self, filename: Optional[FileArg] = None, size: int = 20) -> None: ...
3839
def render(
3940
self,
4041
text: Union[str, bytes, None],
4142
antialias: bool,
4243
color: ColorValue,
4344
bgcolor: Optional[ColorValue] = None,
44-
wraplength: int = 0
45+
wraplength: int = 0,
4546
) -> Surface: ...
4647
def size(self, text: Union[str, bytes]) -> Tuple[int, int]: ...
4748
def set_underline(self, value: bool) -> None: ...

buildconfig/stubs/pygame/freetype.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Font:
4545
ascender: int
4646
descender: int
4747
style: int
48+
style_name: str
4849
underline: bool
4950
strong: bool
5051
oblique: bool

docs/reST/ref/font.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ solves no longer exists, it will likely be removed in the future.
211211

212212
.. ## Font.name ##
213213
214+
.. attribute:: style_name
215+
216+
| :sl:`Gets the font's style_name.`
217+
| :sg:`style_name -> str`
218+
219+
Read only. Returns the font's style name. Style names are arbitrary, can be an empty string.
220+
Here are some examples:
221+
222+
'Black', 'Bold', 'Bold Italic', 'BoldOblique', 'Book', 'BookOblique', 'Condensed', 'Condensed Oblique',
223+
'ExtraLight', 'Italic', 'Light', 'LightOblique', 'Medium', 'MediumOblique', 'Oblique', 'Regular',
224+
'Semibold', 'Semilight', 'Slanted'
225+
226+
227+
.. versionadded:: 2.3.1
228+
229+
.. ## Font.style_name ##
230+
214231
.. attribute:: italic
215232

216233
| :sl:`Gets or sets whether the font should be rendered in (faked) italics.`
@@ -263,7 +280,7 @@ solves no longer exists, it will likely be removed in the future.
263280
264281
.. attribute:: align
265282

266-
| :sl:`Set how rendered text is aligned when given a wrap length`
283+
| :sl:`Set how rendered text is aligned when given a wrap length.`
267284
| :sg:`align -> int`
268285
269286
Can be set to `pygame.FONT_LEFT`, `pygame.FONT_RIGHT`, or

docs/reST/ref/freetype.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,22 @@ loaded. This module must be imported explicitly to be used. ::
224224
Read only. Returns the real (long) name of the font, as
225225
recorded in the font file.
226226

227+
.. attribute:: style_name
228+
229+
| :sl:`Gets the font's style_name.`
230+
| :sg:`style_name -> str`
231+
232+
Read only. Returns the font's style name. Style names are arbitrary, can be an empty string.
233+
Here are some examples:
234+
235+
'Black', 'Bold', 'Bold Italic', 'BoldOblique', 'Book', 'BookOblique', 'Condensed', 'Condensed Oblique',
236+
'ExtraLight', 'Italic', 'Light', 'LightOblique', 'Medium', 'MediumOblique', 'Oblique', 'Regular',
237+
'Semibold', 'Semilight', 'Slanted'
238+
239+
.. versionadded:: 2.3.1
240+
241+
.. ## Font.style_name ##
242+
227243
.. attribute:: path
228244

229245
| :sl:`Font file path`

docs/readmes/README.fr.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Versions des dépendances:
152152
+----------+------------------------+
153153
| SDL_image| >= 2.0.2 |
154154
+----------+------------------------+
155-
| SDL_ttf | >= 2.0.11 |
155+
| SDL_ttf | >= 2.0.12 |
156156
+----------+------------------------+
157157
| SDL_gfx | (Optional, vendored in)|
158158
+----------+------------------------+

docs/readmes/README.zh-cn.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pygame显然依赖于SDL和Python。此外pygame还嵌入了几个较小的库
113113
+----------+------------------------+
114114
| SDL_image| >= 2.0.2 |
115115
+----------+------------------------+
116-
| SDL_ttf | >= 2.0.11 |
116+
| SDL_ttf | >= 2.0.12 |
117117
+----------+------------------------+
118118
| SDL_gfx | (可选,内置) |
119119
+----------+------------------------+

src_c/_freetype.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ _ftfont_setstyle(pgFontObject *, PyObject *, void *);
115115
static PyObject *
116116
_ftfont_getname(pgFontObject *, void *);
117117
static PyObject *
118+
_ftfont_getstylename(pgFontObject *, void *);
119+
static PyObject *
118120
_ftfont_getpath(pgFontObject *, void *);
119121
static PyObject *
120122
_ftfont_getscalable(pgFontObject *, void *);
@@ -550,6 +552,8 @@ static PyGetSetDef _ftfont_getsets[] = {
550552
{"descender", (getter)_ftfont_getfontmetric, 0,
551553
DOC_FREETYPE_FONT_DESCENDER, (void *)_PGFT_Font_GetDescender},
552554
{"name", (getter)_ftfont_getname, 0, DOC_FREETYPE_FONT_NAME, 0},
555+
{"style_name", (getter)_ftfont_getstylename, 0,
556+
DOC_FREETYPE_FONT_STYLENAME, 0},
553557
{"path", (getter)_ftfont_getpath, 0, DOC_FREETYPE_FONT_PATH, 0},
554558
{"scalable", (getter)_ftfont_getscalable, 0, DOC_FREETYPE_FONT_SCALABLE,
555559
0},
@@ -1097,7 +1101,21 @@ _ftfont_getname(pgFontObject *self, void *closure)
10971101
const char *name = _PGFT_Font_GetName(self->freetype, self);
10981102
return name ? PyUnicode_FromString(name) : 0;
10991103
}
1100-
return PyObject_Repr((PyObject *)self);
1104+
1105+
PyErr_SetString(PyExc_AttributeError, "<uninitialized Font object>");
1106+
return NULL;
1107+
}
1108+
1109+
static PyObject *
1110+
_ftfont_getstylename(pgFontObject *self, void *closure)
1111+
{
1112+
if (pgFont_IS_ALIVE(self)) {
1113+
const char *stylename = _PGFT_Font_GetStyleName(self->freetype, self);
1114+
return stylename ? PyUnicode_FromString(stylename) : 0;
1115+
}
1116+
1117+
PyErr_SetString(PyExc_AttributeError, "<uninitialized Font object>");
1118+
return NULL;
11011119
}
11021120

11031121
static PyObject *

src_c/doc/font_doc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
#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"
1212
#define DOC_FONT_FONT_BOLD "bold -> bool\nGets or sets whether the font should be rendered in (faked) bold."
1313
#define DOC_FONT_FONT_NAME "name -> str\nGets the font's name."
14+
#define DOC_FONT_FONT_STYLENAME "style_name -> str\nGets the font's style_name."
1415
#define DOC_FONT_FONT_ITALIC "italic -> bool\nGets or sets whether the font should be rendered in (faked) italics."
1516
#define DOC_FONT_FONT_UNDERLINE "underline -> bool\nGets or sets whether the font should be rendered with an underline."
1617
#define DOC_FONT_FONT_STRIKETHROUGH "strikethrough -> bool\nGets or sets whether the font should be rendered with a strikethrough."
17-
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length"
18+
#define DOC_FONT_FONT_ALIGN "align -> int\nSet how rendered text is aligned when given a wrap length."
1819
#define DOC_FONT_FONT_RENDER "render(text, antialias, color, bgcolor=None, wraplength=0) -> Surface\ndraw text on a new Surface"
1920
#define DOC_FONT_FONT_SIZE "size(text) -> (width, height)\ndetermine the amount of space needed to render text"
2021
#define DOC_FONT_FONT_SETUNDERLINE "set_underline(bool) -> None\ncontrol if text is rendered with an underline"

src_c/doc/freetype_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define DOC_FREETYPE_GETDEFAULTFONT "get_default_font() -> string\nGet the filename of the default font"
1414
#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."
1515
#define DOC_FREETYPE_FONT_NAME "name -> string\nProper font name."
16+
#define DOC_FREETYPE_FONT_STYLENAME "style_name -> str\nGets the font's style_name."
1617
#define DOC_FREETYPE_FONT_PATH "path -> unicode\nFont file path"
1718
#define DOC_FREETYPE_FONT_SIZE "size -> float\nsize -> (float, float)\nThe default point size used in rendering"
1819
#define DOC_FREETYPE_FONT_GETRECT "get_rect(text, style=STYLE_DEFAULT, rotation=0, size=0) -> rect\nReturn the size and offset of rendered text"

0 commit comments

Comments
 (0)