-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add twitter status update on puzzle created
commit 913aaa8 Author: heyrict <xiezh0831@126.com> Date: Tue Jun 12 10:39:57 2018 +0800 add bottom border commit 9d8cf70 Author: heyrict <xiezh0831@126.com> Date: Tue Jun 12 10:16:16 2018 +0800 Drawing with canvas commit 3c8ef3d Author: heyrict <xiezh0831@126.com> Date: Mon Jun 11 08:42:29 2018 +0800 Fix rendering disorder commit a914832 Author: heyrict <xiezh0831@126.com> Date: Mon Jun 11 08:20:23 2018 +0800 Add textify markdown filter commit c865032 Author: heyrict <xiezh0831@126.com> Date: Sun Jun 10 21:04:12 2018 +0800 Add imaging lib
- Loading branch information
Showing
8 changed files
with
247 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
setup: | ||
# Install dependencies (needs sudo priviledge) | ||
apt install libfreetype6-dev libjpeg62-dev fonts-noto-cjk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .puzzle_rendering import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import os | ||
import re | ||
|
||
import markdown | ||
from bs4 import BeautifulSoup | ||
from PIL import Image, ImageDraw, ImageFont | ||
|
||
from .wcstring import wcstr | ||
|
||
BASE_DIR = os.path.split(os.path.abspath(__file__))[0] | ||
FONT_PATH = "NotoSansCJK-Bold.ttc" | ||
CANVAS_WIDTH = 600 | ||
TITLE_FONTSIZE = 18 | ||
TITLE_SPLIT = 63 | ||
CONTENT_FONTSIZE = 16 | ||
CONTENT_SPLIT = 72 | ||
LINE_HEIGHT = 25 | ||
OUTPUT_IMAGE_NAME = '/tmp/puzzle_render_output.png' | ||
APPENDS = 'ウミガメのスープ出題サイトCindy:www.cindythink.com' | ||
APPENDS_INDENT = 200 | ||
APPENDS_FONTSIZE = 14 | ||
|
||
|
||
def _split_lines(text, stop): | ||
output = [] | ||
text = re.split(r'[\r\n]+', text) | ||
for part in text: | ||
if not part: | ||
continue | ||
part = wcstr(part) | ||
for i in range(0, len(part), stop): | ||
output.append(part[i:i + stop]) | ||
|
||
return output | ||
|
||
|
||
def render(title, | ||
content, | ||
appends=APPENDS, | ||
canvas_width=CANVAS_WIDTH, | ||
font_path=FONT_PATH, | ||
title_fontsize=TITLE_FONTSIZE, | ||
title_split=TITLE_SPLIT, | ||
content_fontsize=CONTENT_FONTSIZE, | ||
content_split=CONTENT_SPLIT, | ||
appends_indent=APPENDS_INDENT, | ||
appends_fontsize=APPENDS_FONTSIZE, | ||
line_height=LINE_HEIGHT, | ||
output_image_name=OUTPUT_IMAGE_NAME): | ||
title = _split_lines(title, title_split) | ||
content = _split_lines(content, content_split) | ||
hTitle = len(title) * line_height + line_height // 2 | ||
hContent = len(content) * line_height + line_height // 2 | ||
hAppends = int(line_height * 1.2) | ||
img = Image.new('RGB', (canvas_width, hTitle + hContent + hAppends), "#fcf4dc") | ||
draw = ImageDraw.Draw(img) | ||
title_font = ImageFont.truetype(font_path, title_fontsize) | ||
content_font = ImageFont.truetype(font_path, content_fontsize) | ||
appends_font = ImageFont.truetype(font_path, appends_fontsize) | ||
|
||
# Drawing border | ||
draw.rectangle((0, 0, 2, hTitle + hContent), fill="#c6aa4b") | ||
draw.rectangle( | ||
(canvas_width - 3, 0, canvas_width - 1, hTitle + hContent), | ||
fill="#c6aa4b") | ||
draw.rectangle((0, hTitle + hContent, canvas_width, hTitle + hContent + 2), fill="#c6aa4b") | ||
|
||
# Drawing Title background | ||
draw.rectangle((0, 0, canvas_width, hTitle), fill="#c6aa4b") | ||
|
||
for i, txt in enumerate(title): | ||
draw.text( | ||
(5, i * line_height + 5), txt, font=title_font, fill="#fcf4dc") | ||
|
||
for i, txt in enumerate(content): | ||
draw.text( | ||
(5, i * line_height + hTitle + 5), | ||
txt, | ||
font=content_font, | ||
fill="#c6aa4b") | ||
|
||
draw.text((appends_indent, hTitle + hContent + 5), appends, font=appends_font, fill="#888") | ||
|
||
img.save(output_image_name) | ||
return output_image_name | ||
|
||
|
||
def textify(md): | ||
html = markdown.markdown(md, [ | ||
'markdown.extensions.extra', | ||
'markdown.extensions.nl2br', | ||
'markdown.extensions.tables' | ||
]) # yapf: disable | ||
soup = BeautifulSoup(html, 'html.parser') | ||
return soup.get_text() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Pillow>3.0 | ||
beautifulsoup4==4.5.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class wcstr(str): | ||
def __new__(*args, **kwargs): | ||
return str.__new__(*args, **kwargs) | ||
|
||
def __init__(self, *args, **kwargs): | ||
self._update() | ||
|
||
def _update(self): | ||
self.bitindex = [] | ||
for i, j in zip(str(self), range(len(str(self)))): | ||
iwidth = 1 if len(i.encode('utf8')) <= 2 else 2 | ||
self.bitindex += [j] * iwidth | ||
|
||
def __len__(self): | ||
return len(self.bitindex) | ||
|
||
def __getitem__(self, y): | ||
if type(y) == int: | ||
return wcstr(super(wcstr, self).__getitem__(self.bitindex[y])) | ||
elif type(y) == slice: | ||
start = self.bitindex[y.start] if y.start else None | ||
if y.stop and y.stop < len(self.bitindex): | ||
stop = self.bitindex[y.stop] if y.stop else None | ||
else: | ||
stop = None | ||
step = y.step | ||
return wcstr( | ||
super(wcstr, self).__getitem__(slice(start, stop, step))) | ||
else: | ||
return | ||
|
||
def dupstr(self): | ||
# return a duplicated string with every element | ||
# indicating one-width character | ||
return ''.join([self[i] for i in range(len(self))]) | ||
|
||
# alias for other str methods | ||
|
||
def __add__(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).__add__(*args, **kwargs)) | ||
|
||
def __mul__(self, value): | ||
return wcstr(super(wcstr, self).__mul__(value)) | ||
|
||
def __rmul__(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).__rmul__(*args, **kwargs)) | ||
|
||
def __format__(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).__format__(*args, **kwargs)) | ||
|
||
def center(self, width, fillchar=' '): | ||
filllen = (width - len(self)) // 2 | ||
return wcstr(fillchar * filllen + self + fillchar * | ||
(width - len(self) - filllen)) | ||
|
||
#return super(wcstr, self).center(width - len(self) + | ||
# len(str(self))) | ||
|
||
def casefold(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).casefold(*args, **kwargs)) | ||
|
||
def capitalize(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).capitalize(*args, **kwargs)) | ||
|
||
def expandtabs(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).expandtabs(*args, **kwargs)) | ||
|
||
def format(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).format(*args, **kwargs)) | ||
|
||
def format_map(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).format_map(*args, **kwargs)) | ||
|
||
def join(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).join(*args, **kwargs)) | ||
|
||
def ljust(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).ljust(*args, **kwargs)) | ||
|
||
def lower(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).lower(*args, **kwargs)) | ||
|
||
def lstrip(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).lstrip(*args, **kwargs)) | ||
|
||
def replace(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).replace(*args, **kwargs)) | ||
|
||
def rjust(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).rjust(*args, **kwargs)) | ||
|
||
def rstrip(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).rstrip(*args, **kwargs)) | ||
|
||
def strip(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).strip(*args, **kwargs)) | ||
|
||
def swapcase(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).swapcase(*args, **kwargs)) | ||
|
||
def title(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).title(*args, **kwargs)) | ||
|
||
def translate(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).translate(*args, **kwargs)) | ||
|
||
def upper(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).upper(*args, **kwargs)) | ||
|
||
def zfill(self, *args, **kwargs): | ||
return wcstr(super(wcstr, self).zfill(*args, **kwargs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters