Blob Blame Raw

#include <cctype>

#include <algorithm>
#include <iostream>

#include <glibmm.h>

#include "layout.h"
#include "layoutdraw.h"

#include "surface.h"
#include "log.h"

#include "freetypeview.h"


static void
fill_rect(
	Surface &surface,
	const IntPair2 &rect,
	const Color &color )
{
	const IntPair2 b = rect
					 & IntPair2( IntVector2(0, 0), IntVector2(surface.width(), surface.height()) );
	if (b.empty())
		return;
	const IntVector2 size = b.size();
	const int pitch = surface.pitch();
	const int step = pitch - size.x;
	for(Color *p = &surface[b.p0.y][b.p0.x], *end = p + pitch*size.y; p != end; p += step)
		for(Color *row_end = p + size.x; p < row_end; ++p)
			*p = color;
}

static bool
is_font_file(const std::string &name) {
	static const char *ext[] = { ".otf", ".otc", ".ttf", ".ttc" };
	static const int count = (int)(sizeof(ext)/sizeof(*ext));

	bool found = false;
	for(int i = 0; i < count; ++i) {
		const char *e = ext[i];
		int len = 0; while(e[len]) ++len;
		if ((int)name.size() < len) continue;
		found = true;
		for(int j = 0, k = name.size() - len; j < len; ++j, ++k)
			if (tolower(name[k]) != e[j]) { found = false; break; }
		if (found) break;
	}
	return found;
}



FreeTypeView::FreeTypeView():
	p0(new View::Point( Vector2(0, 0) )),
	px(new View::Point( Vector2(1, 0) )),
	py(new View::Point( Vector2(0, 1) )),
	bounds_p0(new View::Point( Vector2(-2, -2) )),
	bounds_p1(new View::Point( Vector2( 2,  2) ))
{
	prev_p0 = p0->position;
	transform *= Matrix().scaling(Vector2(50, 50));
	points.push_back(p0);
	points.push_back(px);
	points.push_back(py);
	points.push_back(bounds_p0);
	points.push_back(bounds_p1);
	
	set_size_request(400, 400);
	
	//params.family = "Chancery Uralic";
	//params.family = "DejaVu";
	params.family = "data/font/Bradley Gratis/Bradley Gratis.ttf";
	params.bold = false;
	params.italic = false;
	params.size = 24;

	params.spacing = Vector2(3, 2);
	
	params.hinting = false;
	params.antialiasing = true;
	params.invert = false;
	
	params.matrix = Matrix().translation(Vector2(15.5, 18.4));
	params.color = Color(1, 1, 0, 1);

	params.text =
		"Lorem ipsum dolor sit amet, consectetur adipisici elit,"
		"sed eiusmod tempor incidunt ut labore et dolore magna aliqua."
		"Ut enim ad minim veniam, quis nostrud exercitation ullamco "
		"laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure"
		"reprehenderit in voluptate velit esse cillum dolore eu fugiat "
		"nulla pariatur. Excepteur sint obcaecat cupiditat non proident, "
		"sunt in culpa qui officia deserunt mollit anim id est laborum.";
	//params.text = "1\n2\n3\n";
	
	params.alignment = -1;
	params.justify = false;
	params.wrap_width = 600;
	params.alignment_by_origin = true;
	
	params.origin = Vector2(0.25, 0.8);
	
	Glib::signal_timeout().connect(
		sigc::mem_fun(*this, &FreeTypeView::on_timeout),
		50,
		Glib::PRIORITY_DEFAULT_IDLE );
}

FreeTypeView::~FreeTypeView()
	{ }

bool
FreeTypeView::on_timeout() {
	if (!is_visible()) return false;
	p0->position.x += 0.001;
	point_motion(p0);
	return true;
}
	
