summaryrefslogtreecommitdiff
path: root/src/fontpack.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-10-18 08:08:58 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-10-18 08:08:58 +0300
commit917b425c7ed9dbcc6a029260af0b820dd132e35c (patch)
tree0c74d9e0578b87815801bedc62721ab8faf70aff /src/fontpack.c
parent0538ae3ca02b249c784757920e3731efa8175d53 (diff)
FontPack: Improved memory management
Diffstat (limited to 'src/fontpack.c')
-rw-r--r--src/fontpack.c41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/fontpack.c b/src/fontpack.c
index 9b165051..7ed64113 100644
--- a/src/fontpack.c
+++ b/src/fontpack.c
@@ -202,7 +202,7 @@ struct Impl_FontPack {
202 int version; 202 int version;
203 iBool isStandalone; 203 iBool isStandalone;
204 iBool isReadOnly; 204 iBool isReadOnly;
205 iArray fonts; /* array of FontSpecs */ 205 iPtrArray fonts; /* array of FontSpecs */
206 const iArchive *archive; /* opened ZIP archive */ 206 const iArchive *archive; /* opened ZIP archive */
207 iString * loadPath; 207 iString * loadPath;
208 iFontSpec * loadSpec; 208 iFontSpec * loadSpec;
@@ -215,7 +215,7 @@ void init_FontPack(iFontPack *d) {
215 d->version = 0; 215 d->version = 0;
216 d->isStandalone = iFalse; 216 d->isStandalone = iFalse;
217 d->isReadOnly = iFalse; 217 d->isReadOnly = iFalse;
218 init_Array(&d->fonts, sizeof(iFontSpec)); 218 init_PtrArray(&d->fonts);
219 d->archive = NULL; 219 d->archive = NULL;
220 d->loadSpec = NULL; 220 d->loadSpec = NULL;
221 d->loadPath = NULL; 221 d->loadPath = NULL;
@@ -225,10 +225,10 @@ void deinit_FontPack(iFontPack *d) {
225 iAssert(d->archive == NULL); 225 iAssert(d->archive == NULL);
226 iAssert(d->loadSpec == NULL); 226 iAssert(d->loadSpec == NULL);
227 delete_String(d->loadPath); 227 delete_String(d->loadPath);
228 iForEach(Array, i, &d->fonts) { 228 iForEach(PtrArray, i, &d->fonts) {
229 deinit_FontSpec(i.value); 229 delete_FontSpec(i.ptr);
230 } 230 }
231 deinit_Array(&d->fonts); 231 deinit_PtrArray(&d->fonts);
232 deinit_String(&d->id); 232 deinit_String(&d->id);
233 releaseUnusedFiles_Fonts_(&fonts_); 233 releaseUnusedFiles_Fonts_(&fonts_);
234} 234}
@@ -243,11 +243,7 @@ const iString *loadPath_FontPack(const iFontPack *d) {
243 243
244const iPtrArray *listSpecs_FontPack(const iFontPack *d) { 244const iPtrArray *listSpecs_FontPack(const iFontPack *d) {
245 if (!d) return NULL; 245 if (!d) return NULL;
246 iPtrArray *list = collectNew_PtrArray(); 246 return &d->fonts;
247 iConstForEach(Array, i, &d->fonts) {
248 pushBack_PtrArray(list, i.value);
249 }
250 return list;
251} 247}
252 248
253void handleIniTable_FontPack_(void *context, const iString *table, iBool isStart) { 249void handleIniTable_FontPack_(void *context, const iString *table, iBool isStart) {
@@ -282,7 +278,7 @@ void handleIniTable_FontPack_(void *context, const iString *table, iBool isStart
282 } 278 }
283 } 279 }
284 } 280 }
285 pushBack_Array(&d->fonts, d->loadSpec); 281 pushBack_PtrArray(&d->fonts, d->loadSpec);
286 d->loadSpec = NULL; 282 d->loadSpec = NULL;
287 } 283 }
288} 284}
@@ -300,7 +296,6 @@ static iBlock *readFile_FontPack_(const iFontPack *d, const iString *path) {
300 data = readAll_File(srcFile); 296 data = readAll_File(srcFile);
301 } 297 }
302 iRelease(srcFile); 298 iRelease(srcFile);
303 return data;
304 } 299 }
305 return data; 300 return data;
306} 301}
@@ -402,12 +397,14 @@ static iBool load_FontPack_(iFontPack *d, const iString *ini) {
402 return ok; 397 return ok;
403} 398}
404 399
400static const char *fontpackIniEntryPath_ = "fontpack.ini";
401
405iBool detect_FontPack(const iBlock *data) { 402iBool detect_FontPack(const iBlock *data) {
406 iBool ok = iFalse; 403 iBool ok = iFalse;
407 iArchive *zip = new_Archive(); 404 iArchive *zip = new_Archive();
408 if (openData_Archive(zip, data)) { 405 if (openData_Archive(zip, data) && entryCStr_Archive(zip, fontpackIniEntryPath_)) {
409 iString ini; 406 iString ini;
410 initBlock_String(&ini, dataCStr_Archive(zip, "fontpack.ini")); 407 initBlock_String(&ini, dataCStr_Archive(zip, fontpackIniEntryPath_));
411 if (isUtf8_Rangecc(range_String(&ini))) { 408 if (isUtf8_Rangecc(range_String(&ini))) {
412 /* Validate the TOML syntax without actually checking any values. */ 409 /* Validate the TOML syntax without actually checking any values. */
413 iTomlParser *toml = new_TomlParser(); 410 iTomlParser *toml = new_TomlParser();
@@ -423,7 +420,7 @@ iBool detect_FontPack(const iBlock *data) {
423iBool loadArchive_FontPack(iFontPack *d, const iArchive *zip) { 420iBool loadArchive_FontPack(iFontPack *d, const iArchive *zip) {
424 d->archive = zip; 421 d->archive = zip;
425 iBool ok = iFalse; 422 iBool ok = iFalse;
426 const iBlock *iniData = dataCStr_Archive(zip, "fontpack.ini"); 423 const iBlock *iniData = dataCStr_Archive(zip, fontpackIniEntryPath_);
427 if (iniData) { 424 if (iniData) {
428 iString ini; 425 iString ini;
429 initBlock_String(&ini, iniData); 426 initBlock_String(&ini, iniData);
@@ -506,8 +503,8 @@ static void sortSpecs_Fonts_(iFonts *d) {
506 iConstForEach(PtrArray, p, &d->packs) { 503 iConstForEach(PtrArray, p, &d->packs) {
507 const iFontPack *pack = p.ptr; 504 const iFontPack *pack = p.ptr;
508 if (!isDisabled_FontPack(pack)) { 505 if (!isDisabled_FontPack(pack)) {
509 iConstForEach(Array, i, &pack->fonts) { 506 iConstForEach(PtrArray, i, &pack->fonts) {
510 pushBack_PtrArray(&d->specOrder, i.value); 507 pushBack_PtrArray(&d->specOrder, i.ptr);
511 } 508 }
512 } 509 }
513 } 510 }
@@ -547,7 +544,7 @@ void init_Fonts(const char *userDir) {
547 }; 544 };
548 const iString *execDir = collectNewRange_String(dirName_Path(execPath_App())); 545 const iString *execDir = collectNewRange_String(dirName_Path(execPath_App()));
549 iForIndices(i, locations) { 546 iForIndices(i, locations) {
550 const iString *dir = concatCStr_Path(execDir, locations[i]); 547 const iString *dir = collect_String(concatCStr_Path(execDir, locations[i]));
551 iForEach(DirFileInfo, entry, iClob(new_DirFileInfo(dir))) { 548 iForEach(DirFileInfo, entry, iClob(new_DirFileInfo(dir))) {
552 const iString *entryPath = path_FileInfo(entry.value); 549 const iString *entryPath = path_FileInfo(entry.value);
553 if (endsWithCase_String(entryPath, ".fontpack")) { 550 if (endsWithCase_String(entryPath, ".fontpack")) {
@@ -582,7 +579,7 @@ void init_Fonts(const char *userDir) {
582 if (open_File(f, text_FileMode | readOnly_FileMode)) { 579 if (open_File(f, text_FileMode | readOnly_FileMode)) {
583 const iString *src = collect_String(readString_File(f)); 580 const iString *src = collect_String(readString_File(f));
584 iFontPack *pack = new_FontPack(); 581 iFontPack *pack = new_FontPack();
585 pack->loadPath = copy_String(userIni); 582 pack->loadPath = copy_String(userIni); /* no pack ID */
586 if (load_FontPack_(pack, src)) { 583 if (load_FontPack_(pack, src)) {
587 pushBack_PtrArray(&d->packs, pack); 584 pushBack_PtrArray(&d->packs, pack);
588 } 585 }
@@ -711,6 +708,12 @@ const iArray *actions_FontPack(const iFontPack *d) {
711 format_Lang(add_Icon " ${fontpack.install}", fpId), 708 format_Lang(add_Icon " ${fontpack.install}", fpId),
712 SDLK_RETURN, 0, "fontpack.install" 709 SDLK_RETURN, 0, "fontpack.install"
713 }); 710 });
711 pushBack_Array(items, &(iMenuItem){
712 download_Icon " " saveToDownloads_Label,
713 0,
714 0,
715 "document.save"
716 });
714 } 717 }
715 return collect_Array(items); 718 return collect_Array(items);
716} 719}