1/*
2 * fontconfig/src/fcfs.c
3 *
4 * Copyright © 2000 Keith Packard
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission.  The authors make no
13 * representations about the suitability of this software for any purpose.  It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include "fcint.h"
26#include <stdlib.h>
27
28FcFontSet *
29FcFontSetCreate (void)
30{
31    FcFontSet	*s;
32
33    s = (FcFontSet *) malloc (sizeof (FcFontSet));
34    if (!s)
35	return 0;
36    s->nfont = 0;
37    s->sfont = 0;
38    s->fonts = 0;
39    return s;
40}
41
42void
43FcFontSetDestroy (FcFontSet *s)
44{
45    if (s)
46    {
47	int	    i;
48
49	for (i = 0; i < s->nfont; i++)
50	    FcPatternDestroy (s->fonts[i]);
51	if (s->fonts)
52	    free (s->fonts);
53	free (s);
54    }
55}
56
57FcBool
58FcFontSetAdd (FcFontSet *s, FcPattern *font)
59{
60    FcPattern	**f;
61    int		sfont;
62
63    if (s->nfont == s->sfont)
64    {
65	sfont = s->sfont + 32;
66	if (s->fonts)
67	    f = (FcPattern **) realloc (s->fonts, sfont * sizeof (FcPattern *));
68	else
69	    f = (FcPattern **) malloc (sfont * sizeof (FcPattern *));
70	if (!f)
71	    return FcFalse;
72	s->sfont = sfont;
73	s->fonts = f;
74    }
75    s->fonts[s->nfont++] = font;
76    return FcTrue;
77}
78
79FcBool
80FcFontSetSerializeAlloc (FcSerialize *serialize, const FcFontSet *s)
81{
82    int i;
83
84    if (!FcSerializeAlloc (serialize, s, sizeof (FcFontSet)))
85	return FcFalse;
86    if (!FcSerializeAlloc (serialize, s->fonts, s->nfont * sizeof (FcPattern *)))
87	return FcFalse;
88    for (i = 0; i < s->nfont; i++)
89    {
90	if (!FcPatternSerializeAlloc (serialize, s->fonts[i]))
91	    return FcFalse;
92    }
93    return FcTrue;
94}
95
96FcFontSet *
97FcFontSetSerialize (FcSerialize *serialize, const FcFontSet * s)
98{
99    int		i;
100    FcFontSet	*s_serialize;
101    FcPattern	**fonts_serialize;
102    FcPattern	*p_serialize;
103
104    s_serialize = FcSerializePtr (serialize, s);
105    if (!s_serialize)
106	return NULL;
107    *s_serialize = *s;
108    s_serialize->sfont = s_serialize->nfont;
109
110    fonts_serialize = FcSerializePtr (serialize, s->fonts);
111    if (!fonts_serialize)
112	return NULL;
113    s_serialize->fonts = FcPtrToEncodedOffset (s_serialize,
114					       fonts_serialize, FcPattern *);
115
116    for (i = 0; i < s->nfont; i++)
117    {
118	p_serialize = FcPatternSerialize (serialize, s->fonts[i]);
119	if (!p_serialize)
120	    return NULL;
121	fonts_serialize[i] = FcPtrToEncodedOffset (s_serialize,
122						   p_serialize,
123						   FcPattern);
124    }
125
126    return s_serialize;
127}
128
129FcFontSet *
130FcFontSetDeserialize (const FcFontSet *set)
131{
132    int i;
133    FcFontSet *new = FcFontSetCreate ();
134
135    if (!new)
136	return NULL;
137    for (i = 0; i < set->nfont; i++)
138    {
139	if (!FcFontSetAdd (new, FcPatternDuplicate (FcFontSetFont (set, i))))
140	    goto bail;
141    }
142
143    return new;
144bail:
145    FcFontSetDestroy (new);
146
147    return NULL;
148}
149
150#define __fcfs__
151#include "fcaliastail.h"
152#undef __fcfs__
153