Skip to content

Commit fc8d311

Browse files
committed
Use regular zip for font compression, to reduce startup performance impact
1 parent c107d8b commit fc8d311

File tree

2 files changed

+41
-41
lines changed

2 files changed

+41
-41
lines changed

Resources/Scripts/package_resources.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import platform
77
import lzma
8+
import zipfile
89
import tarfile
910

1011
import convert_merda
@@ -322,12 +323,13 @@ def generate_binary_data(output_dir, file_list):
322323
changeWorkingDir("../")
323324

324325
makeArchive("Filesystem", "./", "./plugdata_version")
325-
with open(project_root + "/Resources/Fonts/InterUnicode.ttf", 'rb') as f_in:
326-
with lzma.open(output_dir + "/InterUnicode.ttf.xz", 'wb', preset=5) as f_out:
327-
shutil.copyfileobj(f_in, f_out)
326+
327+
with zipfile.ZipFile(output_dir + "/InterUnicode.ttf.zip", 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
328+
zipf.write(project_root + "/Resources/Fonts/InterUnicode.ttf", arcname="InterUnicode.ttf")
329+
328330
removeDir(output_dir + "/plugdata_version")
329331

330-
splitFile(output_dir + "/InterUnicode.ttf.xz", output_dir + "/InterUnicode_%i", 3)
332+
splitFile(output_dir + "/InterUnicode.ttf.zip", output_dir + "/InterUnicode_%i", 3)
331333

332334
splitFile("./Filesystem", output_dir + "/Filesystem_%i", 8)
333335
removeFile("./Filesystem")

Source/Utility/Fonts.h

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,50 @@ enum FontStyle {
1919
struct Fonts {
2020
Fonts()
2121
{
22-
Typeface::setTypefaceCacheSize(7);
23-
24-
// Our unicode font is too big, the compiler will run out of memory
25-
// To prevent this, we split the BinaryData into multiple files, and add them back together here
26-
HeapArray<uint8_t> interUnicodeXz;
27-
interUnicodeXz.reserve(7 * 1024 * 1024);
22+
HeapArray<uint8_t> interUnicodeZip;
23+
interUnicodeZip.reserve(7 * 1024 * 1024);
2824
int i = 0;
29-
while (true) {
30-
int size;
31-
auto* resource = BinaryData::getNamedResource((String("InterUnicode_") + String(i)).toRawUTF8(), size);
32-
33-
if (!resource) {
25+
while (true)
26+
{
27+
int size = 0;
28+
auto* resource = BinaryData::getNamedResource(("InterUnicode_" + String(i)).toRawUTF8(), size);
29+
if (!resource)
3430
break;
35-
}
36-
37-
interUnicodeXz.insert(interUnicodeXz.end(), resource, resource + size);
38-
i++;
31+
interUnicodeZip.insert(interUnicodeZip.end(), resource, resource + size);
32+
++i;
3933
}
40-
HeapArray<uint8_t> interUnicode;
41-
interUnicode.reserve(17 * 1024 * 1024); // Reserve 17mb to prevent more allocations
42-
{
43-
lzma_stream strm = LZMA_STREAM_INIT;
44-
if (lzma_stream_decoder(&strm, UINT64_MAX, 0) != LZMA_OK)
45-
return; // TODO: handle failure!!
4634

47-
strm.next_in = reinterpret_cast<const uint8_t*>(interUnicodeXz.data());
48-
strm.avail_in = interUnicodeXz.size();
35+
// Create a MemoryInputStream from the combined ZIP data buffer
36+
MemoryInputStream zipInputStream(interUnicodeZip.data(), interUnicodeZip.size(), false);
4937

50-
uint8_t buffer[8192];
51-
lzma_ret ret;
38+
// Open the ZIP archive from memory
39+
ZipFile zipFile(zipInputStream);
5240

53-
do {
54-
strm.next_out = buffer;
55-
strm.avail_out = sizeof(buffer);
41+
HeapArray<uint8_t> interUnicode;
42+
interUnicode.reserve(17 * 1024 * 1024); // reserve enough memory for decompressed font
5643

57-
ret = lzma_code(&strm, LZMA_FINISH);
58-
size_t written = sizeof(buffer) - strm.avail_out;
59-
interUnicode.insert(interUnicode.end(), buffer, buffer + written);
44+
auto numEntries = zipFile.getNumEntries();
45+
if (numEntries == 0)
46+
{
47+
// TODO: handle error
48+
return;
49+
}
6050

61-
if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
62-
lzma_end(&strm);
63-
return;
64-
}
65-
} while (ret != LZMA_STREAM_END);
51+
auto fileEntry = zipFile.getEntry(numEntries - 1); // or use 0 if you want first entry
52+
53+
// Create a InputStream for the file entry
54+
std::unique_ptr<InputStream> fileStream(zipFile.createStreamForEntry(*fileEntry));
6655

67-
lzma_end(&strm);
56+
// Read the decompressed font data into interUnicode array
57+
const int bufferSize = 8192;
58+
char buffer[bufferSize];
59+
60+
while (!fileStream->isExhausted())
61+
{
62+
auto bytesRead = fileStream->read(buffer, bufferSize);
63+
if (bytesRead <= 0)
64+
break;
65+
interUnicode.insert(interUnicode.end(), (const uint8_t*)buffer, (const uint8_t*)buffer + bytesRead);
6866
}
6967

7068
// Initialise typefaces

0 commit comments

Comments
 (0)