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

/*
Copyright (C) 2017 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_SPLITTER_0_PROPERTY,
	BIRD_FONT_KERN_SPLITTER_NUM_PROPERTIES
};
static GParamSpec* bird_font_kern_splitter_properties[BIRD_FONT_KERN_SPLITTER_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 _BirdFontKernSplitterPrivate {
	BirdFontKernList* source_list;
};

static gint BirdFontKernSplitter_private_offset;
static gpointer bird_font_kern_splitter_parent_class = NULL;

static void __lambda60_ (BirdFontKernSplitter* self,
                  BirdFontPairFormat1* pair);
static void ___lambda60__bird_font_kern_list_pair_format1_iterator (BirdFontPairFormat1* k,
                                                             gpointer self);
static void bird_font_kern_splitter_finalize (GObject * obj);
static GType bird_font_kern_splitter_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_kern_splitter_get_instance_private (BirdFontKernSplitter* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontKernSplitter_private_offset);
}

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

static void
__lambda60_ (BirdFontKernSplitter* self,
             BirdFontPairFormat1* pair)
{
	GeeArrayList* _tmp0_;
	g_return_if_fail (pair != NULL);
	_tmp0_ = self->pairs;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp0_, pair);
}

static void
___lambda60__bird_font_kern_list_pair_format1_iterator (BirdFontPairFormat1* k,
                                                        gpointer self)
{
	__lambda60_ ((BirdFontKernSplitter*) self, k);
}

BirdFontKernSplitter*
bird_font_kern_splitter_construct (GType object_type,
                                   BirdFontKernList* kerning_list)
{
	BirdFontKernSplitter * self = NULL;
	BirdFontKernList* _tmp0_;
	GeeArrayList* _tmp1_;
	g_return_val_if_fail (kerning_list != NULL, NULL);
	self = (BirdFontKernSplitter*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (kerning_list);
	_g_object_unref0 (self->priv->source_list);
	self->priv->source_list = _tmp0_;
	_tmp1_ = 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 = _tmp1_;
	bird_font_kern_list_all_single_kern (kerning_list, ___lambda60__bird_font_kern_list_pair_format1_iterator, self);
	return self;
}

BirdFontKernSplitter*
bird_font_kern_splitter_new (BirdFontKernList* kerning_list)
{
	return bird_font_kern_splitter_construct (BIRD_FONT_TYPE_KERN_SPLITTER, kerning_list);
}

/** Prevent integer overflow in coverage table of the PairPos1 subtable. */
gboolean
bird_font_kern_splitter_is_full (BirdFontKernSplitter* self,
                                 BirdFontKernList* kerning_pairs)
{
	guint length = 0U;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (kerning_pairs != NULL, FALSE);
	length = (guint) 10;
	length += bird_font_gpos_table_pairs_offset_length (kerning_pairs);
	length += bird_font_gpos_table_pairs_set_length (kerning_pairs);
	result = length > ((guint) (G_MAXUINT16 - 10));
	return result;
}

