1515 from typing import Generator
1616 from typing import Literal
1717
18+ from PIL .Image import Image as T_Image
19+ from PIL .ImageDraw import ImageDraw as T_ImageDraw
20+
1821 class InternalText (TypedDict ):
1922 start : list
2023 end : list
@@ -45,11 +48,11 @@ class Callbacks(TypedDict):
4548 Image = ImageDraw = ImageFont = None
4649
4750
48- def mm2px (mm , dpi : int ):
51+ def mm2px (mm : float , dpi : int ) -> float :
4952 return (mm * dpi ) / 25.4
5053
5154
52- def pt2mm (pt ) :
55+ def pt2mm (pt : float ) -> float :
5356 return pt * 0.352777778
5457
5558
@@ -58,8 +61,9 @@ def _set_attributes(element, **attributes):
5861 element .setAttribute (key , value )
5962
6063
61- def create_svg_object (with_doctype = False ):
64+ def create_svg_object (with_doctype = False ) -> xml . dom . minidom . Document :
6265 imp = xml .dom .minidom .getDOMImplementation ()
66+ assert imp is not None
6367 doctype = imp .createDocumentType (
6468 "svg" ,
6569 "-//W3C//DTD SVG 1.1//EN" ,
@@ -317,13 +321,13 @@ def __init__(self) -> None:
317321 self ._create_text ,
318322 self ._finish ,
319323 )
320- self .compress = False
321- self .with_doctype = True
322- self ._document = None
323- self ._root = None
324- self ._group = None
324+ self .compress : bool = False
325+ self .with_doctype : bool = True
326+ self ._document : xml . dom . minidom . Document
327+ self ._root : xml . dom . minidom . Element
328+ self ._group : xml . dom . minidom . Element
325329
326- def _init (self , code ):
330+ def _init (self , code : list [ str ] ):
327331 width , height = self .calculate_size (len (code [0 ]), len (code ))
328332 self ._document = create_svg_object (self .with_doctype )
329333 self ._root = self ._document .documentElement
@@ -438,16 +442,18 @@ def __init__(self, format="PNG", mode="RGB", dpi=300) -> None:
438442 self .format = format
439443 self .mode = mode
440444 self .dpi = dpi
441- self ._image = None
442- self ._draw = None
445+ self ._image : T_Image
446+ self ._draw : T_ImageDraw
443447
444- def _init (self , code ):
448+ def _init (self , code : list [str ]) -> None :
449+ if ImageDraw is None :
450+ raise RuntimeError ("Pillow not found. Cannot create image." )
445451 width , height = self .calculate_size (len (code [0 ]), len (code ))
446452 size = (int (mm2px (width , self .dpi )), int (mm2px (height , self .dpi )))
447453 self ._image = Image .new (self .mode , size , self .background )
448454 self ._draw = ImageDraw .Draw (self ._image )
449455
450- def _paint_module (self , xpos , ypos , width , color ):
456+ def _paint_module (self , xpos : float , ypos : float , width : float , color ):
451457 size = [
452458 (mm2px (xpos , self .dpi ), mm2px (ypos , self .dpi )),
453459 (
@@ -458,6 +464,7 @@ def _paint_module(self, xpos, ypos, width, color):
458464 self ._draw .rectangle (size , outline = color , fill = color )
459465
460466 def _paint_text (self , xpos , ypos ):
467+ assert ImageFont is not None
461468 font_size = int (mm2px (pt2mm (self .font_size ), self .dpi ))
462469 if font_size <= 0 :
463470 return
@@ -472,7 +479,7 @@ def _paint_text(self, xpos, ypos):
472479 )
473480 ypos += pt2mm (self .font_size ) / 2 + self .text_line_distance
474481
475- def _finish (self ) -> Image :
482+ def _finish (self ) -> T_Image :
476483 return self ._image
477484
478485 def save (self , filename : str , output ) -> str :
0 commit comments