/* GridTool.c generated by valac 0.56.17, the Vala compiler
 * generated from GridTool.vala, do not modify */

/*
Copyright (C) 2012 Johan Mattsson

This library is free software; you can redistribute it and/or modify 
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation; either version 3 of the 
License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
Lesser General Public License for more details.
*/

#include "birdfont.h"
#include <gee.h>
#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <cairo-gobject.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	BIRD_FONT_GRID_TOOL_0_PROPERTY,
	BIRD_FONT_GRID_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_grid_tool_properties[BIRD_FONT_GRID_TOOL_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _bird_font_color_unref0(var) ((var == NULL) ? NULL : (var = (bird_font_color_unref (var), NULL)))
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

static gpointer bird_font_grid_tool_parent_class = NULL;
static GeeArrayList* bird_font_grid_tool_horizontal;
static GeeArrayList* bird_font_grid_tool_horizontal = NULL;
static GeeArrayList* bird_font_grid_tool_vertical;
static GeeArrayList* bird_font_grid_tool_vertical = NULL;
static gboolean bird_font_grid_tool_grid_visible;
static gboolean bird_font_grid_tool_grid_visible = FALSE;
gboolean bird_font_grid_tool_ttf_units = FALSE;
gdouble bird_font_grid_tool_size_x = 0.0;
gdouble bird_font_grid_tool_size_y = 0.0;
GeeArrayList* bird_font_grid_tool_sizes = NULL;
gboolean bird_font_grid_tool_lock_grid = FALSE;

static void __lambda362_ (BirdFontGridTool* self,
                   BirdFontTool* _self_);
static void ___lambda362__bird_font_tool_select_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gpointer self);
static void __lambda363_ (BirdFontGridTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda363__bird_font_tool_press_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gint button,
                                                gint x,
                                                gint y,
                                                gpointer self);
static void __lambda364_ (BirdFontGridTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda364__bird_font_tool_release_action (BirdFontTool* _sender,
                                                  BirdFontTool* selected,
                                                  gint button,
                                                  gint x,
                                                  gint y,
                                                  gpointer self);
static void __lambda365_ (BirdFontGridTool* self,
                   BirdFontTool* _self_,
                   gint x,
                   gint y);
static void ___lambda365__bird_font_tool_move_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               gint x,
                                               gint y,
                                               gpointer self);
static void __lambda366_ (BirdFontGridTool* self,
                   BirdFontTool* _self_,
                   cairo_t* cairo_context,
                   BirdFontGlyph* glyph);
static void ___lambda366__bird_font_tool_draw_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               cairo_t* cr,
                                               BirdFontGlyph* glyph,
                                               gpointer self);
static void bird_font_grid_tool_finalize (GObject * obj);
static GType bird_font_grid_tool_get_type_once (void);

static void
__lambda362_ (BirdFontGridTool* self,
              BirdFontTool* _self_)
{
	BirdFontToolbox* tb = NULL;
	BirdFontToolbox* _tmp0_;
	BirdFontTool* t = NULL;
	BirdFontToolbox* _tmp1_;
	BirdFontTool* _tmp2_;
	gboolean _tmp3_ = FALSE;
	g_return_if_fail (_self_ != NULL);
	bird_font_grid_tool_grid_visible = !bird_font_grid_tool_grid_visible;
	bird_font_grid_tool_update_lines ();
	bird_font_glyph_canvas_redraw ();
	bird_font_tool_set_selected (_self_, bird_font_grid_tool_grid_visible);
	_tmp0_ = bird_font_main_window_get_toolbox ();
	tb = _tmp0_;
	_tmp1_ = tb;
	_tmp2_ = bird_font_toolbox_get_tool (_tmp1_, "help_lines");
	t = _tmp2_;
	if (bird_font_grid_tool_grid_visible) {
		BirdFontTool* _tmp4_;
		_tmp4_ = t;
		_tmp3_ = !bird_font_tool_is_selected (_tmp4_);
	} else {
		_tmp3_ = FALSE;
	}
	if (_tmp3_) {
		BirdFontToolbox* _tmp5_;
		BirdFontToolbox* _tmp6_;
		BirdFontTool* _tmp7_;
		_tmp5_ = bird_font_main_window_get_toolbox ();
		_tmp6_ = _tmp5_;
		_tmp7_ = t;
		bird_font_toolbox_select_tool (_tmp6_, _tmp7_);
		_g_object_unref0 (_tmp6_);
	}
	_g_object_unref0 (t);
	_g_object_unref0 (tb);
}

