Blob Blame Raw

#include <cstring>

#include <freetype2/ft2build.h>
#include FT_BITMAP_H

#include <pango/pangoft2.h>


#include "surface.h"
#include "matrix.h"

#include "labpangorenderer.h"



struct _LabPangoRenderer {
	PangoRenderer parent_instance;
	LabPangoRendererParams params;
};

struct _LabPangoRendererClass {
  PangoRendererClass parent_class;
};

G_DEFINE_TYPE(LabPangoRenderer, lab_pango_renderer, PANGO_TYPE_RENDERER)


static inline int floor_ft_int(int x) { return (x & -64)/64; }
static inline int ceil_ft_int(int x) { return ((x-1) & -64)/64 + 1; }


static void
put_ft_bitmap(
	Surface &surface,
	const IntPair2 &bounds,
	const FT_Bitmap &bitmap,
	int x,
	int y,
	const Color &color )
{
	const IntPair2 b = bounds
					 & IntPair2( IntVector2(0, 0), IntVector2(surface.width(), surface.height()) )
					 & IntPair2( IntVector2(x, y), IntVector2(x + bitmap.width, y + bitmap.rows) );
	if (b.empty())
		return;
	
	const IntVector2 size = b.size();
	const IntVector2 sp0 = b.p0 - IntVector2(x, y);
	const int pitch = surface.pitch();

	Color *dp = &surface[b.p0.y][b.p0.x];
	const int dstep = pitch - size.x;
	
	const unsigned char *sp = bitmap.buffer + sp0.y*bitmap.pitch + sp0.x;
	const int sstep = bitmap.pitch - size.x;
	
	for(const Color *end = dp + pitch*size.y; dp != end; dp += dstep, sp += sstep) {
		for(const Color *row_end = dp + size.x; dp < row_end; ++dp, ++sp) {
			const Real a = *sp * (1/255.0);
			*dp = *dp*(1-a) + color*a;
		}
	}
}


static void
put_ft_bitmap_mono(
	Surface &surface,
	const IntPair2 &bounds,
	const FT_Bitmap &bitmap,
	int x,
	int y,
	const Color &color )
{
	const IntPair2 b = bounds
					 & IntPair2( IntVector2(0, 0), IntVector2(surface.width(), surface.height()) )
					 & IntPair2( IntVector2(x, y), IntVector2(bitmap.width, bitmap.rows) );
	if (b.empty())
		return;
	
	const IntVector2 size = b.size();
	const IntVector2 sp0 = b.p0 - IntVector2(x, y);
	const int pitch = surface.pitch();

	Color *dp = &surface[b.p0.y][b.p0.x];
	const int dstep = pitch - size.x;
	
	const unsigned char *sp = bitmap.buffer + sp0.y*bitmap.pitch;
	const int sstep = bitmap.pitch;
	const int sp1x = sp0.x + size.x;
	
	for(const Color *end = dp + pitch*size.y; dp != end; dp += dstep, sp += sstep)
		for(int bit = sp0.x; bit < sp1x; ++dp, ++bit)
			if (sp[bit/8] & (128 >> (bit%8)))
				*dp = color;
}