BirdFontKernList*
bird_font_kern_splitter_get_subset (BirdFontKernSplitter* self,
                                    guint offset)
{
	gint count = 0;
	BirdFontKernList* _result_ = NULL;
	BirdFontKernList* _tmp0_;
	BirdFontGlyfTable* _tmp1_;
	BirdFontKernList* _tmp2_;
	BirdFontPairFormat1* current = NULL;
	BirdFontPairFormat1* _tmp3_;
	BirdFontPairFormat1* _tmp4_;
	BirdFontKernList* result;
	g_return_val_if_fail (self != NULL, NULL);
	count = 0;
	_tmp0_ = self->priv->source_list;
	_tmp1_ = _tmp0_->glyf_table;
	_tmp2_ = bird_font_kern_list_new (_tmp1_);
	_result_ = _tmp2_;
	_tmp3_ = bird_font_pair_format1_new ();
	current = _tmp3_;
	_tmp4_ = current;
	_tmp4_->left = (guint16) 0xFFFF;
	{
		guint i = 0U;
		i = offset;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				GeeArrayList* _tmp7_;
				gint _tmp8_;
				gint _tmp9_;
				BirdFontPairFormat1* next = NULL;
				GeeArrayList* _tmp10_;
				gpointer _tmp11_;
				BirdFontKernList* _tmp12_;
				BirdFontPairFormat1* _tmp13_;
				BirdFontPairFormat1* _tmp14_;
				BirdFontPairFormat1* _tmp21_;
				GeeArrayList* _tmp22_;
				gint _tmp23_;
				gint _tmp24_;
				BirdFontPairFormat1* _tmp35_;
				GeeArrayList* _tmp36_;
				BirdFontPairFormat1* _tmp37_;
				GeeArrayList* _tmp38_;
				gpointer _tmp39_;
				BirdFontKern* _tmp40_;
				gint _tmp41_;
				if (!_tmp5_) {
					guint _tmp6_;
					_tmp6_ = i;
					i = _tmp6_ + 1;
				}
				_tmp5_ = FALSE;
				_tmp7_ = self->pairs;
				_tmp8_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp7_);
				_tmp9_ = _tmp8_;
				if (!(i < ((guint) _tmp9_))) {
					break;
				}
				_tmp10_ = self->pairs;
				_tmp11_ = gee_abstract_list_get ((GeeAbstractList*) _tmp10_, (gint) i);
				next = (BirdFontPairFormat1*) _tmp11_;
				_tmp12_ = _result_;
				if (bird_font_kern_splitter_is_full (self, _tmp12_)) {
					_g_object_unref0 (next);
					break;
				}
				_tmp13_ = next;
				_tmp14_ = current;
				if (_tmp13_->left != _tmp14_->left) {
					BirdFontPairFormat1* _tmp15_;
					BirdFontPairFormat1* _tmp16_;
					BirdFontPairFormat1* _tmp17_;
					BirdFontKernList* _tmp18_;
					GeeArrayList* _tmp19_;
					BirdFontPairFormat1* _tmp20_;
					_tmp15_ = bird_font_pair_format1_new ();
					_g_object_unref0 (current);
					current = _tmp15_;
					_tmp16_ = current;
					_tmp17_ = next;
					_tmp16_->left = _tmp17_->left;
					_tmp18_ = _result_;
					_tmp19_ = _tmp18_->pairs;
					_tmp20_ = current;
					gee_abstract_collection_add ((GeeAbstractCollection*) _tmp19_, _tmp20_);
				}
				_tmp21_ = next;
				_tmp22_ = _tmp21_->pairs;
				_tmp23_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp22_);
				_tmp24_ = _tmp23_;
				if (G_UNLIKELY (_tmp24_ != 1)) {
					BirdFontPairFormat1* _tmp25_;
					GeeArrayList* _tmp26_;
					gint _tmp27_;
					gint _tmp28_;
					gchar* _tmp29_;
					gchar* _tmp30_;
					gchar* _tmp31_;
					gchar* _tmp32_;
					gchar* _tmp33_;
					gchar* _tmp34_;
					_tmp25_ = next;
					_tmp26_ = _tmp25_->pairs;
					_tmp27_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp26_);
					_tmp28_ = _tmp27_;
					_tmp29_ = g_strdup_printf ("%i", _tmp28_);
					_tmp30_ = _tmp29_;
					_tmp31_ = g_strconcat ("next.pairs.size: != ", _tmp30_, NULL);
					_tmp32_ = _tmp31_;
					_tmp33_ = g_strconcat ("Splitting kerning pairs failed. ", _tmp32_, NULL);
					_tmp34_ = _tmp33_;
					g_warning ("KernSplitter.vala:60: %s", _tmp34_);
					_g_free0 (_tmp34_);
					_g_free0 (_tmp32_);
					_g_free0 (_tmp30_);
				}
				_tmp35_ = current;
				_tmp36_ = _tmp35_->pairs;
				_tmp37_ = next;
				_tmp38_ = _tmp37_->pairs;
				_tmp39_ = gee_abstract_list_get ((GeeAbstractList*) _tmp38_, 0);
				_tmp40_ = (BirdFontKern*) _tmp39_;
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp36_, _tmp40_);
				_g_object_unref0 (_tmp40_);
				_tmp41_ = count;
				count = _tmp41_ + 1;
				_g_object_unref0 (next);
			}
		}
	}
	{
		GeeArrayList* _kerning_list = NULL;
		BirdFontKernList* _tmp42_;
		GeeArrayList* _tmp43_;
		gint _kerning_size = 0;
		GeeArrayList* _tmp44_;
		gint _tmp45_;
		gint _tmp46_;
		gint _kerning_index = 0;
		_tmp42_ = _result_;
		_tmp43_ = _tmp42_->pairs;
		_kerning_list = _tmp43_;
		_tmp44_ = _kerning_list;
		_tmp45_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp44_);
		_tmp46_ = _tmp45_;
		_kerning_size = _tmp46_;
		_kerning_index = -1;
		while (TRUE) {
			gint _tmp47_;
			gint _tmp48_;
			BirdFontPairFormat1* kerning = NULL;
			GeeArrayList* _tmp49_;
			gpointer _tmp50_;
			BirdFontKernList* _tmp51_;
			BirdFontKernList* _tmp52_;
			BirdFontPairFormat1* _tmp53_;
			GeeArrayList* _tmp54_;
			gint _tmp55_;
			gint _tmp56_;
			_kerning_index = _kerning_index + 1;
			_tmp47_ = _kerning_index;
			_tmp48_ = _kerning_size;
			if (!(_tmp47_ < _tmp48_)) {
				break;
			}
			_tmp49_ = _kerning_list;
			_tmp50_ = gee_abstract_list_get ((GeeAbstractList*) _tmp49_, _kerning_index);
			kerning = (BirdFontPairFormat1*) _tmp50_;
			_tmp51_ = _result_;
			_tmp52_ = _result_;
			_tmp53_ = kerning;
			_tmp54_ = _tmp53_->pairs;
			_tmp55_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp54_);
			_tmp56_ = _tmp55_;
			_tmp52_->num_pairs = _tmp52_->num_pairs + _tmp56_;
			_g_object_unref0 (kerning);
		}
	}
	result = _result_;
	_g_object_unref0 (current);
	return result;
}