static void
___lambda362__bird_font_tool_select_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gpointer self)
{
	__lambda362_ ((BirdFontGridTool*) self, selected);
}

static void
__lambda363_ (BirdFontGridTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
}

static void
___lambda363__bird_font_tool_press_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gint button,
                                           gint x,
                                           gint y,
                                           gpointer self)
{
	__lambda363_ ((BirdFontGridTool*) self, selected, button, x, y);
}

static void
__lambda364_ (BirdFontGridTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
}

static void
___lambda364__bird_font_tool_release_action (BirdFontTool* _sender,
                                             BirdFontTool* selected,
                                             gint button,
                                             gint x,
                                             gint y,
                                             gpointer self)
{
	__lambda364_ ((BirdFontGridTool*) self, selected, button, x, y);
}

static void
__lambda365_ (BirdFontGridTool* self,
              BirdFontTool* _self_,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
}

static void
___lambda365__bird_font_tool_move_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          gint x,
                                          gint y,
                                          gpointer self)
{
	__lambda365_ ((BirdFontGridTool*) self, selected, x, y);
}

static void
__lambda366_ (BirdFontGridTool* self,
              BirdFontTool* _self_,
              cairo_t* cairo_context,
              BirdFontGlyph* glyph)
{
	g_return_if_fail (_self_ != NULL);
	g_return_if_fail (cairo_context != NULL);
	g_return_if_fail (glyph != NULL);
}

static void
___lambda366__bird_font_tool_draw_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          cairo_t* cr,
                                          BirdFontGlyph* glyph,
                                          gpointer self)
{
	__lambda366_ ((BirdFontGridTool*) self, selected, cr, glyph);
}