void
FreeTypeView::update_surface() {
	this->surface.clear();
	
	// prepare matrix
	Matrix to_pixels = transform_to_pixels();
	Matrix in_matrix(
		Vector3(px->position - p0->position)/50,
		Vector3(py->position - p0->position)/50,
		Vector3(p0->position, 1) );
	params.matrix = transform_to_pixels() * in_matrix;
	if (fabs(params.matrix.det()) <= real_precision_cub)
		return;

	// calc bounds
	const int width = get_allocated_width();
	const int height = get_allocated_height();
	Pair2 bounds = to_pixels.transform_bounds(
		  Pair2(bounds_p0->position)
		.expand(bounds_p1->position) );
	bounds &= Pair2(Vector2(), Vector2(width, height));
	if (bounds.empty())
		return;
	IntPair2 int_bounds = IntPair2( IntVector2( (int)floor(bounds.p0.x + real_precision),
												(int)floor(bounds.p0.y + real_precision) ),
									IntVector2( (int)ceil (bounds.p1.x - real_precision),
												(int)ceil (bounds.p1.y - real_precision) ))
						& IntPair2( IntVector2(),
									IntVector2(width, height) );
	if (int_bounds.empty())
		return;

	// calc some measures
	const Vector2 origin(
		clamp(params.origin.x, 0, 1),
		clamp(params.origin.y, 0, 1) );
	const Vector2 spacing(
		params.spacing.x > real_precision ? params.spacing.x : 1,
		params.spacing.y > real_precision ? params.spacing.y : 1 );
	const Matrix2 spacing_matrix(
		Vector2(spacing.x, 0),
		Vector2(0, spacing.y) );
	if (fabs(spacing_matrix.det()) <= real_precision_sqr)
		return;
	
	// init
	Glib::RefPtr<Pango::FontMap> font_map = Layout::create_fontmap(params.hinting, params.antialiasing);
	Glib::RefPtr<Pango::Context> context = font_map->create_context();
	
	// load font
	Glib::RefPtr<Pango::Font> font;
	Pango::FontDescription font_desc;
	font_desc.set_family(params.family);
	font_desc.set_weight(params.bold ? Pango::WEIGHT_BOLD : Pango::WEIGHT_NORMAL);
	font_desc.set_style(params.italic ? Pango::STYLE_ITALIC : Pango::STYLE_NORMAL);
	font_desc.set_absolute_size(params.size * Pango::SCALE);
	if (is_font_file(params.family))
		font = Layout::load_font_file(context, params.family, font_desc);
	if (!font)
		font = context->load_font(font_desc);
	font_desc = font->describe();
	
	// create pango layout
	Glib::RefPtr<Pango::Layout> pango_layout = Pango::Layout::create(context);
	pango_layout->set_font_description(font_desc);
	pango_layout->set_wrap(Pango::WRAP_WORD_CHAR);
	pango_layout->set_width(
		params.wrap_width > real_precision ? (int)round(params.wrap_width*Pango::SCALE) : -1 );
	pango_layout->set_alignment(
		params.alignment < 0 ? Pango::ALIGN_LEFT :
		params.alignment > 0 ? Pango::ALIGN_RIGHT : Pango::ALIGN_CENTER );
	pango_layout->set_justify(params.justify);
	pango_layout->set_text(params.text);
	
	// create lab layout
	RefPtr<Layout> layout(new Layout(pango_layout));
	const Pair2 &logical_bounds = layout->get_logical_bounds();
	const Vector2 layout_position(
		logical_bounds.p1.x * origin.x,
		logical_bounds.p1.y * origin.y );
	RefPtr<TransformedLayout> transformed_layout(
		new TransformedLayout(layout, spacing_matrix, Matrix2()) );

	// prepare surface
	DataSurface surface(width, height);
	if (params.invert)
		fill_rect(surface, int_bounds, params.color.get_mult_alpha());

	// draw
	LayoutDraw::draw(
		surface,
		int_bounds,
		transformed_layout,
		params.matrix,
		params.color,
		params.invert,
		-layout_position,
		params.alignment_by_origin ? &origin.x : nullptr );

	// to cairo
	this->surface = surface.to_cairo_surface(true);
}

void
FreeTypeView::on_point_motion(const View::PointPtr &point) {
	if (point == p0) {
		px->position += p0->position - prev_p0;
		py->position += p0->position - prev_p0;
		prev_p0 = p0->position;
	}
	queue_draw();
}

void
FreeTypeView::on_draw_view(const Cairo::RefPtr<Cairo::Context> &context) {
	update_surface();

	const Real ps = get_pixel_size();

	context->save();
	context->set_line_width(ps);

	// draw transform
	context->set_source_rgba(0, 0, 1, 1);
	context->move_to(px->position.x, px->position.y);
	context->line_to(p0->position.x, p0->position.y);
	context->line_to(py->position.x, py->position.y);
	context->stroke();

	// draw bounds
	context->set_source_rgba(0, 0, 1, 1);
	context->move_to(bounds_p0->position.x, bounds_p0->position.y);
	context->line_to(bounds_p1->position.x, bounds_p0->position.y);
	context->line_to(bounds_p1->position.x, bounds_p1->position.y);
	context->line_to(bounds_p0->position.x, bounds_p1->position.y);
	context->close_path();
	context->stroke();

	if (surface) {
		context->save();
		context->transform(transform_from_pixels().to_cairo());
		context->set_source(surface, 0, 0);
		context->paint();
		context->restore();
	}
	context->restore();
}