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

/*
Copyright (C) 2015 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 <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_CLIG_FEATURE_0_PROPERTY,
	BIRD_FONT_CLIG_FEATURE_NUM_PROPERTIES
};
static GParamSpec* bird_font_clig_feature_properties[BIRD_FONT_CLIG_FEATURE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _BirdFontCligFeaturePrivate {
	BirdFontLookups* lookups;
	BirdFontGlyfTable* glyf_table;
};

static gint BirdFontCligFeature_private_offset;
static gpointer bird_font_clig_feature_parent_class = NULL;

static void bird_font_clig_feature_generate_feature (BirdFontCligFeature* self,
                                              GError** error);
static GeeArrayList* bird_font_clig_feature_get_chaining_contextual_substition_subtable (BirdFontCligFeature* self,
                                                                                  BirdFontContextualLigatureCollection* contexts,
                                                                                  GError** error);
static void bird_font_clig_feature_finalize (GObject * obj);
static GType bird_font_clig_feature_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_clig_feature_get_instance_private (BirdFontCligFeature* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontCligFeature_private_offset);
}

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

BirdFontCligFeature*
bird_font_clig_feature_construct (GType object_type,
                                  BirdFontGlyfTable* glyf_table,
                                  GError** error)
{
	BirdFontCligFeature * self = NULL;
	BirdFontGlyfTable* _tmp0_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (glyf_table != NULL, NULL);
	self = (BirdFontCligFeature*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (glyf_table);
	_g_object_unref0 (self->priv->glyf_table);
	self->priv->glyf_table = _tmp0_;
	bird_font_clig_feature_generate_feature (self, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	return self;
}

BirdFontCligFeature*
bird_font_clig_feature_new (BirdFontGlyfTable* glyf_table,
                            GError** error)
{
	return bird_font_clig_feature_construct (BIRD_FONT_TYPE_CLIG_FEATURE, glyf_table, error);
}

gboolean
bird_font_clig_feature_has_regular_ligatures (BirdFontCligFeature* self)
{
	BirdFontLigatureCollection* _tmp0_;
	GeeArrayList* _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->clig;
	_tmp1_ = _tmp0_->ligature_sets;
	_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
	_tmp3_ = _tmp2_;
	result = _tmp3_ > 0;
	return result;
}

BirdFontLookups*
bird_font_clig_feature_get_lookups (BirdFontCligFeature* self)
{
	BirdFontLookups* _tmp0_;
	BirdFontLookups* _tmp1_;
	BirdFontLookups* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->lookups;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
bird_font_clig_feature_generate_feature (BirdFontCligFeature* self,
                                         GError** error)
{
	GeeArrayList* chain_data = NULL;
	BirdFontFontData* clig_subtable = NULL;
	BirdFontFontData* fd = NULL;
	BirdFontLookup* lookup = NULL;
	BirdFontFontData* _tmp0_;
	BirdFontGlyfTable* _tmp1_;
	BirdFontLigatureCollection* _tmp2_;
	BirdFontGlyfTable* _tmp3_;
	BirdFontContextualLigatureCollection* _tmp4_;
	BirdFontFontData* _tmp5_ = NULL;
	BirdFontLigatureCollection* _tmp6_;
	BirdFontGlyfTable* _tmp7_;
	BirdFontFontData* _tmp8_;
	BirdFontFontData* _tmp9_;
	BirdFontFontData* _tmp10_;
	GeeArrayList* _tmp11_ = NULL;
	BirdFontContextualLigatureCollection* _tmp12_;
	GeeArrayList* _tmp13_;
	GeeArrayList* _tmp14_;
	BirdFontLookups* _tmp15_;
	BirdFontContextualLigatureCollection* _tmp16_;
	BirdFontLookup* _tmp47_;
	BirdFontLookup* _tmp48_;
	BirdFontFontData* _tmp49_;
	BirdFontLookups* _tmp50_;
	BirdFontLookup* _tmp51_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = bird_font_font_data_new ((guint32) 1024);
	_g_object_unref0 (fd);
	fd = _tmp0_;
	_tmp1_ = self->priv->glyf_table;
	_tmp2_ = bird_font_ligature_collection_new_clig (_tmp1_);
	_g_object_unref0 (self->clig);
	self->clig = _tmp2_;
	_tmp3_ = self->priv->glyf_table;
	_tmp4_ = bird_font_contextual_ligature_collection_new (_tmp3_);
	_g_object_unref0 (self->contextual);
	self->contextual = _tmp4_;
	_tmp6_ = self->clig;
	_tmp7_ = self->priv->glyf_table;
	_tmp8_ = bird_font_ligature_collection_get_font_data (_tmp6_, _tmp7_, &_inner_error0_);
	_tmp5_ = _tmp8_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (lookup);
		_g_object_unref0 (fd);
		_g_object_unref0 (clig_subtable);
		_g_object_unref0 (chain_data);
		return;
	}
	_tmp9_ = _tmp5_;
	_tmp5_ = NULL;
	_g_object_unref0 (clig_subtable);
	clig_subtable = _tmp9_;
	_tmp10_ = clig_subtable;
	bird_font_font_data_pad (_tmp10_);
	_tmp12_ = self->contextual;
	_tmp13_ = bird_font_clig_feature_get_chaining_contextual_substition_subtable (self, _tmp12_, &_inner_error0_);
	_tmp11_ = _tmp13_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (_tmp5_);
		_g_object_unref0 (lookup);
		_g_object_unref0 (fd);
		_g_object_unref0 (clig_subtable);
		_g_object_unref0 (chain_data);
		return;
	}
	_tmp14_ = _tmp11_;
	_tmp11_ = NULL;
	_g_object_unref0 (chain_data);
	chain_data = _tmp14_;
	_tmp15_ = bird_font_lookups_new ();
	_g_object_unref0 (self->priv->lookups);
	self->priv->lookups = _tmp15_;
	_tmp16_ = self->contextual;
	if (bird_font_contextual_ligature_collection_has_ligatures (_tmp16_)) {
		BirdFontLookup* _tmp34_;
		BirdFontLookups* _tmp45_;
		BirdFontLookup* _tmp46_;
		{
			GeeArrayList* _s_list = NULL;
			BirdFontContextualLigatureCollection* _tmp17_;
			GeeArrayList* _tmp18_;
			gint _s_size = 0;
			GeeArrayList* _tmp19_;
			gint _tmp20_;
			gint _tmp21_;
			gint _s_index = 0;
			_tmp17_ = self->contextual;
			_tmp18_ = _tmp17_->ligatures;
			_s_list = _tmp18_;
			_tmp19_ = _s_list;
			_tmp20_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp19_);
			_tmp21_ = _tmp20_;
			_s_size = _tmp21_;
			_s_index = -1;
			while (TRUE) {
				gint _tmp22_;
				gint _tmp23_;
				BirdFontLigatureCollection* s = NULL;
				GeeArrayList* _tmp24_;
				gpointer _tmp25_;
				BirdFontLookup* _tmp26_;
				BirdFontFontData* _tmp27_ = NULL;
				BirdFontLigatureCollection* _tmp28_;
				BirdFontGlyfTable* _tmp29_;
				BirdFontFontData* _tmp30_;
				BirdFontLookup* _tmp31_;
				BirdFontLookups* _tmp32_;
				BirdFontLookup* _tmp33_;
				_s_index = _s_index + 1;
				_tmp22_ = _s_index;
				_tmp23_ = _s_size;
				if (!(_tmp22_ < _tmp23_)) {
					break;
				}
				_tmp24_ = _s_list;
				_tmp25_ = gee_abstract_list_get ((GeeAbstractList*) _tmp24_, _s_index);
				s = (BirdFontLigatureCollection*) _tmp25_;
				_tmp26_ = bird_font_lookup_new ((guint16) 4, (guint16) 0, "chained data");
				_g_object_unref0 (lookup);
				lookup = _tmp26_;
				_tmp28_ = s;
				_tmp29_ = self->priv->glyf_table;
				_tmp30_ = bird_font_ligature_collection_get_font_data (_tmp28_, _tmp29_, &_inner_error0_);
				_tmp27_ = _tmp30_;
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					g_propagate_error (error, _inner_error0_);
					_g_object_unref0 (s);
					_g_object_unref0 (_tmp11_);
					_g_object_unref0 (_tmp5_);
					_g_object_unref0 (lookup);
					_g_object_unref0 (fd);
					_g_object_unref0 (clig_subtable);
					_g_object_unref0 (chain_data);
					return;
				}
				_tmp31_ = lookup;
				bird_font_lookup_add_subtable (_tmp31_, _tmp27_);
				_tmp32_ = self->priv->lookups;
				_tmp33_ = lookup;
				bird_font_lookups_add_lookup (_tmp32_, _tmp33_);
				_g_object_unref0 (_tmp27_);
				_g_object_unref0 (s);
			}
		}
		_tmp34_ = bird_font_lookup_new ((guint16) 6, (guint16) 0, "chain context");
		_g_object_unref0 (lookup);
		lookup = _tmp34_;
		{
			GeeArrayList* _d_list = NULL;
			GeeArrayList* _tmp35_;
			gint _d_size = 0;
			GeeArrayList* _tmp36_;
			gint _tmp37_;
			gint _tmp38_;
			gint _d_index = 0;
			_tmp35_ = chain_data;
			_d_list = _tmp35_;
			_tmp36_ = _d_list;
			_tmp37_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp36_);
			_tmp38_ = _tmp37_;
			_d_size = _tmp38_;
			_d_index = -1;
			while (TRUE) {
				gint _tmp39_;
				gint _tmp40_;
				BirdFontFontData* d = NULL;
				GeeArrayList* _tmp41_;
				gpointer _tmp42_;
				BirdFontLookup* _tmp43_;
				BirdFontFontData* _tmp44_;
				_d_index = _d_index + 1;
				_tmp39_ = _d_index;
				_tmp40_ = _d_size;
				if (!(_tmp39_ < _tmp40_)) {
					break;
				}
				_tmp41_ = _d_list;
				_tmp42_ = gee_abstract_list_get ((GeeAbstractList*) _tmp41_, _d_index);
				d = (BirdFontFontData*) _tmp42_;
				_tmp43_ = lookup;
				_tmp44_ = d;
				bird_font_lookup_add_subtable (_tmp43_, _tmp44_);
				_g_object_unref0 (d);
			}
		}
		_tmp45_ = self->priv->lookups;
		_tmp46_ = lookup;
		bird_font_lookups_add_lookup (_tmp45_, _tmp46_);
	}
	_tmp47_ = bird_font_lookup_new ((guint16) 4, (guint16) 0, "ligatures");
	_g_object_unref0 (lookup);
	lookup = _tmp47_;
	_tmp48_ = lookup;
	_tmp49_ = clig_subtable;
	bird_font_lookup_add_subtable (_tmp48_, _tmp49_);
	_tmp50_ = self->priv->lookups;
	_tmp51_ = lookup;
	bird_font_lookups_add_lookup (_tmp50_, _tmp51_);
	_g_object_unref0 (_tmp11_);
	_g_object_unref0 (_tmp5_);
	_g_object_unref0 (lookup);
	_g_object_unref0 (fd);
	_g_object_unref0 (clig_subtable);
	_g_object_unref0 (chain_data);
}

static GeeArrayList*
bird_font_clig_feature_get_chaining_contextual_substition_subtable (BirdFontCligFeature* self,
                                                                    BirdFontContextualLigatureCollection* contexts,
                                                                    GError** error)
{
	GeeArrayList* fd = NULL;
	GeeArrayList* _tmp0_;
	guint16 ligature_lookup_index = 0U;
	GError* _inner_error0_ = NULL;
	GeeArrayList* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (contexts != NULL, NULL);
	_tmp0_ = gee_array_list_new (BIRD_FONT_TYPE_FONT_DATA, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	fd = _tmp0_;
	ligature_lookup_index = (guint16) 0;
	{
		GeeArrayList* _context_list = NULL;
		GeeArrayList* _tmp1_;
		gint _context_size = 0;
		GeeArrayList* _tmp2_;
		gint _tmp3_;
		gint _tmp4_;
		gint _context_index = 0;
		_tmp1_ = contexts->ligature_context;
		_context_list = _tmp1_;
		_tmp2_ = _context_list;
		_tmp3_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp2_);
		_tmp4_ = _tmp3_;
		_context_size = _tmp4_;
		_context_index = -1;
		while (TRUE) {
			gint _tmp5_;
			gint _tmp6_;
			BirdFontContextualLigature* context = NULL;
			GeeArrayList* _tmp7_;
			gpointer _tmp8_;
			BirdFontFontData* _tmp9_ = NULL;
			BirdFontContextualLigature* _tmp10_;
			BirdFontGlyfTable* _tmp11_;
			BirdFontFontData* _tmp12_;
			GeeArrayList* _tmp13_;
			guint16 _tmp14_;
			_context_index = _context_index + 1;
			_tmp5_ = _context_index;
			_tmp6_ = _context_size;
			if (!(_tmp5_ < _tmp6_)) {
				break;
			}
			_tmp7_ = _context_list;
			_tmp8_ = gee_abstract_list_get ((GeeAbstractList*) _tmp7_, _context_index);
			context = (BirdFontContextualLigature*) _tmp8_;
			_tmp10_ = context;
			_tmp11_ = self->priv->glyf_table;
			_tmp12_ = bird_font_contextual_ligature_get_font_data (_tmp10_, _tmp11_, ligature_lookup_index, &_inner_error0_);
			_tmp9_ = _tmp12_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				g_propagate_error (error, _inner_error0_);
				_g_object_unref0 (context);
				_g_object_unref0 (fd);
				return NULL;
			}
			_tmp13_ = fd;
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp13_, _tmp9_);
			_tmp14_ = ligature_lookup_index;
			ligature_lookup_index = _tmp14_ + 1;
			_g_object_unref0 (_tmp9_);
			_g_object_unref0 (context);
		}
	}
	result = fd;
	return result;
}

static void
bird_font_clig_feature_class_init (BirdFontCligFeatureClass * klass,
                                   gpointer klass_data)
{
	bird_font_clig_feature_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontCligFeature_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_clig_feature_finalize;
}

static void
bird_font_clig_feature_instance_init (BirdFontCligFeature * self,
                                      gpointer klass)
{
	self->priv = bird_font_clig_feature_get_instance_private (self);
}

static void
bird_font_clig_feature_finalize (GObject * obj)
{
	BirdFontCligFeature * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_CLIG_FEATURE, BirdFontCligFeature);
	_g_object_unref0 (self->clig);
	_g_object_unref0 (self->contextual);
	_g_object_unref0 (self->priv->lookups);
	_g_object_unref0 (self->priv->glyf_table);
	G_OBJECT_CLASS (bird_font_clig_feature_parent_class)->finalize (obj);
}

static GType
bird_font_clig_feature_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontCligFeatureClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_clig_feature_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontCligFeature), 0, (GInstanceInitFunc) bird_font_clig_feature_instance_init, NULL };
	GType bird_font_clig_feature_type_id;
	bird_font_clig_feature_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontCligFeature", &g_define_type_info, 0);
	BirdFontCligFeature_private_offset = g_type_add_instance_private (bird_font_clig_feature_type_id, sizeof (BirdFontCligFeaturePrivate));
	return bird_font_clig_feature_type_id;
}

GType
bird_font_clig_feature_get_type (void)
{
	static volatile gsize bird_font_clig_feature_type_id__once = 0;
	if (g_once_init_enter (&bird_font_clig_feature_type_id__once)) {
		GType bird_font_clig_feature_type_id;
		bird_font_clig_feature_type_id = bird_font_clig_feature_get_type_once ();
		g_once_init_leave (&bird_font_clig_feature_type_id__once, bird_font_clig_feature_type_id);
	}
	return bird_font_clig_feature_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;
}

