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

/*
Copyright (C) 2014 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 <stdlib.h>
#include <string.h>
#include <glib.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_SPACING_DATA_0_PROPERTY,
	BIRD_FONT_SPACING_DATA_NUM_PROPERTIES
};
static GParamSpec* bird_font_spacing_data_properties[BIRD_FONT_SPACING_DATA_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _BirdFontSpacingDataPrivate {
	GeeArrayList* connections;
};

static gint BirdFontSpacingData_private_offset;
static gpointer bird_font_spacing_data_parent_class = NULL;

static gboolean bird_font_spacing_data_has_connection (BirdFontSpacingData* self,
                                                const gchar* s);
static gint __lambda18_ (BirdFontSpacingData* self,
                  const gchar* a,
                  const gchar* b);
static gint ___lambda18__gcompare_data_func (gconstpointer a,
                                      gconstpointer b,
                                      gpointer self);
static void bird_font_spacing_data_update_all_rows (BirdFontSpacingData* self,
                                             BirdFontSpacingClass* s);
static void _bird_font_spacing_data_update_all_rows_bird_font_spacingclass_updated (BirdFontSpacingClass* _sender,
                                                                             BirdFontSpacingClass* s,
                                                                             gpointer self);
static void _bird_font_spacing_data_update_kerning_bird_font_spacingclass_updated (BirdFontSpacingClass* _sender,
                                                                            BirdFontSpacingClass* s,
                                                                            gpointer self);
static void bird_font_spacing_data_finalize (GObject * obj);
static GType bird_font_spacing_data_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_spacing_data_get_instance_private (BirdFontSpacingData* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontSpacingData_private_offset);
}

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

BirdFontSpacingData*
bird_font_spacing_data_construct (GType object_type,
                                  BirdFontKerningClasses* kerning)
{
	BirdFontSpacingData * self = NULL;
	BirdFontKerningClasses* _tmp0_;
	g_return_val_if_fail (kerning != NULL, NULL);
	self = (BirdFontSpacingData*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (kerning);
	_g_object_unref0 (self->kerning_classes);
	self->kerning_classes = _tmp0_;
	return self;
}

BirdFontSpacingData*
bird_font_spacing_data_new (BirdFontKerningClasses* kerning)
{
	return bird_font_spacing_data_construct (BIRD_FONT_TYPE_SPACING_DATA, kerning);
}

BirdFontKerningClasses*
bird_font_spacing_data_get_kerning_classes (BirdFontSpacingData* self)
{
	BirdFontKerningClasses* _tmp0_;
	BirdFontKerningClasses* _tmp1_;
	BirdFontKerningClasses* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->kerning_classes;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

GeeArrayList*
bird_font_spacing_data_get_all_connections (BirdFontSpacingData* self,
                                            const gchar* glyph)
{
	gchar* t = NULL;
	GeeArrayList* c = NULL;
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	GeeArrayList* _tmp18_;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (glyph != NULL, NULL);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	c = _tmp0_;
	_tmp1_ = self->priv->connections;
	gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp1_);
	bird_font_spacing_data_add_connections (self, glyph);
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp2_ = FALSE;
			_tmp2_ = TRUE;
			while (TRUE) {
				GeeArrayList* _tmp4_;
				gint _tmp5_;
				gint _tmp6_;
				GeeArrayList* _tmp7_;
				gint _tmp8_;
				gint _tmp9_;
				gint _tmp10_;
				GeeArrayList* _tmp11_;
				GeeArrayList* _tmp12_;
				gpointer _tmp13_;
				GeeArrayList* _tmp14_;
				const gchar* _tmp15_;
				gchar* _tmp16_;
				gchar* _tmp17_;
				if (!_tmp2_) {
					gint _tmp3_;
					_tmp3_ = i;
					i = _tmp3_ + 1;
				}
				_tmp2_ = FALSE;
				_tmp4_ = self->priv->connections;
				_tmp5_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp4_);
				_tmp6_ = _tmp5_;
				if (!(i < _tmp6_)) {
					break;
				}
				_tmp7_ = self->priv->connections;
				_tmp8_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp7_);
				_tmp9_ = _tmp8_;
				_tmp10_ = i;
				_tmp11_ = c;
				g_return_val_if_fail ((0 <= _tmp10_) && (_tmp10_ < _tmp9_), _tmp11_);
				_tmp12_ = self->priv->connections;
				_tmp13_ = gee_abstract_list_get ((GeeAbstractList*) _tmp12_, i);
				_g_free0 (t);
				t = (gchar*) _tmp13_;
				_tmp14_ = c;
				_tmp15_ = t;
				_tmp16_ = g_strdup (_tmp15_);
				_tmp17_ = _tmp16_;
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp14_, _tmp17_);
				_g_free0 (_tmp17_);
			}
		}
	}
	_tmp18_ = self->priv->connections;
	gee_abstract_collection_clear ((GeeAbstractCollection*) _tmp18_);
	result = c;
	_g_free0 (t);
	return result;
}

static gboolean
bird_font_spacing_data_has_connection (BirdFontSpacingData* self,
                                       const gchar* s)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (s != NULL, FALSE);
	{
		GeeArrayList* _t_list = NULL;
		GeeArrayList* _tmp0_;
		gint _t_size = 0;
		GeeArrayList* _tmp1_;
		gint _tmp2_;
		gint _tmp3_;
		gint _t_index = 0;
		_tmp0_ = self->priv->connections;
		_t_list = _tmp0_;
		_tmp1_ = _t_list;
		_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
		_tmp3_ = _tmp2_;
		_t_size = _tmp3_;
		_t_index = -1;
		while (TRUE) {
			gint _tmp4_;
			gint _tmp5_;
			gchar* t = NULL;
			GeeArrayList* _tmp6_;
			gpointer _tmp7_;
			const gchar* _tmp8_;
			_t_index = _t_index + 1;
			_tmp4_ = _t_index;
			_tmp5_ = _t_size;
			if (!(_tmp4_ < _tmp5_)) {
				break;
			}
			_tmp6_ = _t_list;
			_tmp7_ = gee_abstract_list_get ((GeeAbstractList*) _tmp6_, _t_index);
			t = (gchar*) _tmp7_;
			_tmp8_ = t;
			if (g_strcmp0 (_tmp8_, s) == 0) {
				result = TRUE;
				_g_free0 (t);
				return result;
			}
			_g_free0 (t);
		}
	}
	result = FALSE;
	return result;
}

static gint
__lambda18_ (BirdFontSpacingData* self,
             const gchar* a,
             const gchar* b)
{
	GCompareFunc _tmp0_;
	gint result;
	g_return_val_if_fail (a != NULL, 0);
	g_return_val_if_fail (b != NULL, 0);
	_tmp0_ = ((GCompareFunc) g_strcmp0);
	result = _tmp0_ ((const gchar*) a, (const gchar*) b);
	return result;
}

static gint
___lambda18__gcompare_data_func (gconstpointer a,
                                 gconstpointer b,
                                 gpointer self)
{
	gint result;
	result = __lambda18_ ((BirdFontSpacingData*) self, (const gchar*) a, (const gchar*) b);
	return result;
}

void
bird_font_spacing_data_add_connections (BirdFontSpacingData* self,
                                        const gchar* glyph)
{
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp21_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (glyph != NULL);
	_tmp0_ = self->priv->connections;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp0_, glyph);
	{
		GeeArrayList* _s_list = NULL;
		GeeArrayList* _tmp1_;
		gint _s_size = 0;
		GeeArrayList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _s_index = 0;
		_tmp1_ = self->classes;
		_s_list = _tmp1_;
		_tmp2_ = _s_list;
		_tmp3_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_s_size = _tmp4_;
		_s_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			BirdFontSpacingClass* s = NULL;
			GeeArrayList* _tmp7_;
			gpointer _tmp8_;
			BirdFontSpacingClass* _tmp9_;
			const gchar* _tmp10_;
			BirdFontSpacingClass* _tmp15_;
			const gchar* _tmp16_;
			_s_index = _s_index + 1;
			_tmp5_ = _s_index;
			_tmp6_ = _s_size;
			if (!(_tmp5_ < _tmp6_)) {
				break;
			}
			_tmp7_ = _s_list;
			_tmp8_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _s_index);
			s = (BirdFontSpacingClass*) _tmp8_;
			_tmp9_ = s;
			_tmp10_ = _tmp9_->first;
			if (g_strcmp0 (_tmp10_, glyph) == 0) {
				BirdFontSpacingClass* _tmp11_;
				const gchar* _tmp12_;
				_tmp11_ = s;
				_tmp12_ = _tmp11_->next;
				if (!bird_font_spacing_data_has_connection (self, _tmp12_)) {
					BirdFontSpacingClass* _tmp13_;
					const gchar* _tmp14_;
					_tmp13_ = s;
					_tmp14_ = _tmp13_->next;
					bird_font_spacing_data_add_connections (self, _tmp14_);
				}
			}
			_tmp15_ = s;
			_tmp16_ = _tmp15_->next;
			if (g_strcmp0 (_tmp16_, glyph) == 0) {
				BirdFontSpacingClass* _tmp17_;
				const gchar* _tmp18_;
				_tmp17_ = s;
				_tmp18_ = _tmp17_->first;
				if (!bird_font_spacing_data_has_connection (self, _tmp18_)) {
					BirdFontSpacingClass* _tmp19_;
					const gchar* _tmp20_;
					_tmp19_ = s;
					_tmp20_ = _tmp19_->first;
					bird_font_spacing_data_add_connections (self, _tmp20_);
				}
			}
			_g_object_unref0 (s);
		}
	}
	_tmp21_ = self->priv->connections;
	gee_list_sort ((GeeList*) _tmp21_, ___lambda18__gcompare_data_func, g_object_ref (self), g_object_unref);
}

static void
_bird_font_spacing_data_update_all_rows_bird_font_spacingclass_updated (BirdFontSpacingClass* _sender,
                                                                        BirdFontSpacingClass* s,
                                                                        gpointer self)
{
	bird_font_spacing_data_update_all_rows ((BirdFontSpacingData*) self, s);
}

static void
_bird_font_spacing_data_update_kerning_bird_font_spacingclass_updated (BirdFontSpacingClass* _sender,
                                                                       BirdFontSpacingClass* s,
                                                                       gpointer self)
{
	bird_font_spacing_data_update_kerning ((BirdFontSpacingData*) self, s);
}

void
bird_font_spacing_data_add_class (BirdFontSpacingData* self,
                                  const gchar* first,
                                  const gchar* next)
{
	BirdFontSpacingClass* spacing_class = NULL;
	BirdFontSpacingClass* _tmp0_;
	GeeArrayList* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (first != NULL);
	g_return_if_fail (next != NULL);
	_tmp0_ = bird_font_spacingclass_new (first, next);
	spacing_class = _tmp0_;
	g_signal_connect_object (spacing_class, "updated", (GCallback) _bird_font_spacing_data_update_all_rows_bird_font_spacingclass_updated, self, 0);
	g_signal_connect_object (spacing_class, "updated", (GCallback) _bird_font_spacing_data_update_kerning_bird_font_spacingclass_updated, self, 0);
	_tmp1_ = self->classes;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp1_, spacing_class);
	bird_font_spacing_data_update_kerning (self, spacing_class);
	_g_object_unref0 (spacing_class);
}

static void
bird_font_spacing_data_update_all_rows (BirdFontSpacingData* self,
                                        BirdFontSpacingClass* s)
{
	BirdFontSpacingClassTab* _tmp0_;
	BirdFontSpacingClassTab* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (s != NULL);
	_tmp0_ = bird_font_main_window_get_spacing_class_tab ();
	_tmp1_ = _tmp0_;
	bird_font_table_update_rows ((BirdFontTable*) _tmp1_);
	_g_object_unref0 (_tmp1_);
}

void
bird_font_spacing_data_update_kerning (BirdFontSpacingData* self,
                                       BirdFontSpacingClass* s)
{
	BirdFontFont* font = NULL;
	BirdFontKerningClasses* _tmp0_;
	BirdFontFont* _tmp1_;
	BirdFontFont* _tmp2_;
	BirdFontGlyphCollection* g = NULL;
	BirdFontGlyphCollection* gc = NULL;
	gboolean _tmp3_ = FALSE;
	const gchar* _tmp4_;
	gboolean _tmp6_ = FALSE;
	const gchar* _tmp7_;
	BirdFontKerningClasses* _tmp9_;
	const gchar* _tmp10_;
	const gchar* _tmp11_;
	BirdFontFont* _tmp12_;
	const gchar* _tmp13_;
	BirdFontGlyphCollection* _tmp14_;
	BirdFontGlyphCollection* _tmp15_;
	BirdFontFont* _tmp21_;
	const gchar* _tmp22_;
	BirdFontGlyphCollection* _tmp23_;
	BirdFontGlyphCollection* _tmp24_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (s != NULL);
	_tmp0_ = self->kerning_classes;
	_tmp1_ = _tmp0_->font;
	_tmp2_ = _g_object_ref0 (_tmp1_);
	font = _tmp2_;
	_tmp4_ = s->next;
	if (g_strcmp0 (_tmp4_, "?") == 0) {
		_tmp3_ = TRUE;
	} else {
		const gchar* _tmp5_;
		_tmp5_ = s->first;
		_tmp3_ = g_strcmp0 (_tmp5_, "?") == 0;
	}
	if (_tmp3_) {
		_g_object_unref0 (gc);
		_g_object_unref0 (g);
		_g_object_unref0 (font);
		return;
	}
	_tmp7_ = s->next;
	if (g_strcmp0 (_tmp7_, "") == 0) {
		_tmp6_ = TRUE;
	} else {
		const gchar* _tmp8_;
		_tmp8_ = s->first;
		_tmp6_ = g_strcmp0 (_tmp8_, "") == 0;
	}
	if (_tmp6_) {
		_g_object_unref0 (gc);
		_g_object_unref0 (g);
		_g_object_unref0 (font);
		return;
	}
	_tmp9_ = self->kerning_classes;
	_tmp10_ = s->first;
	_tmp11_ = s->next;
	bird_font_kerning_classes_copy_single_kerning_pairs (_tmp9_, _tmp10_, _tmp11_);
	_tmp12_ = font;
	_tmp13_ = s->next;
	_tmp14_ = bird_font_font_get_glyph_collection (_tmp12_, _tmp13_);
	_g_object_unref0 (g);
	g = _tmp14_;
	_tmp15_ = g;
	if (_tmp15_ != NULL) {
		BirdFontGlyphCollection* _tmp16_;
		BirdFontGlyphCollection* _tmp17_;
		BirdFontGlyphCollection* _tmp18_;
		BirdFontGlyph* _tmp19_;
		BirdFontGlyph* _tmp20_;
		_tmp16_ = g;
		_tmp17_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp16_, BIRD_FONT_TYPE_GLYPH_COLLECTION, BirdFontGlyphCollection));
		_g_object_unref0 (gc);
		gc = _tmp17_;
		_tmp18_ = gc;
		_tmp19_ = bird_font_glyph_collection_get_current (_tmp18_);
		_tmp20_ = _tmp19_;
		bird_font_glyph_update_spacing_class (_tmp20_);
		_g_object_unref0 (_tmp20_);
	}
	_tmp21_ = font;
	_tmp22_ = s->first;
	_tmp23_ = bird_font_font_get_glyph_collection (_tmp21_, _tmp22_);
	_g_object_unref0 (g);
	g = _tmp23_;
	_tmp24_ = g;
	if (_tmp24_ != NULL) {
		BirdFontGlyphCollection* _tmp25_;
		BirdFontGlyphCollection* _tmp26_;
		BirdFontGlyphCollection* _tmp27_;
		BirdFontGlyph* _tmp28_;
		BirdFontGlyph* _tmp29_;
		_tmp25_ = g;
		_tmp26_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp25_, BIRD_FONT_TYPE_GLYPH_COLLECTION, BirdFontGlyphCollection));
		_g_object_unref0 (gc);
		gc = _tmp26_;
		_tmp27_ = gc;
		_tmp28_ = bird_font_glyph_collection_get_current (_tmp27_);
		_tmp29_ = _tmp28_;
		bird_font_glyph_update_spacing_class (_tmp29_);
		_g_object_unref0 (_tmp29_);
	}
	bird_font_kerning_tools_update_spacing_classes ();
	_g_object_unref0 (gc);
	_g_object_unref0 (g);
	_g_object_unref0 (font);
}

static void
bird_font_spacing_data_class_init (BirdFontSpacingDataClass * klass,
                                   gpointer klass_data)
{
	bird_font_spacing_data_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontSpacingData_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_spacing_data_finalize;
}

static void
bird_font_spacing_data_instance_init (BirdFontSpacingData * self,
                                      gpointer klass)
{
	GeeArrayList* _tmp0_;
	GeeArrayList* _tmp1_;
	self->priv = bird_font_spacing_data_get_instance_private (self);
	_tmp0_ = gee_array_list_new (BIRD_FONT_TYPE_SPACINGCLASS, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	self->classes = _tmp0_;
	_tmp1_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	self->priv->connections = _tmp1_;
}

static void
bird_font_spacing_data_finalize (GObject * obj)
{
	BirdFontSpacingData * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_SPACING_DATA, BirdFontSpacingData);
	_g_object_unref0 (self->kerning_classes);
	_g_object_unref0 (self->classes);
	_g_object_unref0 (self->priv->connections);
	G_OBJECT_CLASS (bird_font_spacing_data_parent_class)->finalize (obj);
}

static GType
bird_font_spacing_data_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontSpacingDataClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_spacing_data_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontSpacingData), 0, (GInstanceInitFunc) bird_font_spacing_data_instance_init, NULL };
	GType bird_font_spacing_data_type_id;
	bird_font_spacing_data_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontSpacingData", &g_define_type_info, 0);
	BirdFontSpacingData_private_offset = g_type_add_instance_private (bird_font_spacing_data_type_id, sizeof (BirdFontSpacingDataPrivate));
	return bird_font_spacing_data_type_id;
}

GType
bird_font_spacing_data_get_type (void)
{
	static volatile gsize bird_font_spacing_data_type_id__once = 0;
	if (g_once_init_enter (&bird_font_spacing_data_type_id__once)) {
		GType bird_font_spacing_data_type_id;
		bird_font_spacing_data_type_id = bird_font_spacing_data_get_type_once ();
		g_once_init_leave (&bird_font_spacing_data_type_id__once, bird_font_spacing_data_type_id);
	}
	return bird_font_spacing_data_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