BirdFontGridTool*
bird_font_grid_tool_construct (GType object_type,
                               const gchar* n)
{
	BirdFontGridTool * self = NULL;
	gchar* units = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	GeeArrayList* _tmp4_;
	GeeArrayList* _tmp5_;
	GeeArrayList* _tmp6_;
	g_return_val_if_fail (n != NULL, NULL);
	_tmp0_ = bird_font_t_ ("Show grid");
	_tmp1_ = _tmp0_;
	self = (BirdFontGridTool*) bird_font_tool_construct (object_type, n, _tmp1_);
	_g_free0 (_tmp1_);
	_tmp2_ = bird_font_preferences_get ("ttf_units");
	_g_free0 (units);
	units = _tmp2_;
	_tmp3_ = units;
	if (g_strcmp0 (_tmp3_, "true") == 0) {
		bird_font_grid_tool_ttf_units = TRUE;
	}
	_tmp4_ = gee_array_list_new (BIRD_FONT_TYPE_LINE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (bird_font_grid_tool_horizontal);
	bird_font_grid_tool_horizontal = _tmp4_;
	_tmp5_ = gee_array_list_new (BIRD_FONT_TYPE_LINE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (bird_font_grid_tool_vertical);
	bird_font_grid_tool_vertical = _tmp5_;
	_tmp6_ = gee_array_list_new (BIRD_FONT_TYPE_SPIN_BUTTON, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (bird_font_grid_tool_sizes);
	bird_font_grid_tool_sizes = _tmp6_;
	bird_font_grid_tool_size_x = (gdouble) 2;
	bird_font_grid_tool_size_y = (gdouble) 2;
	bird_font_grid_tool_update_lines ();
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda362__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "press-action", (GCallback) ___lambda363__bird_font_tool_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "release-action", (GCallback) ___lambda364__bird_font_tool_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "move-action", (GCallback) ___lambda365__bird_font_tool_move_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "draw-action", (GCallback) ___lambda366__bird_font_tool_draw_action, self, 0);
	_g_free0 (units);
	return self;
}

BirdFontGridTool*
bird_font_grid_tool_new (const gchar* n)
{
	return bird_font_grid_tool_construct (BIRD_FONT_TYPE_GRID_TOOL, n);
}

void
bird_font_grid_tool_set_grid_width (gdouble w)
{
	gdouble t = 0.0;
	t = (gdouble) 0;
	if (bird_font_grid_tool_ttf_units) {
		bird_font_grid_tool_ttf_grid_coordinate (&w, &t);
	}
	bird_font_grid_tool_size_x = w;
	bird_font_grid_tool_size_y = w;
	bird_font_grid_tool_update_lines ();
}

void
bird_font_grid_tool_ttf_grid_coordinate (gdouble* x,
                                         gdouble* y)
{
	BirdFontGlyph* _tmp0_;
	BirdFontGlyph* _tmp1_;
	BirdFontFont* _tmp2_;
	BirdFontFont* _tmp3_;
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	_tmp1_ = _tmp0_;
	*x = bird_font_glyf_data_tie_to_ttf_grid_x (_tmp1_, *x);
	_g_object_unref0 (_tmp1_);
	_tmp2_ = bird_font_bird_font_get_current_font ();
	_tmp3_ = _tmp2_;
	*y = bird_font_glyf_data_tie_to_ttf_grid_y (_tmp3_, *y);
	_g_object_unref0 (_tmp3_);
}

void
bird_font_grid_tool_update_lines (void)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	gdouble step = 0.0;
	BirdFontColor* color = NULL;
	BirdFontColor* _tmp1_;
	gdouble i = 0.0;
	gint max_lines = 0;
	gint n = 0;
	BirdFontLine* t = NULL;
	BirdFontLine* l = NULL;
	BirdFontLine* u = NULL;
	BirdFontLine* baseline = NULL;
	BirdFontGlyph* _tmp2_;
	BirdFontLine* _tmp3_;
	BirdFontLine* bottom_margin = NULL;
	BirdFontGlyph* _tmp4_;
	BirdFontLine* _tmp5_;
	BirdFontLine* top_margin = NULL;
	BirdFontGlyph* _tmp6_;
	BirdFontLine* _tmp7_;
	BirdFontLine* left = NULL;
	BirdFontGlyph* _tmp8_;
	BirdFontLine* _tmp9_;
	BirdFontLine* right = NULL;
	BirdFontGlyph* _tmp10_;
	BirdFontLine* _tmp11_;
	GeeArrayList* _tmp12_;
	GeeArrayList* _tmp13_;
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	step = bird_font_grid_tool_size_y;
	_tmp1_ = bird_font_theme_get_color ("Grid");
	color = _tmp1_;
	max_lines = 600;
	_tmp2_ = g;
	_tmp3_ = bird_font_glyph_get_line (_tmp2_, "baseline");
	baseline = _tmp3_;
	_tmp4_ = g;
	_tmp5_ = bird_font_glyph_get_line (_tmp4_, "bottom margin");
	bottom_margin = _tmp5_;
	_tmp6_ = g;
	_tmp7_ = bird_font_glyph_get_line (_tmp6_, "top margin");
	top_margin = _tmp7_;
	_tmp8_ = g;
	_tmp9_ = bird_font_glyph_get_line (_tmp8_, "left");
	left = _tmp9_;
	_tmp10_ = g;
	_tmp11_ = bird_font_glyph_get_line (_tmp10_, "right");
	right = _tmp11_;
	_tmp12_ = bird_font_grid_tool_vertical;
	gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp12_);
	_tmp13_ = bird_font_grid_tool_horizontal;
	gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp13_);
	n = 0;
	{
		BirdFontLine* _tmp14_;
		gboolean _tmp15_ = FALSE;
		_tmp14_ = left;
		i = bird_font_line_get_pos (_tmp14_) - (7 * step);
		_tmp15_ = TRUE;
		while (TRUE) {
			BirdFontLine* _tmp16_;
			BirdFontLine* _tmp17_;
			BirdFontLine* _tmp18_;
			BirdFontLine* _tmp19_;
			BirdFontColor* _tmp20_;
			BirdFontColor* _tmp21_;
			BirdFontColor* _tmp22_;
			BirdFontColor* _tmp23_;
			GeeArrayList* _tmp24_;
			BirdFontLine* _tmp25_;
			gint _tmp26_;
			gint _tmp27_;
			if (!_tmp15_) {
				i += step;
			}
			_tmp15_ = FALSE;
			_tmp16_ = right;
			if (!(i <= (bird_font_line_get_pos (_tmp16_) + (7 * step)))) {
				break;
			}
			_tmp17_ = bird_font_line_new ("grid", "", i, BIRD_FONT_LINE_VERTICAL);
			_g_object_unref0 (l);
			l = _tmp17_;
			_tmp18_ = l;
			bird_font_line_set_moveable (_tmp18_, FALSE);
			_tmp19_ = l;
			_tmp20_ = color;
			_tmp21_ = color;
			_tmp22_ = color;
			_tmp23_ = color;
			bird_font_line_set_color (_tmp19_, _tmp20_->r, _tmp21_->g, _tmp22_->b, _tmp23_->a);
			_tmp24_ = bird_font_grid_tool_vertical;
			_tmp25_ = l;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp24_, _tmp25_);
			n = n + 1;
			_tmp26_ = n;
			_tmp27_ = max_lines;
			if (_tmp26_ >= _tmp27_) {
				break;
			}
		}
	}
	n = 0;
	{
		BirdFontLine* _tmp28_;
		gboolean _tmp29_ = FALSE;
		_tmp28_ = baseline;
		i = bird_font_line_get_pos (_tmp28_) - step;
		_tmp29_ = TRUE;
		while (TRUE) {
			BirdFontLine* _tmp30_;
			BirdFontLine* _tmp31_;
			BirdFontLine* _tmp32_;
			BirdFontLine* _tmp33_;
			BirdFontColor* _tmp34_;
			BirdFontColor* _tmp35_;
			BirdFontColor* _tmp36_;
			BirdFontColor* _tmp37_;
			GeeArrayList* _tmp38_;
			BirdFontLine* _tmp39_;
			gint _tmp40_;
			gint _tmp41_;
			if (!_tmp29_) {
				i -= step;
			}
			_tmp29_ = FALSE;
			_tmp30_ = bottom_margin;
			if (!(i >= (bird_font_line_get_pos (_tmp30_) - (7 * step)))) {
				break;
			}
			_tmp31_ = bird_font_line_new ("grid", "", i, BIRD_FONT_LINE_HORIZONTAL);
			_g_object_unref0 (t);
			t = _tmp31_;
			_tmp32_ = t;
			bird_font_line_set_moveable (_tmp32_, FALSE);
			_tmp33_ = t;
			_tmp34_ = color;
			_tmp35_ = color;
			_tmp36_ = color;
			_tmp37_ = color;
			bird_font_line_set_color (_tmp33_, _tmp34_->r, _tmp35_->g, _tmp36_->b, _tmp37_->a);
			_tmp38_ = bird_font_grid_tool_horizontal;
			_tmp39_ = t;
			gee_abstract_list_insert ((GeeAbstractList*) _tmp38_, 0, _tmp39_);
			n = n + 1;
			_tmp40_ = n;
			_tmp41_ = max_lines;
			if (_tmp40_ >= _tmp41_) {
				break;
			}
		}
	}
	{
		BirdFontLine* _tmp42_;
		gboolean _tmp43_ = FALSE;
		_tmp42_ = baseline;
		i = bird_font_line_get_pos (_tmp42_);
		_tmp43_ = TRUE;
		while (TRUE) {
			BirdFontLine* _tmp44_;
			BirdFontLine* _tmp45_;
			BirdFontLine* _tmp46_;
			BirdFontLine* _tmp47_;
			BirdFontColor* _tmp48_;
			BirdFontColor* _tmp49_;
			BirdFontColor* _tmp50_;
			BirdFontColor* _tmp51_;
			GeeArrayList* _tmp52_;
			BirdFontLine* _tmp53_;
			gint _tmp54_;
			gint _tmp55_;
			if (!_tmp43_) {
				i += step;
			}
			_tmp43_ = FALSE;
			_tmp44_ = top_margin;
			if (!(i <= (bird_font_line_get_pos (_tmp44_) + (7 * step)))) {
				break;
			}
			_tmp45_ = bird_font_line_new ("grid", "", i, BIRD_FONT_LINE_HORIZONTAL);
			_g_object_unref0 (u);
			u = _tmp45_;
			_tmp46_ = u;
			bird_font_line_set_moveable (_tmp46_, FALSE);
			_tmp47_ = u;
			_tmp48_ = color;
			_tmp49_ = color;
			_tmp50_ = color;
			_tmp51_ = color;
			bird_font_line_set_color (_tmp47_, _tmp48_->r, _tmp49_->g, _tmp50_->b, _tmp51_->a);
			_tmp52_ = bird_font_grid_tool_horizontal;
			_tmp53_ = u;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp52_, _tmp53_);
			n = n + 1;
			_tmp54_ = n;
			_tmp55_ = max_lines;
			if (_tmp54_ >= _tmp55_) {
				break;
			}
		}
	}
	_g_object_unref0 (right);
	_g_object_unref0 (left);
	_g_object_unref0 (top_margin);
	_g_object_unref0 (bottom_margin);
	_g_object_unref0 (baseline);
	_g_object_unref0 (u);
	_g_object_unref0 (l);
	_g_object_unref0 (t);
	_bird_font_color_unref0 (color);
	_g_object_unref0 (g);
}

