summaryrefslogtreecommitdiff
path: root/pango/pango-item.c
blob: 37622e87a060cb659343876e792c2946a5ee4fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* Pango
 * pango-item.c: Single run handling
 *
 * Copyright (C) 2000 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 "pango-attributes.h"
#include "pango-item.h"
#include "pango-impl-utils.h"

/**
 * pango_item_new:
 * 
 * Creates a new #PangoItem structure initialized to default values.
 * 
 * Return value: the newly allocated #PangoItem, which should
 *               be freed with pango_item_free().
 **/
PangoItem *
pango_item_new (void)
{
  PangoItem *result = g_slice_new0 (PangoItem);

  return result;
}

/**
 * pango_item_copy:
 * @item: a #PangoItem
 * 
 * Copy an existing #PangoItem structure.
 * 
 * Return value: the newly allocated #PangoItem, which should
 *               be freed with pango_item_free().
 **/
PangoItem *
pango_item_copy (PangoItem *item)
{
  GSList *extra_attrs, *tmp_list;
  PangoItem *result = g_slice_new (PangoItem);

  result->offset = item->offset;
  result->length = item->length;
  result->num_chars = item->num_chars;

  result->analysis = item->analysis;
  if (result->analysis.font)
    g_object_ref (result->analysis.font);
  
  extra_attrs = NULL;
  tmp_list = item->analysis.extra_attrs;
  while (tmp_list)
    {
      extra_attrs = g_slist_prepend (extra_attrs, pango_attribute_copy (tmp_list->data));
      tmp_list = tmp_list->next;
    }

  result->analysis.extra_attrs = g_slist_reverse (extra_attrs);

  return result;
}

/**
 * pango_item_free:
 * @item: a #PangoItem
 * 
 * Free a #PangoItem and all associated memory.
 **/
void
pango_item_free (PangoItem *item)
{
  if (item->analysis.extra_attrs)
    {
      g_slist_foreach (item->analysis.extra_attrs, (GFunc)pango_attribute_destroy, NULL);
      g_slist_free (item->analysis.extra_attrs);
    }

  if (item->analysis.font)
    g_object_unref (item->analysis.font);

  g_slice_free (PangoItem, item);
}

GType
pango_item_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
    our_type = g_boxed_type_register_static (I_("PangoItem"),
                                             (GBoxedCopyFunc) pango_item_copy,
                                             (GBoxedFreeFunc) pango_item_free);
  return our_type;
}

/**
 * pango_item_split:
 * @orig: a #PangoItem
 * @split_index: byte index of position to split item, relative to the start of the item
 * @split_offset: number of chars between start of @orig and @split_index
 * 
 * Modifies @orig to cover only the text after @split_index, and
 * returns a new item that covers the text before @split_index that
 * used to be in @orig. You can think of @split_index as the length of
 * the returned item. @split_index may not be 0, and it may not be
 * greater than or equal to the length of @orig (that is, there must
 * be at least one byte assigned to each item, you can't create a
 * zero-length item). @split_offset is the length of the first item in
 * chars, and must be provided because the text used to generate the
 * item isn't available, so pango_item_split() can't count the char
 * length of the split items itself.
 * 
 * Return value: new item representing text before @split_index, which
 *               should be freed with pango_item_free().
 **/
PangoItem*
pango_item_split (PangoItem  *orig,
                  int         split_index,
                  int         split_offset)
{
  PangoItem *new_item = pango_item_copy (orig);

  g_return_val_if_fail (orig != NULL, NULL);
  g_return_val_if_fail (orig->length > 0, NULL);
  g_return_val_if_fail (split_index > 0, NULL);
  g_return_val_if_fail (split_index < orig->length, NULL);
  g_return_val_if_fail (split_offset > 0, NULL);
  g_return_val_if_fail (split_offset < orig->num_chars, NULL);
  
  new_item->length = split_index;
  new_item->num_chars = split_offset;
  
  orig->offset += split_index;
  orig->length -= split_index;
  orig->num_chars -= split_offset;
  
  return new_item;
}