/* Ligature.c generated by valac 0.56.17, the Vala compiler
 * generated from Ligature.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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.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_LIGATURE_0_PROPERTY,
	BIRD_FONT_LIGATURE_NUM_PROPERTIES
};
static GParamSpec* bird_font_ligature_properties[BIRD_FONT_LIGATURE_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer bird_font_ligature_parent_class = NULL;

static void __lambda5_ (BirdFontLigature* self,
                 const gchar* text);
static void ___lambda5__bird_font_text_listener_signal_text_input (BirdFontTextListener* _sender,
                                                            const gchar* text,
                                                            gpointer self);
static void __lambda6_ (BirdFontLigature* self);
static void ___lambda6__bird_font_text_listener_signal_submit (BirdFontTextListener* _sender,
                                                        const gchar* text,
                                                        gpointer self);
static void __lambda404_ (BirdFontLigature* self,
                   const gchar* text);
static void ___lambda404__bird_font_text_listener_signal_text_input (BirdFontTextListener* _sender,
                                                              const gchar* text,
                                                              gpointer self);
static void __lambda405_ (BirdFontLigature* self);
static void ___lambda405__bird_font_text_listener_signal_submit (BirdFontTextListener* _sender,
                                                          const gchar* text,
                                                          gpointer self);
static void bird_font_ligature_finalize (GObject * obj);
static GType bird_font_ligature_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

BirdFontLigature*
bird_font_ligature_construct (GType object_type,
                              const gchar* ligature,
                              const gchar* substitution)
{
	BirdFontLigature * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (ligature != NULL, NULL);
	g_return_val_if_fail (substitution != NULL, NULL);
	self = (BirdFontLigature*) g_object_new (object_type, NULL);
	_tmp0_ = g_strdup (ligature);
	_g_free0 (self->ligature);
	self->ligature = _tmp0_;
	_tmp1_ = g_strdup (substitution);
	_g_free0 (self->substitution);
	self->substitution = _tmp1_;
	return self;
}

BirdFontLigature*
bird_font_ligature_new (const gchar* ligature,
                        const gchar* substitution)
{
	return bird_font_ligature_construct (BIRD_FONT_TYPE_LIGATURE, ligature, substitution);
}

static gboolean
string_get_next_char (const gchar* self,
                      gint* index,
                      gunichar* c)
{
	gunichar _vala_c = 0U;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_vala_c = g_utf8_get_char (((gchar*) self) + (*index));
	if (_vala_c != ((gunichar) 0)) {
		gchar* _tmp0_;
		_tmp0_ = g_utf8_next_char (((gchar*) self) + (*index));
		*index = (gint) (_tmp0_ - ((gchar*) self));
		result = TRUE;
		if (c) {
			*c = _vala_c;
		}
		return result;
	} else {
		result = FALSE;
		if (c) {
			*c = _vala_c;
		}
		return result;
	}
}

gunichar
bird_font_ligature_get_first_char (BirdFontLigature* self)
{
	gunichar first = 0U;
	gint index = 0;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gunichar _tmp3_ = 0U;
	gunichar result;
	g_return_val_if_fail (self != NULL, 0U);
	index = 0;
	_tmp0_ = self->substitution;
	_tmp1_ = bird_font_ligature_get_coverage (_tmp0_);
	_tmp2_ = _tmp1_;
	string_get_next_char (_tmp2_, &index, &_tmp3_);
	first = _tmp3_;
	_g_free0 (_tmp2_);
	result = first;
	return result;
}

static gchar*
g_unichar_to_string (gunichar self)
{
	gchar* str = NULL;
	gchar* _tmp0_;
	gchar* result;
	_tmp0_ = g_new0 (gchar, 7);
	str = (gchar*) _tmp0_;
	g_unichar_to_utf8 (self, str);
	result = str;
	return result;
}

gchar*
bird_font_ligature_get_coverage (const gchar* ligatures)
{
	gchar** sp = NULL;
	gint sp_length1 = 0;
	gint _sp_size_ = 0;
	gunichar first = 0U;
	gint index = 0;
	gchar* characters = NULL;
	gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	const gchar* _tmp11_;
	gchar** _tmp12_;
	gchar** _tmp13_;
	gchar** _tmp14_;
	gint _tmp14__length1;
	gchar** _tmp16_;
	gint _tmp16__length1;
	const gchar* _tmp17_;
	gchar** _tmp20_;
	gint _tmp20__length1;
	const gchar* _tmp21_;
	gunichar _tmp22_ = 0U;
	gchar* _tmp23_;
	gchar* result;
	g_return_val_if_fail (ligatures != NULL, NULL);
	index = 0;
	_tmp0_ = g_strdup (ligatures);
	characters = _tmp0_;
	_tmp2_ = characters;
	if (g_str_has_prefix (_tmp2_, "U+")) {
		_tmp1_ = TRUE;
	} else {
		const gchar* _tmp3_;
		_tmp3_ = characters;
		_tmp1_ = g_str_has_prefix (_tmp3_, "u+");
	}
	if (_tmp1_) {
		const gchar* _tmp4_;
		gchar** _tmp5_;
		gchar** _tmp6_;
		gchar** _tmp7_;
		gint _tmp7__length1;
		gchar** _tmp8_;
		gint _tmp8__length1;
		const gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp4_ = characters;
		_tmp6_ = _tmp5_ = g_strsplit (_tmp4_, " ", 0);
		sp = (_vala_array_free (sp, sp_length1, (GDestroyNotify) g_free), NULL);
		sp = _tmp6_;
		sp_length1 = _vala_array_length (_tmp5_);
		_sp_size_ = sp_length1;
		_tmp7_ = sp;
		_tmp7__length1 = sp_length1;
		g_return_val_if_fail (_tmp7__length1 > 0, "");
		_tmp8_ = sp;
		_tmp8__length1 = sp_length1;
		_tmp9_ = _tmp8_[0];
		_tmp10_ = g_unichar_to_string (bird_font_font_to_unichar (_tmp9_));
		_g_free0 (characters);
		characters = (gchar*) _tmp10_;
	}
	_tmp11_ = characters;
	_tmp13_ = _tmp12_ = g_strsplit (_tmp11_, " ", 0);
	sp = (_vala_array_free (sp, sp_length1, (GDestroyNotify) g_free), NULL);
	sp = _tmp13_;
	sp_length1 = _vala_array_length (_tmp12_);
	_sp_size_ = sp_length1;
	_tmp14_ = sp;
	_tmp14__length1 = sp_length1;
	if (_tmp14__length1 == 0) {
		gchar* _tmp15_;
		_tmp15_ = g_strdup ("");
		result = _tmp15_;
		_g_free0 (characters);
		sp = (_vala_array_free (sp, sp_length1, (GDestroyNotify) g_free), NULL);
		return result;
	}
	_tmp16_ = sp;
	_tmp16__length1 = sp_length1;
	_tmp17_ = _tmp16_[0];
	if (g_strcmp0 (_tmp17_, "space") == 0) {
		gchar** _tmp18_;
		gint _tmp18__length1;
		gchar* _tmp19_;
		_tmp18_ = sp;
		_tmp18__length1 = sp_length1;
		_tmp19_ = g_strdup (" ");
		_g_free0 (_tmp18_[0]);
		_tmp18_[0] = _tmp19_;
	}
	_tmp20_ = sp;
	_tmp20__length1 = sp_length1;
	_tmp21_ = _tmp20_[0];
	string_get_next_char (_tmp21_, &index, &_tmp22_);
	first = _tmp22_;
	_tmp23_ = g_unichar_to_string (first);
	result = (gchar*) _tmp23_;
	_g_free0 (characters);
	sp = (_vala_array_free (sp, sp_length1, (GDestroyNotify) g_free), NULL);
	return result;
}

static void
__lambda5_ (BirdFontLigature* self,
            const gchar* text)
{
	gchar* _tmp0_;
	g_return_if_fail (text != NULL);
	_tmp0_ = g_strdup (text);
	_g_free0 (self->ligature);
	self->ligature = _tmp0_;
}

static void
___lambda5__bird_font_text_listener_signal_text_input (BirdFontTextListener* _sender,
                                                       const gchar* text,
                                                       gpointer self)
{
	__lambda5_ ((BirdFontLigature*) self, text);
}

static void
__lambda6_ (BirdFontLigature* self)
{
	BirdFontLigatureList* _tmp0_;
	BirdFontLigatureList* _tmp1_;
	bird_font_tab_content_hide_text_input ();
	_tmp0_ = bird_font_main_window_get_ligature_display ();
	_tmp1_ = _tmp0_;
	bird_font_table_update_rows ((BirdFontTable*) _tmp1_);
	_g_object_unref0 (_tmp1_);
}

static void
___lambda6__bird_font_text_listener_signal_submit (BirdFontTextListener* _sender,
                                                   const gchar* text,
                                                   gpointer self)
{
	__lambda6_ ((BirdFontLigature*) self);
}

void
bird_font_ligature_set_ligature (BirdFontLigature* self)
{
	BirdFontTextListener* listener = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	BirdFontTextListener* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = bird_font_t_ ("Ligature");
	_tmp1_ = _tmp0_;
	_tmp2_ = self->ligature;
	_tmp3_ = bird_font_t_ ("Set");
	_tmp4_ = _tmp3_;
	_tmp5_ = bird_font_text_listener_new (_tmp1_, _tmp2_, _tmp4_);
	_g_object_unref0 (listener);
	listener = _tmp5_;
	_g_free0 (_tmp4_);
	_g_free0 (_tmp1_);
	g_signal_connect_object (listener, "signal-text-input", (GCallback) ___lambda5__bird_font_text_listener_signal_text_input, self, 0);
	g_signal_connect_object (listener, "signal-submit", (GCallback) ___lambda6__bird_font_text_listener_signal_submit, self, 0);
	bird_font_tab_content_show_text_input (listener);
	_g_object_unref0 (listener);
}

static void
__lambda404_ (BirdFontLigature* self,
              const gchar* text)
{
	BirdFontFont* f = NULL;
	BirdFontFont* _tmp0_;
	BirdFontLigatures* l = NULL;
	BirdFontLigatures* _tmp1_;
	gchar* _tmp2_;
	g_return_if_fail (text != NULL);
	_tmp0_ = bird_font_bird_font_get_current_font ();
	f = _tmp0_;
	_tmp1_ = bird_font_font_get_ligatures (f);
	l = _tmp1_;
	_tmp2_ = g_strdup (text);
	_g_free0 (self->substitution);
	self->substitution = _tmp2_;
	bird_font_ligatures_sort_ligatures (l);
	_g_object_unref0 (l);
	_g_object_unref0 (f);
}

static void
___lambda404__bird_font_text_listener_signal_text_input (BirdFontTextListener* _sender,
                                                         const gchar* text,
                                                         gpointer self)
{
	__lambda404_ ((BirdFontLigature*) self, text);
}

static void
__lambda405_ (BirdFontLigature* self)
{
	BirdFontLigatureList* _tmp0_;
	BirdFontLigatureList* _tmp1_;
	bird_font_tab_content_hide_text_input ();
	_tmp0_ = bird_font_main_window_get_ligature_display ();
	_tmp1_ = _tmp0_;
	bird_font_table_update_rows ((BirdFontTable*) _tmp1_);
	_g_object_unref0 (_tmp1_);
}

static void
___lambda405__bird_font_text_listener_signal_submit (BirdFontTextListener* _sender,
                                                     const gchar* text,
                                                     gpointer self)
{
	__lambda405_ ((BirdFontLigature*) self);
}

void
bird_font_ligature_set_substitution (BirdFontLigature* self)
{
	BirdFontTextListener* listener = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	BirdFontTextListener* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = bird_font_t_ ("Text");
	_tmp1_ = _tmp0_;
	_tmp2_ = self->substitution;
	_tmp3_ = bird_font_t_ ("Set");
	_tmp4_ = _tmp3_;
	_tmp5_ = bird_font_text_listener_new (_tmp1_, _tmp2_, _tmp4_);
	_g_object_unref0 (listener);
	listener = _tmp5_;
	_g_free0 (_tmp4_);
	_g_free0 (_tmp1_);
	g_signal_connect_object (listener, "signal-text-input", (GCallback) ___lambda404__bird_font_text_listener_signal_text_input, self, 0);
	g_signal_connect_object (listener, "signal-submit", (GCallback) ___lambda405__bird_font_text_listener_signal_submit, self, 0);
	bird_font_tab_content_show_text_input (listener);
	_g_object_unref0 (listener);
}

static void
bird_font_ligature_class_init (BirdFontLigatureClass * klass,
                               gpointer klass_data)
{
	bird_font_ligature_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = bird_font_ligature_finalize;
}

static void
bird_font_ligature_instance_init (BirdFontLigature * self,
                                  gpointer klass)
{
	gchar* _tmp0_;
	gchar* _tmp1_;
	_tmp0_ = g_strdup ("");
	self->ligature = _tmp0_;
	_tmp1_ = g_strdup ("");
	self->substitution = _tmp1_;
}

static void
bird_font_ligature_finalize (GObject * obj)
{
	BirdFontLigature * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_LIGATURE, BirdFontLigature);
	_g_free0 (self->ligature);
	_g_free0 (self->substitution);
	G_OBJECT_CLASS (bird_font_ligature_parent_class)->finalize (obj);
}

static GType
bird_font_ligature_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontLigatureClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_ligature_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontLigature), 0, (GInstanceInitFunc) bird_font_ligature_instance_init, NULL };
	GType bird_font_ligature_type_id;
	bird_font_ligature_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontLigature", &g_define_type_info, 0);
	return bird_font_ligature_type_id;
}

GType
bird_font_ligature_get_type (void)
{
	static volatile gsize bird_font_ligature_type_id__once = 0;
	if (g_once_init_enter (&bird_font_ligature_type_id__once)) {
		GType bird_font_ligature_type_id;
		bird_font_ligature_type_id = bird_font_ligature_get_type_once ();
		g_once_init_leave (&bird_font_ligature_type_id__once, bird_font_ligature_type_id);
	}
	return bird_font_ligature_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

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;
}

