unifont.c revision 86db5dfb6d334e583ea4161191754522ce850eed
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
/*
* Unifont
* This implements the unifont glyph-array parser and provides it via a simple
* API to the caller. No heavy transformations are performed so glyph-lookups
* stay as fast as possible.
*/
#include <endian.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "macro.h"
#include "unifont-def.h"
#include "unifont-internal.h"
#include "util.h"
struct unifont {
unsigned long ref;
int fd;
const void *glyphs; /* unaligned! */
};
static int unifont_fetch_header(unifont *u) {
unifont_header h = { };
if (u->size < UNIFONT_HEADER_SIZE_MIN)
return -EBFONT;
assert_cc(sizeof(h) >= UNIFONT_HEADER_SIZE_MIN);
return -EBFONT;
if (h.incompatible_flags != 0)
return -EBFONT;
return -EBFONT;
return -EBFONT;
return -EBFONT;
u->n_glyphs = 0;
} else {
}
return 0;
}
static int unifont_fetch_glyph(unifont *u, unifont_glyph_header *out_header, const void **out_body, uint32_t ucs4) {
unifont_glyph_header glyph_header = { };
const void *glyph_body = NULL;
const uint8_t *p;
return -ENOENT;
p = u->glyphs;
/* copy glyph-header data */
/* copy glyph-body pointer */
p += u->header.glyph_header_size;
glyph_body = p;
return -ENOENT;
return -EBFONT;
*out_body = glyph_body;
return 0;
}
int r;
if (!u)
return -ENOMEM;
u->ref = 1;
u->fd = -1;
u->map = MAP_FAILED;
if (u->fd < 0)
return -errno;
if (r < 0)
return -errno;
if (u->map == MAP_FAILED)
return -errno;
r = unifont_fetch_header(u);
if (r < 0)
return r;
*out = u;
u = NULL;
return 0;
}
if (!u || !u->ref)
return NULL;
++u->ref;
return u;
}
return NULL;
if (u->map != MAP_FAILED)
free(u);
return NULL;
}
unsigned int unifont_get_stride(unifont *u) {
assert(u);
return u->header.glyph_stride;
}
unifont_glyph_header h = { };
const void *b = NULL;
unifont_glyph g = { };
int r;
assert_return(u, -EINVAL);
r = unifont_fetch_glyph(u, &h, &b, ucs4);
if (r < 0)
return r;
g.height = 16U;
g.data = b;
if (out)
return 0;
}