static void
bird_font_kern_splitter_class_init (BirdFontKernSplitterClass * klass,
                                    gpointer klass_data)
{
	bird_font_kern_splitter_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontKernSplitter_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_kern_splitter_finalize;
}

static void
bird_font_kern_splitter_instance_init (BirdFontKernSplitter * self,
                                       gpointer klass)
{
	self->priv = bird_font_kern_splitter_get_instance_private (self);
}

static void
bird_font_kern_splitter_finalize (GObject * obj)
{
	BirdFontKernSplitter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_KERN_SPLITTER, BirdFontKernSplitter);
	_g_object_unref0 (self->pairs);
	_g_object_unref0 (self->priv->source_list);
	G_OBJECT_CLASS (bird_font_kern_splitter_parent_class)->finalize (obj);
}

static GType
bird_font_kern_splitter_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontKernSplitterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_kern_splitter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontKernSplitter), 0, (GInstanceInitFunc) bird_font_kern_splitter_instance_init, NULL };
	GType bird_font_kern_splitter_type_id;
	bird_font_kern_splitter_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontKernSplitter", &g_define_type_info, 0);
	BirdFontKernSplitter_private_offset = g_type_add_instance_private (bird_font_kern_splitter_type_id, sizeof (BirdFontKernSplitterPrivate));
	return bird_font_kern_splitter_type_id;
}

GType
bird_font_kern_splitter_get_type (void)
{
	static volatile gsize bird_font_kern_splitter_type_id__once = 0;
	if (g_once_init_enter (&bird_font_kern_splitter_type_id__once)) {
		GType bird_font_kern_splitter_type_id;
		bird_font_kern_splitter_type_id = bird_font_kern_splitter_get_type_once ();
		g_once_init_leave (&bird_font_kern_splitter_type_id__once, bird_font_kern_splitter_type_id);
	}
	return bird_font_kern_splitter_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;
}