void
bird_font_grid_tool_tie_coordinate (gdouble* x,
                                    gdouble* y)
{
	bird_font_grid_tool_tie_point (x, y, TRUE);
}

void
bird_font_grid_tool_tie_point (gdouble* x,
                               gdouble* y,
                               gboolean coordinate)
{
	*x = bird_font_grid_tool_tie_point_x (*x, coordinate);
	*y = bird_font_grid_tool_tie_point_y (*y, coordinate);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

gdouble
bird_font_grid_tool_tie_point_x (gdouble x,
                                 gboolean coordinate)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gdouble d = 0.0;
	gdouble m = 0.0;
	BirdFontLine* xmin = NULL;
	GeeArrayList* _tmp3_;
	gpointer _tmp4_;
	BirdFontLine* xpos = NULL;
	BirdFontLine* startx = NULL;
	GeeArrayList* _tmp5_;
	gpointer _tmp6_;
	BirdFontLine* stopx = NULL;
	GeeArrayList* _tmp7_;
	GeeArrayList* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	gpointer _tmp11_;
	gdouble result;
	_tmp0_ = bird_font_grid_tool_vertical;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	_vala_return_val_if_fail (_tmp2_ >= 2, "vertical.size >= 2", 0.0);
	_tmp3_ = bird_font_grid_tool_vertical;
	_tmp4_ = gee_abstract_list_get ((GeeAbstractList*) _tmp3_, 0);
	xmin = (BirdFontLine*) _tmp4_;
	_tmp5_ = bird_font_grid_tool_vertical;
	_tmp6_ = gee_abstract_list_get ((GeeAbstractList*) _tmp5_, 0);
	startx = (BirdFontLine*) _tmp6_;
	_tmp7_ = bird_font_grid_tool_vertical;
	_tmp8_ = bird_font_grid_tool_vertical;
	_tmp9_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _tmp10_ - 1);
	stopx = (BirdFontLine*) _tmp11_;
	if (!coordinate) {
		BirdFontLine* _tmp12_;
		BirdFontLine* _tmp13_;
		gdouble _tmp14_;
		_tmp12_ = startx;
		_tmp13_ = stopx;
		_tmp14_ = bird_font_glyph_path_coordinate_x (x);
		if (!((_tmp12_->pos < _tmp14_) && (_tmp14_ < _tmp13_->pos))) {
			result = x;
			_g_object_unref0 (stopx);
			_g_object_unref0 (startx);
			_g_object_unref0 (xpos);
			_g_object_unref0 (xmin);
			return result;
		}
	} else {
		BirdFontLine* _tmp15_;
		BirdFontLine* _tmp16_;
		gdouble _tmp17_;
		_tmp15_ = startx;
		_tmp16_ = stopx;
		_tmp17_ = x;
		if (!((_tmp15_->pos < _tmp17_) && (_tmp17_ < _tmp16_->pos))) {
			result = x;
			_g_object_unref0 (stopx);
			_g_object_unref0 (startx);
			_g_object_unref0 (xpos);
			_g_object_unref0 (xmin);
			return result;
		}
	}
	if (!coordinate) {
		BirdFontLine* _tmp18_;
		BirdFontLine* _tmp19_;
		_tmp18_ = bird_font_line_new ("", "", (gdouble) 0, BIRD_FONT_LINE_VERTICAL);
		_g_object_unref0 (xpos);
		xpos = _tmp18_;
		_tmp19_ = xpos;
		_tmp19_->pos = bird_font_glyph_path_coordinate_x (x);
	} else {
		BirdFontLine* _tmp20_;
		_tmp20_ = bird_font_line_new ("", "", x, BIRD_FONT_LINE_VERTICAL);
		_g_object_unref0 (xpos);
		xpos = _tmp20_;
	}
	m = DBL_MAX;
	{
		GeeArrayList* _line_list = NULL;
		GeeArrayList* _tmp21_;
		gint _line_size = 0;
		GeeArrayList* _tmp22_;
		gint _tmp23_;
		gint _tmp24_;
		gint _line_index = 0;
		_tmp21_ = bird_font_grid_tool_vertical;
		_line_list = _tmp21_;
		_tmp22_ = _line_list;
		_tmp23_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp22_);
		_tmp24_ = _tmp23_;
		_line_size = _tmp24_;
		_line_index = -1;
		while (TRUE) {
			gint _tmp25_;
			gint _tmp26_;
			BirdFontLine* line = NULL;
			GeeArrayList* _tmp27_;
			gpointer _tmp28_;
			BirdFontLine* _tmp29_;
			BirdFontLine* _tmp30_;
			_line_index = _line_index + 1;
			_tmp25_ = _line_index;
			_tmp26_ = _line_size;
			if (!(_tmp25_ < _tmp26_)) {
				break;
			}
			_tmp27_ = _line_list;
			_tmp28_ = gee_abstract_list_get ((GeeAbstractList*) _tmp27_, _line_index);
			line = (BirdFontLine*) _tmp28_;
			_tmp29_ = line;
			_tmp30_ = xpos;
			d = fabs (bird_font_line_get_pos (_tmp29_) - bird_font_line_get_pos (_tmp30_));
			if (d <= m) {
				BirdFontLine* _tmp31_;
				BirdFontLine* _tmp32_;
				m = d;
				_tmp31_ = line;
				_tmp32_ = _g_object_ref0 (_tmp31_);
				_g_object_unref0 (xmin);
				xmin = _tmp32_;
			}
			_g_object_unref0 (line);
		}
	}
	if (!coordinate) {
		BirdFontLine* _tmp33_;
		_tmp33_ = xmin;
		x = (gdouble) bird_font_glyph_reverse_path_coordinate_x (bird_font_line_get_pos (_tmp33_));
	} else {
		BirdFontLine* _tmp34_;
		_tmp34_ = xmin;
		x = bird_font_line_get_pos (_tmp34_);
	}
	result = x;
	_g_object_unref0 (stopx);
	_g_object_unref0 (startx);
	_g_object_unref0 (xpos);
	_g_object_unref0 (xmin);
	return result;
}

