text: font: Simplify font-system and add freetype2 renderer

The freetype2 renderer uses ft2 and fontconfig to provide a very
simplistic renderer. We also simplify the logic of the whole subsystem a
lot and guarantee that each glyph will have the same size now.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann committed 2012-08-06 14:48:22 +02:00
1 parent 1ce3b14ca6
commit 0fee19a8c0
9 files changed
+996 -614

No files matched your search

+16 -3
View File
@@ -121,15 +121,28 @@ libkmscon_core_la_SOURCES = \
src/text.h \
src/text.c \
src/text_bblit.c \
src/text_font.c \
src/text_font_8x16.c \
src/text_font_pango.c
src/text_font.c
if KMSCON_HAVE_GLES2
libkmscon_core_la_SOURCES += \
src/gl_shader.c
endif
if KMSCON_HAVE_8X16
libkmscon_core_la_SOURCES += \
src/text_font_8x16.c
endif
if KMSCON_HAVE_FREETYPE2
libkmscon_core_la_SOURCES += \
src/text_font_freetype2.c
endif
if KMSCON_HAVE_PANGO
libkmscon_core_la_SOURCES += \
src/text_font_pango.c
endif
libkmscon_core_la_CPPFLAGS = \
$(AM_CPPFLAGS) \
$(GLES2_CFLAGS) \
+89 -4
View File
@@ -76,12 +76,12 @@ PKG_CHECK_MODULES([GLES2], [glesv2],
PKG_CHECK_MODULES([XKBCOMMON], [xkbcommon],
[have_xkbcommon=yes], [have_xkbcommon=no])
PKG_CHECK_MODULES([FREETYPE2], [freetype2 fontconfig],
[have_freetype2=yes], [have_freetype2=no])
PKG_CHECK_MODULES([PANGO], [pango pangoft2],
[have_pango=yes], [have_pango=no])
PKG_CHECK_MODULES([FREETYPE2], [freetype2],
[have_freetype2=yes], [have_freetype2=no])
#
# Parse arguments
# Parse all command line arguments and save the values so we can check them
@@ -100,6 +100,12 @@ AC_ARG_ENABLE([udev],
[enable device hotplug support with udev])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to use dbus for IPC])
AC_ARG_ENABLE([dbus],
[AS_HELP_STRING([--enable-dbus],
[enable dbus IPC])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to use uterm fbdev video backend])
AC_ARG_ENABLE([fbdev],
[AS_HELP_STRING([--enable-fbdev],
@@ -124,6 +130,24 @@ AC_ARG_ENABLE([xkbcommon],
[disable xkbcommon keyboard backend])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to use static 8x16 font backend])
AC_ARG_ENABLE([f8x16],
[AS_HELP_STRING([--disable-f8x16],
[disable static 8x16 font backend])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to use freetype2 font backend])
AC_ARG_ENABLE([freetype2],
[AS_HELP_STRING([--disable-freetype2],
[disable freetype2 font backend])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to use pango font backend])
AC_ARG_ENABLE([pango],
[AS_HELP_STRING([--disable-pango],
[disable pango font backend])])
AC_MSG_RESULT([ok])
AC_MSG_CHECKING([whether to build with debugging on])
AC_ARG_ENABLE([debug],
[AS_HELP_STRING([--enable-debug],
@@ -345,6 +369,62 @@ fi
AM_CONDITIONAL([UTERM_HAVE_XKBCOMMON], [test x$xkbcommon_enabled = xyes])
#
# Font backends
# This checks for the 8x16, freetype2 and pango font backends and enables them
# if requested and available.
#
f8x16_enabled=no
if test ! x$enable_f8x16 = xno ; then
f8x16_enabled=yes
fi
if test x$f8x16_enabled = xyes ; then
AC_DEFINE([KMSCON_HAVE_8X16], [1],
[Use static 8x16 font backend])
fi
AM_CONDITIONAL([KMSCON_HAVE_8X16], [test x$f8x16_enabled = xyes])
freetype2_enabled=no
if test ! x$enable_freetype2 = xno ; then
if test x$have_freetype2 = xyes ; then
freetype2_enabled=yes
elif test x$enable_freetype2 = xyes ; then
AC_ERROR([freetype2/fontconfig not found for freetype2 backend])
fi
fi
if test x$freetype2_enabled = xyes ; then
AC_DEFINE([KMSCON_HAVE_FREETYPE2], [1],
[Use freetype2 as font backend])
else
FREETYPE2_CFLAGS=""
FREETYPE2_LIBS=""
fi
AM_CONDITIONAL([KMSCON_HAVE_FREETYPE2], [test x$freetype2_enabled = xyes])
pango_enabled=no
if test ! x$enable_pango = xno ; then
if test x$have_pango = xyes ; then
pango_enabled=yes
elif test x$enable_pango = xyes ; then
AC_ERROR([pango not found for pango font backend])
fi
fi
if test x$pango_enabled = xyes ; then
AC_DEFINE([KMSCON_HAVE_PANGO], [1],
[Use pango as font backend])
else
PANGO_CFLAGS=""
PANGO_LIBS=""
fi
AM_CONDITIONAL([KMSCON_HAVE_PANGO], [test x$pango_enabled = xyes])
#
# Makefile vars
# After everything is configured, we correctly substitute the values for the
@@ -390,10 +470,15 @@ AC_MSG_NOTICE([Build configuration:
dbus: $dbus_enabled
xkbcommon: $xkbcommon_enabled
libuterm video backends:
libuterm video backends:
fbdev: $fbdev_enabled
dumb drm: $dumb_enabled
drm: $drm_enabled
OpenGLES2: $gles2_enabled
font backends:
8x16: $f8x16_enabled
freetype2: $freetype2_enabled
pango: $pango_enabled
Run "make" to start compilation process])
+3
View File
@@ -336,6 +336,7 @@ int main(int argc, char **argv)
kmscon_font_8x16_load();
kmscon_font_pango_load();
kmscon_font_freetype2_load();
kmscon_text_bblit_load();
memset(&app, 0, sizeof(app));
@@ -367,6 +368,7 @@ int main(int argc, char **argv)
destroy_app(&app);
kmscon_text_bblit_unload();
kmscon_font_freetype2_unload();
kmscon_font_pango_unload();
kmscon_font_8x16_unload();
conf_free();
@@ -376,6 +378,7 @@ int main(int argc, char **argv)
err_unload:
kmscon_text_bblit_unload();
kmscon_font_freetype2_unload();
kmscon_font_pango_unload();
kmscon_font_8x16_unload();
err_out:
+58 -6
View File
@@ -69,9 +69,8 @@ bool kmscon_font_attr_match(const struct kmscon_font_attr *a1,
const struct kmscon_font_attr *a2);
struct kmscon_glyph {
unsigned int ascent;
unsigned int descent;
struct uterm_video_buffer buf;
void *data;
};
struct kmscon_font {
@@ -89,8 +88,10 @@ struct kmscon_font_ops {
void (*destroy) (struct kmscon_font *font);
int (*render) (struct kmscon_font *font, kmscon_symbol_t sym,
const struct kmscon_glyph **out);
void (*drop) (struct kmscon_font *font,
const struct kmscon_glyph *glyph);
int (*render_empty) (struct kmscon_font *font,
const struct kmscon_glyph **out);
int (*render_inval) (struct kmscon_font *font,
const struct kmscon_glyph **out);
};
int kmscon_font_register(const struct kmscon_font_ops *ops);
@@ -104,8 +105,10 @@ void kmscon_font_unref(struct kmscon_font *font);
int kmscon_font_render(struct kmscon_font *font, kmscon_symbol_t sym,
const struct kmscon_glyph **out);
void kmscon_font_drop(struct kmscon_font *font,
const struct kmscon_glyph *glyph);
int kmscon_font_render_empty(struct kmscon_font *font,
const struct kmscon_glyph **out);
int kmscon_font_render_inval(struct kmscon_font *font,
const struct kmscon_glyph **out);
/* text renderer */
@@ -164,11 +167,60 @@ void kmscon_text_render(struct kmscon_text *txt);
/* modularized backends */
#ifdef KMSCON_HAVE_8X16
int kmscon_font_8x16_load(void);
void kmscon_font_8x16_unload(void);
#else
static inline int kmscon_font_8x16_load(void)
{
return -EOPNOTSUPP;
}
static inline void kmscon_font_8x16_unload(void)
{
}
#endif
#ifdef KMSCON_HAVE_FREETYPE2
int kmscon_font_freetype2_load(void);
void kmscon_font_freetype2_unload(void);
#else
static inline int kmscon_font_freetype2_load(void)
{
return -EOPNOTSUPP;
}
static inline void kmscon_font_freetype2_unload(void)
{
}
#endif
#ifdef KMSCON_HAVE_PANGO
int kmscon_font_pango_load(void);
void kmscon_font_pango_unload(void);
#else
static inline int kmscon_font_pango_load(void)
{
return -EOPNOTSUPP;
}
static inline void kmscon_font_pango_unload(void)
{
}
#endif
int kmscon_text_bblit_load(void);
void kmscon_text_bblit_unload(void);
+9 -14
View File
@@ -128,35 +128,30 @@ static void bblit_draw(struct kmscon_text *txt, kmscon_symbol_t ch,
const struct kmscon_glyph *glyph;
int ret;
struct bblit *bblit = txt->data;
int off, pos;
ret = kmscon_font_render(txt->font, ch, &glyph);
if (ch == 0 || ch == ' ') {
ret = kmscon_font_render_empty(txt->font, &glyph);
} else {
ret = kmscon_font_render(txt->font, ch, &glyph);
}
if (ret) {
/* TODO: document 0 as fallback glyph */
ret = kmscon_font_render(txt->font, 0, &glyph);
ret = kmscon_font_render_inval(txt->font, &glyph);
if (ret)
return;
}
/* position glyph correctly */
off = bblit->base - glyph->ascent;
pos = posy * bblit->advance_y;
if (pos + off < 0)
pos = 0;
else
pos += off;
/* draw glyph */
if (attr->inverse) {
uterm_screen_blend(txt->screen, &glyph->buf,
posx * bblit->advance_x,
pos,
posy * bblit->advance_y,
attr->br, attr->bg, attr->bb,
attr->fr, attr->fg, attr->fb);
} else {
uterm_screen_blend(txt->screen, &glyph->buf,
posx * bblit->advance_x,
pos,
posy * bblit->advance_y,
attr->fr, attr->fg, attr->fb,
attr->br, attr->bg, attr->bb);
}
+38 -13
View File
@@ -341,7 +341,7 @@ int kmscon_font_find(struct kmscon_font **out,
ret = -EFAULT;
} else {
ret = 0;
def = kmscon_dlist_entry(font__list.next,
def = kmscon_dlist_entry(font__list.prev,
struct font_backend,
list);
if (!backend) {
@@ -453,8 +453,10 @@ void kmscon_font_unref(struct kmscon_font *font)
* @out: Output buffer for glyph
*
* Renders the glyph for symbol @sym and places a pointer to the glyph in @out.
* If the glyph cannot be found or is invalid, an error is returned. You must
* release the glyph with kmscon_font_drop() if you don't need it anymore.
* If the glyph cannot be found or is invalid, an error is returned. The glyph
* is cached internally and removed when the last reference to this font is
* dropped.
* If the glyph is no available in this font-set, then -ERANGE is returned.
*
* Returns: 0 on success, negative error code on failure
*/
@@ -468,19 +470,42 @@ int kmscon_font_render(struct kmscon_font *font, kmscon_symbol_t sym,
}
/**
* kmscon_font_drop:
* kmscon_font_render_empty:
* @font: Valid font object
* @glyph: Glyph object from font @font
* @out: Output buffer for glyph
*
* This releases a glyph that was previously retrieved via kmscon_font_render().
* Please note that the glyphs are shared and this does not necessarily free the
* memory if there are still other users.
* Same as kmscon_font_render() but this renders a glyph that has no content and
* can be used to blit solid backgrounds. That is, the resulting buffer will be
* all 0 but the dimensions are the same as for all other glyphs.
*
* Returns: 0 on success, negative error code on failure
*/
void kmscon_font_drop(struct kmscon_font *font,
const struct kmscon_glyph *glyph)
int kmscon_font_render_empty(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
if (!font || !glyph)
return;
if (!font || !out)
return -EINVAL;
font->ops->drop(font, glyph);
return font->ops->render_empty(font, out);
}
/**
* kmscon_font_render_inval:
* @font: Valid font object
* @out: Output buffer for glyph
*
* Same sa kmscon_font_render_empty() but renders a glyph that can be used as
* replacement for any other non-drawable glyph. That is, if
* kmscon_font_render() returns -ERANGE, then this glyph can be used as
* replacement.
*
* Returns: 0 on success ,engative error code on failure
*/
int kmscon_font_render_inval(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
if (!font || !out)
return -EINVAL;
return font->ops->render_inval(font, out);
}
+13 -515
View File
File diff suppressed because it is too large. Load diff
+705
View File
@@ -0,0 +1,705 @@
/*
* kmscon - Freetype2 backend for font handling of text renderer
*
* Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
* Copyright (c) 2011 University of Tuebingen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* SECTION:text_font_freetype2.c
* @short_description: Freetype2 backend for font handling of text renderer
* @include: text.h
*
* The freetype2 backend uses freetype2 to render glyphs into memory
* buffers. It uses a hashmap to cache all rendered glyphs of a single
* font-face. Therefore, rendering should be very fast. Also, when loading a
* glyph it pre-renders all common (mostly ASCII) characters, so it can measure
* the font and return a valid font width.
*/
#include <errno.h>
#include <fontconfig/fontconfig.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "log.h"
#include "static_misc.h"
#include "text.h"
#include "unicode.h"
#include "uterm.h"
#define LOG_SUBSYSTEM "text_font_freetype2"
struct glyph {
bool shrinked;
unsigned int width;
};
struct face {
unsigned long ref;
struct kmscon_dlist list;
bool shrink;
struct kmscon_font_attr attr;
struct kmscon_font_attr real_attr;
unsigned int baseline;
FT_Face face;
pthread_mutex_t glyph_lock;
struct kmscon_hashtable *glyphs;
struct kmscon_glyph empty;
struct kmscon_glyph inval;
};
static pthread_mutex_t manager_mutex = PTHREAD_MUTEX_INITIALIZER;
static unsigned long manager__refcnt;
static FT_Library manager__lib;
static struct kmscon_dlist manager__list = KMSCON_DLIST_INIT(manager__list);
static void manager_lock()
{
pthread_mutex_lock(&manager_mutex);
}
static void manager_unlock()
{
pthread_mutex_unlock(&manager_mutex);
}
/* TODO: We currently load the default font-config configuration on start-up but
* should probably provide a way to refresh it on SIGHUP or similar. Font-config
* provides the FcInitBringUptoDate() or FcInitReinitialize() functions. */
static int manager__ref()
{
FT_Error err;
if (!manager__refcnt++) {
err = FT_Init_FreeType(&manager__lib);
if (err) {
log_warn("cannot initialize freetype2");
--manager__refcnt;
return -EFAULT;
}
if (!FcInit()) {
log_warn("cannot initialize fontconfig library");
err = FT_Done_FreeType(manager__lib);
if (err)
log_warn("cannot deinitialize freetype2");
--manager__refcnt;
return -EFAULT;
}
}
return 0;
}
static void manager__unref()
{
FT_Error err;
if (!--manager__refcnt) {
/* FcFini() uses assert() to check whether all resources were
* correctly freed before FcFini() is called. As an emergency
* console, we cannot risk being killed because we have a small
* memory leak. Therefore, we rather skip deinitializing
* fontconfig and blame their authors here.
* Never ever use assert()/abort()/etc. in critical code paths!
* Bullshit...
* TODO: Fix upstream fontconfig to drop all those ugly
* assertions. */
// FcFini();
err = FT_Done_FreeType(manager__lib);
if (err)
log_warn("cannot deinitialize freetype2");
}
}
static int get_glyph(struct face *face, struct kmscon_glyph **out,
kmscon_symbol_t ch)
{
struct kmscon_glyph *glyph;
struct glyph *data;
FT_Error err;
FT_UInt idx;
FT_Bitmap *bmap;
FT_GlyphSlot slot;
size_t len;
const uint32_t *val;
bool res;
unsigned int i, j, wmax, hmax, idx1, idx2;
int ret, hoff1, hoff2, woff1, woff2;
pthread_mutex_lock(&face->glyph_lock);
res = kmscon_hashtable_find(face->glyphs, (void**)&glyph,
(void*)(long)ch);
pthread_mutex_unlock(&face->glyph_lock);
if (res) {
*out = glyph;
return 0;
}
manager_lock();
glyph = malloc(sizeof(*glyph) + sizeof(struct glyph));
if (!glyph) {
log_error("cannot allocate memory for new glyph");
ret = -ENOMEM;
goto out_unlock;
}
memset(glyph, 0, sizeof(*glyph) + sizeof(struct glyph));
glyph->data = (void*)(((uint8_t*)glyph) + sizeof(*glyph));
data = glyph->data;
/* We currently ignore composed-symbols. That is, we only use the first
* UCS-4 code and draw this character. This works great for most simple
* ASCII characters but composed CJK characters often consist of
* multiple UCS-4 codes.
* TODO: Fix this by drawing all related characters into a single glyph
* and saving it or simply refer to the pango backend which already does
* that. */
val = kmscon_symbol_get(&ch, &len);
if (len > 1 || !*val) {
ret = -ERANGE;
goto out_glyph;
}
idx = FT_Get_Char_Index(face->face, *val);
err = FT_Load_Glyph(face->face, idx, FT_LOAD_DEFAULT);
if (err) {
ret = -ERANGE;
goto out_glyph;
}
err = FT_Render_Glyph(face->face->glyph, FT_RENDER_MODE_NORMAL);
if (err) {
ret = -ERANGE;
goto out_glyph;
}
slot = face->face->glyph;
bmap = &slot->bitmap;
if (slot->format != FT_GLYPH_FORMAT_BITMAP ||
bmap->pixel_mode != FT_PIXEL_MODE_GRAY ||
bmap->num_grays != 256 ||
!bmap->rows ||
!bmap->width) {
ret = -ERANGE;
goto out_glyph;
}
data->width = bmap->width;
glyph->buf.width = face->real_attr.width;
glyph->buf.height = face->real_attr.height;
glyph->buf.stride = glyph->buf.width;
glyph->buf.format = UTERM_FORMAT_GREY;
glyph->buf.data = malloc(glyph->buf.stride * glyph->buf.height);
if (!glyph->buf.data) {
ret = -ENOMEM;
goto out_glyph;
}
memset(glyph->buf.data, 0, glyph->buf.stride * glyph->buf.height);
/* compute width-offsets and relative width-differences */
if (slot->bitmap_left >= glyph->buf.width) {
wmax = 0;
woff1 = 0;
woff2 = 0;
} else if (slot->bitmap_left < 0) {
if (glyph->buf.width > bmap->width)
wmax = bmap->width;
else
wmax = glyph->buf.width;
woff1 = 0;
woff2 = 0;
} else {
wmax = glyph->buf.width - slot->bitmap_left;
if (wmax > bmap->width)
wmax = bmap->width;
woff1 = slot->bitmap_left;
woff2 = 0;
}
/* compute height-offsets and relative height-differences */
hoff1 = (int)glyph->buf.height - face->baseline;
if (hoff1 > slot->bitmap_top) {
hoff1 -= slot->bitmap_top;
hoff2 = 0;
} else {
hoff2 = slot->bitmap_top - hoff1;
hoff1 = 0;
}
if (bmap->rows - hoff2 > glyph->buf.height - hoff1)
hmax = glyph->buf.height - hoff1;
else
hmax = bmap->rows - hoff2;
/* copy bitmap into glyph buffer */
for (i = 0; i < hmax; ++i) {
for (j = 0; j < wmax; ++j) {
idx1 = (i + hoff1) * glyph->buf.stride + (j + woff1);
idx2 = (i + hoff2) * bmap->pitch + (j + woff2);
glyph->buf.data[idx1] = bmap->buffer[idx2];
}
}
pthread_mutex_lock(&face->glyph_lock);
ret = kmscon_hashtable_insert(face->glyphs, (void*)(long)ch, glyph);
pthread_mutex_unlock(&face->glyph_lock);
if (ret) {
log_error("cannot add glyph to hashtable");
goto out_buffer;
}
*out = glyph;
goto out_unlock;
out_buffer:
free(glyph->buf.data);
out_glyph:
free(glyph);
out_unlock:
manager_unlock();
return ret;
}
static void free_glyph(void *data)
{
struct kmscon_glyph *glyph = data;
free(glyph->buf.data);
free(glyph);
}
static int manager_get_face(struct face **out, struct kmscon_font_attr *attr)
{
struct kmscon_dlist *iter;
struct face *face, *f;
FcPattern *pat, *mat;
FcResult res;
FcChar8 *fname;
FT_Error err;
int ret, tmp, idx, weight, slant;
double s, em, xsc, ysc;
manager_lock();
if (!attr->height) {
ret = -EINVAL;
goto out_unlock;
}
if (!attr->width)
attr->width = attr->height;
kmscon_dlist_for_each(iter, &manager__list) {
face = kmscon_dlist_entry(iter, struct face, list);
if (kmscon_font_attr_match(&face->attr, attr)) {
++face->ref;
*out = face;
ret = 0;
goto out_unlock;
}
}
ret = manager__ref();
if (ret)
goto out_unlock;
face = malloc(sizeof(*face));
if (!face) {
log_error("cannot allocate memory for new face");
ret = -ENOMEM;
goto err_manager;
}
memset(face, 0, sizeof(*face));
face->ref = 1;
memcpy(&face->attr, attr, sizeof(*attr));
memcpy(&face->real_attr, attr, sizeof(*attr));
ret = pthread_mutex_init(&face->glyph_lock, NULL);
if (ret) {
log_error("cannot initialize glyph lock");
goto err_free;
}
ret = kmscon_hashtable_new(&face->glyphs, kmscon_direct_hash,
kmscon_direct_equal, NULL, free_glyph);
if (ret) {
log_error("cannot allocate hashtable");
goto err_lock;
}
s = face->attr.height;
weight = face->attr.bold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
slant = face->attr.italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
pat = FcPatternBuild(NULL, FC_FAMILY, FcTypeString, face->attr.name,
FC_PIXEL_SIZE, FcTypeDouble, s,
FC_WEIGHT, FcTypeInteger, weight,
FC_SLANT, FcTypeInteger, slant,
NULL);
if (!pat) {
log_error("cannot create font-config pattern");
ret = -EFAULT;
goto err_htable;
}
if (!FcConfigSubstitute(NULL, pat, FcMatchPattern)) {
FcPatternDestroy(pat);
log_error("cannot perform font-config substitutions");
ret = -ENOMEM;
goto err_htable;
}
res = FcResultMatch;
mat = FcFontMatch(NULL, pat, &res);
if (res != FcResultMatch) {
if (mat)
FcPatternDestroy(mat);
FcPatternDestroy(pat);
log_error("font-config cannot find font: %d", res);
ret = -EFAULT;
goto err_htable;
}
res = FcPatternGetString(mat, FC_FILE, 0, &fname);
if (res != FcResultMatch) {
FcPatternDestroy(mat);
FcPatternDestroy(pat);
log_error("font-config cannot find font (name)");
ret = -EFAULT;
goto err_htable;
}
res = FcPatternGetInteger(mat, FC_INDEX, 0, &idx);
if (res != FcResultMatch) {
FcPatternDestroy(mat);
FcPatternDestroy(pat);
log_error("font-config cannot find font (index)");
ret = -EFAULT;
goto err_htable;
}
log_debug("loading font %s:%d", (const char*)fname, idx);
err = FT_New_Face(manager__lib, (const char*)fname, idx, &face->face);
FcPatternDestroy(mat);
FcPatternDestroy(pat);
if (err) {
if (err == FT_Err_Unknown_File_Format)
log_error("unknown font file format");
else
log_error("cannot load font");
ret = -EFAULT;
goto err_htable;
}
if (!face->face->charmap) {
log_warn("cannot load charmap of new font");
ret = -EFAULT;
goto err_face;
}
if (!FT_IS_SCALABLE(face->face)) {
log_warn("non-scalable font");
ret = -EFAULT;
goto err_face;
}
err = FT_Set_Pixel_Sizes(face->face, face->attr.width,
face->attr.height);
if (err) {
log_warn("cannot set pixel size of font");
ret = -EFAULT;
goto err_face;
}
/* Every font provides an ascender/descender value which we use to
* compute glyph-height and the baseline offset. We need monospace fonts
* as we have the same fixed bounding-box for every glyph. However, if
* the font is not a monospace font, then the most straight-forward
* approach would be using the biggest bounding box. This, however, will
* not work as some characters are extremely wide and the text will look
* horrible. Therefore, we use the ascender/descender values provided
* with each font. This guarantees that special characters like
* line-segments are properly aligned without spacing. If the font does
* not provide proper asc/desc values, then freetype2 will return proper
* subsitutions. */
em = face->face->units_per_EM;
xsc = face->face->size->metrics.x_ppem / em;
ysc = face->face->size->metrics.y_ppem / em;
tmp = face->face->descender * ysc;
if (tmp > 0)
tmp = 0;
face->baseline = -tmp;
tmp = face->face->ascender * ysc + face->baseline;
if (tmp < 0 || tmp < face->baseline) {
log_warn("invalid ascender/descender values for font");
ret = -EFAULT;
goto err_face;
}
face->real_attr.height = tmp;
/* For font-width we use the biggest bounding-box-width. After the font
* has been loaded, this is cut down by pre-rendering some characters
* and computing a better average. */
tmp = 1 + (int)(xsc * (face->face->bbox.xMax - face->face->bbox.xMin));
if (tmp < 0)
tmp = 0;
face->real_attr.width = tmp;
kmscon_font_attr_normalize(&face->real_attr);
if (!face->real_attr.height || !face->real_attr.width) {
log_warn("invalid scaled font sizes");
ret = -EFAULT;
goto err_face;
}
/* The real metrics probably differ from the requested metrics so try
* again to find a suitable cached font. */
kmscon_dlist_for_each(iter, &manager__list) {
f = kmscon_dlist_entry(iter, struct face, list);
if (kmscon_font_attr_match(&f->real_attr, &face->real_attr)) {
++f->ref;
*out = f;
ret = 0;
goto err_face;
}
}
memcpy(attr, &face->real_attr, sizeof(*attr));
kmscon_dlist_link(&manager__list, &face->list);
*out = face;
ret = 0;
goto out_unlock;
err_face:
FT_Done_Face(face->face);
err_htable:
kmscon_hashtable_free(face->glyphs);
err_lock:
pthread_mutex_destroy(&face->glyph_lock);
err_free:
free(face);
err_manager:
manager__unref();
out_unlock:
manager_unlock();
return ret;
}
static void manager_put_face(struct face *face)
{
manager_lock();
if (--face->ref) {
kmscon_dlist_unlink(&face->list);
kmscon_hashtable_free(face->glyphs);
pthread_mutex_destroy(&face->glyph_lock);
FT_Done_Face(face->face);
free(face);
manager__unref();
}
manager_unlock();
}
static int generate_specials(struct face *face)
{
size_t s;
struct kmscon_glyph *g;
int ret;
face->empty.data = NULL;
face->empty.buf.width = face->real_attr.width;
face->empty.buf.height = face->real_attr.height;
face->empty.buf.stride = face->empty.buf.width;
face->empty.buf.format = UTERM_FORMAT_GREY;
s = face->empty.buf.stride * face->empty.buf.height;
face->empty.buf.data = malloc(s);
if (!face->empty.buf.data)
return -ENOMEM;
memset(face->empty.buf.data, 0, s);
ret = get_glyph(face, &g, kmscon_symbol_make('?'));
if (ret) {
memcpy(&face->inval, &face->empty, sizeof(face->inval));
} else {
memcpy(&face->inval, g, sizeof(face->inval));
}
return 0;
}
static int kmscon_font_freetype2_init(struct kmscon_font *out,
const struct kmscon_font_attr *attr)
{
struct face *face;
int ret;
kmscon_symbol_t ch;
unsigned int i, width;
struct kmscon_glyph *glyph;
struct glyph *data;
memcpy(&out->attr, attr, sizeof(*attr));
kmscon_font_attr_normalize(&out->attr);
log_debug("loading freetype2 font %s", out->attr.name);
ret = manager_get_face(&face, &out->attr);
if (ret)
return ret;
out->baseline = face->baseline;
/* Shrinking is done to get a better width-value for fonts. As not all
* fonts provide monospace-glyphs, we need to calculate a proper width
* by pre-rendering all ASCII characters and using the widest value.
* TODO: We should extend this with a better algorithm as there are
* common non-ASCII glyphs which are much wider.
* We enable shrinking by default as most fonts have a maximum-width
* which is about 3 times the size of 'M'.*/
face->shrink = true;
if (face->shrink) {
width = 0;
for (i = 0x20; i < 0x7f; ++i) {
ch = kmscon_symbol_make(i);
ret = get_glyph(face, &glyph, ch);
if (ret)
continue;
data = glyph->data;
if (data->width > width)
width = data->width;
}
if (!width) {
log_warning("cannot measure font");
face->shrink = false;
} else if (width < face->real_attr.width) {
face->real_attr.width = width;
kmscon_font_attr_normalize(&face->real_attr);
memcpy(&out->attr, &face->real_attr, sizeof(out->attr));
}
}
/* generate inval/empty glyphs after shrinking */
ret = generate_specials(face);
if (ret)
goto err_face;
out->data = face;
return 0;
err_face:
manager_put_face(face);
return ret;
}
static void kmscon_font_freetype2_destroy(struct kmscon_font *font)
{
struct face *face;
face = font->data;
log_debug("unloading freetype2 font %s", face->real_attr.name);
free(face->empty.buf.data);
manager_put_face(face);
}
static int kmscon_font_freetype2_render(struct kmscon_font *font,
kmscon_symbol_t sym,
const struct kmscon_glyph **out)
{
struct kmscon_glyph *glyph;
struct glyph *data;
struct face *face;
int ret;
ret = get_glyph(font->data, &glyph, sym);
if (ret)
return ret;
face = font->data;
data = glyph->data;
if (face->shrink && !data->shrinked) {
data->shrinked = true;
glyph->buf.width = face->real_attr.width;
}
*out = glyph;
return 0;
}
static int kmscon_font_freetype2_render_empty(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
struct face *face = font->data;
*out = &face->empty;
return 0;
}
static int kmscon_font_freetype2_render_inval(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
struct face *face = font->data;
*out = &face->inval;
return 0;
}
static const struct kmscon_font_ops kmscon_font_freetype2_ops = {
.name = "freetype2",
.init = kmscon_font_freetype2_init,
.destroy = kmscon_font_freetype2_destroy,
.render = kmscon_font_freetype2_render,
.render_empty = kmscon_font_freetype2_render_empty,
.render_inval = kmscon_font_freetype2_render_inval,
};
int kmscon_font_freetype2_load(void)
{
int ret;
ret = kmscon_font_register(&kmscon_font_freetype2_ops);
if (ret) {
log_error("cannot register freetype2 font");
return ret;
}
return 0;
}
void kmscon_font_freetype2_unload(void)
{
kmscon_font_unregister(kmscon_font_freetype2_ops.name);
}
+65 -59
View File
@@ -64,6 +64,8 @@ struct face {
struct kmscon_dlist list;
struct kmscon_font_attr attr;
struct kmscon_font_attr real_attr;
unsigned int baseline;
PangoContext *ctx;
pthread_mutex_t glyph_lock;
struct kmscon_hashtable *glyphs;
@@ -159,15 +161,12 @@ static int get_glyph(struct face *face, struct kmscon_glyph **out,
line = pango_layout_get_line_readonly(layout, 0);
pango_layout_line_get_pixel_extents(line, NULL, &rec);
glyph->ascent = -rec.y;
glyph->descent = rec.height - glyph->ascent;
glyph->buf.width = rec.width;
glyph->buf.height = rec.height;
glyph->buf.stride = rec.width;
glyph->buf.width = face->real_attr.width;
glyph->buf.height = face->real_attr.height;
glyph->buf.stride = glyph->buf.width;
glyph->buf.format = UTERM_FORMAT_GREY;
if (!glyph->buf.width || !glyph->buf.height ||
glyph->ascent > glyph->buf.height) {
if (!glyph->buf.width || !glyph->buf.height) {
ret = -ERANGE;
goto out_glyph;
}
@@ -222,9 +221,12 @@ static void free_glyph(void *data)
static int manager_get_face(struct face **out, struct kmscon_font_attr *attr)
{
struct kmscon_dlist *iter;
struct face *face;
struct face *face, *f;
PangoFontDescription *desc;
int ret;
PangoLayout *layout;
PangoRectangle rec;
int ret, num;
const char *str;
manager_lock();
@@ -270,15 +272,8 @@ static int manager_get_face(struct face **out, struct kmscon_font_attr *attr)
pango_context_set_language(face->ctx, pango_language_get_default());
desc = pango_font_description_from_string(attr->name);
/* TODO: even though we specify an absolute size, we currently get
* slightly smaller fonts back from pango. This is very odd and we
* haven't found the reason for it, yet. It's not very problematic as
* our text renderer expects such behavior. However, fixing it would be
* nice. */
pango_font_description_set_absolute_size(desc,
PANGO_SCALE * face->attr.height);
pango_font_description_set_weight(desc,
attr->bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
pango_font_description_set_style(desc,
@@ -289,11 +284,51 @@ static int manager_get_face(struct face **out, struct kmscon_font_attr *attr)
pango_context_set_font_description(face->ctx, desc);
pango_font_description_free(desc);
/* measure font */
layout = pango_layout_new(face->ctx);
pango_layout_set_height(layout, 0);
pango_layout_set_spacing(layout, 0);
str = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"@!\"$%&/()=?\\}][{°^~+*#'<>|-_.:,;`´";
num = strlen(str);
pango_layout_set_text(layout, str, num);
pango_layout_get_pixel_extents(layout, NULL, &rec);
memcpy(&face->real_attr, &face->attr, sizeof(face->attr));
face->real_attr.height = rec.height;
face->real_attr.width = rec.width / num;
face->baseline = PANGO_PIXELS_CEIL(pango_layout_get_baseline(layout));
g_object_unref(layout);
kmscon_font_attr_normalize(&face->real_attr);
if (!face->real_attr.height || !face->real_attr.width) {
log_warning("invalid scaled font sizes");
ret = -EFAULT;
goto err_face;
}
/* The real metrics probably differ from the requested metrics so try
* again to find a suitable cached font. */
kmscon_dlist_for_each(iter, &manager__list) {
f = kmscon_dlist_entry(iter, struct face, list);
if (kmscon_font_attr_match(&f->real_attr, &face->real_attr)) {
++f->ref;
*out = f;
ret = 0;
goto err_face;
}
}
memcpy(attr, &face->real_attr, sizeof(*attr));
kmscon_dlist_link(&manager__list, &face->list);
*out = face;
ret = 0;
goto out_unlock;
err_face:
g_object_unref(face->ctx);
kmscon_hashtable_free(face->glyphs);
err_lock:
pthread_mutex_destroy(&face->glyph_lock);
err_free:
@@ -326,53 +361,16 @@ static int kmscon_font_pango_init(struct kmscon_font *out,
{
struct face *face;
int ret;
kmscon_symbol_t ch;
unsigned int i, num, width, asc, desc;
struct kmscon_glyph *glyph;
log_debug("loading pango font %s", attr->name);
memcpy(&out->attr, attr, sizeof(*attr));
kmscon_font_attr_normalize(&out->attr);
log_debug("loading pango font %s", out->attr.name);
ret = manager_get_face(&face, &out->attr);
if (ret)
return ret;
/* Measure font face: We prerender all ASCII characters here so we can
* measure the average and maximum font width/height and as a bonus all
* characters will already be cached so the next renderings will be
* pretty fast. */
num = 0;
width = 0;
asc = 0;
desc = 0;
for (i = 0x20; i < 0x7f; ++i) {
ch = kmscon_symbol_make(i);
ret = get_glyph(face, &glyph, ch);
if (ret)
continue;
if (glyph->ascent > asc)
asc = glyph->ascent;
if (glyph->descent > desc)
desc = glyph->descent;
if (glyph->buf.width > width)
width = glyph->buf.width;
++num;
}
if (!num) {
log_warning("cannot measure font");
out->attr.width = out->attr.height;
out->baseline = 0;
} else {
out->attr.width = width;
out->attr.height = asc + desc;
out->baseline = desc;
}
kmscon_font_attr_normalize(&out->attr);
out->baseline = face->baseline;
out->data = face;
return 0;
@@ -402,9 +400,16 @@ static int kmscon_font_pango_render(struct kmscon_font *font,
return 0;
}
static void kmscon_font_pango_drop(struct kmscon_font *font,
const struct kmscon_glyph *glyph)
static int kmscon_font_pango_render_empty(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
return kmscon_font_pango_render(font, ' ', out);
}
static int kmscon_font_pango_render_inval(struct kmscon_font *font,
const struct kmscon_glyph **out)
{
return kmscon_font_pango_render(font, '?', out);
}
static const struct kmscon_font_ops kmscon_font_pango_ops = {
@@ -412,7 +417,8 @@ static const struct kmscon_font_ops kmscon_font_pango_ops = {
.init = kmscon_font_pango_init,
.destroy = kmscon_font_pango_destroy,
.render = kmscon_font_pango_render,
.drop = kmscon_font_pango_drop,
.render_empty = kmscon_font_pango_render_empty,
.render_inval = kmscon_font_pango_render_inval,
};
int kmscon_font_pango_load(void)