Git init
[external/pango1.0.git] / pango / pango-item.c
1 /* Pango
2  * pango-item.c: Single run handling
3  *
4  * Copyright (C) 2000 Red Hat Software
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23 #include "pango-attributes.h"
24 #include "pango-item.h"
25 #include "pango-impl-utils.h"
26
27 /**
28  * pango_item_new:
29  *
30  * Creates a new #PangoItem structure initialized to default values.
31  *
32  * Return value: the newly allocated #PangoItem, which should
33  *               be freed with pango_item_free().
34  **/
35 PangoItem *
36 pango_item_new (void)
37 {
38   PangoItem *result = g_slice_new0 (PangoItem);
39
40   return result;
41 }
42
43 /**
44  * pango_item_copy:
45  * @item: a #PangoItem, may be %NULL
46  *
47  * Copy an existing #PangoItem structure.
48  *
49  * Return value: the newly allocated #PangoItem, which should
50  *               be freed with pango_item_free(), or %NULL if
51  *               @item was NULL.
52  **/
53 PangoItem *
54 pango_item_copy (PangoItem *item)
55 {
56   GSList *extra_attrs, *tmp_list;
57   PangoItem *result;
58
59   if (item == NULL)
60     return NULL;
61   
62   result = g_slice_new (PangoItem);
63
64   result->offset = item->offset;
65   result->length = item->length;
66   result->num_chars = item->num_chars;
67
68   result->analysis = item->analysis;
69   if (result->analysis.font)
70     g_object_ref (result->analysis.font);
71
72   extra_attrs = NULL;
73   tmp_list = item->analysis.extra_attrs;
74   while (tmp_list)
75     {
76       extra_attrs = g_slist_prepend (extra_attrs, pango_attribute_copy (tmp_list->data));
77       tmp_list = tmp_list->next;
78     }
79
80   result->analysis.extra_attrs = g_slist_reverse (extra_attrs);
81
82   return result;
83 }
84
85 /**
86  * pango_item_free:
87  * @item: a #PangoItem, may be %NULL
88  *
89  * Free a #PangoItem and all associated memory.
90  **/
91 void
92 pango_item_free (PangoItem *item)
93 {
94   if (item == NULL)
95     return;
96
97   if (item->analysis.extra_attrs)
98     {
99       g_slist_foreach (item->analysis.extra_attrs, (GFunc)pango_attribute_destroy, NULL);
100       g_slist_free (item->analysis.extra_attrs);
101     }
102
103   if (item->analysis.font)
104     g_object_unref (item->analysis.font);
105
106   g_slice_free (PangoItem, item);
107 }
108
109 GType
110 pango_item_get_type (void)
111 {
112   static GType our_type = 0;
113
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);
118   return our_type;
119 }
120
121 /**
122  * pango_item_split:
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
126  *
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.
137  *
138  * Return value: new item representing text before @split_index, which
139  *               should be freed with pango_item_free().
140  **/
141 PangoItem*
142 pango_item_split (PangoItem  *orig,
143                   int         split_index,
144                   int         split_offset)
145 {
146   PangoItem *new_item;
147
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);
153
154   new_item = pango_item_copy (orig);
155   new_item->length = split_index;
156   new_item->num_chars = split_offset;
157
158   orig->offset += split_index;
159   orig->length -= split_index;
160   orig->num_chars -= split_offset;
161
162   return new_item;
163 }