/* KernSubtable.c generated by valac 0.56.17, the Vala compiler
 * generated from KernSubtable.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 <glib-object.h>
#include <glib.h>
#include <gee.h>
#include <stdlib.h>
#include <string.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_KERN_SUBTABLE_0_PROPERTY,
	BIRD_FONT_KERN_SUBTABLE_NUM_PROPERTIES
};
static GParamSpec* bird_font_kern_subtable_properties[BIRD_FONT_KERN_SUBTABLE_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))
typedef struct _Block80Data Block80Data;

struct _Block80Data {
	int _ref_count_;
	BirdFontKernSubtable* self;
	guint len;
};

static gpointer bird_font_kern_subtable_parent_class = NULL;

static Block80Data* block80_data_ref (Block80Data* _data80_);
static void block80_data_unref (void * _userdata_);
static void __lambda601_ (Block80Data* _data80_,
                   BirdFontPairFormat1* p);
static void ___lambda601__bird_font_kern_subtable_pair_format1_iterator (BirdFontPairFormat1* k,
                                                                  gpointer self);
static void bird_font_kern_subtable_finalize (GObject * obj);
static GType bird_font_kern_subtable_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

BirdFontKernSubtable*
bird_font_kern_subtable_construct (GType object_type)
{
	BirdFontKernSubtable * self = NULL;
	GeeArrayList* _tmp0_;
	self = (BirdFontKernSubtable*) g_object_new (object_type, NULL);
	_tmp0_ = gee_array_list_new (BIRD_FONT_TYPE_PAIR_FORMAT1, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->pairs);
	self->pairs = _tmp0_;
	self->num_pairs = (guint) 0;
	return self;
}

BirdFontKernSubtable*
bird_font_kern_subtable_new (void)
{
	return bird_font_kern_subtable_construct (BIRD_FONT_TYPE_KERN_SUBTABLE);
}

void
bird_font_kern_subtable_add (BirdFontKernSubtable* self,
                             BirdFontPairFormat1* kerning_pair)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	GeeArrayList* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (kerning_pair != NULL);
	_tmp0_ = kerning_pair->pairs;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	self->num_pairs = self->num_pairs + _tmp2_;
	_tmp3_ = self->pairs;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp3_, kerning_pair);
}

/** @return the number of glyphs that is kerned on the left side in 
 * pair pos format 1.
 */
guint
bird_font_kern_subtable_get_length_left (BirdFontKernSubtable* self)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	_tmp0_ = self->pairs;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	result = (guint) _tmp2_;
	return result;
}

/** @return the total number of pairs. */
guint
bird_font_kern_subtable_get_length (BirdFontKernSubtable* self)
{
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	result = self->num_pairs;
	return result;
}

