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 void
put_ft_bitmap(Surface &surface, FT_Bitmap &bitmap, int x, int y, const Color &color) {
	if (x >= surface.width() || y >= surface.height())
		return;
	if (x + (int)bitmap.width <= 0 || y + (int)bitmap.rows <= 0)
		return;
	
	const int bx = std::max(0, -x);
	const int by = std::max(0, -y);
	x = std::max(0, x);
	y = std::max(0, y);
	const int w = std::min(surface.width() - x, (int)bitmap.width - bx);
	const int h = std::min(surface.height() - y, (int)bitmap.rows - by);
	const int pitch = surface.pitch();

	Color *dst_pixel = &surface[y][x];
	const int dst_row_step = pitch - w;
	
	const unsigned char *src_pixel = bitmap.buffer + by*bitmap.pitch + bx;
	const int src_row_step = bitmap.pitch - w;
	
	for(const Color *end = dst_pixel + pitch*h; dst_pixel != end; dst_pixel += dst_row_step, src_pixel += src_row_step) {
		for(const Color *row_end = dst_pixel + w; dst_pixel != row_end; ++dst_pixel, ++src_pixel) {
			Real a = *src_pixel * (1/255.0);
			*dst_pixel = *dst_pixel*(1-a) + color*a;
		}
	}
}


static void
put_ft_bitmap_mono(Surface &surface, FT_Bitmap &bitmap, int x, int y, const Color &color) {
	if (x >= surface.width() || y >= surface.height())
		return;
	if (x + (int)bitmap.width <= 0 || y + (int)bitmap.rows <= 0)
		return;
	
	const int bx = std::max(0, -x);
	const int by = std::max(0, -y);
	x = std::max(0, x);
	y = std::max(0, y);
	const int w = std::min(surface.width() - x, (int)bitmap.width - bx);
	const int h = std::min(surface.height() - y, (int)bitmap.rows - by);
	const int pitch = surface.pitch();

	Color *dst_pixel = &surface[y][x];
	const int dst_row_step = pitch - w;
	
	const unsigned char *src_row = bitmap.buffer + by*bitmap.pitch;
	const int src_row_end = bx + w;
	
	for(const Color *end = dst_pixel + pitch*h; dst_pixel != end; dst_pixel += dst_row_step, src_row += bitmap.pitch)
		for(int src_x = bx; src_x < src_row_end; ++dst_pixel, ++src_x)
			if (src_row[src_x/8] & (128 >> (src_x%8)))
				*dst_pixel = 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;
	
	Vector3 pos = params.matrix*Vector3(x, y, 1);
	
	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);
	
	FT_Face face = pango_fc_font_lock_face((PangoFcFont*)font);
	FT_Set_Transform(face, &mat, &vec);
	if (!FT_Load_Glyph(face, glyph, params.hinting ? FT_LOAD_DEFAULT : FT_LOAD_NO_HINTING)) {
		if (!FT_Render_Glyph(face->glyph, params.antialiasing ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO)) {
			if (params.antialiasing) {
				put_ft_bitmap(
					*params.surface,
					face->glyph->bitmap,
					face->glyph->bitmap_left + ix,
					iy - face->glyph->bitmap_top,
					params.color );
			} else {
				put_ft_bitmap_mono(
					*params.surface,
					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;
}