Skip to content

Commit

Permalink
refactor: Support Chinese text logo in play data ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
dddddluo committed Jan 8, 2025
1 parent 0972ff6 commit 21290b0
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions bot/ranks_helper/ranks_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def __init__(self, embyname=None, weekly=False, backdrop=False):
else:
mask_path = os.path.join('bot', 'ranks_helper', "resource", "day_ranks_mask.png")
font_path = os.path.join('bot', 'ranks_helper', "resource", 'font', "PingFang Bold.ttf")
logo_font_path = os.path.join('bot', 'ranks_helper', 'resource', 'font', 'Provicali.otf')
# 随机调取背景
bg_list = os.listdir(bg_path)
bg_path = os.path.join(bg_path, random.choice(bg_list))
Expand All @@ -52,7 +51,7 @@ def __init__(self, embyname=None, weekly=False, backdrop=False):
self.font = ImageFont.truetype(font_path, 18)
self.font_small = ImageFont.truetype(font_path, 14)
self.font_count = ImageFont.truetype(font_path, 12)
self.font_logo = ImageFont.truetype(logo_font_path, 100)
self.font_logo = ImageFont.truetype(font_path, 60)
self.embyname = embyname
self.backdrop = backdrop

Expand Down Expand Up @@ -169,7 +168,7 @@ async def draw(self, movies=[], tvshows=[], draw_text=False):
# 绘制Logo名字
if self.embyname:
if self.backdrop:
draw_text_psd_style(text, (1470, 830), self.embyname, self.font_logo, 126)
draw_text_psd_style(text, (1900, 830), self.embyname, self.font_logo, 126, align='right')
else:
draw_text_psd_style(text, (90, 1100), self.embyname, self.font_logo, 126)

Expand Down Expand Up @@ -246,9 +245,9 @@ def test(self, movies=[], tvshows=[], show_count=False):
# 绘制Logo名字
if self.embyname:
if self.backdrop:
draw_text_psd_style(text, (1470, 830), self.embyname, self.font_logo, 126)
draw_text_psd_style(text, (1900, 830), self.embyname, self.font_logo, 126, align='right')
else:
draw_text_psd_style(text, (90, 1100), self.embyname, self.font_logo, 126)
draw_text_psd_style(text, (90, 1100), self.embyname, self.font_logo, 126, align='left')

@staticmethod
async def hb_test_draw(money: int, members: int, user_pic: bytes = None, first_name: str = None):
Expand Down Expand Up @@ -299,10 +298,10 @@ async def draw_cover_text(cover, first_name, money, members):
return cover


def draw_text_psd_style(draw, xy, text, font, tracking=0, leading=None, **kwargs):
def draw_text_psd_style(draw, xy, text, font, tracking=0, leading=None, align='left', **kwargs):
"""
usage: draw_text_psd_style(draw, (0, 0), "Test",
tracking=-0.1, leading=32, fill="Blue")
tracking=-0.1, leading=32, fill="Blue", align='left')
Leading is measured from the baseline of one line of text to the
baseline of the line above it. Baseline is the invisible line on which most
Expand All @@ -328,16 +327,24 @@ def stutter_chunk(lst, size, overlap=0, default=None):
lines = text.splitlines()
if leading is None:
leading = font.size * 1.2

for line in lines:
# 计算整行文本宽度
total_width = font.getlength(line) + (tracking / 1000) * font_size * (len(line) - 1)

# 如果是右对齐,调整起始 x 坐标
current_x = x
if align == 'right':
current_x = x - total_width

for a, b in stutter_chunk(line, 2, 1, ' '):
w = font.getlength(a + b) - font.getlength(b)
draw.text((x, y), a, font=font, **kwargs)
x += w + (tracking / 1000) * font_size
draw.text((current_x, y), a, font=font, **kwargs)
current_x += w + (tracking / 1000) * font_size
y += leading
x = xy[0]
#
#


# if __name__ == "__main__":
# draw = RanksDraw(embyname='SAKURA', weekly=True, backdrop=True)
# draw = RanksDraw(embyname='SAKURA欢迎你', weekly=True, backdrop=False)
# draw.test()
# draw.save()

0 comments on commit 21290b0

Please sign in to comment.