Skip to content

Commit fcf7a8f

Browse files
authored
Merge pull request #4 from miguelTorrinhaPereira/master
added support to draw all characters in all sizes
2 parents ab77549 + e6566dd commit fcf7a8f

32 files changed

+5412
-541
lines changed

casioplot/casioplot.py

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
You can also use :py:data:`casioplot_settings` to change some behavior.
1111
"""
1212

13-
from os import path
1413
from typing import Literal
1514
from PIL import Image, ImageTk
1615
import tkinter as tk
17-
from configs import get_config
16+
from configs import _get_config
17+
from characters import _get_char
1818

1919
# color type
2020
COLOR = tuple[int, int, int]
2121
# some frequently used colors
22-
_WHITE: COLOR = (255, 255, 255) # RGBA white
22+
_WHITE: COLOR = (255, 255, 255) # RGB white
2323
_BLACK: COLOR = (0, 0, 0) # RGBA black
2424
# a list of all settings that if change should trigger the _redraw_screen function
2525
redraw_settings: tuple = ('width', 'height', 'left_margin', 'right_margin', 'top_margin', 'bottom_margin')
@@ -71,8 +71,8 @@ def _screen_dimensions(self) -> tuple[int, int]:
7171

7272

7373
def config_to(self, config: str = "default") -> None:
74-
global _screen
75-
for setting, value in get_config(config).items():
74+
global _screen, _window
75+
for setting, value in _get_config(config).items():
7676
setattr(self, setting, value)
7777

7878
# a configuration may have a background image
@@ -102,6 +102,7 @@ def set(self, **settings) -> None:
102102
should_redraw_screen = True
103103

104104
elif setting == "show_screen":
105+
global _window
105106
if value is True:
106107
_window.deiconify()
107108
else:
@@ -127,7 +128,7 @@ def _redraw_screen() -> None:
127128
Only called when casioplot_settings.set() is called,
128129
used to redraw _image with custom margins, width and height.
129130
"""
130-
global _screen
131+
global _screen, _window
131132
screen_width, screen_height = casioplot_settings._screen_dimensions()
132133

133134
# Create a new white image
@@ -167,6 +168,7 @@ def show_screen() -> None:
167168
The image is saved with the filename found in `casioplot_settings.get('filename')`
168169
"""
169170
if casioplot_settings.get("show_screen") is True:
171+
global _photo_image, _screen_display, _window
170172
# show the screen
171173
_photo_image = ImageTk.PhotoImage(_screen)
172174
_screen_display["image"] = _photo_image
@@ -206,61 +208,45 @@ def set_pixel(x: int, y: int, color: COLOR = _BLACK) -> None:
206208
:param y: y coordinate (from the top)
207209
:param color: The pixel color. A tuple that contain 3 integers from 0 to 255.
208210
"""
211+
global _screen
209212
if _coordenates_in_bounds(x, y):
210213
_screen.putpixel(_canvas_to_screen(x, y), color)
211214

212215

213-
def _get_filename(character, size: Literal["small", "medium", "large"] = "medium"):
214-
"""Get the file where a character is saved and return the ``space`` file if the character doesn't exist.
215-
216-
:param character: The character to find
217-
:param size: The size of the character
218-
:return: The character filename. A string: "{``./chars`` folder absolute path}/{character}_{size}.png"
219-
"""
220-
special_chars = {" ": "space"}
221-
filename = special_chars.get(character, character) + ".txt"
222-
file_path = path.join(path.abspath(path.dirname(__file__)), "chars", size, filename)
223-
224-
if not path.isfile(file_path):
225-
print(f'WARNING: No character "{character}" found for size "{size}".')
226-
file_path = path.join(
227-
path.abspath(path.dirname(__file__)), "chars", size, "space.txt"
228-
)
229-
return file_path
230-
231-
232216
def draw_string(
233217
x: int,
234218
y: int,
235219
text: str,
236220
color: COLOR = _BLACK,
237-
size: Literal["small", "medium", "large"] = "medium",
221+
size: Literal["small", "medium", "large"] = "medium"
238222
) -> None:
239223
"""Draw a string on the virtual screen with the given RGB color and size.
240224
241225
:param x: x coordinate (from the left)
242226
:param y: y coordinate (from the top)
243-
:param text: text that will be shown
244-
:param color: The text color. A tuple that contain 3 integers from 0 to 255.
227+
:param text: text that will be drawn
228+
:param color: The color of the text. A tuple that contain 3 integers from 0 to 255.
245229
:param size: Size of the text. String from the following values: "small", "medium" or "large".
246230
:raise ValueError: Raise a ValueError if the size isn't correct.
247231
"""
248-
sizes = {"small": (10, 10), "medium": (13, 17), "large": (18, 23)}
249-
250-
if size not in sizes.keys():
251-
raise ValueError(
252-
f'Unknown size "{size}". Size must be one of the following: "small", "medium" or "large"'
253-
)
254-
255-
for character in text:
256-
filename = _get_filename(character, size)
257-
n = 0
258-
with open(filename, "r") as c:
259-
count = 0
260-
for line in c:
261-
for j, k in enumerate(line):
262-
if k in ["$"]:
263-
set_pixel(x + j, y + count, color)
264-
n = j
265-
count += 1
266-
x += n
232+
def draw_char(x: int, y: int, char_map: tuple, color: COLOR) -> None:
233+
"""Draws a single character
234+
235+
:param x: x coordinate (from the left)
236+
:param y: y coordinate (from the top)
237+
:param char_mpa: a tuple of strings, where every string represents a row
238+
of the character that is going to be drawn
239+
:param color: The color of the character
240+
"""
241+
for y2, row in enumerate(char_map):
242+
for x2, pixel in enumerate(row):
243+
if pixel == 'X':
244+
set_pixel(x+x2, y+y2, color)
245+
246+
for char in text:
247+
if not _coordenates_in_bounds(x, y):
248+
return
249+
250+
char_map = _get_char(char, size)
251+
draw_char(x, y, char_map, color)
252+
x += len(char_map[0])

0 commit comments

Comments
 (0)