Pre-render the basic ASCII chars
This commit is contained in:
parent
412c5982e8
commit
cc1a277eeb
@ -8,6 +8,7 @@ import re
|
|||||||
import ctypes
|
import ctypes
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
from threading import Lock
|
||||||
|
|
||||||
from freetype import (
|
from freetype import (
|
||||||
Face, FT_LOAD_RENDER, FT_LOAD_TARGET_NORMAL, FT_LOAD_TARGET_LIGHT,
|
Face, FT_LOAD_RENDER, FT_LOAD_TARGET_NORMAL, FT_LOAD_TARGET_LIGHT,
|
||||||
@ -216,10 +217,9 @@ def create_cell_buffer(bitmap_char, src_start_row, dest_start_row, row_count, sr
|
|||||||
class GlyphCache:
|
class GlyphCache:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.char_map = {}
|
self.lock = Lock()
|
||||||
self.second_char_map = {}
|
self.clear()
|
||||||
self.data = ()
|
self.prepopulate()
|
||||||
self.width_map = {}
|
|
||||||
|
|
||||||
def render(self, text, bold=False, italic=False):
|
def render(self, text, bold=False, italic=False):
|
||||||
first, second = render_cell(text, bold, italic)
|
first, second = render_cell(text, bold, italic)
|
||||||
@ -229,20 +229,39 @@ class GlyphCache:
|
|||||||
self.second_char_map[text] = self.add_cell(second)
|
self.second_char_map[text] = self.add_cell(second)
|
||||||
|
|
||||||
def add_cell(self, data):
|
def add_cell(self, data):
|
||||||
i = len(self.data)
|
with self.lock:
|
||||||
ndata = ctypes.c_ubyte * (i + len(data))
|
i = len(self.texture_array)
|
||||||
if self.data:
|
self.texture_array.append(data)
|
||||||
ndata[:i] = self.data
|
self.to_sync.append(i)
|
||||||
ndata[i:] = data
|
return i
|
||||||
return i
|
|
||||||
|
def clear(self):
|
||||||
|
with self.lock:
|
||||||
|
self.char_map = {}
|
||||||
|
self.second_char_map = {}
|
||||||
|
self.texture_array = []
|
||||||
|
self.to_sync = []
|
||||||
|
self.width_map = {}
|
||||||
|
|
||||||
def width(self, text):
|
def width(self, text):
|
||||||
try:
|
try:
|
||||||
return self.width_map[text]
|
return self.width_map[text]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
self.render(text)
|
pass
|
||||||
|
self.render(text)
|
||||||
return self.width_map[text]
|
return self.width_map[text]
|
||||||
|
|
||||||
|
def prepopulate(self):
|
||||||
|
# Pre-render the basic ASCII chars in the current font
|
||||||
|
for i in range(32, 128):
|
||||||
|
self.render(chr(i))
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
with self.lock:
|
||||||
|
for f in self.to_sync:
|
||||||
|
yield f, self.texture_array[f]
|
||||||
|
self.to_sync = []
|
||||||
|
|
||||||
|
|
||||||
def join_cells(*cells):
|
def join_cells(*cells):
|
||||||
dstride = len(cells) * cell_width
|
dstride = len(cells) * cell_width
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user