From 7b5f1b920842d7cdde7429c22abe065b9b212e15 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sat, 14 Sep 2024 13:49:16 -0400 Subject: [PATCH 1/3] add .tracking and px -> em conversion --- arcade/text.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/arcade/text.py b/arcade/text.py index 1407d58a3d..d1463b7bc4 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -613,6 +613,42 @@ def position(self, point: Point): else: self._label.position = x, y, self._label.z + @property + def tracking(self) -> float | None: + """ + Get the tracking amount for this text object, or rather, + the added space between each character. + + Returns a pixel amount, or None if the tracking is inconsistent. + """ + kerning = self._label.get_style("kerning") + return kerning if kerning != pyglet.text.document.STYLE_INDETERMINATE else None + + @tracking.setter + def tracking(self, value: float): + """ + Get the tracking amount for this text object, or rather, + the added space between each character. + + `value` is an amount in pixels and can be negative. + To convert from the em unit, use Text.em_to_px(). + """ + self._label.set_style("kerning", value) + + def em_to_px(self, em: float) -> float: + """Convert from an em value to a pixel amount. + + 1em is defined as `font_size`pt. + """ + return (em * self.font_size) * (4 / 3) + + def px_to_em(self, px: float) -> float: + """Convert from an em value to a pixel amount. + + 1em is defined as `font_size`pt. + """ + return px / (4 / 3) / self.font_size + def create_text_sprite( text: str, From 2225a3f14fa49b66d2c5724b853f9605c7a35915 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sat, 14 Sep 2024 13:56:32 -0400 Subject: [PATCH 2/3] I hate Sphinx It's coarse, and rough, and irritating, and it gets everywhere. --- arcade/text.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index d1463b7bc4..5385ed41d0 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -638,14 +638,14 @@ def tracking(self, value: float): def em_to_px(self, em: float) -> float: """Convert from an em value to a pixel amount. - 1em is defined as `font_size`pt. + 1em is defined as ``font_size``pt. """ return (em * self.font_size) * (4 / 3) def px_to_em(self, px: float) -> float: - """Convert from an em value to a pixel amount. + """Convert from a pixel amount to a value in ems. - 1em is defined as `font_size`pt. + 1em is defined as ``font_size``pt. """ return px / (4 / 3) / self.font_size From f1e4496b83d2ed82763d9029e010cd3d84d152a1 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Sat, 14 Sep 2024 18:20:31 -0400 Subject: [PATCH 3/3] fix docs --- arcade/text.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/arcade/text.py b/arcade/text.py index 5385ed41d0..419baa5386 100644 --- a/arcade/text.py +++ b/arcade/text.py @@ -616,36 +616,33 @@ def position(self, point: Point): @property def tracking(self) -> float | None: """ - Get the tracking amount for this text object, or rather, + Get/set the tracking amount for this text object, or rather, the added space between each character. - Returns a pixel amount, or None if the tracking is inconsistent. + The value is an amount in pixels and can be negative. + To convert from the em unit, use Text.em_to_px(). + + Returns: + a pixel amount, or None if the tracking is inconsistent. """ kerning = self._label.get_style("kerning") return kerning if kerning != pyglet.text.document.STYLE_INDETERMINATE else None @tracking.setter def tracking(self, value: float): - """ - Get the tracking amount for this text object, or rather, - the added space between each character. - - `value` is an amount in pixels and can be negative. - To convert from the em unit, use Text.em_to_px(). - """ self._label.set_style("kerning", value) def em_to_px(self, em: float) -> float: """Convert from an em value to a pixel amount. - 1em is defined as ``font_size``pt. + 1em is defined as ``font_size`` pt. """ return (em * self.font_size) * (4 / 3) def px_to_em(self, px: float) -> float: """Convert from a pixel amount to a value in ems. - 1em is defined as ``font_size``pt. + 1em is defined as ``font_size`` pt. """ return px / (4 / 3) / self.font_size