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;
	Surface *surface;
	Color color;
	Matrix matrix;
};

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
lab_pango_renderer_draw_glyph(
	PangoRenderer *renderer,
	PangoFont     *font,
	PangoGlyph     glyph,
	double         x,
	double         y )
{
	LabPangoRenderer *lab_renderer = (LabPangoRenderer*)renderer;
	if (!lab_renderer->surface)
		return;
	
	Vector3 pos = lab_renderer->matrix*Vector3(x, y, 1);
	
	const int fx = (int)round(pos.x*64);
	const int fy = (int)round(pos.y*64);
	const int ix = fx / 64;
	const int iy = fy / 64;
	FT_Vector vec = {};
	vec.x = fx % 64;
	vec.y = fy % 64;
	
	FT_Face face = pango_fc_font_lock_face((PangoFcFont*)font);
	FT_Set_Transform(face, nullptr, &vec);
	if (!FT_Load_Glyph(face, glyph, FT_LOAD_NO_HINTING)) {
		if (!FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) {
			put_ft_bitmap(
				*lab_renderer->surface,
				face->glyph->bitmap,
				face->glyph->bitmap_left + ix,
				iy - face->glyph->bitmap_top,
				lab_renderer->color );
		}
	}
	pango_fc_font_unlock_face((PangoFcFont*)font);
}


static void
lab_pango_renderer_init(LabPangoRenderer *renderer) {
	memset( renderer + sizeof(renderer->parent_instance), 0,
			sizeof(*renderer) - sizeof(renderer->parent_instance) );
	renderer->matrix = Matrix();
}

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_unset_hinting(FcPattern *pattern, gpointer)
	{ FcPatternDel(pattern, FC_HINTING); }

static void
lab_pango_fc_pattern_set_hinting(FcPattern *pattern, gpointer) {
	FcPatternDel(pattern, FC_HINTING);
	FcPatternAddBool(pattern, FC_HINTING, FcFalse);
}


PangoRenderer*
lab_pango_renderer_new()
	{ return (PangoRenderer*)g_object_new(LAB_TYPE_PANGO_RENDERER, nullptr); }

void
lab_pango_renderer_set_matrix(LabPangoRenderer *renderer, Matrix *matrix)
	{ renderer->matrix = matrix ? *matrix : Matrix(); }

void
lab_pango_renderer_get_matrix(LabPangoRenderer *renderer, Matrix *matrix)
	{ if (matrix) *matrix = renderer->matrix; }

void
lab_pango_renderer_set_color(LabPangoRenderer *renderer, Color *color)
	{ renderer->color = color ? *color : Color(); }

void
lab_pango_renderer_get_color(LabPangoRenderer *renderer, Color *color)
	{ if (color) *color = renderer->color; }

void
lab_pango_renderer_set_surface(LabPangoRenderer *renderer, Surface *surface)
	{ renderer->surface = surface; }

Surface*
lab_pango_renderer_get_surface(LabPangoRenderer *renderer)
	{ return renderer->surface; }


PangoFontMap*
lab_pango_font_map_new(bool hinting) {
	PangoFontMap *font_map = pango_ft2_font_map_new();
	pango_ft2_font_map_set_default_substitute(
		(PangoFT2FontMap*)font_map,
		hinting ? &lab_pango_fc_pattern_set_hinting
				: &lab_pango_fc_pattern_unset_hinting,
		nullptr,
		nullptr );
	return font_map;
}