* PERFORMANCE OF THIS SOFTWARE.
*/
-#include <stdlib.h>
#include "fcint.h"
+#include <stdlib.h>
FcFontSet *
FcFontSetCreate (void)
return FcTrue;
}
-FcBool
-FcFontSetPrepareSerialize (FcFontSet *s)
-{
- int i;
-
- for (i = 0; i < s->nfont; i++)
- if (!FcPatternPrepareSerialize(s->fonts[i]))
- return FcFalse;
+static int * fcfs_pat_count;
- return FcTrue;
+void
+FcFontSetNewBank (void)
+{
+ FcPatternNewBank();
}
-FcBool
-FcFontSetSerialize (FcFontSet * s)
+int
+FcFontSetNeededBytes (FcFontSet *s)
{
- int i;
- FcPattern * p;
+ int i, c, cum = 0;
for (i = 0; i < s->nfont; i++)
{
- p = FcPatternSerialize (s->fonts[i]);
- if (!p) return FcFalse;
- FcPatternDestroy (s->fonts[i]);
-
- s->fonts[i] = p;
+ c = FcPatternNeededBytes(s->fonts[i]);
+ if (c < 0)
+ return c;
+ cum += c;
}
- return FcTrue;
+ if (cum > 0)
+ return cum + sizeof(int) + FcObjectNeededBytes();
+ else
+ return 0;
}
-void
-FcFontSetClearStatic (void)
+/* Returns an overestimate of the number of bytes that
+ * might later get eaten up by padding in the ALIGN macro. */
+int
+FcFontSetNeededBytesAlign (void)
{
- FcPatternClearStatic();
+ return fc_alignof (int) +
+ FcPatternNeededBytesAlign () + FcObjectNeededBytesAlign ();
}
-FcBool
-FcFontSetRead(int fd, FcConfig * config, FcCache metadata)
+void *
+FcFontSetDistributeBytes (FcCache * metadata, void * block_ptr)
{
- int i, mz, j;
- FcPattern * buf;
+ block_ptr = ALIGN (block_ptr, int);
+ fcfs_pat_count = (int *)block_ptr;
+ block_ptr = (int *)block_ptr + 1;
+ /* we don't consume any bytes for the fontset itself, */
+ /* since we don't allocate it statically. */
+ block_ptr = FcPatternDistributeBytes (metadata, block_ptr);
+
+ /* for good measure, write out the object ids used for */
+ /* this bank to the file. */
+ return FcObjectDistributeBytes (metadata, block_ptr);
+}
- lseek(fd, metadata.fontsets_offset, SEEK_SET);
- for (i = FcSetSystem; i <= FcSetApplication; i++)
- {
- if (config->fonts[i])
- {
- if (config->fonts[i]->nfont > 0 && config->fonts[i]->fonts)
- free (config->fonts[i]->fonts);
- free (config->fonts[i]);
- }
- }
+FcBool
+FcFontSetSerialize (int bank, FcFontSet * s)
+{
+ int i;
+ FcPattern * p;
+ *fcfs_pat_count = s->nfont;
- for (i = FcSetSystem; i <= FcSetApplication; i++)
+ for (i = 0; i < s->nfont; i++)
{
- read(fd, &mz, sizeof(int));
- if (mz != FC_CACHE_MAGIC)
- continue;
-
- config->fonts[i] = malloc(sizeof(FcFontSet));
- if (!config->fonts[i])
- return FcFalse;
- FcMemAlloc(FC_MEM_FONTSET, sizeof(FcFontSet));
-
- if (read(fd, config->fonts[i], sizeof(FcFontSet)) == -1)
- goto bail;
- if (config->fonts[i]->sfont > 0)
- {
- config->fonts[i]->fonts = malloc
- (config->fonts[i]->sfont*sizeof(FcPattern *));
- buf = malloc (config->fonts[i]->sfont * sizeof(FcPattern));
- if (!config->fonts[i]->fonts || !buf)
- goto bail;
- for (j = 0; j < config->fonts[i]->nfont; j++)
- {
- config->fonts[i]->fonts[j] = buf+j;
- if (read(fd, buf+j, sizeof(FcPattern)) == -1)
- goto bail;
- }
- }
+ p = FcPatternSerialize (bank, s->fonts[i]);
+ if (!p) return FcFalse;
}
+ FcObjectSerialize();
return FcTrue;
- bail:
- for (i = FcSetSystem; i <= FcSetApplication; i++)
- {
- if (config->fonts[i])
- {
- if (config->fonts[i]->fonts)
- free (config->fonts[i]->fonts);
- free(config->fonts[i]);
- }
- config->fonts[i] = 0;
- }
- return FcFalse;
}
FcBool
-FcFontSetWrite(int fd, FcConfig * config, FcCache * metadata)
+FcFontSetUnserialize(FcCache * metadata, FcFontSet * s, void * block_ptr)
{
- int c, t, i, j;
- int m = FC_CACHE_MAGIC, z = 0;
+ int nfont;
+ int i, n;
- metadata->fontsets_offset = FcCacheNextOffset(fd);
- lseek(fd, metadata->fontsets_offset, SEEK_SET);
- for (i = FcSetSystem; i <= FcSetApplication; i++)
+ block_ptr = ALIGN (block_ptr, int);
+ nfont = *(int *)block_ptr;
+ block_ptr = (int *)block_ptr + 1;
+
+ /* comparing nfont and metadata.count is a bit like comparing
+ apples and oranges. Its just for rejecting totally insane
+ nfont values, and for that its good enough */
+ if (nfont > 0 && nfont < metadata->count / sizeof(void*))
{
- if (!config->fonts[i])
- {
- write(fd, &z, sizeof(int));
- continue;
- }
- else
- write(fd, &m, sizeof(int));
-
- if ((c = write(fd, config->fonts[i], sizeof(FcFontSet))) == -1)
- return FcFalse;
- t = c;
- if (config->fonts[i]->nfont > 0)
- {
- for (j = 0; j < config->fonts[i]->nfont; j++)
- {
- if ((c = write(fd, config->fonts[i]->fonts[j],
- sizeof(FcPattern))) == -1)
- return FcFalse;
- }
- }
+ FcPattern * p = (FcPattern *)block_ptr;
+
+ if (s->sfont < s->nfont + nfont)
+ {
+ int sfont = s->nfont + nfont;
+ FcPattern ** pp;
+ pp = realloc (s->fonts, sfont * sizeof (FcPattern));
+ if (!pp)
+ return FcFalse;
+ s->fonts = pp;
+ s->sfont = sfont;
+ }
+ n = s->nfont;
+ s->nfont += nfont;
+
+ /* The following line is a bit counterintuitive. The usual
+ * convention is that FcPatternUnserialize is responsible for
+ * aligning the FcPattern. However, the FontSet also stores
+ * the FcPatterns in its own array, so we need to align here
+ * too. */
+ p = ALIGN(p, FcPattern);
+ for (i = 0; i < nfont; i++)
+ s->fonts[n + i] = p+i;
+
+ block_ptr = FcPatternUnserialize (metadata, block_ptr);
+ block_ptr = FcObjectUnserialize (metadata, block_ptr);
+ return block_ptr != 0;
}
- return FcTrue;
+
+ return FcFalse;
}