[renderer] Switch to using HarfBuzz for glyph positioning

This means that QF should support more exotic fonts without any issue
(once the rest of the text handling system is up to snuff) as HarfBuzz
does all the hard work of handling OpenType, Graphite, etc text shaping,
including kerning (when enabled).

Also, font loading now loads all the glyphs into the atlas (preload is
gone).
This commit is contained in:
Bill Currie 2022-09-04 20:56:38 +09:00
parent db69f5fd23
commit 935e883d8d
10 changed files with 268 additions and 105 deletions

View File

@ -26,7 +26,7 @@ NOCONV_DIST= \
BUILT_SOURCES = $(top_srcdir)/.version
#AM_CFLAGS= @PREFER_NON_PIC@
AM_CPPFLAGS= -I$(top_srcdir)/include $(PTHREAD_CFLAGS) $(FNM_FLAGS) $(NCURSES_CFLAGS) $(FREETYPE_CFLAGS)
AM_CPPFLAGS= -I$(top_srcdir)/include $(PTHREAD_CFLAGS) $(FNM_FLAGS) $(NCURSES_CFLAGS) $(FREETYPE_CFLAGS) $(HARFBUZZ_CFLAGS)
common_ldflags= -export-dynamic @PTHREAD_LDFLAGS@

View File

@ -13,3 +13,20 @@ AC_SUBST(FREETYPE_LIBS)
if test "x$HAVE_FREETYPE" == "xyes"; then
AC_DEFINE(HAVE_FREETYPE, 1, [Define if you have the freetype library])
fi
AC_ARG_ENABLE(harfbuzz,
AS_HELP_STRING([--disable-harfbuzz], [disable HarfBuzz support]))
if test "x$enable_harfbuzz" != "xno"; then
if test "x$PKG_CONFIG" != "x"; then
PKG_CHECK_MODULES([HARFBUZZ], [harfbuzz], HAVE_HARFBUZZ=yes, HAVE_HARFBUZZ=no)
fi
else
HAVE_HARFBUZZ=no
HARFBUZZ_LIBS=
fi
AC_SUBST(HARFBUZZ_LIBS)
if test "x$HAVE_HARFBUZZ" == "xyes"; then
AC_DEFINE(HAVE_HARFBUZZ, 1, [Define if you have the HarfBuzz library])
fi

View File

@ -61,10 +61,12 @@ EXTRA_DIST += \
include/quicksort.h \
include/r_cvar.h \
include/r_dynamic.h \
include/r_font.h \
include/r_internal.h \
include/r_local.h \
include/r_scrap.h \
include/r_shared.h \
include/r_text.h \
include/regex.h \
include/rua_internal.h \
include/sbar.h \

View File

@ -34,9 +34,6 @@
#include <ft2build.h>
#include FT_FREETYPE_H
#include "QF/hash.h"
#include "QF/progs.h" //FIXME for PR_RESMAP
#include "QF/ui/vrect.h"
#include "QF/simd/types.h"
@ -55,12 +52,12 @@ typedef struct rfont_s {
FT_Face face;
rscrap_t scrap;
byte *scrap_bitmap;
hashtab_t *glyphmap;
PR_RESMAP(rglyph_t) glyphs;
FT_Long num_glyphs;
rglyph_t *glyphs;
} rfont_t;
void R_FontInit (void);
void R_FontFree (rfont_t *font);
rfont_t *R_FontLoad (QFile *font_file, int size, const int *preload);
rfont_t *R_FontLoad (QFile *font_file, int size);
#endif//__r_font_h

73
include/r_text.h Normal file
View File

