3636from collections .abc import Sequence
3737from typing import cast
3838
39- from . import Image , ImageColor , ImageText
39+ from . import Image , ImageColor , ImageFont , ImageText
4040
4141TYPE_CHECKING = False
4242if TYPE_CHECKING :
4343 from collections .abc import Callable
4444 from types import ModuleType
4545 from typing import Any , AnyStr
4646
47- from . import ImageDraw2 , ImageFont
47+ from . import ImageDraw2
4848 from ._typing import Coords , _Ink
4949
5050# experimental access to the outline API
5959
6060
6161class ImageDraw :
62- font : (
63- ImageFont .ImageFont | ImageFont .FreeTypeFont | ImageFont .TransposedFont | None
64- ) = None
62+ font : ImageFont .BaseImageFont | None = None
6563
6664 def __init__ (self , im : Image .Image , mode : str | None = None ) -> None :
6765 """
@@ -105,7 +103,7 @@ def __init__(self, im: Image.Image, mode: str | None = None) -> None:
105103
106104 def getfont (
107105 self ,
108- ) -> ImageFont .ImageFont | ImageFont . FreeTypeFont | ImageFont . TransposedFont :
106+ ) -> ImageFont .BaseImageFont :
109107 """
110108 Get the current default font.
111109
@@ -125,17 +123,11 @@ def getfont(
125123 :returns: An image font."""
126124 if not self .font :
127125 # FIXME: should add a font repository
128- from . import ImageFont
129-
130126 self .font = ImageFont .load_default ()
131127 return self .font
132128
133- def _getfont (
134- self , font_size : float | None
135- ) -> ImageFont .ImageFont | ImageFont .FreeTypeFont | ImageFont .TransposedFont :
129+ def _getfont (self , font_size : float | None ) -> ImageFont .BaseImageFont :
136130 if font_size is not None :
137- from . import ImageFont
138-
139131 return ImageFont .load_default (font_size )
140132 else :
141133 return self .getfont ()
@@ -540,12 +532,7 @@ def text(
540532 xy : tuple [float , float ],
541533 text : AnyStr | ImageText .Text [AnyStr ],
542534 fill : _Ink | None = None ,
543- font : (
544- ImageFont .ImageFont
545- | ImageFont .FreeTypeFont
546- | ImageFont .TransposedFont
547- | None
548- ) = None ,
535+ font : ImageFont .BaseImageFont | None = None ,
549536 anchor : str | None = None ,
550537 spacing : float = 4 ,
551538 align : str = "left" ,
@@ -600,26 +587,26 @@ def draw_text(ink: int, stroke_width: float = 0) -> None:
600587 x = int (line .x )
601588 y = int (line .y )
602589 start = (math .modf (line .x )[0 ], math .modf (line .y )[0 ])
603- try :
604- mask , offset = image_text .font .getmask2 ( # type: ignore[union-attr,misc]
590+ if isinstance ( image_text . font , ImageFont . FreeTypeFont ) :
591+ mask , offset = image_text .font .getmask2 (
605592 line .text ,
606593 mode ,
607- direction = direction ,
608- features = features ,
609- language = language ,
610- stroke_width = stroke_width ,
594+ direction ,
595+ features ,
596+ language ,
597+ stroke_width ,
598+ line .anchor ,
599+ ink ,
600+ start ,
611601 stroke_filled = True ,
612- anchor = line .anchor ,
613- ink = ink ,
614- start = start ,
615602 * args ,
616603 ** kwargs ,
617604 )
618605 x += offset [0 ]
619606 y += offset [1 ]
620- except AttributeError :
607+ else :
621608 try :
622- mask = image_text .font .getmask ( # type: ignore[misc]
609+ mask = image_text .font .getmask (
623610 line .text ,
624611 mode ,
625612 direction ,
@@ -664,12 +651,7 @@ def multiline_text(
664651 xy : tuple [float , float ],
665652 text : AnyStr ,
666653 fill : _Ink | None = None ,
667- font : (
668- ImageFont .ImageFont
669- | ImageFont .FreeTypeFont
670- | ImageFont .TransposedFont
671- | None
672- ) = None ,
654+ font : ImageFont .BaseImageFont | None = None ,
673655 anchor : str | None = None ,
674656 spacing : float = 4 ,
675657 align : str = "left" ,
@@ -702,12 +684,7 @@ def multiline_text(
702684 def textlength (
703685 self ,
704686 text : AnyStr ,
705- font : (
706- ImageFont .ImageFont
707- | ImageFont .FreeTypeFont
708- | ImageFont .TransposedFont
709- | None
710- ) = None ,
687+ font : ImageFont .BaseImageFont | None = None ,
711688 direction : str | None = None ,
712689 features : list [str ] | None = None ,
713690 language : str | None = None ,
@@ -734,12 +711,7 @@ def textbbox(
734711 self ,
735712 xy : tuple [float , float ],
736713 text : AnyStr ,
737- font : (
738- ImageFont .ImageFont
739- | ImageFont .FreeTypeFont
740- | ImageFont .TransposedFont
741- | None
742- ) = None ,
714+ font : ImageFont .BaseImageFont | None = None ,
743715 anchor : str | None = None ,
744716 spacing : float = 4 ,
745717 align : str = "left" ,
@@ -767,12 +739,7 @@ def multiline_textbbox(
767739 self ,
768740 xy : tuple [float , float ],
769741 text : AnyStr ,
770- font : (
771- ImageFont .ImageFont
772- | ImageFont .FreeTypeFont
773- | ImageFont .TransposedFont
774- | None
775- ) = None ,
742+ font : ImageFont .BaseImageFont | None = None ,
776743 anchor : str | None = None ,
777744 spacing : float = 4 ,
778745 align : str = "left" ,
0 commit comments