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

/*
Copyright (C) 2012 2013 2014 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 <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_CMAP_SUBTABLE_FORMAT12_0_PROPERTY,
	BIRD_FONT_CMAP_SUBTABLE_FORMAT12_NUM_PROPERTIES
};
static GParamSpec* bird_font_cmap_subtable_format12_properties[BIRD_FONT_CMAP_SUBTABLE_FORMAT12_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _bird_font_glyph_range_unref0(var) ((var == NULL) ? NULL : (var = (bird_font_glyph_range_unref (var), NULL)))

struct _BirdFontCmapSubtableFormat12Private {
	BirdFontFontData* cmap_data;
};

static gint BirdFontCmapSubtableFormat12_private_offset;
static gpointer bird_font_cmap_subtable_format12_parent_class = NULL;

static gushort bird_font_cmap_subtable_format12_real_get_platform (BirdFontCmapSubtable* base);
static gushort bird_font_cmap_subtable_format12_real_get_encoding (BirdFontCmapSubtable* base);
static BirdFontFontData* bird_font_cmap_subtable_format12_real_get_cmap_data (BirdFontCmapSubtable* base);
static void bird_font_cmap_subtable_format12_real_generate_cmap_data (BirdFontCmapSubtable* base,
                                                               BirdFontGlyfTable* glyf_table,
                                                               GError** error);
static void bird_font_cmap_subtable_format12_finalize (GObject * obj);
static GType bird_font_cmap_subtable_format12_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_cmap_subtable_format12_get_instance_private (BirdFontCmapSubtableFormat12* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontCmapSubtableFormat12_private_offset);
}

BirdFontCmapSubtableFormat12*
bird_font_cmap_subtable_format12_construct (GType object_type)
{
	BirdFontCmapSubtableFormat12 * self = NULL;
	self = (BirdFontCmapSubtableFormat12*) bird_font_cmap_subtable_construct (object_type);
	return self;
}

BirdFontCmapSubtableFormat12*
bird_font_cmap_subtable_format12_new (void)
{
	return bird_font_cmap_subtable_format12_construct (BIRD_FONT_TYPE_CMAP_SUBTABLE_FORMAT12);
}

static gushort
bird_font_cmap_subtable_format12_real_get_platform (BirdFontCmapSubtable* base)
{
	BirdFontCmapSubtableFormat12 * self;
	gushort result;
	self = (BirdFontCmapSubtableFormat12*) base;
	result = (gushort) 3;
	return result;
}

static gushort
bird_font_cmap_subtable_format12_real_get_encoding (BirdFontCmapSubtable* base)
{
	BirdFontCmapSubtableFormat12 * self;
	gushort result;
	self = (BirdFontCmapSubtableFormat12*) base;
	result = (gushort) 10;
	return result;
}

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

static BirdFontFontData*
bird_font_cmap_subtable_format12_real_get_cmap_data (BirdFontCmapSubtable* base)
{
	BirdFontCmapSubtableFormat12 * self;
	BirdFontFontData* _tmp0_;
	BirdFontFontData* _tmp1_;
	BirdFontFontData* result;
	self = (BirdFontCmapSubtableFormat12*) base;
	_tmp0_ = self->priv->cmap_data;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
bird_font_cmap_subtable_format12_real_generate_cmap_data (BirdFontCmapSubtable* base,
                                                          BirdFontGlyfTable* glyf_table,
                                                          GError** error)
{
	BirdFontCmapSubtableFormat12 * self;
	BirdFontGlyphRange* glyph_range = NULL;
	BirdFontGlyphRange* _tmp0_;
	GeeArrayList* ranges = NULL;
	BirdFontFontData* fd = NULL;
	BirdFontFontData* _tmp1_;
	guint32 first_assigned = 0U;
	guint32 index = 0U;
	BirdFontGlyphRange* _tmp15_;
	GeeArrayList* _tmp16_;
	GeeArrayList* _tmp17_;
	BirdFontFontData* _tmp18_;
	BirdFontFontData* _tmp19_;
	BirdFontFontData* _tmp20_;
	GeeArrayList* _tmp21_;
	gint _tmp22_;
	gint _tmp23_;
	BirdFontFontData* _tmp24_;
	BirdFontFontData* _tmp25_;
	GeeArrayList* _tmp26_;
	gint _tmp27_;
	gint _tmp28_;
	BirdFontFontData* _tmp46_;
	BirdFontFontData* _tmp47_;
	self = (BirdFontCmapSubtableFormat12*) base;
	g_return_if_fail (glyf_table != NULL);
	_tmp0_ = bird_font_glyph_range_new ();
	glyph_range = _tmp0_;
	_tmp1_ = bird_font_font_data_new ((guint32) 1024);
	fd = _tmp1_;
	first_assigned = (guint32) 1;
	{
		GeeArrayList* _g_list = NULL;
		GeeArrayList* _tmp2_;
		gint _g_size = 0;
		GeeArrayList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _g_index = 0;
		_tmp2_ = glyf_table->glyphs;
		_g_list = _tmp2_;
		_tmp3_ = _g_list;
		_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_g_size = _tmp5_;
		_g_index = -1;
		while (TRUE) {
			gint _tmp6_;
			gint _tmp7_;
			BirdFontGlyphCollection* g = NULL;
			GeeArrayList* _tmp8_;
			gpointer _tmp9_;
			gboolean _tmp10_ = FALSE;
			BirdFontGlyphCollection* _tmp11_;
			_g_index = _g_index + 1;
			_tmp6_ = _g_index;
			_tmp7_ = _g_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _g_list;
			_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _g_index);
			g = (BirdFontGlyphCollection*) _tmp9_;
			_tmp11_ = g;
			if (!bird_font_glyph_collection_is_unassigned (_tmp11_)) {
				BirdFontGlyphCollection* _tmp12_;
				_tmp12_ = g;
				_tmp10_ = ((gint64) bird_font_glyph_collection_get_unicode_character (_tmp12_)) < 0xFFFFFFFFLL;
			} else {
				_tmp10_ = FALSE;
			}
			if (_tmp10_) {
				BirdFontGlyphRange* _tmp13_;
				BirdFontGlyphCollection* _tmp14_;
				_tmp13_ = glyph_range;
				_tmp14_ = g;
				bird_font_glyph_range_add_single (_tmp13_, bird_font_glyph_collection_get_unicode_character (_tmp14_));
			}
			_g_object_unref0 (g);
		}
	}
	_tmp15_ = glyph_range;
	_tmp16_ = bird_font_glyph_range_get_ranges (_tmp15_);
	_tmp17_ = _g_object_ref0 (_tmp16_);
	_g_object_unref0 (ranges);
	ranges = _tmp17_;
	_tmp18_ = fd;
	bird_font_font_data_add_u16 (_tmp18_, (guint16) 12);
	_tmp19_ = fd;
	bird_font_font_data_add_u16 (_tmp19_, (guint16) 0);
	_tmp20_ = fd;
	_tmp21_ = ranges;
	_tmp22_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp21_);
	_tmp23_ = _tmp22_;
	bird_font_font_data_add_u32 (_tmp20_, (guint32) (16 + (_tmp23_ * 12)));
	_tmp24_ = fd;
	bird_font_font_data_add_u32 (_tmp24_, (guint32) 0);
	_tmp25_ = fd;
	_tmp26_ = ranges;
	_tmp27_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp26_);
	_tmp28_ = _tmp27_;
	bird_font_font_data_add_u32 (_tmp25_, (guint32) _tmp28_);
	index = first_assigned;
	{
		GeeArrayList* _u_list = NULL;
		GeeArrayList* _tmp29_;
		gint _u_size = 0;
		GeeArrayList* _tmp30_;
		gint _tmp31_;
		gint _tmp32_;
		gint _u_index = 0;
		_tmp29_ = ranges;
		_u_list = _tmp29_;
		_tmp30_ = _u_list;
		_tmp31_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp30_);
		_tmp32_ = _tmp31_;
		_u_size = _tmp32_;
		_u_index = -1;
		while (TRUE) {
			gint _tmp33_;
			gint _tmp34_;
			BirdFontUniRange* u = NULL;
			GeeArrayList* _tmp35_;
			gpointer _tmp36_;
			gboolean _tmp37_ = FALSE;
			BirdFontUniRange* _tmp38_;
			_u_index = _u_index + 1;
			_tmp33_ = _u_index;
			_tmp34_ = _u_size;
			if (!(_tmp33_ < _tmp34_)) {
				break;
			}
			_tmp35_ = _u_list;
			_tmp36_ = gee_abstract_list_get ((GeeAbstractList*) _tmp35_, _u_index);
			u = (BirdFontUniRange*) _tmp36_;
			_tmp38_ = u;
			if (((gint64) _tmp38_->start) >= 0xFFFFFFFFLL) {
				_tmp37_ = TRUE;
			} else {
				BirdFontUniRange* _tmp39_;
				_tmp39_ = u;
				_tmp37_ = ((gint64) _tmp39_->stop) >= 0xFFFFFFFFLL;
			}
			if (_tmp37_) {
				g_warning ("CmapSubtableFormat12.vala:66: Glyph range not supported by CmapSubtabl" \
"eFormat12.");
			} else {
				BirdFontFontData* _tmp40_;
				BirdFontUniRange* _tmp41_;
				BirdFontFontData* _tmp42_;
				BirdFontUniRange* _tmp43_;
				BirdFontFontData* _tmp44_;
				BirdFontUniRange* _tmp45_;
				_tmp40_ = fd;
				_tmp41_ = u;
				bird_font_font_data_add_u32 (_tmp40_, (guint32) _tmp41_->start);
				_tmp42_ = fd;
				_tmp43_ = u;
				bird_font_font_data_add_u32 (_tmp42_, (guint32) _tmp43_->stop);
				_tmp44_ = fd;
				bird_font_font_data_add_u32 (_tmp44_, index);
				_tmp45_ = u;
				index += (guint32) bird_font_uni_range_length (_tmp45_);
			}
			_g_object_unref0 (u);
		}
	}
	_tmp46_ = fd;
	_tmp47_ = _g_object_ref0 (_tmp46_);
	_g_object_unref0 (self->priv->cmap_data);
	self->priv->cmap_data = _tmp47_;
	_g_object_unref0 (fd);
	_g_object_unref0 (ranges);
	_bird_font_glyph_range_unref0 (glyph_range);
}

static void
bird_font_cmap_subtable_format12_class_init (BirdFontCmapSubtableFormat12Class * klass,
                                             gpointer klass_data)
{
	bird_font_cmap_subtable_format12_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontCmapSubtableFormat12_private_offset);
	((BirdFontCmapSubtableClass *) klass)->get_platform = (gushort (*) (BirdFontCmapSubtable*)) bird_font_cmap_subtable_format12_real_get_platform;
	((BirdFontCmapSubtableClass *) klass)->get_encoding = (gushort (*) (BirdFontCmapSubtable*)) bird_font_cmap_subtable_format12_real_get_encoding;
	((BirdFontCmapSubtableClass *) klass)->get_cmap_data = (BirdFontFontData* (*) (BirdFontCmapSubtable*)) bird_font_cmap_subtable_format12_real_get_cmap_data;
	((BirdFontCmapSubtableClass *) klass)->generate_cmap_data = (void (*) (BirdFontCmapSubtable*, BirdFontGlyfTable*, GError**)) bird_font_cmap_subtable_format12_real_generate_cmap_data;
	G_OBJECT_CLASS (klass)->finalize = bird_font_cmap_subtable_format12_finalize;
}

static void
bird_font_cmap_subtable_format12_instance_init (BirdFontCmapSubtableFormat12 * self,
                                                gpointer klass)
{
	self->priv = bird_font_cmap_subtable_format12_get_instance_private (self);
}

static void
bird_font_cmap_subtable_format12_finalize (GObject * obj)
{
	BirdFontCmapSubtableFormat12 * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_CMAP_SUBTABLE_FORMAT12, BirdFontCmapSubtableFormat12);
	_g_object_unref0 (self->priv->cmap_data);
	G_OBJECT_CLASS (bird_font_cmap_subtable_format12_parent_class)->finalize (obj);
}

/** Format 12 cmap subtable */
static GType
bird_font_cmap_subtable_format12_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontCmapSubtableFormat12Class), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_cmap_subtable_format12_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontCmapSubtableFormat12), 0, (GInstanceInitFunc) bird_font_cmap_subtable_format12_instance_init, NULL };
	GType bird_font_cmap_subtable_format12_type_id;
	bird_font_cmap_subtable_format12_type_id = g_type_register_static (BIRD_FONT_TYPE_CMAP_SUBTABLE, "BirdFontCmapSubtableFormat12", &g_define_type_info, 0);
	BirdFontCmapSubtableFormat12_private_offset = g_type_add_instance_private (bird_font_cmap_subtable_format12_type_id, sizeof (BirdFontCmapSubtableFormat12Private));
	return bird_font_cmap_subtable_format12_type_id;
}

GType
bird_font_cmap_subtable_format12_get_type (void)
{
	static volatile gsize bird_font_cmap_subtable_format12_type_id__once = 0;
	if (g_once_init_enter (&bird_font_cmap_subtable_format12_type_id__once)) {
		GType bird_font_cmap_subtable_format12_type_id;
		bird_font_cmap_subtable_format12_type_id = bird_font_cmap_subtable_format12_get_type_once ();
		g_once_init_leave (&bird_font_cmap_subtable_format12_type_id__once, bird_font_cmap_subtable_format12_type_id);
	}
	return bird_font_cmap_subtable_format12_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;
}

