2 * pango-item.c: Single run handling
4 * Copyright (C) 2000 Red Hat Software
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 #include "pango-attributes.h"
24 #include "pango-item.h"
25 #include "pango-impl-utils.h"
30 * Creates a new #PangoItem structure initialized to default values.
32 * Return value: the newly allocated #PangoItem, which should
33 * be freed with pango_item_free().
38 PangoItem *result = g_slice_new0 (PangoItem);
45 * @item: a #PangoItem, may be %NULL
47 * Copy an existing #PangoItem structure.
49 * Return value: the newly allocated #PangoItem, which should
50 * be freed with pango_item_free(), or %NULL if
54 pango_item_copy (PangoItem *item)
56 GSList *extra_attrs, *tmp_list;
62 result = g_slice_new (PangoItem);
64 result->offset = item->offset;
65 result->length = item->length;
66 result->num_chars = item->num_chars;
68 result->analysis = item->analysis;
69 if (result->analysis.font)
70 g_object_ref (result->analysis.font);
73 tmp_list = item->analysis.extra_attrs;
76 extra_attrs = g_slist_prepend (extra_attrs, pango_attribute_copy (tmp_list->data));
77 tmp_list = tmp_list->next;
80 result->analysis.extra_attrs = g_slist_reverse (extra_attrs);
87 * @item: a #PangoItem, may be %NULL
89 * Free a #PangoItem and all associated memory.
92 pango_item_free (PangoItem *item)
97 if (item->analysis.extra_attrs)
99 g_slist_foreach (item->analysis.extra_attrs, (GFunc)pango_attribute_destroy, NULL);
100 g_slist_free (item->analysis.extra_attrs);
103 if (item->analysis.font)
104 g_object_unref (item->analysis.font);
106 g_slice_free (PangoItem, item);
110 pango_item_get_type (void)
112 static GType our_type = 0;
114 if (G_UNLIKELY (our_type == 0))
115 our_type = g_boxed_type_register_static (I_("PangoItem"),
116 (GBoxedCopyFunc) pango_item_copy,
117 (GBoxedFreeFunc) pango_item_free);
123 * @orig: a #PangoItem
124 * @split_index: byte index of position to split item, relative to the start of the item
125 * @split_offset: number of chars between start of @orig and @split_index
127 * Modifies @orig to cover only the text after @split_index, and
128 * returns a new item that covers the text before @split_index that
129 * used to be in @orig. You can think of @split_index as the length of
130 * the returned item. @split_index may not be 0, and it may not be
131 * greater than or equal to the length of @orig (that is, there must
132 * be at least one byte assigned to each item, you can't create a
133 * zero-length item). @split_offset is the length of the first item in
134 * chars, and must be provided because the text used to generate the
135 * item isn't available, so pango_item_split() can't count the char
136 * length of the split items itself.
138 * Return value: new item representing text before @split_index, which
139 * should be freed with pango_item_free().
142 pango_item_split (PangoItem *orig,
148 g_return_val_if_fail (orig != NULL, NULL);
149 g_return_val_if_fail (split_index > 0, NULL);
150 g_return_val_if_fail (split_index < orig->length, NULL);
151 g_return_val_if_fail (split_offset > 0, NULL);
152 g_return_val_if_fail (split_offset < orig->num_chars, NULL);
154 new_item = pango_item_copy (orig);
155 new_item->length = split_index;
156 new_item->num_chars = split_offset;
158 orig->offset += split_index;
159 orig->length -= split_index;
160 orig->num_chars -= split_offset;