/* Lookup.c generated by valac 0.56.17, the Vala compiler
 * generated from Lookup.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.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gee.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_LOOKUP_0_PROPERTY,
	BIRD_FONT_LOOKUP_NUM_PROPERTIES
};
static GParamSpec* bird_font_lookup_properties[BIRD_FONT_LOOKUP_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))

static gpointer bird_font_lookup_parent_class = NULL;

static void bird_font_lookup_finalize (GObject * obj);
static GType bird_font_lookup_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

BirdFontLookup*
bird_font_lookup_construct (GType object_type,
                            guint16 type,
                            guint16 flags,
                            const gchar* token)
{
	BirdFontLookup * self = NULL;
	GeeArrayList* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (token != NULL, NULL);
	self = (BirdFontLookup*) g_object_new (object_type, NULL);
	self->type = type;
	self->flags = flags;
	_tmp0_ = gee_array_list_new (BIRD_FONT_TYPE_FONT_DATA, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->subtables);
	self->subtables = _tmp0_;
	_tmp1_ = g_strdup (token);
	_g_free0 (self->token);
	self->token = _tmp1_;
	self->subtable_offset = (guint) 0;
	return self;
}

BirdFontLookup*
bird_font_lookup_new (guint16 type,
                      guint16 flags,
                      const gchar* token)
{
	return bird_font_lookup_construct (BIRD_FONT_TYPE_LOOKUP, type, flags, token);
}

void
bird_font_lookup_add_subtable (BirdFontLookup* self,
                               BirdFontFontData* subtable)
{
	GeeArrayList* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (subtable != NULL);
	_tmp0_ = self->subtables;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp0_, subtable);
}

guint
bird_font_lookup_get_lookup_entry_size (BirdFontLookup* self,
                                        GError** error)
{
	BirdFontFontData* _tmp0_ = NULL;
	BirdFontFontData* _tmp1_;
	GError* _inner_error0_ = NULL;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	_tmp1_ = bird_font_lookup_get_lookup_entry (self, (guint) 0, &_inner_error0_);
	_tmp0_ = _tmp1_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		guint _tmp2_ = 0U;
		g_propagate_error (error, _inner_error0_);
		return _tmp2_;
	}
	result = bird_font_font_data_length_with_padding (_tmp0_);
	_g_object_unref0 (_tmp0_);
	return result;
}

guint
bird_font_lookup_get_subtable_size (BirdFontLookup* self,
                                    GError** error)
{
	guint size = 0U;
	guint s = 0U;
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	size = (guint) 0;
	{
		GeeArrayList* _subtable_list = NULL;
		GeeArrayList* _tmp0_;
		gint _subtable_size = 0;
		GeeArrayList* _tmp1_;
		gint _tmp2_;
		gint _tmp3_;
		gint _subtable_index = 0;
		_tmp0_ = self->subtables;
		_subtable_list = _tmp0_;
		_tmp1_ = _subtable_list;
		_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
		_tmp3_ = _tmp2_;
		_subtable_size = _tmp3_;
		_subtable_index = -1;
		while (TRUE) {
			gint _tmp4_;
			gint _tmp5_;
			BirdFontFontData* subtable = NULL;
			GeeArrayList* _tmp6_;
			gpointer _tmp7_;
			BirdFontFontData* _tmp8_;
			_subtable_index = _subtable_index + 1;
			_tmp4_ = _subtable_index;
			_tmp5_ = _subtable_size;
			if (!(_tmp4_ < _tmp5_)) {
				break;
			}
			_tmp6_ = _subtable_list;
			_tmp7_ = gee_abstract_list_get ((GeeAbstractList*) _tmp6_, _subtable_index);
			subtable = (BirdFontFontData*) _tmp7_;
			_tmp8_ = subtable;
			s = bird_font_font_data_length_with_padding (_tmp8_);
			if (s == ((guint) 0)) {
				g_warning ("Lookup.vala:57: Zero size in subtable.");
			}
			size += s;
			_g_object_unref0 (subtable);
		}
	}
	g_warn_if_fail (size != ((guint) 0));
	result = size;
	return result;
}

BirdFontFontData*
bird_font_lookup_get_lookup_entry (BirdFontLookup* self,
                                   guint offset,
                                   GError** error)
{
	BirdFontFontData* fd = NULL;
	BirdFontFontData* _tmp0_;
	GeeArrayList* _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	BirdFontFontData* _tmp4_;
	BirdFontFontData* _tmp5_;
	BirdFontFontData* _tmp6_;
	BirdFontFontData* _tmp7_;
	GeeArrayList* _tmp8_;
	gint _tmp9_;
	gint _tmp10_;
	guint s = 0U;
	GError* _inner_error0_ = NULL;
	BirdFontFontData* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = bird_font_font_data_new ((guint32) 1024);
	fd = _tmp0_;
	_tmp1_ = self->subtables;
	_tmp2_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = fd;
	g_return_val_if_fail (_tmp3_ > 0, _tmp4_);
	_tmp5_ = fd;
	bird_font_font_data_add_ushort (_tmp5_, self->type, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (fd);
		return NULL;
	}
	_tmp6_ = fd;
	bird_font_font_data_add_ushort (_tmp6_, self->flags, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (fd);
		return NULL;
	}
	_tmp7_ = fd;
	_tmp8_ = self->subtables;
	_tmp9_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp8_);
	_tmp10_ = _tmp9_;
	bird_font_font_data_add_ushort (_tmp7_, (guint16) _tmp10_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (fd);
		return NULL;
	}
	{
		GeeArrayList* _subtable_list = NULL;
		GeeArrayList* _tmp11_;
		gint _subtable_size = 0;
		GeeArrayList* _tmp12_;
		gint _tmp13_;
		gint _tmp14_;
		gint _subtable_index = 0;
		_tmp11_ = self->subtables;
		_subtable_list = _tmp11_;
		_tmp12_ = _subtable_list;
		_tmp13_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp12_);
		_tmp14_ = _tmp13_;
		_subtable_size = _tmp14_;
		_subtable_index = -1;
		while (TRUE) {
			gint _tmp15_;
			gint _tmp16_;
			BirdFontFontData* subtable = NULL;
			GeeArrayList* _tmp17_;
			gpointer _tmp18_;
			BirdFontFontData* _tmp19_;
			BirdFontFontData* _tmp20_;
			_subtable_index = _subtable_index + 1;
			_tmp15_ = _subtable_index;
			_tmp16_ = _subtable_size;
			if (!(_tmp15_ < _tmp16_)) {
				break;
			}
			_tmp17_ = _subtable_list;
			_tmp18_ = gee_abstract_list_get ((GeeAbstractList*) _tmp17_, _subtable_index);
			subtable = (BirdFontFontData*) _tmp18_;
			_tmp19_ = fd;
			bird_font_font_data_add_ushort (_tmp19_, (guint16) offset, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				g_propagate_error (error, _inner_error0_);
				_g_object_unref0 (subtable);
				_g_object_unref0 (fd);
				return NULL;
			}
			_tmp20_ = subtable;
			s = bird_font_font_data_length_with_padding (_tmp20_);
			if (s == ((guint) 0)) {
				g_warning ("Lookup.vala:86: Zero size in subtable.");
			}
			offset = offset + (2 + s);
			_g_object_unref0 (subtable);
		}
	}
	result = fd;
	return result;
}

static void
bird_font_lookup_class_init (BirdFontLookupClass * klass,
                             gpointer klass_data)
{
	bird_font_lookup_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = bird_font_lookup_finalize;
}

static void
bird_font_lookup_instance_init (BirdFontLookup * self,
                                gpointer klass)
{
	self->subtable_offset = (guint) 0;
	self->entry_offset = (guint) 0;
}

static void
bird_font_lookup_finalize (GObject * obj)
{
	BirdFontLookup * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_LOOKUP, BirdFontLookup);
	_g_object_unref0 (self->subtables);
	_g_free0 (self->token);
	G_OBJECT_CLASS (bird_font_lookup_parent_class)->finalize (obj);
}

/** Representation of one lookup in gsub and gpos tables. */
static GType
bird_font_lookup_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontLookupClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_lookup_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontLookup), 0, (GInstanceInitFunc) bird_font_lookup_instance_init, NULL };
	GType bird_font_lookup_type_id;
	bird_font_lookup_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontLookup", &g_define_type_info, 0);
	return bird_font_lookup_type_id;
}

GType
bird_font_lookup_get_type (void)
{
	static volatile gsize bird_font_lookup_type_id__once = 0;
	if (g_once_init_enter (&bird_font_lookup_type_id__once)) {
		GType bird_font_lookup_type_id;
		bird_font_lookup_type_id = bird_font_lookup_get_type_once ();
		g_once_init_leave (&bird_font_lookup_type_id__once, bird_font_lookup_type_id);
	}
	return bird_font_lookup_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;
}

