diff options
Diffstat (limited to 'src/fontpack.c')
-rw-r--r-- | src/fontpack.c | 84 |
1 files changed, 70 insertions, 14 deletions
diff --git a/src/fontpack.c b/src/fontpack.c index 0ca0d486..a1f3b829 100644 --- a/src/fontpack.c +++ b/src/fontpack.c | |||
@@ -30,6 +30,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
30 | #include <the_Foundation/string.h> | 30 | #include <the_Foundation/string.h> |
31 | #include <the_Foundation/toml.h> | 31 | #include <the_Foundation/toml.h> |
32 | 32 | ||
33 | #include "embedded.h" | ||
34 | |||
33 | float scale_FontSize(enum iFontSize size) { | 35 | float scale_FontSize(enum iFontSize size) { |
34 | static const float sizes[max_FontSize] = { | 36 | static const float sizes[max_FontSize] = { |
35 | 1.000, /* UI sizes */ | 37 | 1.000, /* UI sizes */ |
@@ -154,7 +156,7 @@ iDeclareType(FontPack) | |||
154 | iDeclareTypeConstruction(FontPack) | 156 | iDeclareTypeConstruction(FontPack) |
155 | 157 | ||
156 | struct Impl_FontPack { | 158 | struct Impl_FontPack { |
157 | iArchive * archive; /* opened ZIP archive */ | 159 | const iArchive *archive; /* opened ZIP archive */ |
158 | iArray fonts; /* array of FontSpecs */ | 160 | iArray fonts; /* array of FontSpecs */ |
159 | iString * loadPath; | 161 | iString * loadPath; |
160 | iFontSpec *loadSpec; | 162 | iFontSpec *loadSpec; |
@@ -207,6 +209,24 @@ void handleIniTable_FontPack_(void *context, const iString *table, iBool isStart | |||
207 | } | 209 | } |
208 | } | 210 | } |
209 | 211 | ||
212 | static iBlock *readFile_FontPack_(const iFontPack *d, const iString *path) { | ||
213 | iBlock *data = NULL; | ||
214 | if (d->archive) { | ||
215 | /* Loading from a ZIP archive. */ | ||
216 | data = copy_Block(data_Archive(d->archive, path)); | ||
217 | } | ||
218 | else if (d->loadPath) { | ||
219 | /* Loading from a regular file. */ | ||
220 | iFile *srcFile = new_File(collect_String(concat_Path(d->loadPath, path))); | ||
221 | if (open_File(srcFile, readOnly_FileMode)) { | ||
222 | data = readAll_File(srcFile); | ||
223 | } | ||
224 | iRelease(srcFile); | ||
225 | return data; | ||
226 | } | ||
227 | return data; | ||
228 | } | ||
229 | |||
210 | void handleIniKeyValue_FontPack_(void *context, const iString *table, const iString *key, | 230 | void handleIniKeyValue_FontPack_(void *context, const iString *table, const iString *key, |
211 | const iTomlValue *value) { | 231 | const iTomlValue *value) { |
212 | iFontPack *d = context; | 232 | iFontPack *d = context; |
@@ -245,24 +265,41 @@ void handleIniKeyValue_FontPack_(void *context, const iString *table, const iStr | |||
245 | iForIndices(i, styles) { | 265 | iForIndices(i, styles) { |
246 | if (!cmp_String(key, styles[i]) && !d->loadSpec->styles[i]) { | 266 | if (!cmp_String(key, styles[i]) && !d->loadSpec->styles[i]) { |
247 | iFontFile *ff = NULL; | 267 | iFontFile *ff = NULL; |
248 | /* Loading from a regular file. */ | 268 | iBlock *data = readFile_FontPack_(d, value->value.string); |
249 | iFile *srcFile = new_File(collect_String(concat_Path(d->loadPath, | 269 | if (data) { |
250 | value->value.string))); | ||
251 | if (open_File(srcFile, readOnly_FileMode)) { | ||
252 | ff = new_FontFile(); | 270 | ff = new_FontFile(); |
253 | iBlock *data = readAll_File(srcFile); | ||
254 | load_FontFile_(ff, data); | 271 | load_FontFile_(ff, data); |
255 | delete_Block(data); | ||
256 | pushBack_PtrArray(&fonts_.files, ff); /* centralized ownership */ | 272 | pushBack_PtrArray(&fonts_.files, ff); /* centralized ownership */ |
257 | d->loadSpec->styles[i] = ff; | 273 | d->loadSpec->styles[i] = ff; |
274 | delete_Block(data); | ||
258 | } | 275 | } |
259 | iRelease(srcFile); | ||
260 | break; | 276 | break; |
261 | } | 277 | } |
262 | } | 278 | } |
263 | } | 279 | } |
264 | } | 280 | } |
265 | 281 | ||
282 | static iBool load_FontPack_(iFontPack *d, const iString *ini) { | ||
283 | iBeginCollect(); | ||
284 | iBool ok = iFalse; | ||
285 | // iFile *f = iClob(new_File(iniPath)); | ||
286 | //if (open_File(f, text_FileMode | readOnly_FileMode)) { | ||
287 | // d->loadPath = collect_String(newRange_String(dirName_Path(iniPath))); | ||
288 | // iString *src = collect_String(readString_File(f)); | ||
289 | iTomlParser *toml = collect_TomlParser(new_TomlParser()); | ||
290 | setHandlers_TomlParser(toml, handleIniTable_FontPack_, handleIniKeyValue_FontPack_, d); | ||
291 | if (parse_TomlParser(toml, ini)) { | ||
292 | ok = iTrue; | ||
293 | // fprintf(stderr, "[FontPack] error parsing %s\n", cstr_String(iniPath)); | ||
294 | } | ||
295 | iAssert(d->loadSpec == NULL); | ||
296 | // d->loadPath = NULL; | ||
297 | // } | ||
298 | iEndCollect(); | ||
299 | return ok; | ||
300 | } | ||
301 | |||
302 | #if 0 | ||
266 | iBool loadIniFile_FontPack(iFontPack *d, const iString *iniPath) { | 303 | iBool loadIniFile_FontPack(iFontPack *d, const iString *iniPath) { |
267 | iBeginCollect(); | 304 | iBeginCollect(); |
268 | iBool ok = iFalse; | 305 | iBool ok = iFalse; |
@@ -270,6 +307,7 @@ iBool loadIniFile_FontPack(iFontPack *d, const iString *iniPath) { | |||
270 | if (open_File(f, text_FileMode | readOnly_FileMode)) { | 307 | if (open_File(f, text_FileMode | readOnly_FileMode)) { |
271 | d->loadPath = collect_String(newRange_String(dirName_Path(iniPath))); | 308 | d->loadPath = collect_String(newRange_String(dirName_Path(iniPath))); |
272 | iString *src = collect_String(readString_File(f)); | 309 | iString *src = collect_String(readString_File(f)); |
310 | |||
273 | iTomlParser *ini = collect_TomlParser(new_TomlParser()); | 311 | iTomlParser *ini = collect_TomlParser(new_TomlParser()); |
274 | setHandlers_TomlParser(ini, handleIniTable_FontPack_, handleIniKeyValue_FontPack_, d); | 312 | setHandlers_TomlParser(ini, handleIniTable_FontPack_, handleIniKeyValue_FontPack_, d); |
275 | if (!parse_TomlParser(ini, src)) { | 313 | if (!parse_TomlParser(ini, src)) { |
@@ -282,6 +320,22 @@ iBool loadIniFile_FontPack(iFontPack *d, const iString *iniPath) { | |||
282 | iEndCollect(); | 320 | iEndCollect(); |
283 | return ok; | 321 | return ok; |
284 | } | 322 | } |
323 | #endif | ||
324 | |||
325 | iBool loadArchive_FontPack(iFontPack *d, const iArchive *zip) { | ||
326 | d->archive = zip; | ||
327 | iBool ok = iFalse; | ||
328 | const iBlock *iniData = dataCStr_Archive(zip, "fontpack.ini"); | ||
329 | if (iniData) { | ||
330 | iString ini; | ||
331 | initBlock_String(&ini, iniData); | ||
332 | if (load_FontPack_(d, &ini)) { | ||
333 | ok = iTrue; | ||
334 | } | ||
335 | deinit_String(&ini); | ||
336 | } | ||
337 | return ok; | ||
338 | } | ||
285 | 339 | ||
286 | /*----------------------------------------------------------------------------------------------*/ | 340 | /*----------------------------------------------------------------------------------------------*/ |
287 | 341 | ||
@@ -323,13 +377,15 @@ void init_Fonts(const char *userDir) { | |||
323 | init_PtrArray(&d->packs); | 377 | init_PtrArray(&d->packs); |
324 | init_PtrArray(&d->files); | 378 | init_PtrArray(&d->files); |
325 | init_PtrArray(&d->specOrder); | 379 | init_PtrArray(&d->specOrder); |
326 | /* Load the required fonts. */ | 380 | /* Load the required fonts. */ { |
327 | iFontPack *pack = new_FontPack(); | 381 | iFontPack *pack = new_FontPack(); |
328 | /* TODO: put default.fontpack in resources.lgr as a binary blob (uncompressed) */ | 382 | iArchive *defaultPack = new_Archive(); |
383 | openData_Archive(defaultPack, &fontpackDefault_Embedded); | ||
384 | loadArchive_FontPack(pack, defaultPack); | ||
385 | iRelease(defaultPack); | ||
386 | pushBack_PtrArray(&d->packs, pack); | ||
387 | } | ||
329 | /* TODO: find and load .fontpack files in known locations */ | 388 | /* TODO: find and load .fontpack files in known locations */ |
330 | loadIniFile_FontPack(pack, collectNewCStr_String("/Users/jaakko/src/lagrange/" | ||
331 | "res/fonts/fontpack.ini")); | ||
332 | pushBack_PtrArray(&d->packs, pack); | ||
333 | sortSpecs_Fonts_(d); | 389 | sortSpecs_Fonts_(d); |
334 | } | 390 | } |
335 | 391 | ||