summaryrefslogtreecommitdiff
path: root/src/fontpack.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-10-17 11:15:14 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-10-17 11:15:14 +0300
commit2e2b823bfb5d34d42c6b6c1b289193c854459b45 (patch)
tree345f37aa1b84d8dedb98ff260265b41495e10878 /src/fontpack.c
parent2f3987f5e54d95658f95c6991b0644bc15eedabf (diff)
Media and FontPacks (work in progress)
Saving this as the last point of progress. This direction is too complicated: Media needs to be a lot more sophisticated to allow dynamic and interactive media at the level of FontPacks. (A bit like Player handles audio playback.) This will be reverted. FontPack management will happen using an another method.
Diffstat (limited to 'src/fontpack.c')
-rw-r--r--src/fontpack.c69
1 files changed, 64 insertions, 5 deletions
diff --git a/src/fontpack.c b/src/fontpack.c
index b135ea43..9603bded 100644
--- a/src/fontpack.c
+++ b/src/fontpack.c
@@ -22,6 +22,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22 22
23#include "fontpack.h" 23#include "fontpack.h"
24#include "embedded.h" 24#include "embedded.h"
25#include "ui/window.h"
25#include "app.h" 26#include "app.h"
26 27
27#include <the_Foundation/archive.h> 28#include <the_Foundation/archive.h>
@@ -396,6 +397,24 @@ static iBool load_FontPack_(iFontPack *d, const iString *ini) {
396 return ok; 397 return ok;
397} 398}
398 399
400iBool detect_FontPack(const iBlock *data) {
401 iBool ok = iFalse;
402 iArchive *zip = new_Archive();
403 if (openData_Archive(zip, data)) {
404 iString ini;
405 initBlock_String(&ini, dataCStr_Archive(zip, "fontpack.ini"));
406 if (isUtf8_Rangecc(range_String(&ini))) {
407 /* Validate the TOML syntax without actually checking any values. */
408 iTomlParser *toml = new_TomlParser();
409 ok = parse_TomlParser(toml, &ini);
410 delete_TomlParser(toml);
411 }
412 deinit_String(&ini);
413 }
414 iRelease(zip);
415 return ok;
416}
417
399iBool loadArchive_FontPack(iFontPack *d, const iArchive *zip) { 418iBool loadArchive_FontPack(iFontPack *d, const iArchive *zip) {
400 d->archive = zip; 419 d->archive = zip;
401 iBool ok = iFalse; 420 iBool ok = iFalse;
@@ -467,16 +486,24 @@ static void sortSpecs_Fonts_(iFonts *d) {
467 clear_PtrArray(&d->specOrder); 486 clear_PtrArray(&d->specOrder);
468 iConstForEach(PtrArray, p, &d->packs) { 487 iConstForEach(PtrArray, p, &d->packs) {
469 const iFontPack *pack = p.ptr; 488 const iFontPack *pack = p.ptr;
470 iConstForEach(Array, i, &pack->fonts) { 489 if (!contains_StringSet(prefs_App()->disabledFontPacks, &pack->id)) {
471 pushBack_PtrArray(&d->specOrder, i.value); 490 iConstForEach(Array, i, &pack->fonts) {
491 pushBack_PtrArray(&d->specOrder, i.value);
492 }
472 } 493 }
473 } 494 }
474 sort_Array(&d->specOrder, cmpPriority_FontSpecPtr_); 495 sort_Array(&d->specOrder, cmpPriority_FontSpecPtr_);
475} 496}
476 497
498static const iString *userFontsDirectory_Fonts_(const iFonts *d) {
499 return collect_String(concatCStr_Path(&d->userDir, "fonts"));
500}
501
477void init_Fonts(const char *userDir) { 502void init_Fonts(const char *userDir) {
478 iFonts *d = &fonts_; 503 iFonts *d = &fonts_;
479 initCStr_String(&d->userDir, userDir); 504 initCStr_String(&d->userDir, userDir);
505 const iString *userFontsDir = userFontsDirectory_Fonts_(d);
506 makeDirs_Path(userFontsDir);
480 init_PtrArray(&d->packs); 507 init_PtrArray(&d->packs);
481 d->files = new_ObjectList(); 508 d->files = new_ObjectList();
482 init_PtrArray(&d->specOrder); 509 init_PtrArray(&d->specOrder);
@@ -496,7 +523,7 @@ void init_Fonts(const char *userDir) {
496 "./fonts", 523 "./fonts",
497 "../share/lagrange", /* Note: These must match CMakeLists.txt install destination */ 524 "../share/lagrange", /* Note: These must match CMakeLists.txt install destination */
498 "../../share/lagrange", 525 "../../share/lagrange",
499 concatPath_CStr(userDir, "fonts"), 526 cstr_String(userFontsDir),
500 userDir, 527 userDir,
501 }; 528 };
502 const iString *execDir = collectNewRange_String(dirName_Path(execPath_App())); 529 const iString *execDir = collectNewRange_String(dirName_Path(execPath_App()));
@@ -555,7 +582,6 @@ void init_Fonts(const char *userDir) {
555void deinit_Fonts(void) { 582void deinit_Fonts(void) {
556 iFonts *d = &fonts_; 583 iFonts *d = &fonts_;
557 unloadFonts_Fonts_(d); 584 unloadFonts_Fonts_(d);
558 //unloadFiles_Fonts_(d);
559 iAssert(isEmpty_ObjectList(d->files)); 585 iAssert(isEmpty_ObjectList(d->files));
560 deinit_PtrArray(&d->specOrder); 586 deinit_PtrArray(&d->specOrder);
561 deinit_PtrArray(&d->packs); 587 deinit_PtrArray(&d->packs);
@@ -617,7 +643,18 @@ const iString *infoPage_Fonts(void) {
617 return str; 643 return str;
618} 644}
619 645
620const iFontPack *findPack_Fonts(const iString *path) { 646const iFontPack *pack_Fonts(const char *packId) {
647 iFonts *d = &fonts_;
648 iConstForEach(PtrArray, i, &d->packs) {
649 const iFontPack *pack = i.ptr;
650 if (!cmp_String(&pack->id, packId)) {
651 return pack;
652 }
653 }
654 return NULL;
655}
656
657const iFontPack *packByPath_Fonts(const iString *path) {
621 iFonts *d = &fonts_; 658 iFonts *d = &fonts_;
622 iConstForEach(PtrArray, i, &d->packs) { 659 iConstForEach(PtrArray, i, &d->packs) {
623 const iFontPack *pack = i.ptr; 660 const iFontPack *pack = i.ptr;
@@ -628,6 +665,28 @@ const iFontPack *findPack_Fonts(const iString *path) {
628 return NULL; 665 return NULL;
629} 666}
630 667
668void reload_Fonts(void) {
669 iFonts *d = &fonts_;
670 iString *userDir = copy_String(&d->userDir);
671 deinit_Fonts(); /* `d->userDir` is freed */
672 init_Fonts(cstr_String(userDir));
673 resetFonts_App();
674 invalidate_Window(get_MainWindow());
675 delete_String(userDir);
676}
677
678void install_Fonts(const iString *fontId, const iBlock *data) {
679 iFonts *d = &fonts_;
680 iFile *f = new_File(collect_String(concatCStr_Path(
681 userFontsDirectory_Fonts_(d), format_CStr("%s.fontpack", cstr_String(fontId)))));
682 if (open_File(f, writeOnly_FileMode)) {
683 write_File(f, data);
684 }
685 iRelease(f);
686 /* Newly installed fontpacks may have a higher priority that overrides other fonts. */
687 reload_Fonts();
688}
689
631iBool preloadLocalFontpackForPreview_Fonts(iGmDocument *doc) { 690iBool preloadLocalFontpackForPreview_Fonts(iGmDocument *doc) {
632 iBool wasLoaded = iFalse; 691 iBool wasLoaded = iFalse;
633 for (size_t linkId = 1; ; linkId++) { 692 for (size_t linkId = 1; ; linkId++) {