Skip to content

Commit

Permalink
fix: Disable auto-canvasing on manual canvasing
Browse files Browse the repository at this point in the history
  • Loading branch information
ful1e5 committed May 24, 2024
1 parent 30a8d5a commit 5b10f5d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
14 changes: 11 additions & 3 deletions src/clickgen/cursors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ class CursorImage:
image: Image
hotspot: Tuple[int, int]
nominal: int

def __init__(self, image: Image, hotspot: Tuple[int, int], nominal: int) -> None:
re_canvas: bool

def __init__(
self,
image: Image,
hotspot: Tuple[int, int],
nominal: int,
re_canvas: bool = False,
) -> None:
self.image = image
self.hotspot = hotspot
self.nominal = nominal
self.re_canvas = re_canvas

def __repr__(self) -> str:
return f"CursorImage(image={self.image!r}, hotspot={self.hotspot!r}, nominal={self.nominal!r})"
return f"CursorImage(image={self.image!r}, hotspot={self.hotspot!r}, nominal={self.nominal!r}, re_canvas={self.re_canvas!r})"


class CursorFrame:
Expand Down
3 changes: 2 additions & 1 deletion src/clickgen/cursors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class CursorImage:
image: Image
hotspot: tuple[int, int]
nominal: int
def __init__(self, image: Image, hotspot: tuple[int, int], nominal: int) -> None: ...
re_canvas: bool
def __init__(self, image: Image, hotspot: tuple[int, int], nominal: int, re_canvas: bool = False) -> None: ...

class CursorFrame:
images: list[CursorImage]
Expand Down
11 changes: 8 additions & 3 deletions src/clickgen/parser/png.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ def _parse(self) -> List[CursorFrame]:
res_img = self._image.resize((size, size), 1)

if size != canvas_size:
canvas = Image.new("RGBA", (size, size), (0, 0, 0, 0))
canvas = Image.new("RGBA", (canvas_size, canvas_size), (0, 0, 0, 0))
canvas.paste(res_img, (0, 0))
res_img = canvas

res_hotspot = self._cal_hotspot(res_img)
images.append(
CursorImage(image=res_img, hotspot=res_hotspot, nominal=canvas_size)
CursorImage(
image=res_img,
hotspot=res_hotspot,
nominal=canvas_size,
re_canvas=size != canvas_size,
)
)

return [CursorFrame(images, delay=self.delay)]
Expand All @@ -109,7 +114,7 @@ def __init__(
self,
blobs: List[bytes],
hotspot: Tuple[int, int],
sizes: Optional[List[int]] = None,
sizes: Optional[List[Union[int, str]]] = None,
delay: Optional[int] = None,
) -> None:
super().__init__(blobs[0])
Expand Down
2 changes: 1 addition & 1 deletion src/clickgen/parser/png.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class MultiPNGParser(BaseParser):
@classmethod
def can_parse(cls, blobs: list[bytes]) -> bool: ...
frames: Incomplete
def __init__(self, blobs: list[bytes], hotspot: tuple[int, int], sizes: list[int] | None = None, delay: int | None = None) -> None: ...
def __init__(self, blobs: list[bytes], hotspot: tuple[int, int], sizes: list[int | str] | None = None, delay: int | None = None) -> None: ...
25 changes: 14 additions & 11 deletions src/clickgen/writer/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,21 @@ def re_canvas(size: int, img: Image.Image):
# | 256 | 170.666 → 171 | 204.8 → 205 | 256 |

blob = BytesIO()
if width <= 32 or height <= 32:
re_canvas(32, clone).save(blob, "PNG")
elif width <= 48 or height <= 48:
re_canvas(48, clone).save(blob, "PNG")
elif width <= 64 or height <= 64:
re_canvas(64, clone).save(blob, "PNG")
elif width <= 96 or height <= 96:
re_canvas(96, clone).save(blob, "PNG")
elif width <= 128 or height <= 128:
re_canvas(128, clone).save(blob, "PNG")
if not image.re_canvas:
if width <= 32 or height <= 32:
re_canvas(32, clone).save(blob, "PNG")
elif width <= 48 or height <= 48:
re_canvas(48, clone).save(blob, "PNG")
elif width <= 64 or height <= 64:
re_canvas(64, clone).save(blob, "PNG")
elif width <= 96 or height <= 96:
re_canvas(96, clone).save(blob, "PNG")
elif width <= 128 or height <= 128:
re_canvas(128, clone).save(blob, "PNG")
else:
re_canvas(256, clone).save(blob, "PNG")
else:
re_canvas(256, clone).save(blob, "PNG")
image.image.save(blob, "PNG")

blob.seek(0)
image_data.append(blob.read())
Expand Down

0 comments on commit 5b10f5d

Please sign in to comment.