static void
lab_pango_renderer_draw_glyph(
	PangoRenderer *renderer,
	PangoFont     *font,
	PangoGlyph     glyph,
	double         x,
	double         y )
{
	const LabPangoRendererParams &params = ((LabPangoRenderer*)renderer)->params;
	if (!params.surface)
		return;

	const IntPair2 surface_bounds(
		IntVector2(0, 0),
		IntVector2(params.surface->width(), params.surface->height()) );

	FT_Face face = pango_fc_font_lock_face((PangoFcFont*)font);
	
	// find glyph center for proper scaling
	Vector2 origin;
	FT_Set_Transform(face, nullptr, nullptr);
	if (!FT_Load_Glyph(face, glyph, FT_LOAD_NO_HINTING)) {
		const FT_Glyph_Metrics &metrics = face->glyph->metrics;
		origin = Vector2(metrics.horiBearingX - metrics.vertBearingX, 0)/64;
	}
	
	// prepare transformation
	const Vector2 pos = (params.matrix*Vector3(x + origin.x, y + origin.y, 1)).vec2()
					  - params.glyph_matrix*origin;
	const int fx = (int)round(pos.x*64);
	const int fy = (int)round(pos.y*64);
	int ix = fx / 64;
	int iy = fy / 64;
	FT_Vector vec = {};
	vec.x = fx % 64;
	vec.y = fy % 64;
	if (params.hinting) {
		if (vec.x >= 32) ++ix;
		if (vec.y >= 32) ++iy;
		vec.x = 0;
		vec.y = 0;
	}
	vec.y = -vec.y;

	FT_Matrix mat = {};
	mat.xx = (int)round( params.glyph_matrix.m00*65536);
	mat.xy = (int)round(-params.glyph_matrix.m10*65536);
	mat.yx = (int)round(-params.glyph_matrix.m01*65536);
	mat.yy = (int)round( params.glyph_matrix.m11*65536);
	
	// draw
	FT_Set_Transform(face, &mat, &vec);
	if (!FT_Load_Glyph(face, glyph, params.hinting ? FT_LOAD_DEFAULT : FT_LOAD_NO_HINTING)) {
		const FT_Glyph_Metrics &metrics = face->glyph->metrics;
		IntPair2 glyph_bounds(
			IntVector2(
				floor_ft_int( metrics.horiBearingX) + ix,
				floor_ft_int(-metrics.horiBearingY) + iy ),
			IntVector2(
				ceil_ft_int( metrics.horiBearingX + metrics.width ) + ix,
				ceil_ft_int(-metrics.horiBearingY + metrics.height) + iy ));
		if (!(params.bounds & glyph_bounds).empty()) {
			if (!FT_Render_Glyph(face->glyph, params.antialiasing ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO)) {
				if (params.antialiasing) {
					put_ft_bitmap(
						*params.surface,
						surface_bounds,
						face->glyph->bitmap,
						face->glyph->bitmap_left + ix,
						iy - face->glyph->bitmap_top,
						params.color );
				} else {
					put_ft_bitmap_mono(
						*params.surface,
						surface_bounds,
						face->glyph->bitmap,
						face->glyph->bitmap_left + ix,
						iy - face->glyph->bitmap_top,
						params.color );
				}
			}
		}
	}

	pango_fc_font_unlock_face((PangoFcFont*)font);
}


static void
lab_pango_renderer_init(LabPangoRenderer *renderer) {
	memset(&renderer->params, 0, sizeof(renderer->params));
	renderer->params = LabPangoRendererParams();
}

static void
lab_pango_renderer_class_init(LabPangoRendererClass *klass)
{
	PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS(klass);
	renderer_class->draw_glyph = lab_pango_renderer_draw_glyph;
}


static void
lab_pango_fc_pattern_set_substitute(FcPattern *pattern, gpointer data) {
	const int &args = *(const int*)data;
	FcPatternDel(pattern, FC_HINTING);
	FcPatternDel(pattern, FC_ANTIALIAS);
	FcPatternAddBool(pattern, FC_HINTING, (args & 1) ? FcTrue : FcFalse);
	FcPatternAddBool(pattern, FC_ANTIALIAS, (args & 2) ? FcTrue : FcFalse);
}


PangoRenderer*
lab_pango_renderer_new(LabPangoRendererParams *params) {
	PangoRenderer *renderer = (PangoRenderer*)g_object_new(LAB_TYPE_PANGO_RENDERER, nullptr);
	lab_pango_renderer_set_params((LabPangoRenderer*)renderer, params);
	return renderer;
}

void
lab_pango_renderer_set_params(LabPangoRenderer *renderer, LabPangoRendererParams *params)
	{ if (params) renderer->params = *params; }

void
lab_pango_renderer_get_params(LabPangoRenderer *renderer, LabPangoRendererParams *params)
	{ if (params) *params = renderer->params; }


PangoFontMap*
lab_pango_font_map_new(bool hinting, bool antialiasing) {
	static int args[4] { 0, 1, 2, 3 }; // to avoid encoding of int into pointer
	
	PangoFontMap *font_map = pango_ft2_font_map_new();
	pango_ft2_font_map_set_default_substitute(
		(PangoFT2FontMap*)font_map,
		lab_pango_fc_pattern_set_substitute,
		args + (hinting ? 1 : 0) + (antialiasing ? 2 : 0),
		nullptr );
	return font_map;
}