Skip to content

Commit

Permalink
More types!
Browse files Browse the repository at this point in the history
  • Loading branch information
naslundx committed Aug 24, 2024
1 parent a5efcc7 commit 5ce8527
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ build-backend = "poetry.core.masonry.api"

[tool.isort]
profile = "black"

[tool.mypy]
python_version = "3.9"
disallow_untyped_defs = false
show_error_codes = true
no_implicit_optional = true
warn_return_any = true
warn_unused_ignores = true
6 changes: 4 additions & 2 deletions wrdcld/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from dataclasses import replace
from pathlib import Path

from PIL.Image import Image

from .font import FontWrapper
from .image import ImageWrapper
from .main import fill_next_word
Expand All @@ -27,9 +29,9 @@ def make_word_cloud(
maximum_font_size: int = 100,
word_padding: int = 0, # TODO
scaling_func: Callable[[float], float] = math.sqrt,
mask=None, # TODO
mask: Image | None = None, # TODO
seed: int | float | str | bytes | bytearray | None = None,
):
) -> Image:
if seed is not None:
random.seed(seed)

Expand Down
16 changes: 8 additions & 8 deletions wrdcld/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

from PIL import Image, ImageDraw, ImageFont
from PIL.ImageFont import FreeTypeFont

from .image import ImageWrapper
from .rectangle import Rectangle
Expand All @@ -20,25 +21,24 @@ def color(self, frequency: float) -> Color:
return self.color_func(frequency)

@lru_cache(maxsize=1024)
def get(self):
return ImageFont.truetype(self.path, self.size)
def get(self) -> FreeTypeFont:
return ImageFont.truetype(str(self.path), self.size)

@lru_cache(maxsize=1024)
def getbbox(self, word: str):
def getbbox(self, word: str) -> Rectangle:
bbox = self.get().getbbox(word)
return Rectangle(
x=bbox[0], y=bbox[1], width=bbox[2] - bbox[0], height=bbox[3] - bbox[1]
)

def __getitem__(self, new_size: float):
def __getitem__(self, new_size: float) -> "FontWrapper":
rounded_new_size = int(round(new_size))
return replace(self, size=rounded_new_size)

def get_length_of_word(self, word: str) -> float:
return self.get().getlength(word)

def find_fontsize_for_width(self, width: int, word: str) -> int:

# Check if the word can fit even with the smallest font size
test_font = replace(self, size=1)
if test_font.get_length_of_word(word) > width:
Expand Down Expand Up @@ -72,7 +72,7 @@ def find_fontsize_for_width(self, width: int, word: str) -> int:
return int(fontsize)

@staticmethod
def default_font():
def default_font() -> Path:
return get_repo_root() / "fonts" / "OpenSans-Regular.ttf"


Expand All @@ -91,7 +91,7 @@ def draw_text(
text_bbox = font.getbbox(word)

if rotate:
text_image = Image.new("RGB", rectangle.rotated_ccw.wh, image.background_color)
text_image = Image.new("RGB", rectangle.rotated_ccw.wh, image.background_color) # type: ignore
text_draw = ImageDraw.Draw(text_image)

text_draw.text(
Expand All @@ -101,7 +101,7 @@ def draw_text(
fill=font.color(frequency),
)
rotated_text_image = text_image.rotate(90, expand=True)
image.img.paste(rotated_text_image, rectangle.xy)
image.img.paste(rotated_text_image, rectangle.xy) # type: ignore

else:
image.canvas.text(
Expand Down
10 changes: 8 additions & 2 deletions wrdcld/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def _fill(
rectangle: Rectangle,
image: ImageWrapper,
word_length: int,
word_length: float,
word: str,
font: FontWrapper,
frequency: float,
Expand Down Expand Up @@ -53,7 +53,13 @@ def _fill(
return text_rectangle


def fill_next_word(word, available_rectangles, image, font, frequency):
def fill_next_word(
word: str,
available_rectangles: list,
image: ImageWrapper,
font: FontWrapper,
frequency: float,
):
word_length = font.get_length_of_word(word)

suitable_horizontal_rectangles = [
Expand Down
20 changes: 10 additions & 10 deletions wrdcld/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,43 @@ class Rectangle:
y: float

@property
def xy(self):
def xy(self) -> tuple[float, float]:
"""
Returns the coordinates of the rectangle as a tuple (x, y).
"""
return (int(self.x + 0.5), int(self.y + 0.5))

@property
def wh(self):
def wh(self) -> tuple[float, float]:
"""
Returns the width and height of the rectangle as a tuple (width, height).
"""
return (math.ceil(self.width), math.ceil(self.height))

@property
def right(self):
def right(self) -> float:
return self.x + self.width

@property
def bottom(self):
def bottom(self) -> float:
return self.y + self.height

@property
def xyrb(self):
def xyrb(self) -> tuple[float, float, float, float]:
"""
Returns the coordinates of the rectangle as a tuple (x, y, right, bottom).
"""
return (self.x, self.y, self.x + self.width, self.y + self.height)

@property
def area(self):
def area(self) -> float:
"""
Returns the area of the rectangle.
"""
return self.width * self.height

@property
def rotated_ccw(self):
def rotated_ccw(self) -> "Rectangle":
"""
Returns a new rectangle that is rotated 90 degrees counter-clockwise.
"""
Expand Down Expand Up @@ -107,7 +107,7 @@ def contains_other(self, other: "Rectangle") -> bool:
"""
return other.is_inside(self)

def __repr__(self):
def __repr__(self) -> str:
return f"Rectangle(x={int(self.x)} y={int(self.y)} w={int(self.width)} h={int(self.height)})"


Expand Down Expand Up @@ -314,7 +314,7 @@ def _make_new_rectangles(
row_ind: int,
left_inds: list[int],
right_inds: list[int],
):
) -> list[Rectangle]:

new_rectangles = []
for left_ind, right_ind in zip(left_inds, right_inds):
Expand Down Expand Up @@ -366,7 +366,7 @@ def fill_space_around_word(
img = image.img
background_color = image.background_color

img_section = img.crop(text_rect.xyrb)
img_section = img.crop(text_rect.xyrb) # type: ignore
img_width, img_height = img_section.size

# get the image data as a 2D array
Expand Down
2 changes: 1 addition & 1 deletion wrdcld/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
Color = Tuple[int, int, int]


def get_repo_root():
def get_repo_root() -> Path:
return Path(__file__).parent.parent

0 comments on commit 5ce8527

Please sign in to comment.