Skip to content

Commit

Permalink
add moar examples
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jul 19, 2024
1 parent 0e0d094 commit 178df8f
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 58 deletions.
Binary file modified docs/img/thanks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
582 changes: 558 additions & 24 deletions docs/unicode.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions examples/boost_utf_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using boost::locale::conv::utf_to_utf;
using boost::locale::conv::from_utf;

int main() {
setlocale(LC_ALL, "C.utf-8");
std::string s = "你好";
// UTF-8 转 UTF-32:
std::u32string s32 = utf_to_utf<char32_t>(s);
Expand Down
17 changes: 17 additions & 0 deletions examples/date_time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <ctime>
#include <iomanip>
#include <iostream>
#include <locale>

int main() {
time_t t = time(NULL);
tm *tm = localtime(&t);

auto locale_zh = std::locale("zh_CN.UTF-8");
std::cout.imbue(locale_zh);
std::cout << std::put_time(tm, "%c") << '\n';

auto locale_en = std::locale("en_US.UTF-8");
std::cout.imbue(locale_en);
std::cout << std::put_time(tm, "%c") << '\n';
}
31 changes: 31 additions & 0 deletions examples/locale_wctype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <fmt/format.h>
#include <boost/locale.hpp>

int main() {
fmt::println("默认: {}", iswpunct(L''));

setlocale(LC_ALL, "C");
fmt::println("C: {}", iswpunct(L''));

setlocale(LC_ALL, "C.UTF-8");
fmt::println("C.UTF-8: {}", iswpunct(L''));

setlocale(LC_ALL, "zh_CN.UTF-8");
fmt::println("zh_CN.UTF-8: {}", iswpunct(L''));

setlocale(LC_ALL, "zh_CN.GBK");
fmt::println("zh_CN.GBK: {}", iswpunct(L''));

setlocale(LC_ALL, "en_US.ISO-8859-1");
fmt::println("en_US.ISO-8859-1: {}", iswpunct(L''));

setlocale(LC_ALL, "POSIX");
fmt::println("POSIX: {}", iswpunct(L''));

setlocale(LC_ALL, "en_CA.ISO-8859-1");
fmt::println("en_CA: {}", iswalpha(L'é'));
setlocale(LC_ALL, "fr_CA.ISO-8859-1");
fmt::println("fr_CA: {}", iswalpha(L'é'));

return 0;
}
8 changes: 8 additions & 0 deletions examples/wcout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

int main() {
setlocale(LC_ALL, "");
std::wcout << L"我是 wcout!" << L'\n';
std::cout << "我是 cout!" << '\n';
return 0;
}
106 changes: 73 additions & 33 deletions misc/afdian.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

class User(namedtuple('User', ['name', 'avatar', 'all_sum_amount'])):
class User(namedtuple('User', ['name', 'avatar', 'all_sum_amount', 'remark'])):
pass

class Order(namedtuple('Order', ['remark'])):
pass

manual_sponsors = [
User('等疾风', 'https://i0.hdslb.com/bfs/face/b658b5ca52f41e53321d04f978be6784ca6f8687.jpg', '1000.00'),
User('只喝白开水', 'https://i2.hdslb.com/bfs/face/821b88a24c1319d1fb51b3854884e2f829855c75.jpg', '100.00'),
User('包乾', '', '26.90'),
User('柿柿如意', '', '20.00'),
User('Starry', '', '100.00'),
User('阿哲', '', '100.00'),
User('Eureka', '', '20.00'),
User('孙斌', '', '200.00'),
User('nullptr', 'https://i0.hdslb.com/bfs/face/effa1ec9bb0f5d09ed415da75129aca9d16092ac.jpg', '23.30'),
User('Fred Song', '', '25.00'),
User('**振', '', '20.00'),
User('等疾风', 'https://i0.hdslb.com/bfs/face/b658b5ca52f41e53321d04f978be6784ca6f8687.jpg', '1000.00', '小彭老师加油,希望给个赞助位'),
User('只喝白开水', 'https://i2.hdslb.com/bfs/face/821b88a24c1319d1fb51b3854884e2f829855c75.jpg', '100.00', '确实快了 30 倍[赞]'),
User('包乾', '', '26.90', ''),
User('柿柿如意', '', '20.00', '请小彭老师喝奶茶'),
User('Starry', '', '100.00', '小彭老师加油!'),
User('阿哲', '', '100.00', '从小彭老师视频里学到太多'),
User('Eureka', '', '20.00', '希望我能赚的多一点,之后发电也就多一点'),
User('孙斌', '', '200.00', '06班孙斌,望越来越好'),
User('nullptr', 'https://i0.hdslb.com/bfs/face/effa1ec9bb0f5d09ed415da75129aca9d16092ac.jpg', '23.30', '小彭老师千古,伟大无需多言'),
User('Fred Song', '', '25.00', '小彭老师早点休息'),
User('**振', '', '20.00', ''),
User('**伟', '', '20.00', ''),
User('**枫', '', '26.90', ''),
]

def afd_query(which, **params):
Expand All @@ -39,34 +44,58 @@ def afd_query(which, **params):
assert res['ec'] == 200, res
return res['data']

def afd_query_sponsors():
def afd_paged_query(which):
i = 1
res = []
while True:
page = afd_query('query-sponsor', page=i)
page = afd_query(which, page=i)
n = page['total_page']
for user in page['list']:
res.append(User(user['user']['name'], user['user']['avatar'], user['all_sum_amount']))
for item in page['list']:
yield item
if i >= n:
break
i += 1

def afd_query_orders():
order_lut = {}
for order in afd_paged_query('query-order'):
print(f'{order['user_id']}: {order['remark']}')
order_lut[order['user_id']] = Order(order['remark'])
return order_lut

def afd_query_sponsors():
res = []
order_lut = afd_query_orders()
for user in afd_paged_query('query-sponsor'):
user_id = user['user']['user_id']
last_order = order_lut.get(user_id, None)
if last_order:
remark = last_order.remark
else:
remark = ''
user_obj = User(user['user']['name'],
user['user']['avatar'],
user['all_sum_amount'],
remark)
res.append(user_obj)
return res

def afd_gen_thank_list():
sponsors = afd_query_sponsors()
sponsors += manual_sponsors
max_x = 30
max_y = 30
limit_y = 600
stride_x = 450
stride_y = 120
limit_y = 720
max_max_y = max_y
for user in sponsors:
max_y += 100
max_y += stride_y
if max_y + 10 >= limit_y:
max_max_y = max(max_max_y, max_y)
max_y = 30
max_x += 400
max_x += stride_x
max_max_y = max(max_max_y, max_y)
max_max_x = max_x + 400
max_max_x = max_x + stride_x
max_y += 10
img = Image.new('RGB', (max_max_x, max_max_y), color='#19242e')
x = 30
Expand All @@ -75,27 +104,38 @@ def afd_gen_thank_list():
for user in sponsors:
draw = ImageDraw.Draw(img)
font = ImageFont.truetype('/usr/share/fonts/noto-cjk/NotoSansCJK-Medium.ttc', size=20)
font_small = ImageFont.truetype('/usr/share/fonts/noto-cjk/NotoSerifCJK-Medium.ttc', size=13)
if user.avatar:
avatar = Image.open(BytesIO(requests.get(user.avatar).content))
elif os.path.exists(f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png'):
avatar = Image.open(f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png')
else:
this_dir = os.path.dirname(os.path.abspath(__file__))
avatar = Image.open(os.path.join(this_dir, '../docs/img/favicon.ico'))
avatar = avatar.resize((80, 80))
wx_path = f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png'
if os.path.exists(wx_path):
avatar = Image.open(wx_path)
else:
this_dir = os.path.dirname(os.path.abspath(__file__))
avatar = Image.open(os.path.join(this_dir, '../docs/img/favicon.ico'))
avatar = avatar.resize((90, 90))
img.paste(avatar, (x, y))
draw.text((x + 100, y), f'{user.name}', fill='white', font=font)
draw.text((x + 100, y + 30), f'¥{user.all_sum_amount}', fill='#aaaaaa', font=font)
print(f'{user.name}{user.all_sum_amount}')
remark = user.remark
if remark:
remark = remark.rstrip('。').replace('.。', ',')
draw.text((x + 100, y + 60), f'{remark}', fill='#779977', font=font_small)
print(f'{user.name}{user.all_sum_amount} {remark}')
total += float(user.all_sum_amount)
y += 100
y += stride_y
if y + 10 >= limit_y:
y = 30
x += 400
x += stride_x
print(total)
return img

img = afd_gen_thank_list()
file = 'docs/img/thanks.png'
img.save(file)
img.show()
def main():
img = afd_gen_thank_list()
file = 'docs/img/thanks.png'
img.save(file)
os.system(f'display {file}')

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion misc/export_to_pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console.log('Saving', url, 'to', pdfPath);
// totalPages – total pages in the document
headerHtml = `
<div style="font-size: 10px; padding-right: 1em; text-align: right; width: 100%;">
<span>${title}</span> <span class="pageNumber"></span> / <span class="totalPages"></span>
<span>${title}</span> <span class="pageNumber"></span> 页,共 <span class="totalPages"></span>
</div>`;

footerHtml = ` `;
Expand Down

0 comments on commit 178df8f

Please sign in to comment.