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

/*
Copyright (C) 2012, 2013 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 <gio/gio.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_OPEN_FONT_FORMAT_WRITER_0_PROPERTY,
	BIRD_FONT_OPEN_FONT_FORMAT_WRITER_NUM_PROPERTIES
};
static GParamSpec* bird_font_open_font_format_writer_properties[BIRD_FONT_OPEN_FONT_FORMAT_WRITER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _BirdFontOpenFontFormatWriterPrivate {
	GDataOutputStream* os;
	GDataOutputStream* os_mac;
	BirdFontDirectoryTable* directory_table;
};

static gint BirdFontOpenFontFormatWriter_private_offset;
static gpointer bird_font_open_font_format_writer_parent_class = NULL;
BirdFontFont* bird_font_open_font_format_writer_font = NULL;

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

static inline gpointer
bird_font_open_font_format_writer_get_instance_private (BirdFontOpenFontFormatWriter* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontOpenFontFormatWriter_private_offset);
}

BirdFontOpenFontFormatWriter*
bird_font_open_font_format_writer_construct (GType object_type,
                                             gint units_per_em)
{
	BirdFontOpenFontFormatWriter * self = NULL;
	BirdFontDirectoryTable* _tmp0_;
	self = (BirdFontOpenFontFormatWriter*) g_object_new (object_type, NULL);
	_tmp0_ = bird_font_directory_table_new ();
	_g_object_unref0 (self->priv->directory_table);
	self->priv->directory_table = _tmp0_;
	bird_font_head_table_init (units_per_em);
	return self;
}

BirdFontOpenFontFormatWriter*
bird_font_open_font_format_writer_new (gint units_per_em)
{
	return bird_font_open_font_format_writer_construct (BIRD_FONT_TYPE_OPEN_FONT_FORMAT_WRITER, units_per_em);
}

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

BirdFontFont*
bird_font_open_font_format_writer_get_current_font (void)
{
	BirdFontFont* _tmp0_;
	BirdFontFont* _tmp1_;
	BirdFontFont* result;
	_tmp0_ = bird_font_open_font_format_writer_font;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}

void
bird_font_open_font_format_writer_open (BirdFontOpenFontFormatWriter* self,
                                        GFile* ttf,
                                        GFile* ttf_mac,
                                        GError** error)
{
	gboolean _tmp0_ = FALSE;
	GFileOutputStream* _tmp2_ = NULL;
	GFileOutputStream* _tmp3_;
	GDataOutputStream* _tmp4_;
	GFileOutputStream* _tmp5_ = NULL;
	GFileOutputStream* _tmp6_;
	GDataOutputStream* _tmp7_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (ttf != NULL);
	g_return_if_fail (ttf_mac != NULL);
	if (g_file_query_exists (ttf, NULL)) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_file_query_exists (ttf_mac, NULL);
	}
	if (_tmp0_) {
		GError* _tmp1_;
		g_warning ("OpenFontFormatWriter.vala:36: File exists in export.");
		_tmp1_ = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_EXIST, "OpenFontFormatWriter: file exists.");
		_inner_error0_ = _tmp1_;
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp3_ = g_file_create (ttf, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &_inner_error0_);
	_tmp2_ = _tmp3_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp4_ = g_data_output_stream_new ((GOutputStream*) _tmp2_);
	_g_object_unref0 (self->priv->os);
	self->priv->os = _tmp4_;
	_tmp6_ = g_file_create (ttf_mac, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &_inner_error0_);
	_tmp5_ = _tmp6_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (_tmp2_);
		return;
	}
	_tmp7_ = g_data_output_stream_new ((GOutputStream*) _tmp5_);
	_g_object_unref0 (self->priv->os_mac);
	self->priv->os_mac = _tmp7_;
	_g_object_unref0 (_tmp5_);
	_g_object_unref0 (_tmp2_);
}

void
bird_font_open_font_format_writer_write_ttf_font (BirdFontOpenFontFormatWriter* self,
                                                  BirdFontFont* nfont,
                                                  GError** error)
{
	glong dl = 0L;
	guint8* data = NULL;
	GeeArrayList* tables = NULL;
	BirdFontFontData* fd = NULL;
	guint l = 0U;
	BirdFontFont* _tmp0_;
	BirdFontDirectoryTable* _tmp1_;
	BirdFontDirectoryTable* _tmp2_;
	GeeArrayList* _tmp3_;
	BirdFontDirectoryTable* _tmp4_;
	BirdFontDirectoryTable* _tmp23_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (nfont != NULL);
	_tmp0_ = _g_object_ref0 (nfont);
	_g_object_unref0 (bird_font_open_font_format_writer_font);
	bird_font_open_font_format_writer_font = _tmp0_;
	_tmp1_ = self->priv->directory_table;
	bird_font_directory_table_process (_tmp1_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (fd);
		_g_object_unref0 (tables);
		return;
	}
	_tmp2_ = self->priv->directory_table;
	_tmp3_ = bird_font_directory_table_get_tables (_tmp2_);
	_g_object_unref0 (tables);
	tables = _tmp3_;
	_tmp4_ = self->priv->directory_table;
	dl = bird_font_directory_table_get_font_file_size (_tmp4_);
	if (dl == ((glong) 0)) {
		g_warning ("OpenFontFormatWriter.vala:59: font size is zero");
		_g_object_unref0 (fd);
		_g_object_unref0 (tables);
		return;
	}
	{
		GeeArrayList* _t_list = NULL;
		GeeArrayList* _tmp5_;
		gint _t_size = 0;
		GeeArrayList* _tmp6_;
		gint _tmp7_;
		gint _tmp8_;
		gint _t_index = 0;
		_tmp5_ = tables;
		_t_list = _tmp5_;
		_tmp6_ = _t_list;
		_tmp7_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp6_);
		_tmp8_ = _tmp7_;
		_t_size = _tmp8_;
		_t_index = -1;
		while (TRUE) {
			gint _tmp9_;
			gint _tmp10_;
			BirdFontOtfTable* t = NULL;
			GeeArrayList* _tmp11_;
			gpointer _tmp12_;
			BirdFontOtfTable* _tmp13_;
			BirdFontFontData* _tmp14_;
			BirdFontFontData* _tmp15_;
			guint8* _tmp16_;
			BirdFontFontData* _tmp17_;
			_t_index = _t_index + 1;
			_tmp9_ = _t_index;
			_tmp10_ = _t_size;
			if (!(_tmp9_ < _tmp10_)) {
				break;
			}
			_tmp11_ = _t_list;
			_tmp12_ = gee_abstract_list_get ((GeeAbstractList*) _tmp11_, _t_index);
			t = (BirdFontOtfTable*) _tmp12_;
			_tmp13_ = t;
			_tmp14_ = bird_font_otf_table_get_font_data (_tmp13_);
			_g_object_unref0 (fd);
			fd = _tmp14_;
			_tmp15_ = fd;
			_tmp16_ = _tmp15_->table_data;
			data = _tmp16_;
			_tmp17_ = fd;
			l = bird_font_font_data_length_with_padding (_tmp17_);
			{
				gint j = 0;
				j = 0;
				{
					gboolean _tmp18_ = FALSE;
					_tmp18_ = TRUE;
					while (TRUE) {
						GDataOutputStream* _tmp20_;
						guint8* _tmp21_;
						guint8 _tmp22_;
						if (!_tmp18_) {
							gint _tmp19_;
							_tmp19_ = j;
							j = _tmp19_ + 1;
						}
						_tmp18_ = FALSE;
						if (!(((guint) j) < l)) {
							break;
						}
						_tmp20_ = self->priv->os;
						_tmp21_ = data;
						_tmp22_ = _tmp21_[j];
						g_data_output_stream_put_byte (_tmp20_, _tmp22_, NULL, &_inner_error0_);
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							g_propagate_error (error, _inner_error0_);
							_g_object_unref0 (t);
							_g_object_unref0 (fd);
							_g_object_unref0 (tables);
							return;
						}
					}
				}
			}
			_g_object_unref0 (t);
		}
	}
	_tmp23_ = self->priv->directory_table;
	bird_font_directory_table_process_mac (_tmp23_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (fd);
		_g_object_unref0 (tables);
		return;
	}
	{
		GeeArrayList* _t_list = NULL;
		GeeArrayList* _tmp24_;
		gint _t_size = 0;
		GeeArrayList* _tmp25_;
		gint _tmp26_;
		gint _tmp27_;
		gint _t_index = 0;
		_tmp24_ = tables;
		_t_list = _tmp24_;
		_tmp25_ = _t_list;
		_tmp26_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp25_);
		_tmp27_ = _tmp26_;
		_t_size = _tmp27_;
		_t_index = -1;
		while (TRUE) {
			gint _tmp28_;
			gint _tmp29_;
			BirdFontOtfTable* t = NULL;
			GeeArrayList* _tmp30_;
			gpointer _tmp31_;
			BirdFontOtfTable* _tmp32_;
			BirdFontFontData* _tmp33_;
			BirdFontFontData* _tmp34_;
			guint8* _tmp35_;
			BirdFontFontData* _tmp36_;
			_t_index = _t_index + 1;
			_tmp28_ = _t_index;
			_tmp29_ = _t_size;
			if (!(_tmp28_ < _tmp29_)) {
				break;
			}
			_tmp30_ = _t_list;
			_tmp31_ = gee_abstract_list_get ((GeeAbstractList*) _tmp30_, _t_index);
			t = (BirdFontOtfTable*) _tmp31_;
			_tmp32_ = t;
			_tmp33_ = bird_font_otf_table_get_font_data (_tmp32_);
			_g_object_unref0 (fd);
			fd = _tmp33_;
			_tmp34_ = fd;
			_tmp35_ = _tmp34_->table_data;
			data = _tmp35_;
			_tmp36_ = fd;
			l = bird_font_font_data_length_with_padding (_tmp36_);
			{
				gint j = 0;
				j = 0;
				{
					gboolean _tmp37_ = FALSE;
					_tmp37_ = TRUE;
					while (TRUE) {
						GDataOutputStream* _tmp39_;
						guint8* _tmp40_;
						guint8 _tmp41_;
						if (!_tmp37_) {
							gint _tmp38_;
							_tmp38_ = j;
							j = _tmp38_ + 1;
						}
						_tmp37_ = FALSE;
						if (!(((guint) j) < l)) {
							break;
						}
						_tmp39_ = self->priv->os_mac;
						_tmp40_ = data;
						_tmp41_ = _tmp40_[j];
						g_data_output_stream_put_byte (_tmp39_, _tmp41_, NULL, &_inner_error0_);
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							g_propagate_error (error, _inner_error0_);
							_g_object_unref0 (t);
							_g_object_unref0 (fd);
							_g_object_unref0 (tables);
							return;
						}
					}
				}
			}
			_g_object_unref0 (t);
		}
	}
	_g_object_unref0 (fd);
	_g_object_unref0 (tables);
}

void
bird_font_open_font_format_writer_close (BirdFontOpenFontFormatWriter* self,
                                         GError** error)
{
	GDataOutputStream* _tmp0_;
	GDataOutputStream* _tmp1_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->os;
	g_output_stream_close ((GOutputStream*) _tmp0_, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp1_ = self->priv->os_mac;
	g_output_stream_close ((GOutputStream*) _tmp1_, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
}

static void
bird_font_open_font_format_writer_class_init (BirdFontOpenFontFormatWriterClass * klass,
                                              gpointer klass_data)
{
	bird_font_open_font_format_writer_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontOpenFontFormatWriter_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_open_font_format_writer_finalize;
}

static void
bird_font_open_font_format_writer_instance_init (BirdFontOpenFontFormatWriter * self,
                                                 gpointer klass)
{
	self->priv = bird_font_open_font_format_writer_get_instance_private (self);
}

static void
bird_font_open_font_format_writer_finalize (GObject * obj)
{
	BirdFontOpenFontFormatWriter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_OPEN_FONT_FORMAT_WRITER, BirdFontOpenFontFormatWriter);
	_g_object_unref0 (self->priv->os);
	_g_object_unref0 (self->priv->os_mac);
	_g_object_unref0 (self->priv->directory_table);
	G_OBJECT_CLASS (bird_font_open_font_format_writer_parent_class)->finalize (obj);
}

static GType
bird_font_open_font_format_writer_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontOpenFontFormatWriterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_open_font_format_writer_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontOpenFontFormatWriter), 0, (GInstanceInitFunc) bird_font_open_font_format_writer_instance_init, NULL };
	GType bird_font_open_font_format_writer_type_id;
	bird_font_open_font_format_writer_type_id = g_type_register_static (G_TYPE_OBJECT, "BirdFontOpenFontFormatWriter", &g_define_type_info, 0);
	BirdFontOpenFontFormatWriter_private_offset = g_type_add_instance_private (bird_font_open_font_format_writer_type_id, sizeof (BirdFontOpenFontFormatWriterPrivate));
	return bird_font_open_font_format_writer_type_id;
}

GType
bird_font_open_font_format_writer_get_type (void)
{
	static volatile gsize bird_font_open_font_format_writer_type_id__once = 0;
	if (g_once_init_enter (&bird_font_open_font_format_writer_type_id__once)) {
		GType bird_font_open_font_format_writer_type_id;
		bird_font_open_font_format_writer_type_id = bird_font_open_font_format_writer_get_type_once ();
		g_once_init_leave (&bird_font_open_font_format_writer_type_id__once, bird_font_open_font_format_writer_type_id);
	}
	return bird_font_open_font_format_writer_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;
}