@ -0,0 +1,73 @@
/*
r_text.h
Renderer text management
Copyright (C) 2022 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2022/9/4
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifndef __r_text_h
#define __r_text_h
#include <hb.h>
#include <hb-ft.h>
#include "QF/darray.h"
typedef struct rtext_s {
const char *text;
const char *language;
hb_script_t script;
hb_direction_t direction;
} rtext_t;
typedef struct r_hb_featureset_s DARRAY_TYPE (hb_feature_t) r_hb_featureset_t;
struct rfont_s;
struct rglyph_s;
typedef void rtext_render_t (struct rglyph_s *glyph, int x, int y, void *data);
typedef struct rshaper_s {
struct rfont_s *rfont;
hb_font_t *font;
hb_buffer_t *buffer;
r_hb_featureset_t features;
} rshaper_t;
extern hb_feature_t LiagureOff;
extern hb_feature_t LiagureOn;
extern hb_feature_t KerningOff;
extern hb_feature_t KerningOn;
extern hb_feature_t CligOff;
extern hb_feature_t CligOn;
rshaper_t *RText_NewShaper (struct rfont_s *font);
void RText_DeleteShaper (rshaper_t *shaper);
void RText_AddFeature (rshaper_t *shaper, hb_feature_t feature);
void RText_ShapeText (rshaper_t *shaper, rtext_t *text);
void RText_RenderText (rshaper_t *shaper, rtext_t *text, int x, int y,
rtext_render_t *render, void *data);
#endif//__r_text_h

View File

@ -33,12 +33,14 @@ video_renderer_common_sources = \
libs/video/renderer/r_font.c \
libs/video/renderer/r_iqm.c \
libs/video/renderer/r_sprite.c \
libs/video/renderer/r_text.c \
libs/video/renderer/vid_common.c
renderer_libs= \
@vid_render_static_plugin_libs@ \
libs/util/libQFutil.la \
$(FREETYPE_LIBS)
$(FREETYPE_LIBS) \
$(HARFBUZZ_LIBS)
libs_video_renderer_libQFrenderer_la_LDFLAGS= @STATIC@
libs_video_renderer_libQFrenderer_la_LIBADD= $(renderer_libs)

View File

@ -42,35 +42,8 @@
static FT_Library ft;
static uintptr_t
glyph_get_hash (const void *_glyph, void *unused)
{
__auto_type glyph = (const rglyph_t *) _glyph;
return glyph->charcode;
}
static int
glyph_compare (const void *_a, const void *_b, void *unused)
{
__auto_type a = (const rglyph_t *) _a;
__auto_type b = (const rglyph_t *) _b;
return a->charcode == b->charcode;
}
static rglyph_t *
alloc_glyph (rfont_t *font)
{
return PR_RESNEW(font->glyphs);
}
static void
free_glyph (rfont_t *font, rglyph_t *glyph)
{
PR_RESFREE (font->glyphs, glyph);
}
static void
copy_glypn (rglyph_t *glyph, FT_GlyphSlot src_glyph)
copy_glyph (rglyph_t *glyph, FT_GlyphSlot src_glyph)
{
int dst_pitch = glyph->font->scrap.width;
byte *dst = glyph->font->scrap_bitmap + glyph->rect->x + glyph->rect->y * dst_pitch;
@ -84,14 +57,6 @@ copy_glypn (rglyph_t *glyph, FT_GlyphSlot src_glyph)
}
}
static void
glyphmap_free_glyph (void *_g, void *_f)
{
rglyph_t *glyph = _g;
rfont_t *font = _f;
free_glyph (font, glyph);
}
VISIBLE void
R_FontInit (void)
{
@ -106,19 +71,17 @@ R_FontFree (rfont_t *font)
if (font->face) {
FT_Done_Face (font->face);
}
if (font->glyphmap) {
Hash_DelTable (font->glyphmap);
}
if (font->scrap.rects || font->scrap.free_rects) {
R_ScrapDelete (&font->scrap);
}
free (font->glyphs);
free (font->scrap_bitmap);
free (font->font_resource);
free (font);
}
VISIBLE rfont_t *
R_FontLoad (QFile *font_file, int size, const int *preload)
R_FontLoad (QFile *font_file, int size)
{
byte *font_data = QFS_LoadFile (font_file, 0);
if (!font_data) {
@ -132,54 +95,35 @@ R_FontLoad (QFile *font_file, int size, const int *preload)
return 0;
}
font->glyphmap = Hash_NewTable (0x10000, 0, glyphmap_free_glyph, font, 0);
Hash_SetHashCompare (font->glyphmap, glyph_get_hash, glyph_compare);
FT_Set_Pixel_Sizes(font->face, 0, size);
int pixels = 0;
for (const int *c = preload; *c; c++) {
rglyph_t search = { .charcode = *c };
rglyph_t *glyph = Hash_FindElement (font->glyphmap, &search);
if (glyph) {
continue;
}
if (FT_Load_Char(font->face, *c, FT_LOAD_DEFAULT)) {
continue;
}
for (FT_Long gind = 0; gind < font->face->num_glyphs; gind++) {
FT_Load_Glyph (font->face, gind, FT_LOAD_DEFAULT);
__auto_type g = font->face->glyph;
pixels += g->bitmap.width * g->bitmap.rows;
glyph = alloc_glyph (font);
glyph->charcode = *c;
Hash_AddElement (font->glyphmap, glyph);
}
Hash_FlushTable (font->glyphmap);
pixels = sqrt (2 * pixels);
pixels = BITOP_RUP (pixels);
R_ScrapInit (&font->scrap, pixels, pixels);
font->scrap_bitmap = calloc (1, pixels * pixels);
font->num_glyphs = font->face->num_glyphs;
font->glyphs = malloc (font->num_glyphs * sizeof (rglyph_t));
for (const int *c = preload; *c; c++) {
rglyph_t search = { .charcode = *c };
rglyph_t *glyph = Hash_FindElement (font->glyphmap, &search);
if (glyph) {
continue;
}
if (FT_Load_Char(font->face, *c, FT_LOAD_RENDER)) {
continue;
}
__auto_type g = font->face->glyph;
int width = g->bitmap.width;
int height = g->bitmap.rows;
glyph = alloc_glyph (font);
for (FT_Long gind = 0; gind < font->face->num_glyphs; gind++) {
rglyph_t *glyph = &font->glyphs[gind];
FT_Load_Glyph (font->face, gind, FT_LOAD_DEFAULT);
__auto_type slot = font->face->glyph;
FT_Render_Glyph (slot, FT_RENDER_MODE_NORMAL);
int width = slot->bitmap.width;
int height = slot->bitmap.rows;
glyph->font = font;
glyph->rect = R_ScrapAlloc (&font->scrap, width, height);
glyph->bearing = (vec2i_t) { g->bitmap_left, g->bitmap_top };
glyph->advance = g->advance.x;
glyph->charcode = *c;
Hash_AddElement (font->glyphmap, glyph);
glyph->bearing = (vec2i_t) { slot->bitmap_left, slot->bitmap_top };
glyph->advance = slot->advance.x;
glyph->charcode = gind;
copy_glypn (glyph, g);
copy_glyph (glyph, slot);
}
return font;

View File

@ -339,10 +339,9 @@ bi_Font_Load (progs_t *pr, void *_res)
{
const char *font_path = P_GSTRING (pr, 0);
int font_size = P_INT (pr, 1);
int *preload = (int *) P_GPOINTER (pr, 2);
QFile *font_file = QFS_FOpenFile (font_path);
rfont_t *font = R_FontLoad (font_file, font_size, preload);
rfont_t *font = R_FontLoad (font_file, font_size);
r_funcs->Draw_AddFont (font);
}
@ -405,7 +404,7 @@ static builtin_t builtins[] = {
bi(Draw_Line, 5, p(int), p(int), p(int), p(int), p(int)),
bi(Draw_Crosshair, 5, p(int), p(int), p(int), p(int)),
bi(Font_Load, 3, p(string), p(int), p(ptr)),
bi(Font_Load, 2, p(string), p(int)),
bi(Font_String, 3, p(int), p(int), p(string)),
{0}
};

View File

@ -0,0 +1,113 @@
/*
r_font.c
Renderer font management management
Copyright (C) 2022 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2022/8/26
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <math.h>
#include "QF/quakefs.h"
#include "QF/sys.h"
#include "r_font.h"
#include "r_text.h"
#include "compat.h"
rshaper_t *
RText_NewShaper (rfont_t *font)
{
rshaper_t *shaper = malloc (sizeof (rshaper_t));
shaper->rfont = font;
shaper->font = hb_ft_font_create (font->face, 0);
shaper->buffer = hb_buffer_create ();
shaper->features = (r_hb_featureset_t) DARRAY_STATIC_INIT (4);
hb_buffer_allocation_successful (shaper->buffer);
return shaper;
}
void
RText_DeleteShaper (rshaper_t *shaper)
{
hb_buffer_destroy (shaper->buffer);
hb_font_destroy (shaper->font);
DARRAY_CLEAR (&shaper->features);
free (shaper);
}
void
RText_AddFeature (rshaper_t *shaper, hb_feature_t feature)
{
DARRAY_APPEND (&shaper->features, feature);
}
void
RText_ShapeText (rshaper_t *shaper, rtext_t *text)
{
hb_buffer_reset (shaper->buffer);
hb_buffer_set_direction (shaper->buffer, text->direction);
hb_buffer_set_script (shaper->buffer, text->script);
const char *lang = text->language;
hb_buffer_set_language (shaper->buffer,
hb_language_from_string (lang, strlen (lang)));
size_t length = strlen (text->text);
hb_buffer_add_utf8 (shaper->buffer, text->text, length, 0, length);
hb_shape (shaper->font, shaper->buffer,
shaper->features.a, shaper->features.size);
}
void
RText_RenderText (rshaper_t *shaper, rtext_t *text, int x, int y,
rtext_render_t *render, void *data)
{
RText_ShapeText (shaper, text);
unsigned count;
__auto_type glyphInfo = hb_buffer_get_glyph_infos (shaper->buffer, &count);
__auto_type glyphPos = hb_buffer_get_glyph_positions (shaper->buffer,
&count);
rfont_t *font = shaper->rfont;
for (unsigned i = 0; i < count; i++) {
rglyph_t *glyph = &font->glyphs[glyphInfo[i].codepoint];
int xp = x + glyphPos[i].x_offset / 64;
int yp = y + glyphPos[i].y_offset / 64;
int xa = glyphPos[i].x_advance / 64;
int ya = glyphPos[i].y_advance / 64;
xp += glyph->bearing[0];
yp -= glyph->bearing[1];
render (glyph, xp, yp, data);
x += xa;
y += ya;
}
}

View File

@ -68,6 +68,7 @@
#include "QF/ui/view.h"
#include "r_font.h"
#include "r_text.h"
#include "r_internal.h"
#include "vid_vulkan.h"
@ -1041,6 +1042,25 @@ Vulkan_Draw_AddFont (rfont_t *font, vulkan_ctx_t *ctx)
}
}
typedef struct {
drawframe_t *dframe;
qpic_t pic;
subpic_t *subpic;
quat_t color;
} rgctx_t;
static void
vulkan_render_glyph (rglyph_t *glyph, int x, int y, void *_rgctx)
{
rgctx_t *rgctx = _rgctx;
*((vrect_t **) &rgctx->subpic->rect) = glyph->rect;
int gw = glyph->rect->width;
int gh = glyph->rect->height;
draw_pic (x, y, gw, gh, &rgctx->pic, 0, 0, gw, gh, rgctx->color,
rgctx->dframe);
}
void
Vulkan_Draw_FontString (int x, int y, const char *str, vulkan_ctx_t *ctx)
{
@ -1054,36 +1074,32 @@ Vulkan_Draw_FontString (int x, int y, const char *str, vulkan_ctx_t *ctx)
drawvert_t *quad_verts = dframe->quad_verts;
dframe->num_quads = dframe->num_iaquads;
dframe->quad_verts = dframe->iaquad_verts;
subpic_t glyph_subpic = {
.width = dctx->font->scrap.width,
.height = dctx->font->scrap.height,
.size = 1.0 / dctx->font->scrap.width,
};
struct {
qpic_t pic;
subpic_t *subpic;
} glyph_pic = {
rgctx_t rgctx = {
.dframe = dframe,
.pic = {
.width = 1,
.height = 1,
},
.subpic = &glyph_subpic,
.color = { 0.5, 1, 0.6, 1 },
};
quat_t color = { 0.5, 1, 0.6, 1 };
for (const char *c = str; *c; c++) {
rglyph_t search = { .charcode = *c };
rglyph_t *glyph = Hash_FindElement (dctx->font->glyphmap, &search);
if (!glyph) {
continue;
}
*((vrect_t **) &glyph_subpic.rect) = glyph->rect;
int gw = glyph->rect->width;
int gh = glyph->rect->height;
vec2i_t pos = (vec2i_t) { x, y } + glyph->bearing * (vec2i_t) { 1, -1 };
draw_pic (pos[0], pos[1], gw, gh, &glyph_pic.pic,
0, 0, gw, gh, color, dframe);
x += glyph->advance / 64;
}
rtext_t text = {
.text = str,
.language = "en",
.script = HB_SCRIPT_LATIN,
.direction = HB_DIRECTION_LTR,
};
rshaper_t *shaper = RText_NewShaper (dctx->font);
RText_RenderText (shaper, &text, x, y, vulkan_render_glyph, &rgctx);
RText_DeleteShaper (shaper);
dframe->num_iaquads = dframe->num_quads;
dframe->iaquad_verts = dframe->quad_verts;
dframe->num_quads = num_quads;