gdouble
bird_font_grid_tool_tie_point_y (gdouble y,
                                 gboolean coordinate)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gdouble d = 0.0;
	gdouble m = 0.0;
	BirdFontLine* ymin = NULL;
	GeeArrayList* _tmp3_;
	gpointer _tmp4_;
	BirdFontLine* ypos = NULL;
	BirdFontLine* starty = NULL;
	GeeArrayList* _tmp5_;
	gpointer _tmp6_;
	BirdFontLine* stopy = NULL;
	GeeArrayList* _tmp7_;
	GeeArrayList* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	gpointer _tmp11_;
	gdouble result;
	_tmp0_ = bird_font_grid_tool_horizontal;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	_vala_return_val_if_fail (_tmp2_ >= 2, "horizontal.size >= 2", 0.0);
	_tmp3_ = bird_font_grid_tool_horizontal;
	_tmp4_ = gee_abstract_list_get ((GeeAbstractList*) _tmp3_, 0);
	ymin = (BirdFontLine*) _tmp4_;
	_tmp5_ = bird_font_grid_tool_horizontal;
	_tmp6_ = gee_abstract_list_get ((GeeAbstractList*) _tmp5_, 0);
	starty = (BirdFontLine*) _tmp6_;
	_tmp7_ = bird_font_grid_tool_horizontal;
	_tmp8_ = bird_font_grid_tool_horizontal;
	_tmp9_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _tmp10_ - 1);
	stopy = (BirdFontLine*) _tmp11_;
	if (!coordinate) {
		BirdFontLine* _tmp12_;
		BirdFontLine* _tmp13_;
		gdouble _tmp14_;
		_tmp12_ = starty;
		_tmp13_ = stopy;
		_tmp14_ = bird_font_glyph_path_coordinate_y (y);
		if (!((_tmp12_->pos < _tmp14_) && (_tmp14_ < _tmp13_->pos))) {
			result = y;
			_g_object_unref0 (stopy);
			_g_object_unref0 (starty);
			_g_object_unref0 (ypos);
			_g_object_unref0 (ymin);
			return result;
		}
	} else {
		BirdFontLine* _tmp15_;
		BirdFontLine* _tmp16_;
		gdouble _tmp17_;
		_tmp15_ = starty;
		_tmp16_ = stopy;
		_tmp17_ = y;
		if (!((_tmp15_->pos < _tmp17_) && (_tmp17_ < _tmp16_->pos))) {
			result = y;
			_g_object_unref0 (stopy);
			_g_object_unref0 (starty);
			_g_object_unref0 (ypos);
			_g_object_unref0 (ymin);
			return result;
		}
	}
	if (!coordinate) {
		BirdFontLine* _tmp18_;
		BirdFontLine* _tmp19_;
		_tmp18_ = bird_font_line_new ("", "", (gdouble) 0, BIRD_FONT_LINE_HORIZONTAL);
		_g_object_unref0 (ypos);
		ypos = _tmp18_;
		_tmp19_ = ypos;
		_tmp19_->pos = bird_font_glyph_path_coordinate_y (y);
	} else {
		BirdFontLine* _tmp20_;
		_tmp20_ = bird_font_line_new ("", "", y, BIRD_FONT_LINE_HORIZONTAL);
		_g_object_unref0 (ypos);
		ypos = _tmp20_;
	}
	m = DBL_MAX;
	{
		GeeArrayList* _line_list = NULL;
		GeeArrayList* _tmp21_;
		gint _line_size = 0;
		GeeArrayList* _tmp22_;
		gint _tmp23_;
		gint _tmp24_;
		gint _line_index = 0;
		_tmp21_ = bird_font_grid_tool_horizontal;
		_line_list = _tmp21_;
		_tmp22_ = _line_list;
		_tmp23_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp22_);
		_tmp24_ = _tmp23_;
		_line_size = _tmp24_;
		_line_index = -1;
		while (TRUE) {
			gint _tmp25_;
			gint _tmp26_;
			BirdFontLine* line = NULL;
			GeeArrayList* _tmp27_;
			gpointer _tmp28_;
			BirdFontLine* _tmp29_;
			BirdFontLine* _tmp30_;
			_line_index = _line_index + 1;
			_tmp25_ = _line_index;
			_tmp26_ = _line_size;
			if (!(_tmp25_ < _tmp26_)) {
				break;
			}
			_tmp27_ = _line_list;
			_tmp28_ = gee_abstract_list_get ((GeeAbstractList*) _tmp27_, _line_index);
			line = (BirdFontLine*) _tmp28_;
			_tmp29_ = line;
			_tmp30_ = ypos;
			d = fabs (bird_font_line_get_pos (_tmp29_) - bird_font_line_get_pos (_tmp30_));
			if (d <= m) {
				BirdFontLine* _tmp31_;
				BirdFontLine* _tmp32_;
				m = d;
				_tmp31_ = line;
				_tmp32_ = _g_object_ref0 (_tmp31_);
				_g_object_unref0 (ymin);
				ymin = _tmp32_;
			}
			_g_object_unref0 (line);
		}
	}
	if (!coordinate) {
		BirdFontLine* _tmp33_;
		_tmp33_ = ymin;
		y = (gdouble) bird_font_glyph_reverse_path_coordinate_y (bird_font_line_get_pos (_tmp33_));
	} else {
		BirdFontLine* _tmp34_;
		_tmp34_ = ymin;
		y = bird_font_line_get_pos (_tmp34_);
	}
	result = y;
	_g_object_unref0 (stopy);
	_g_object_unref0 (starty);
	_g_object_unref0 (ypos);
	_g_object_unref0 (ymin);
	return result;
}

