2022-08-27 08:53:03 +00:00
|
|
|
/*
|
2022-12-07 06:11:08 +00:00
|
|
|
font.c
|
2022-08-27 08:53:03 +00:00
|
|
|
|
2022-12-07 06:11:08 +00:00
|
|
|
Font management
|
2022-08-27 08:53:03 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2023-07-01 10:15:22 +00:00
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
|
2022-08-27 08:53:03 +00:00
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/math/bitop.h"
|
|
|
|
|
2022-12-08 07:57:45 +00:00
|
|
|
#include "QF/plugin/vid_render.h"
|
|
|
|
|
2022-12-07 06:11:08 +00:00
|
|
|
#include "QF/ui/font.h"
|
2022-08-27 08:53:03 +00:00
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
static FT_Library ft;
|
|
|
|
|
|
|
|
static void
|
2022-12-07 06:11:08 +00:00
|
|
|
copy_glyph (vrect_t *rect, FT_GlyphSlot src_glyph, font_t *font)
|
2022-08-27 08:53:03 +00:00
|
|
|
{
|
2022-10-20 06:50:53 +00:00
|
|
|
int dst_pitch = font->scrap.width;
|
|
|
|
byte *dst = font->scrap_bitmap + rect->x + rect->y * dst_pitch;
|
2022-08-27 08:53:03 +00:00
|
|
|
int src_pitch = src_glyph->bitmap.pitch;
|
|
|
|
byte *src = src_glyph->bitmap.buffer;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < src_glyph->bitmap.rows; i++) {
|
|
|
|
memcpy (dst, src, src_glyph->bitmap.width);
|
|
|
|
dst += dst_pitch;
|
|
|
|
src += src_pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 08:28:22 +00:00
|
|
|
static void
|
|
|
|
Font_shutdown (void *data)
|
|
|
|
{
|
|
|
|
FT_Done_FreeType (ft);
|
|
|
|
}
|
|
|
|
|
2022-08-27 08:53:03 +00:00
|
|
|
VISIBLE void
|
2022-12-07 06:11:08 +00:00
|
|
|
Font_Init (void)
|
2022-08-27 08:53:03 +00:00
|
|
|
{
|
|
|
|
if (FT_Init_FreeType (&ft)) {
|
|
|
|
Sys_Error ("Could not init FreeType library");
|
|
|
|
}
|
2023-03-05 08:28:22 +00:00
|
|
|
Sys_RegisterShutdown (Font_shutdown, 0);
|
2022-08-27 08:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
2022-12-07 06:11:08 +00:00
|
|
|
Font_Free (font_t *font)
|
2022-08-27 08:53:03 +00:00
|
|
|
{
|
|
|
|
if (font->face) {
|
|
|
|
FT_Done_Face (font->face);
|
|
|
|
}
|
|
|
|
if (font->scrap.rects || font->scrap.free_rects) {
|
|
|
|
R_ScrapDelete (&font->scrap);
|
|
|
|
}
|
2022-10-20 06:50:53 +00:00
|
|
|
free (font->glyph_rects);
|
|
|
|
free (font->glyph_bearings);
|
2022-08-27 08:53:03 +00:00
|
|
|
free (font->scrap_bitmap);
|
|
|
|
free (font->font_resource);
|
|
|
|
free (font);
|
|
|
|
}
|
|
|
|
|
2022-12-07 06:11:08 +00:00
|
|
|
VISIBLE font_t *
|
|
|
|
Font_Load (QFile *font_file, int size)
|
2022-08-27 08:53:03 +00:00
|
|
|
{
|
|
|
|
byte *font_data = QFS_LoadFile (font_file, 0);
|
|
|
|
if (!font_data) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-15 05:32:16 +00:00
|
|
|
size_t font_size = qfs_filesize;
|
2022-12-07 06:11:08 +00:00
|
|
|
font_t *font = calloc (1, sizeof (font_t));
|
2022-08-27 08:53:03 +00:00
|
|
|
font->font_resource = font_data;
|
|
|
|
if (FT_New_Memory_Face (ft, font_data, font_size, 0, &font->face)) {
|
2022-12-07 06:11:08 +00:00
|
|
|
Font_Free (font);
|
2022-08-27 08:53:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Set_Pixel_Sizes(font->face, 0, size);
|
|
|
|
int pixels = 0;
|
2022-09-04 11:56:38 +00:00
|
|
|
for (FT_Long gind = 0; gind < font->face->num_glyphs; gind++) {
|
|
|
|
FT_Load_Glyph (font->face, gind, FT_LOAD_DEFAULT);
|
|
|
|
|
2022-08-27 08:53:03 +00:00
|
|
|
__auto_type g = font->face->glyph;
|
2022-11-19 18:59:01 +00:00
|
|
|
// include padding around the glyph to avoid texel leaks
|
|
|
|
pixels += (g->bitmap.width + 1) * (g->bitmap.rows + 1);
|
2022-08-27 08:53:03 +00:00
|
|
|
}
|
2022-10-07 07:58:04 +00:00
|
|
|
pixels = sqrt (5 * pixels / 4);
|
2022-08-27 08:53:03 +00:00
|
|
|
pixels = BITOP_RUP (pixels);
|
|
|
|
R_ScrapInit (&font->scrap, pixels, pixels);
|
|
|
|
font->scrap_bitmap = calloc (1, pixels * pixels);
|
2022-09-04 11:56:38 +00:00
|
|
|
font->num_glyphs = font->face->num_glyphs;
|
2022-10-20 06:50:53 +00:00
|
|
|
font->glyph_rects = malloc (font->num_glyphs * sizeof (vrect_t));
|
|
|
|
font->glyph_bearings = malloc (font->num_glyphs * sizeof (vec2i_t));
|
2022-09-04 11:56:38 +00:00
|
|
|
|
|
|
|
for (FT_Long gind = 0; gind < font->face->num_glyphs; gind++) {
|
2022-10-20 06:50:53 +00:00
|
|
|
vrect_t *rect = &font->glyph_rects[gind];
|
|
|
|
vec2i_t *bearing = &font->glyph_bearings[gind];
|
2022-09-04 11:56:38 +00:00
|
|
|
FT_Load_Glyph (font->face, gind, FT_LOAD_DEFAULT);
|
|
|
|
__auto_type slot = font->face->glyph;
|
|
|
|
FT_Render_Glyph (slot, FT_RENDER_MODE_NORMAL);
|
2022-11-19 18:59:01 +00:00
|
|
|
// add padding to create a buffer around the glyph to prevent texel
|
|
|
|
// leaks
|
|
|
|
int width = slot->bitmap.width + 1;
|
|
|
|
int height = slot->bitmap.rows + 1;
|
2022-10-20 06:50:53 +00:00
|
|
|
*rect = *R_ScrapAlloc (&font->scrap, width, height);
|
|
|
|
*bearing = (vec2i_t) { slot->bitmap_left, slot->bitmap_top };
|
2022-11-19 18:59:01 +00:00
|
|
|
// shrink the rect so as to NOT include the padding
|
|
|
|
rect->width -= 1;
|
|
|
|
rect->height -= 1;
|
2022-08-27 08:53:03 +00:00
|
|
|
|
2022-10-20 06:50:53 +00:00
|
|
|
copy_glyph (rect, slot, font);
|
2022-08-27 08:53:03 +00:00
|
|
|
}
|
2022-12-08 07:57:45 +00:00
|
|
|
font->fontid = r_funcs->Draw_AddFont (font);
|
2022-08-27 08:53:03 +00:00
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
2023-07-01 10:15:22 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
Font_SystemFont (const char *font_pattern)
|
|
|
|
{
|
|
|
|
auto config = FcInitLoadConfigAndFonts ();
|
|
|
|
auto pattern = FcNameParse ((const FcChar8 *) font_pattern);
|
|
|
|
FcConfigSubstitute (config, pattern, FcMatchPattern);
|
|
|
|
FcDefaultSubstitute (pattern);
|
|
|
|
|
|
|
|
FcResult result;
|
|
|
|
auto font = FcFontMatch (config, pattern, &result);
|
|
|
|
char *filename = 0;
|
|
|
|
if (font) {
|
|
|
|
FcChar8 *str;
|
|
|
|
if (FcPatternGetString (font, FC_FILE, 0, &str) == FcResultMatch) {
|
|
|
|
filename = strdup ((char *) str);
|
|
|
|
}
|
|
|
|
FcPatternDestroy (font);
|
|
|
|
}
|
|
|
|
FcPatternDestroy (pattern);
|
|
|
|
return filename;
|
|
|
|
}
|