void
bird_font_kern_subtable_all_kern (BirdFontKernSubtable* self,
                                  BirdFontKernSubtableKernIterator iter,
                                  gpointer iter_target,
                                  gint limit)
{
	gint i = 0;
	g_return_if_fail (self != NULL);
	i = 0;
	{
		GeeArrayList* _p_list = NULL;
		GeeArrayList* _tmp0_;
		gint _p_size = 0;
		GeeArrayList* _tmp1_;
		gint _tmp2_;
		gint _tmp3_;
		gint _p_index = 0;
		_tmp0_ = self->pairs;
		_p_list = _tmp0_;
		_tmp1_ = _p_list;
		_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
		_tmp3_ = _tmp2_;
		_p_size = _tmp3_;
		_p_index = -1;
		while (TRUE) {
			gint _tmp4_;
			gint _tmp5_;
			BirdFontPairFormat1* p = NULL;
			GeeArrayList* _tmp6_;
			gpointer _tmp7_;
			_p_index = _p_index + 1;
			_tmp4_ = _p_index;
			_tmp5_ = _p_size;
			if (!(_tmp4_ < _tmp5_)) {
				break;
			}
			_tmp6_ = _p_list;
			_tmp7_ = gee_abstract_list_get ((GeeAbstractList*) _tmp6_, _p_index);
			p = (BirdFontPairFormat1*) _tmp7_;
			{
				GeeArrayList* _k_list = NULL;
				BirdFontPairFormat1* _tmp8_;
				GeeArrayList* _tmp9_;
				gint _k_size = 0;
				GeeArrayList* _tmp10_;
				gint _tmp11_;
				gint _tmp12_;
				gint _k_index = 0;
				_tmp8_ = p;
				_tmp9_ = _tmp8_->pairs;
				_k_list = _tmp9_;
				_tmp10_ = _k_list;
				_tmp11_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp10_);
				_tmp12_ = _tmp11_;
				_k_size = _tmp12_;
				_k_index = -1;
				while (TRUE) {
					gint _tmp13_;
					gint _tmp14_;
					BirdFontKern* k = NULL;
					GeeArrayList* _tmp15_;
					gpointer _tmp16_;
					BirdFontKern* _tmp21_;
					gint _tmp22_;
					_k_index = _k_index + 1;
					_tmp13_ = _k_index;
					_tmp14_ = _k_size;
					if (!(_tmp13_ < _tmp14_)) {
						break;
					}
					_tmp15_ = _k_list;
					_tmp16_ = gee_abstract_list_get ((GeeAbstractList*) _tmp15_, _k_index);
					k = (BirdFontKern*) _tmp16_;
					if (G_UNLIKELY (i >= limit)) {
						gchar* _tmp17_;
						gchar* _tmp18_;
						gchar* _tmp19_;
						gchar* _tmp20_;
						_tmp17_ = g_strdup_printf ("%i", limit);
						_tmp18_ = _tmp17_;
						_tmp19_ = g_strconcat ("Too many pairs. Limit: ", _tmp18_, NULL);
						_tmp20_ = _tmp19_;
						g_warning ("KernSubtable.vala:52: %s", _tmp20_);
						_g_free0 (_tmp20_);
						_g_free0 (_tmp18_);
						_g_object_unref0 (k);
						_g_object_unref0 (p);
						return;
					}
					_tmp21_ = k;
					iter (_tmp21_, iter_target);
					_tmp22_ = i;
					i = _tmp22_ + 1;
					_g_object_unref0 (k);
				}
			}
			_g_object_unref0 (p);
		}
	}
}

void
bird_font_kern_subtable_all_pairs_format1 (BirdFontKernSubtable* self,
                                           BirdFontKernSubtablePairFormat1Iterator iter,
                                           gpointer iter_target,
                                           gint limit)
{
	guint i = 0U;
	g_return_if_fail (self != NULL);
	i = (guint) 0;
	{
		GeeArrayList* _p_list = NULL;
		GeeArrayList* _tmp0_;
		gint _p_size = 0;
		GeeArrayList* _tmp1_;
		gint _tmp2_;
		gint _tmp3_;
		gint _p_index = 0;
		_tmp0_ = self->pairs;
		_p_list = _tmp0_;
		_tmp1_ = _p_list;
		_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
		_tmp3_ = _tmp2_;
		_p_size = _tmp3_;
		_p_index = -1;
		while (TRUE) {
			gint _tmp4_;
			gint _tmp5_;
			BirdFontPairFormat1* p = NULL;
			GeeArrayList* _tmp6_;
			gpointer _tmp7_;
			gboolean _tmp8_ = FALSE;
			BirdFontPairFormat1* _tmp13_;
			guint _tmp14_;
			_p_index = _p_index + 1;
			_tmp4_ = _p_index;
			_tmp5_ = _p_size;
			if (!(_tmp4_ < _tmp5_)) {
				break;
			}
			_tmp6_ = _p_list;
			_tmp7_ = gee_abstract_list_get ((GeeAbstractList*) _tmp6_, _p_index);
			p = (BirdFontPairFormat1*) _tmp7_;
			if (i >= ((guint) limit)) {
				_tmp8_ = limit != -1;
			} else {
				_tmp8_ = FALSE;
			}
			if (G_UNLIKELY (_tmp8_)) {
				gchar* _tmp9_;
				gchar* _tmp10_;
				gchar* _tmp11_;
				gchar* _tmp12_;
				_tmp9_ = g_strdup_printf ("%i", limit);
				_tmp10_ = _tmp9_;
				_tmp11_ = g_strconcat ("Too many pairs. Limit: ", _tmp10_, NULL);
				_tmp12_ = _tmp11_;
				g_warning ("KernSubtable.vala:67: %s", _tmp12_);
				_g_free0 (_tmp12_);
				_g_free0 (_tmp10_);
				_g_object_unref0 (p);
				return;
			}
			_tmp13_ = p;
			iter (_tmp13_, iter_target);
			_tmp14_ = i;
			i = _tmp14_ + 1;
			_g_object_unref0 (p);
		}
	}
}

