Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/asynckivy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__all__ = (
'MotionEventAlreadyEndedError',
'animate',
'create_texture_from_text',
'event',
'fade_transition',
'interpolate',
Expand Down Expand Up @@ -28,4 +29,4 @@
from ._touch import watch_touch, rest_of_touch_events, rest_of_touch_moves, touch_up_event
from ._threading import run_in_executor, run_in_thread
from ._n_frames import one_frame, n_frames
from ._utils import transform, suppress_event
from ._utils import transform, suppress_event, create_texture_from_text
2 changes: 1 addition & 1 deletion src/asynckivy/_touch.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async def touch_up_event(widget, touch, *, stop_dispatching=False, timeout=1.) -
'''
*(experimental state)*

Return an awaitable that can be used to wait for the ``on_touch_up`` event of the given ``touch`` to occur.
Return an awaitable that can be used to wait for the ``on_touch_up`` event of the given ``touch`` to occur.

.. code-block::

Expand Down
28 changes: 27 additions & 1 deletion src/asynckivy/_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
__all__ = ('transform', 'suppress_event', )
__all__ = ('transform', 'suppress_event', 'create_texture_from_text', )
import typing as T
from contextlib import contextmanager

from kivy.graphics import PushMatrix, PopMatrix, InstructionGroup
from kivy.graphics.texture import Texture
from kivy.core.text import Label as CoreLabel
from kivy.core.text.markup import MarkupLabel as CoreMarkupLabel


@contextmanager
Expand Down Expand Up @@ -166,3 +169,26 @@ def __enter__(self):

def __exit__(self, *args):
self._dispatcher.unbind_uid(self._name, self._bind_uid)


def create_texture_from_text(**label_kwargs) -> Texture:
'''
Create a :external:kivy:doc:`api-kivy.graphics.texture` from text.

.. code-block::

from kivy.metrics import sp

texture = create_texture_from_text(
text='Hello',
font_size=sp(50),
font_name='Roboto',
color=(1, 0, 0, 1),
)

The keyword arguments are likely to be very similar to :external:kivy:doc:`api-kivy.uix.label` 's.
'''
core = CoreMarkupLabel if label_kwargs.pop('markup', False) else CoreLabel
label = core(**label_kwargs)
label.refresh()
return label.texture