diff options
Diffstat (limited to 'pango/pango-attr.c')
-rw-r--r-- | pango/pango-attr.c | 540 |
1 files changed, 540 insertions, 0 deletions
diff --git a/pango/pango-attr.c b/pango/pango-attr.c new file mode 100644 index 00000000..0314bdcf --- /dev/null +++ b/pango/pango-attr.c @@ -0,0 +1,540 @@ +/* Pango + * pango-attr.c: Attributed text + * + * Copyright (C) 2000-2002 Red Hat Software + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" +#include <string.h> + +#include "pango-attr-private.h" +#include "pango-impl-utils.h" + + +/* {{{ Generic attribute code */ + +G_LOCK_DEFINE_STATIC (attr_type); +static GArray *attr_type; + +#define MIN_CUSTOM 1000 + +typedef struct _PangoAttrClass PangoAttrClass; + +struct _PangoAttrClass { + guint type; + const char *name; + PangoAttrDataCopyFunc copy; + GDestroyNotify destroy; + GEqualFunc equal; + PangoAttrDataSerializeFunc serialize; +}; + +static const char * +get_attr_type_nick (PangoAttrType type) +{ + GEnumClass *enum_class; + GEnumValue *enum_value; + + enum_class = g_type_class_ref (pango_attr_type_get_type ()); + enum_value = g_enum_get_value (enum_class, type); + g_type_class_unref (enum_class); + + return enum_value->value_nick; +} + +/** + * pango_attr_type_register: + * @copy: function to copy the data of an attribute + * of this type + * @destroy: function to free the data of an attribute + * of this type + * @equal: function to compare the data of two attributes + * of this type + * @name: (nullable): an identifier for the type + * @serialize: (nullable): function to serialize the data + * of an attribute of this type + * + * Register a new attribute type. + * + * The attribute type name can be accessed later + * by using [func@Pango.AttrType.get_name]. + * + * If @name and to @serialize are provided, they will be used + * to serialize attributes of this type. + * + * To create attributes with the new type, use [func@Pango.attr_custom_new]. + * + * Return value: the attribute type ID + */ +guint +pango_attr_type_register (PangoAttrDataCopyFunc copy, + GDestroyNotify destroy, + GEqualFunc equal, + const char *name, + PangoAttrDataSerializeFunc serialize) +{ + static guint current_id = MIN_CUSTOM; /* MT-safe */ + PangoAttrClass class; + + G_LOCK (attr_type); + + class.type = PANGO_ATTR_VALUE_POINTER | (current_id << 8); + current_id++; + + class.copy = copy; + class.destroy = destroy; + class.equal = equal; + class.serialize = serialize; + + if (name) + class.name = g_intern_string (name); + + if (attr_type == NULL) + attr_type = g_array_new (FALSE, FALSE, sizeof (PangoAttrClass)); + + g_array_append_val (attr_type, class); + + G_UNLOCK (attr_type); + + return class.type; +} + +/** + * pango_attr_type_get_name: + * @type: an attribute type ID to fetch the name for + * + * Fetches the attribute type name. + * + * The attribute type name is the string passed in + * when registering the type using + * [func@Pango.AttrType.register]. + * + * The returned value is an interned string (see + * g_intern_string() for what that means) that should + * not be modified or freed. + * + * Return value: (nullable): the type ID name (which + * may be %NULL), or %NULL if @type is a built-in Pango + * attribute type or invalid. + * + * Since: 1.22 + */ +const char * +pango_attr_type_get_name (guint type) +{ + const char *result = NULL; + + if ((type >> 8) < MIN_CUSTOM) + return get_attr_type_nick (type); + + G_LOCK (attr_type); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == type) + { + result = class->name; + break; + } + } + + G_UNLOCK (attr_type); + + return result; +} + +/** + * pango_attribute_copy: + * @attr: a `PangoAttribute` + * + * Make a copy of an attribute. + * + * Return value: (transfer full): the newly allocated + * `PangoAttribute`, which should be freed with + * [method@Pango.Attribute.destroy]. + */ +PangoAttribute * +pango_attribute_copy (const PangoAttribute *attr) +{ + PangoAttribute *result; + + g_return_val_if_fail (attr != NULL, NULL); + + result = g_slice_dup (PangoAttribute, attr); + + switch (PANGO_ATTR_VALUE_TYPE (attr)) + { + case PANGO_ATTR_VALUE_STRING: + result->str_value = g_strdup (attr->str_value); + break; + + case PANGO_ATTR_VALUE_FONT_DESC: + result->font_value = pango_font_description_copy (attr->font_value); + break; + + case PANGO_ATTR_VALUE_POINTER: + { + PangoAttrDataCopyFunc copy = NULL; + + G_LOCK (attr_type); + + g_assert (attr_type != NULL); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == attr->type) + { + copy = class->copy; + break; + } + } + + G_UNLOCK (attr_type); + + g_assert (copy != NULL); + + result->pointer_value = copy (attr->pointer_value); + } + break; + + case PANGO_ATTR_VALUE_INT: + case PANGO_ATTR_VALUE_BOOLEAN: + case PANGO_ATTR_VALUE_FLOAT: + case PANGO_ATTR_VALUE_COLOR: + case PANGO_ATTR_VALUE_LANGUAGE: + default: ; + } + + return result; +} + +/** + * pango_attribute_destroy: + * @attr: a `PangoAttribute`. + * + * Destroy a `PangoAttribute` and free all associated memory. + */ +void +pango_attribute_destroy (PangoAttribute *attr) +{ + g_return_if_fail (attr != NULL); + + switch (PANGO_ATTR_VALUE_TYPE (attr)) + { + case PANGO_ATTR_VALUE_STRING: + g_free (attr->str_value); + break; + + case PANGO_ATTR_VALUE_FONT_DESC: + pango_font_description_free (attr->font_value); + break; + + case PANGO_ATTR_VALUE_POINTER: + { + GDestroyNotify destroy = NULL; + + G_LOCK (attr_type); + + g_assert (attr_type != NULL); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == attr->type) + { + destroy = class->destroy; + break; + } + } + + G_UNLOCK (attr_type); + + g_assert (destroy != NULL); + + destroy (attr->pointer_value); + } + break; + + case PANGO_ATTR_VALUE_INT: + case PANGO_ATTR_VALUE_BOOLEAN: + case PANGO_ATTR_VALUE_FLOAT: + case PANGO_ATTR_VALUE_COLOR: + case PANGO_ATTR_VALUE_LANGUAGE: + default: ; + } + + g_slice_free (PangoAttribute, attr); +} + +G_DEFINE_BOXED_TYPE (PangoAttribute, pango_attribute, + pango_attribute_copy, + pango_attribute_destroy); + +/** + * pango_attribute_equal: + * @attr1: a `PangoAttribute` + * @attr2: another `PangoAttribute` + * + * Compare two attributes for equality. + * + * This compares only the actual value of the two + * attributes and not the ranges that the attributes + * apply to. + * + * Return value: %TRUE if the two attributes have the same value + */ +gboolean +pango_attribute_equal (const PangoAttribute *attr1, + const PangoAttribute *attr2) +{ + g_return_val_if_fail (attr1 != NULL, FALSE); + g_return_val_if_fail (attr2 != NULL, FALSE); + + if (attr1->type != attr2->type) + return FALSE; + + switch (PANGO_ATTR_VALUE_TYPE (attr1)) + { + case PANGO_ATTR_VALUE_STRING: + return strcmp (attr1->str_value, attr2->str_value) == 0; + + case PANGO_ATTR_VALUE_INT: + return attr1->int_value == attr2->int_value; + + case PANGO_ATTR_VALUE_BOOLEAN: + return attr1->boolean_value == attr2->boolean_value; + + case PANGO_ATTR_VALUE_FLOAT: + return attr1->double_value == attr2->double_value; + + case PANGO_ATTR_VALUE_COLOR: + return memcmp (&attr1->color_value, &attr2->color_value, sizeof (PangoColor)) == 0; + + case PANGO_ATTR_VALUE_LANGUAGE: + return attr1->lang_value == attr2->lang_value; + + case PANGO_ATTR_VALUE_FONT_DESC: + return pango_font_description_equal (attr1->font_value, attr2->font_value); + + case PANGO_ATTR_VALUE_POINTER: + { + GEqualFunc equal = NULL; + + G_LOCK (attr_type); + + g_assert (attr_type != NULL); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == attr1->type) + { + equal = class->equal; + break; + } + } + + G_UNLOCK (attr_type); + + g_assert (equal != NULL); + + return equal (attr1->pointer_value, attr2->pointer_value); + } + + default: + g_assert_not_reached (); + } +} + +/** + * pango_attr_custom_new: + * @type: the attribute type + * @user_data: data for the attribute + * + * Creates a new attribute for the given type. + * + * The type must have been registered with [func@Pango.register_attr_type] + * before. @user_data will be copied with the copy function that + * was given when the type was registered. + * + * Return value: (transfer full): the newly allocated + * `PangoAttribute`, which should be freed with + * [method@Pango.Attribute.destroy] + */ +PangoAttribute * +pango_attr_custom_new (guint type, + gpointer user_data) +{ + PangoAttribute *attr; + PangoAttrDataCopyFunc copy = NULL; + + g_return_val_if_fail (PANGO_ATTR_TYPE_VALUE_TYPE (type) == PANGO_ATTR_VALUE_POINTER, NULL); + + G_LOCK (attr_type); + + g_assert (attr_type != NULL); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == type) + { + copy = class->copy; + break; + } + } + + g_assert (copy != NULL); + + G_UNLOCK (attr_type); + + attr = g_slice_new (PangoAttribute); + attr->type = type; + attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING; + attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END; + attr->pointer_value = copy (user_data); + + return attr; +} + +/* }}} */ +/* {{{ Private API */ + +char * +pango_attr_value_serialize (PangoAttribute *attr) +{ + PangoAttrDataSerializeFunc serialize = NULL; + + G_LOCK (attr_type); + + g_assert (attr_type != NULL); + + for (int i = 0; i < attr_type->len; i++) + { + PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i); + if (class->type == attr->type) + { + serialize = class->serialize; + break; + } + } + + G_UNLOCK (attr_type); + + if (serialize) + return serialize (attr->pointer_value); + + return NULL; +} + +/* }}} */ +/* {{{ Binding Helpers */ + +gboolean +pango_attribute_get_string (PangoAttribute *attribute, + const char **value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_STRING) + return FALSE; + + *value = attribute->str_value; + return TRUE; +} + +gboolean +pango_attribute_get_language (PangoAttribute *attribute, + PangoLanguage **value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_LANGUAGE) + return FALSE; + + *value = attribute->lang_value; + return TRUE; +} + +gboolean +pango_attribute_get_int (PangoAttribute *attribute, + int *value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_INT) + return FALSE; + + *value = attribute->int_value; + return TRUE; +} + +gboolean +pango_attribute_get_boolean (PangoAttribute *attribute, + int *value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_BOOLEAN) + return FALSE; + + *value = attribute->boolean_value; + return TRUE; +} + +gboolean +pango_attribute_get_float (PangoAttribute *attribute, + double *value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_FLOAT) + return FALSE; + + *value = attribute->double_value; + return TRUE; +} + +gboolean +pango_attribute_get_color (PangoAttribute *attribute, + PangoColor *value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_COLOR) + return FALSE; + + *value = attribute->color_value; + return TRUE; +} + +gboolean +pango_attribute_get_font_desc (PangoAttribute *attribute, + PangoFontDescription **value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_FONT_DESC) + return FALSE; + + *value = attribute->font_value; + return TRUE; +} + +gboolean +pango_attribute_get_custom (PangoAttribute *attribute, + gpointer *value) +{ + if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_POINTER) + return FALSE; + + *value = attribute->pointer_value; + return TRUE; +} + +/* }}} */ + +/* vim:set foldmethod=marker expandtab: */ |