Files
strans/font.c

167 lines
3.9 KiB
C

#include "dat.h"
#include <pango/pangocairo.h>
#include "fn.h"
static PangoFontMap *fontmap;
static PangoContext *context;
static PangoLayout *layout;
static void
textclear(void)
{
if(layout != nil)
g_object_unref(layout);
if(context != nil)
g_object_unref(context);
if(fontmap != nil)
g_object_unref(fontmap);
layout = nil;
context = nil;
fontmap = nil;
}
static void
setfont(void)
{
PangoFontDescription *font;
PangoRectangle r;
int size;
font = pango_font_description_new();
pango_font_description_set_family(font, "sans");
pango_font_description_set_absolute_size(font, Fontsz * PANGO_SCALE);
pango_layout_set_font_description(layout, font);
pango_layout_set_text(layout, "Mg", -1);
pango_layout_get_pixel_extents(layout, nil, &r);
if(r.height > 0){
/* Fontsz is the popup row height, not a point size. */
size = Fontsz * PANGO_SCALE * Fontsz / r.height;
pango_font_description_set_absolute_size(font, max(size, PANGO_SCALE));
pango_layout_set_font_description(layout, font);
}
pango_font_description_free(font);
}
int
textinit(char **path, int npath)
{
GError *err;
int i;
textclear();
if(path == nil || npath < 1 || npath > Maxfonts){
fprint(2, "strans: popup: need 1-%d font files\n", Maxfonts);
return 0;
}
fontmap = pango_cairo_font_map_new();
if(fontmap == nil){
fprint(2, "strans: popup: can't initialize PangoCairo\n");
return 0;
}
for(i = 0; i < npath; i++){
if(path[i] == nil){
fprint(2, "strans: popup: invalid font file\n");
textclear();
return 0;
}
err = nil;
if(!pango_font_map_add_font_file(fontmap, path[i], &err)){
fprint(2, "strans: popup: can't register font: %s: %s\n",
path[i], err == nil ? "unknown error" : err->message);
if(err != nil)
g_error_free(err);
textclear();
return 0;
}
}
pango_cairo_font_map_set_resolution(PANGO_CAIRO_FONT_MAP(fontmap), 96);
context = pango_font_map_create_context(fontmap);
if(context != nil)
layout = pango_layout_new(context);
if(context == nil || layout == nil){
fprint(2, "strans: popup: can't create text layout\n");
textclear();
return 0;
}
pango_layout_set_single_paragraph_mode(layout, TRUE);
setfont();
return 1;
}
static int
settext(Str *s)
{
char utf[Maxutf];
int n;
if(layout == nil || s == nil || s->n <= 0)
return 0;
n = stoutf(s, utf, sizeof utf);
pango_layout_set_text(layout, utf, n);
return n > 0;
}
static void
textextents(PangoRectangle *r)
{
PangoRectangle ink, logical;
int x0, y0, x1, y1;
pango_layout_get_pixel_extents(layout, &ink, &logical);
x0 = min(ink.x, logical.x);
y0 = min(ink.y, logical.y);
x1 = max(ink.x + ink.width, logical.x + logical.width);
y1 = max(ink.y + ink.height, logical.y + logical.height);
r->x = x0;
r->y = y0;
r->width = max(x1 - x0, 0);
r->height = max(y1 - y0, 0);
}
int
textwidth(Str *s)
{
PangoRectangle r;
if(!settext(s))
return 0;
textextents(&r);
return r.width;
}
void
textdraw(u32int *buf, int w, int h, int x, int y, Str *s)
{
PangoRectangle r;
cairo_surface_t *surface;
cairo_t *cr;
int stride;
if(buf == nil || w <= 0 || h <= 0 || !settext(s))
return;
stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, w);
if(stride < 0 || (vlong)stride != (vlong)w * sizeof buf[0])
return;
surface = cairo_image_surface_create_for_data((uchar*)buf,
CAIRO_FORMAT_RGB24, w, h, stride);
if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS){
cairo_surface_destroy(surface);
return;
}
cairo_surface_mark_dirty(surface);
cr = cairo_create(surface);
if(cairo_status(cr) == CAIRO_STATUS_SUCCESS){
pango_cairo_update_layout(cr, layout);
textextents(&r);
/* A fallback face must not paint into another popup row. */
cairo_rectangle(cr, 0, y, w, Fontsz);
cairo_clip(cr);
cairo_move_to(cr, x - r.x, y + (Fontsz - r.height) / 2 - r.y);
cairo_set_source_rgb(cr, 0, 0, 0);
pango_cairo_show_layout(cr, layout);
}
cairo_destroy(cr);
cairo_surface_flush(surface);
cairo_surface_destroy(surface);
}