HelenOS sources
This source file includes following definitions.
- gfx_typeface_create
- gfx_typeface_destroy
- gfx_typeface_first_font
- gfx_typeface_next_font
- gfx_typeface_open
- gfx_typeface_loadin
- gfx_typeface_save
#include <adt/list.h>
#include <assert.h>
#include <errno.h>
#include <gfx/bitmap.h>
#include <gfx/font.h>
#include <gfx/glyph.h>
#include <gfx/typeface.h>
#include <mem.h>
#include <riff/chunk.h>
#include <stdlib.h>
#include "../private/font.h"
#include "../private/glyph.h"
#include "../private/tpf_file.h"
#include "../private/typeface.h"
errno_t gfx_typeface_create(gfx_context_t *gc, gfx_typeface_t **rtface)
{
gfx_typeface_t *tface;
tface = calloc(1, sizeof(gfx_typeface_t));
if (tface == NULL)
return ENOMEM;
tface->gc = gc;
list_initialize(&tface->fonts);
*rtface = tface;
return EOK;
}
void gfx_typeface_destroy(gfx_typeface_t *tface)
{
gfx_font_info_t *finfo;
if (tface->riffr != NULL)
(void) riff_rclose(tface->riffr);
finfo = gfx_typeface_first_font(tface);
while (finfo != NULL) {
if (finfo->font != NULL)
gfx_font_close(finfo->font);
list_remove(&finfo->lfonts);
free(finfo);
finfo = gfx_typeface_first_font(tface);
}
free(tface);
}
gfx_font_info_t *gfx_typeface_first_font(gfx_typeface_t *tface)
{
link_t *link;
link = list_first(&tface->fonts);
if (link == NULL)
return NULL;
return list_get_instance(link, gfx_font_info_t, lfonts);
}
gfx_font_info_t *gfx_typeface_next_font(gfx_font_info_t *cur)
{
link_t *link;
link = list_next(&cur->lfonts, &cur->typeface->fonts);
if (link == NULL)
return NULL;
return list_get_instance(link, gfx_font_info_t, lfonts);
}
errno_t gfx_typeface_open(gfx_context_t *gc, const char *fname,
gfx_typeface_t **rtface)
{
riffr_t *riffr = NULL;
gfx_typeface_t *tface = NULL;
errno_t rc;
riff_rchunk_t riffck;
uint32_t format;
rc = gfx_typeface_create(gc, &tface);
if (rc != EOK)
goto error;
rc = riff_ropen(fname, &riffck, &riffr);
if (rc != EOK)
goto error;
rc = riff_read_uint32(&riffck, &format);
if (rc != EOK)
goto error;
if (format != FORM_TPFC) {
rc = ENOTSUP;
goto error;
}
while (true) {
rc = gfx_font_info_load(tface, &riffck);
if (rc == ENOENT)
break;
if (rc != EOK)
goto error;
}
rc = riff_rchunk_end(&riffck);
if (rc != EOK)
goto error;
tface->riffr = riffr;
*rtface = tface;
return EOK;
error:
if (riffr != NULL)
riff_rclose(riffr);
if (tface != NULL)
gfx_typeface_destroy(tface);
return rc;
}
static errno_t gfx_typeface_loadin(gfx_typeface_t *tface)
{
gfx_font_t *font;
gfx_font_info_t *finfo;
errno_t rc;
finfo = gfx_typeface_first_font(tface);
while (finfo != NULL) {
rc = gfx_font_open(finfo, &font);
if (rc != EOK)
return rc;
(void)font;
finfo = gfx_typeface_next_font(finfo);
}
return EOK;
}
errno_t gfx_typeface_save(gfx_typeface_t *tface, const char *fname)
{
riffw_t *riffw = NULL;
errno_t rc;
gfx_font_info_t *finfo;
riff_wchunk_t riffck;
rc = gfx_typeface_loadin(tface);
if (rc != EOK)
return rc;
rc = riff_wopen(fname, &riffw);
if (rc != EOK)
return rc;
rc = riff_wchunk_start(riffw, CKID_RIFF, &riffck);
if (rc != EOK)
goto error;
rc = riff_write_uint32(riffw, FORM_TPFC);
if (rc != EOK)
goto error;
finfo = gfx_typeface_first_font(tface);
while (finfo != NULL) {
rc = gfx_font_save(finfo, riffw);
if (rc != EOK)
goto error;
finfo = gfx_typeface_next_font(finfo);
}
rc = riff_wchunk_end(riffw, &riffck);
if (rc != EOK)
goto error;
rc = riff_wclose(riffw);
if (rc != EOK)
return rc;
return EOK;
error:
if (riffw != NULL)
riff_wclose(riffw);
return rc;
}
HelenOS homepage, sources at GitHub