103 lines
2.8 KiB
Python
Raw Normal View History

2024-01-08 23:37:00 +08:00
import json
2024-04-02 15:36:52 +08:00
import os
2024-05-29 21:30:28 +08:00
from qtsymbols import *
2024-04-02 15:36:52 +08:00
2024-01-08 23:37:00 +08:00
class CharIconPainter:
2024-04-02 15:36:52 +08:00
2024-08-01 17:19:53 +08:00
def paint(self, iconic, painter, rect, mode, state, char, color):
2024-04-02 15:36:52 +08:00
painter.save()
qcolor = QColor(color)
2024-01-08 23:37:00 +08:00
painter.setPen(qcolor)
2024-04-02 15:36:52 +08:00
draw_size = round(0.875 * rect.height())
painter.setFont(iconic.font(draw_size))
2024-05-29 21:30:28 +08:00
painter.drawText(
rect,
int(Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignVCenter),
char,
)
2024-01-08 23:37:00 +08:00
painter.restore()
2024-04-02 15:36:52 +08:00
2024-01-08 23:37:00 +08:00
class CharIconEngine(QIconEngine):
2024-04-02 15:36:52 +08:00
def __init__(self, iconic, painter, char, color):
2024-01-08 23:37:00 +08:00
super().__init__()
self.iconic = iconic
self.painter = painter
2024-04-02 15:36:52 +08:00
self.char = char
self.color = color
2024-01-08 23:37:00 +08:00
2024-08-01 17:19:53 +08:00
def paint(self, painter, rect, mode, state):
self.painter.paint(
self.iconic, painter, rect, mode, state, self.char, self.color
)
2024-01-08 23:37:00 +08:00
def pixmap(self, size, mode, state):
pm = QPixmap(size)
2024-05-29 21:30:28 +08:00
pm.fill(Qt.GlobalColor.transparent)
2024-08-01 17:19:53 +08:00
self.paint(QPainter(pm), QRect(QPoint(0, 0), size), mode, state)
2024-01-08 23:37:00 +08:00
return pm
2024-04-02 15:36:52 +08:00
class IconicFont(QObject):
def __init__(self, ttf_filename, charmap_filename):
2024-01-08 23:37:00 +08:00
super().__init__()
2024-04-02 15:36:52 +08:00
self.painter = CharIconPainter()
2024-01-08 23:37:00 +08:00
self.charmap = {}
self.icon_cache = {}
self.load_font(ttf_filename, charmap_filename)
2024-04-02 15:36:52 +08:00
def load_font(self, ttf_filename, charmap_filename):
directory = "./files/fonts"
2024-01-08 23:37:00 +08:00
2024-04-02 15:36:52 +08:00
with open(os.path.join(directory, ttf_filename), "rb") as font_data:
2024-01-08 23:37:00 +08:00
id_ = QFontDatabase.addApplicationFontFromData(QByteArray(font_data.read()))
loadedFontFamilies = QFontDatabase.applicationFontFamilies(id_)
2024-04-02 15:36:52 +08:00
2024-01-08 23:37:00 +08:00
self.fontname = loadedFontFamilies[0]
2024-04-02 15:36:52 +08:00
with open(os.path.join(directory, charmap_filename), "r") as codes:
self.charmap = json.load(codes)
def icon(self, name, color):
cache_key = "{}{}".format(name, color)
2024-01-08 23:37:00 +08:00
if cache_key not in self.icon_cache:
2024-04-02 15:36:52 +08:00
char = chr(int((self.charmap[name[3:]]), 16))
self.icon_cache[cache_key] = self._icon_by_painter(
self.painter, char, color
)
return self.icon_cache[cache_key]
def font(self, size):
2024-01-08 23:37:00 +08:00
font = QFont()
font.setFamily(self.fontname)
2024-04-02 15:36:52 +08:00
font.setPixelSize(round(size))
2024-01-08 23:37:00 +08:00
return font
2024-04-02 15:36:52 +08:00
def _icon_by_painter(self, painter, char, color):
engine = CharIconEngine(self, painter, char, color)
2024-01-08 23:37:00 +08:00
return QIcon(engine)
2024-04-02 15:36:52 +08:00
_resource = {"iconic": None}
def _instance():
if _resource["iconic"] is None:
_resource["iconic"] = IconicFont(
"fontawesome4.7-webfont.ttf", "fontawesome4.7-webfont-charmap.json"
)
return _resource["iconic"]
def icon(name, color="#000000"):
return _instance().icon(name, color)