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

/* vim:set et sts=4 sw=4:
 *
 * ibus - The Input Bus
 *
 * Copyright(c) 2011 Peng Huang <shawn.p.huang@gmail.com>
 *
 * 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 2.1 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <glib.h>
#include <glib-object.h>
#include <ibus.h>
#include <pango/pango.h>
#include <stdlib.h>
#include <string.h>

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _pango_attribute_destroy0(var) ((var == NULL) ? NULL : (var = (pango_attribute_destroy (var), NULL)))
#define __vala_PangoAttrList_free0(var) ((var == NULL) ? NULL : (var = (_vala_PangoAttrList_free (var), NULL)))



PangoAttrList* get_pango_attr_list_from_ibus_text (IBusText* text);
static void _vala_PangoAttrList_free (PangoAttrList* self);


static gint string_index_of_nth_char (const gchar* self, glong c) {
	gint result = 0;
	glong _tmp0_;
	gchar* _tmp1_ = NULL;
	g_return_val_if_fail (self != NULL, 0);
	_tmp0_ = c;
	_tmp1_ = g_utf8_offset_to_pointer (self, _tmp0_);
	result = (gint) (_tmp1_ - ((gchar*) self));
	return result;
}


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


static void _vala_PangoAttrList_free (PangoAttrList* self) {
	g_boxed_free (pango_attr_list_get_type (), self);
}


PangoAttrList* get_pango_attr_list_from_ibus_text (IBusText* text) {
	PangoAttrList* result = NULL;
	PangoAttrList* _tmp0_;
	PangoAttrList* pango_attrs;
	IBusText* _tmp1_;
	IBusAttrList* _tmp2_ = NULL;
	IBusAttrList* attrs;
	IBusAttrList* _tmp3_;
	IBusText* _tmp4_;
	const gchar* _tmp5_ = NULL;
	const gchar* str;
	const gchar* _tmp6_;
	gint _tmp7_ = 0;
	glong nchars;
	glong _tmp8_;
	glong* _tmp9_ = NULL;
	glong* offsets;
	gint offsets_length1;
	gint _offsets_size_;
	IBusAttribute* attr = NULL;
	gint i;
	g_return_val_if_fail (text != NULL, NULL);
	_tmp0_ = pango_attr_list_new ();
	pango_attrs = _tmp0_;
	_tmp1_ = text;
	_tmp2_ = ibus_text_get_attributes (_tmp1_);
	attrs = _tmp2_;
	_tmp3_ = attrs;
	if (_tmp3_ == NULL) {
		result = pango_attrs;
		return result;
	}
	_tmp4_ = text;
	_tmp5_ = ibus_text_get_text (_tmp4_);
	str = _tmp5_;
	_tmp6_ = str;
	_tmp7_ = g_utf8_strlen (_tmp6_, (gssize) (-1));
	nchars = (glong) _tmp7_;
	_tmp8_ = nchars;
	_tmp9_ = g_new0 (glong, _tmp8_ + 1);
	offsets = _tmp9_;
	offsets_length1 = _tmp8_ + 1;
	_offsets_size_ = offsets_length1;
	{
		gint i;
		i = 0;
		{
			gboolean _tmp10_;
			_tmp10_ = TRUE;
			while (TRUE) {
				gboolean _tmp11_;
				gint _tmp13_;
				glong _tmp14_;
				glong* _tmp15_;
				gint _tmp15__length1;
				gint _tmp16_;
				const gchar* _tmp17_;
				gint _tmp18_;
				gint _tmp19_ = 0;
				glong _tmp20_;
				_tmp11_ = _tmp10_;
				if (!_tmp11_) {
					gint _tmp12_;
					_tmp12_ = i;
					i = _tmp12_ + 1;
				}
				_tmp10_ = FALSE;
				_tmp13_ = i;
				_tmp14_ = nchars;
				if (!(((glong) _tmp13_) <= _tmp14_)) {
					break;
				}
				_tmp15_ = offsets;
				_tmp15__length1 = offsets_length1;
				_tmp16_ = i;
				_tmp17_ = str;
				_tmp18_ = i;
				_tmp19_ = string_index_of_nth_char (_tmp17_, (glong) _tmp18_);
				_tmp15_[_tmp16_] = (glong) _tmp19_;
				_tmp20_ = _tmp15_[_tmp16_];
			}
		}
	}
	i = 0;
	while (TRUE) {
		IBusAttrList* _tmp21_;
		gint _tmp22_;
		IBusAttribute* _tmp23_ = NULL;
		IBusAttribute* _tmp24_;
		IBusAttribute* _tmp25_;
		IBusAttribute* _tmp26_;
		guint _tmp27_;
		glong start_index;
		glong _tmp28_;
		glong _tmp29_ = 0L;
		glong _tmp30_;
		glong _tmp31_;
		glong _tmp37_;
		IBusAttribute* _tmp38_;
		guint _tmp39_;
		glong end_index;
		glong _tmp40_;
		glong _tmp41_ = 0L;
		glong _tmp42_;
		glong _tmp43_;
		glong _tmp49_;
		PangoAttribute* pango_attr;
		IBusAttribute* _tmp50_;
		guint _tmp51_;
		PangoAttribute* _tmp75_;
		glong _tmp76_;
		PangoAttribute* _tmp77_;
		glong _tmp78_;
		PangoAttrList* _tmp79_;
		PangoAttribute* _tmp80_;
		_tmp21_ = attrs;
		_tmp22_ = i;
		i = _tmp22_ + 1;
		_tmp23_ = ibus_attr_list_get (_tmp21_, (guint) _tmp22_);
		_tmp24_ = _g_object_ref0 (_tmp23_);
		_g_object_unref0 (attr);
		attr = _tmp24_;
		_tmp25_ = attr;
		if (_tmp25_ == NULL) {
			break;
		}
		_tmp26_ = attr;
		_tmp27_ = _tmp26_->start_index;
		start_index = (glong) _tmp27_;
		_tmp28_ = start_index;
		if (_tmp28_ <= ((glong) 0)) {
			start_index = (glong) 0;
		}
		_tmp30_ = start_index;
		_tmp31_ = nchars;
		if (_tmp30_ <= _tmp31_) {
			glong* _tmp32_;
			gint _tmp32__length1;
			glong _tmp33_;
			glong _tmp34_;
			_tmp32_ = offsets;
			_tmp32__length1 = offsets_length1;
			_tmp33_ = start_index;
			_tmp34_ = _tmp32_[_tmp33_];
			_tmp29_ = _tmp34_;
		} else {
			glong* _tmp35_;
			gint _tmp35__length1;
			glong _tmp36_;
			_tmp35_ = offsets;
			_tmp35__length1 = offsets_length1;
			_tmp36_ = _tmp35_[-1];
			_tmp29_ = _tmp36_;
		}
		_tmp37_ = _tmp29_;
		start_index = _tmp37_;
		_tmp38_ = attr;
		_tmp39_ = _tmp38_->end_index;
		end_index = (glong) _tmp39_;
		_tmp40_ = end_index;
		if (_tmp40_ <= ((glong) 0)) {
			end_index = (glong) 0;
		}
		_tmp42_ = end_index;
		_tmp43_ = nchars;
		if (_tmp42_ <= _tmp43_) {
			glong* _tmp44_;
			gint _tmp44__length1;
			glong _tmp45_;
			glong _tmp46_;
			_tmp44_ = offsets;
			_tmp44__length1 = offsets_length1;
			_tmp45_ = end_index;
			_tmp46_ = _tmp44_[_tmp45_];
			_tmp41_ = _tmp46_;
		} else {
			glong* _tmp47_;
			gint _tmp47__length1;
			glong _tmp48_;
			_tmp47_ = offsets;
			_tmp47__length1 = offsets_length1;
			_tmp48_ = _tmp47_[-1];
			_tmp41_ = _tmp48_;
		}
		_tmp49_ = _tmp41_;
		end_index = _tmp49_;
		pango_attr = NULL;
		_tmp50_ = attr;
		_tmp51_ = _tmp50_->type;
		switch (_tmp51_) {
			case IBUS_ATTR_TYPE_FOREGROUND:
			{
				{
					IBusAttribute* _tmp52_;
					guint _tmp53_;
					guint16 r;
					IBusAttribute* _tmp54_;
					guint _tmp55_;
					guint16 g;
					IBusAttribute* _tmp56_;
					guint _tmp57_;
					guint16 b;
					guint16 _tmp58_;
					guint16 _tmp59_;
					guint16 _tmp60_;
					PangoAttribute* _tmp61_ = NULL;
					_tmp52_ = attr;
					_tmp53_ = _tmp52_->value;
					r = (guint16) ((_tmp53_ & 0x00ff0000) >> 8);
					_tmp54_ = attr;
					_tmp55_ = _tmp54_->value;
					g = (guint16) (_tmp55_ & 0x0000ff00);
					_tmp56_ = attr;
					_tmp57_ = _tmp56_->value;
					b = (guint16) ((_tmp57_ & 0x000000ff) << 8);
					_tmp58_ = r;
					_tmp59_ = g;
					_tmp60_ = b;
					_tmp61_ = pango_attr_foreground_new (_tmp58_, _tmp59_, _tmp60_);
					_pango_attribute_destroy0 (pango_attr);
					pango_attr = _tmp61_;
					break;
				}
			}
			case IBUS_ATTR_TYPE_BACKGROUND:
			{
				{
					IBusAttribute* _tmp62_;
					guint _tmp63_;
					guint16 r;
					IBusAttribute* _tmp64_;
					guint _tmp65_;
					guint16 g;
					IBusAttribute* _tmp66_;
					guint _tmp67_;
					guint16 b;
					guint16 _tmp68_;
					guint16 _tmp69_;
					guint16 _tmp70_;
					PangoAttribute* _tmp71_ = NULL;
					_tmp62_ = attr;
					_tmp63_ = _tmp62_->value;
					r = (guint16) ((_tmp63_ & 0x00ff0000) >> 8);
					_tmp64_ = attr;
					_tmp65_ = _tmp64_->value;
					g = (guint16) (_tmp65_ & 0x0000ff00);
					_tmp66_ = attr;
					_tmp67_ = _tmp66_->value;
					b = (guint16) ((_tmp67_ & 0x000000ff) << 8);
					_tmp68_ = r;
					_tmp69_ = g;
					_tmp70_ = b;
					_tmp71_ = pango_attr_background_new (_tmp68_, _tmp69_, _tmp70_);
					_pango_attribute_destroy0 (pango_attr);
					pango_attr = _tmp71_;
					break;
				}
			}
			case IBUS_ATTR_TYPE_UNDERLINE:
			{
				{
					IBusAttribute* _tmp72_;
					guint _tmp73_;
					PangoAttribute* _tmp74_ = NULL;
					_tmp72_ = attr;
					_tmp73_ = _tmp72_->value;
					_tmp74_ = pango_attr_underline_new ((PangoUnderline) _tmp73_);
					_pango_attribute_destroy0 (pango_attr);
					pango_attr = _tmp74_;
					break;
				}
			}
			default:
			{
				continue;
			}
		}
		_tmp75_ = pango_attr;
		_tmp76_ = start_index;
		_tmp75_->start_index = (guint) _tmp76_;
		_tmp77_ = pango_attr;
		_tmp78_ = end_index;
		_tmp77_->end_index = (guint) _tmp78_;
		_tmp79_ = pango_attrs;
		_tmp80_ = pango_attr;
		pango_attr = NULL;
		pango_attr_list_insert (_tmp79_, _tmp80_);
		_pango_attribute_destroy0 (pango_attr);
	}
	result = pango_attrs;
	_g_object_unref0 (attr);
	offsets = (g_free (offsets), NULL);
	return result;
}



