Skip to content

Commit

Permalink
Allow to choose the number of letters to underline (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: SkwalExe <[email protected]>
  • Loading branch information
SkwalExe and SkwalExe authored Mar 29, 2024
1 parent 3c6e46f commit 9803e7b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# v1.1.1 - Unreleased
# v2.0.0 - Unreleased

### Added
- Custom number of letters to underline

### Fixed
- Error when entering decimal numbers

### Dependencies
- pillow : `9.5.0` -> `10.1.0`
- pillow : `9.5.0` -> `10.2.0`
- click-extra : `4.6.0` -> `4.7.2`

# v1.1.0 - 2023-10-02
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ python3 src/main.py

# Styles

### Style 1 - First letter underlined
### Style 1 - First letters underlined

> The number of letters to underline is customizable since version 2.0.0
<p align="center">
<img src="assets/style1.png">
</p>


### Style 2 - All letters underlined

<p align="center">
<img src="assets/style2.png">
</p>

# Color schemes

### Color scheme 1 - adi1090x's color scheme
Expand Down
Binary file removed assets/style2.png
Binary file not shown.
9 changes: 0 additions & 9 deletions src/styles/all_underlined.py

This file was deleted.

4 changes: 2 additions & 2 deletions src/styles/first_letter_underlined.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from . import underline_core

display_name = "First letter underlined"
display_name = "First letters underlined"
active = True
questions = underline_core.questions


def get_image(answers):
return underline_core.get_image(answers, "first_letter")
return underline_core.get_image(answers)
49 changes: 22 additions & 27 deletions src/styles/underline_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
questions = [
SelectQuestion("font", "Select a font", [(font, font) for font in font_list], "Iosevka-Nerd-Font-Complete.ttf"),
SelectQuestion("color", "Select a color scheme", color_scheme_names, "adi1090x"),
TextQuestion("underline_count", "Lettrs to undrline", [Number(minimum=0)], "1", "1"),
TextQuestion("padding_x", "Padding x (px)", [Number()], "200", "200"),
TextQuestion("padding_y", "Padding y (px)", [Number()], "20", "20"),
TextQuestion("gap", "Gap between text and bar (px)", [Number()], "20", "20"),
Expand All @@ -20,14 +21,12 @@
active = False


def get_image(answers, type):
if type not in ['all', 'first_letter']:
raise ValueError("Invalid type")

def get_image(answers):
# Load the selected font
font_size = 500
font = ImageFont.truetype(os.path.join(FONTS_DIR, answers["font"]), font_size)

# Set the colors
background = ImageColor.getrgb(color_schemes[answers['color']]["background"])
text = ImageColor.getrgb(color_schemes[answers['color']]["text"])
accent = ImageColor.getrgb(color_schemes[answers['color']]["accent"])
Expand All @@ -44,41 +43,37 @@ def get_image(answers, type):
image = Image.new("RGB", (image_width, image_height), background)
draw = ImageDraw.Draw(image)

# Get the anchor position and type
# Get the text anchor type and position on the image (where the text will be drawn)
# LM = Left/Middle
anchor_type = "lm"
anchor_x = int(answers['padding_x'])
anchor_y = image_height / 2 - (int(answers['gap']) + int(answers['bar_size'])) / 2

anchor_pos = (anchor_x, anchor_y)

# Get the bbox of the first letter

first_letter_bbox = draw.textbbox(
anchor_pos, answers['name'][0], font=font, anchor=anchor_type
)
if int(answers['underline_count']) > 0:
# Get the bbox of the first n letter to underline
first_letters_bbox = draw.textbbox(
anchor_pos,
answers['name'][:int(answers['underline_count'])],
font=font,
anchor=anchor_type)

# Get the underline position
underline_start_x = first_letter_bbox[0] - int(answers['additionnal_bar_width'])
underline_start_y = first_letter_bbox[3] + int(answers['gap'])
# Get the underline position
underline_start_x = first_letters_bbox[0] - int(answers['additionnal_bar_width'])
underline_start_y = first_letters_bbox[3] + int(answers['gap'])

# The end of the underline depends on the type
# If the type is 'all', the underline will go from the start of the first letter to the end of the text
# If the type is 'first_letter', the underline will go
# from the start of the first letter to the end of the first letter
if type == 'first_letter':
underline_end_x = int(answers['additionnal_bar_width']) + (first_letter_bbox[2])
else:
underline_end_x = int(int(answers['padding_x']) + text_width)
underline_end_x = int(answers['additionnal_bar_width']) + first_letters_bbox[2]

underline_end_y = underline_start_y + int(answers['bar_size'])
underline_end_y = underline_start_y + int(answers['bar_size'])

underline_start = (underline_start_x, underline_start_y)
underline_end = (underline_end_x, underline_end_y)
underline_start = (underline_start_x, underline_start_y)
underline_end = (underline_end_x, underline_end_y)

underline_pos = [underline_start, underline_end]
underline_pos = [underline_start, underline_end]

# Underline the first letter
draw.rectangle(underline_pos, fill=accent, width=answers['bar_size'])
# Underline the first letter
draw.rectangle(underline_pos, fill=accent, width=answers['bar_size'])

# Draw the text
draw.text(
Expand Down

0 comments on commit 9803e7b

Please sign in to comment.