gboolean
bird_font_grid_tool_has_ttf_grid (void)
{
	gboolean result;
	result = bird_font_grid_tool_ttf_units;
	return result;
}

gboolean
bird_font_grid_tool_is_visible (void)
{
	gboolean result;
	result = bird_font_grid_tool_grid_visible;
	return result;
}

GeeArrayList*
bird_font_grid_tool_get_horizontal_lines (void)
{
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	GeeArrayList* result;
	_tmp0_ = bird_font_grid_tool_horizontal;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

GeeArrayList*
bird_font_grid_tool_get_vertical_lines (void)
{
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	GeeArrayList* result;
	_tmp0_ = bird_font_grid_tool_vertical;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
bird_font_grid_tool_class_init (BirdFontGridToolClass * klass,
                                gpointer klass_data)
{
	bird_font_grid_tool_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = bird_font_grid_tool_finalize;
}

static void
bird_font_grid_tool_instance_init (BirdFontGridTool * self,
                                   gpointer klass)
{
}

static void
bird_font_grid_tool_finalize (GObject * obj)
{
	BirdFontGridTool * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_GRID_TOOL, BirdFontGridTool);
	G_OBJECT_CLASS (bird_font_grid_tool_parent_class)->finalize (obj);
}

static GType
bird_font_grid_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontGridToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_grid_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontGridTool), 0, (GInstanceInitFunc) bird_font_grid_tool_instance_init, NULL };
	GType bird_font_grid_tool_type_id;
	bird_font_grid_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontGridTool", &g_define_type_info, 0);
	return bird_font_grid_tool_type_id;
}

GType
bird_font_grid_tool_get_type (void)
{
	static volatile gsize bird_font_grid_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_grid_tool_type_id__once)) {
		GType bird_font_grid_tool_type_id;
		bird_font_grid_tool_type_id = bird_font_grid_tool_get_type_once ();
		g_once_init_leave (&bird_font_grid_tool_type_id__once, bird_font_grid_tool_type_id);
	}
	return bird_font_grid_tool_type_id__once;
}