static Block80Data*
block80_data_ref (Block80Data* _data80_)
{
	g_atomic_int_inc (&_data80_->_ref_count_);
	return _data80_;
}

static void
block80_data_unref (void * _userdata_)
{
	Block80Data* _data80_;
	_data80_ = (Block80Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data80_->_ref_count_)) {
		BirdFontKernSubtable* self;
		self = _data80_->self;
		_g_object_unref0 (self);
		g_slice_free (Block80Data, _data80_);
	}
}

static void
__lambda601_ (Block80Data* _data80_,
              BirdFontPairFormat1* p)
{
	BirdFontKernSubtable* self;
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	self = _data80_->self;
	g_return_if_fail (p != NULL);
	_tmp0_ = p->pairs;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	_data80_->len += (guint) (2 + (4 * _tmp2_));
}

static void
___lambda601__bird_font_kern_subtable_pair_format1_iterator (BirdFontPairFormat1* k,
                                                             gpointer self)
{
	__lambda601_ (self, k);
}

guint
bird_font_kern_subtable_get_pairs_set_length (BirdFontKernSubtable* self)
{
	Block80Data* _data80_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	_data80_ = g_slice_new0 (Block80Data);
	_data80_->_ref_count_ = 1;
	_data80_->self = g_object_ref (self);
	_data80_->len = (guint) 0;
	bird_font_kern_subtable_all_pairs_format1 (self, ___lambda601__bird_font_kern_subtable_pair_format1_iterator, _data80_, -1);
	result = _data80_->len;
	block80_data_unref (_data80_);
	_data80_ = NULL;
	return result;
}

guint
bird_font_kern_subtable_get_pairs_offset_length (BirdFontKernSubtable* self)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	_tmp0_ = self->pairs;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	result = (guint) (2 * _tmp2_);
	return result;
}

guint
bird_font_kern_subtable_get_bytes_used (BirdFontKernSubtable* self)
{
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	result = bird_font_kern_subtable_get_pairs_set_length (self) + bird_font_kern_subtable_get_pairs_offset_length (self);
	return result;
}

void
bird_font_kern_subtable_remove_last (BirdFontKernSubtable* self)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	GeeArrayList* _tmp3_;
	GeeArrayList* _tmp4_;
	gint _tmp5_;
	gint _tmp6_;
	gpointer _tmp7_;
	BirdFontPairFormat1* _tmp8_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->pairs;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	g_return_if_fail (_tmp2_ > 0);
	_tmp3_ = self->pairs;
	_tmp4_ = self->pairs;
	_tmp5_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = gee_abstract_list_remove_at ((GeeAbstractList*) _tmp3_, _tmp6_ - 1);
	_tmp8_ = (BirdFontPairFormat1*) _tmp7_;
	_g_object_unref0 (_tmp8_);
}

static void
bird_font_kern_subtable_class_init (BirdFontKernSubtableClass * klass,
                                    gpointer klass_data)
{
	bird_font_kern_subtable_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = bird_font_kern_subtable_finalize;
}

static void
bird_font_kern_subtable_instance_init (BirdFontKernSubtable * self,
                                       gpointer klass)
{
}

static void
bird_font_kern_subtable_finalize (GObject * obj)
{
	BirdFontKernSubtable * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_KERN_SUBTABLE, BirdFontKernSubtable);
	_g_object_unref0 (self->pairs);
	G_OBJECT_CLASS (bird_font_kern_subtable_parent_class)->finalize (obj);
}

static GType
bird_font_kern_subtable_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontKernSubtableClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_kern_subtable_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontKernSubtable), 0, (GInstanceInitFunc) bird_font_kern_subtable_instance_init, NULL };
	GType bird_font_kern_subtable_type_id;
	bird_font_kern_subtable_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontKernSubtable", &g_define_type_info, 0);
	return bird_font_kern_subtable_type_id;
}

GType
bird_font_kern_subtable_get_type (void)
{
	static volatile gsize bird_font_kern_subtable_type_id__once = 0;
	if (g_once_init_enter (&bird_font_kern_subtable_type_id__once)) {
		GType bird_font_kern_subtable_type_id;
		bird_font_kern_subtable_type_id = bird_font_kern_subtable_get_type_once ();
		g_once_init_leave (&bird_font_kern_subtable_type_id__once, bird_font_kern_subtable_type_id);
	}
	return bird_font_kern_subtable_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;
}

