summaryrefslogtreecommitdiff
path: root/Build/source/texk/ttfdump/libttf/loca.c
blob: 44ec5e05fccaab118fec051f5b0b912d79ae1554 (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
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "ttf.h"
#include "ttfutil.h"

/* 	$Id: loca.c,v 1.1.1.1 1998/06/05 07:47:52 robert Exp $	 */

static LOCAPtr ttfAllocLOCA(TTFontPtr font);
static void ttfLoadLOCA(FILE *fp,LOCAPtr loca,ULONG offset);

void ttfInitLOCA(TTFontPtr font)
{
    ULONG tag = FT_MAKE_TAG ('l', 'o', 'c', 'a');
    TableDirPtr ptd;

    if ((ptd = ttfLookUpTableDir(tag,font)) != NULL)
	{
	    font->loca = (LOCAPtr) ttfAllocLOCA(font);
	    ttfLoadLOCA(font->fp,font->loca,ptd->offset);
	}
}
static LOCAPtr ttfAllocLOCA(TTFontPtr font)
{
    USHORT n=0;
    LOCAPtr loca;

    loca = XCALLOC1 (LOCA);
    loca->indexToLocFormat = font->head->indexToLocFormat;
    loca->numGlyphs = n = font->maxp->numGlyphs;

    n += 1;/* the number of loca entry is numberOfGlyph+1 */
    loca->offset = XCALLOC (n, ULONG);
    
    return loca; 
}
static void ttfLoadLOCA(FILE *fp,LOCAPtr loca,ULONG offset)
{
    /* warning: the number of loca entry is numberOfGlyph+1 !! */
    USHORT i,n = loca->numGlyphs+1;
    
    xfseek(fp, offset, SEEK_SET, "ttfLoadLOCA");
 
    switch (loca->indexToLocFormat)
	{
	case LOCA_OFFSET_SHORT:
	    for (i=0;i<n;i++)
		{
		    (loca->offset)[i] = (ULONG) ttfGetUSHORT(fp)*2;
		}
	    break;
	case LOCA_OFFSET_LONG:
	    ttfReadULONG (loca->offset, n, fp);
	    break;
	}
}

void ttfPrintLOCA(FILE *fp,LOCAPtr loca)
{
    USHORT i;
    
    fprintf(fp,"'loca' Table - Index to Location\n");
    fprintf(fp,"--------------------------------\n");
    for (i=0;i<loca->numGlyphs;i++)
	{
	    fprintf(fp,"\t Idx %6d -> GlyphOffset 0x%08x\n",i,
		    (loca->offset)[i]);
	}
    fprintf (fp,"\t Ended at 0x%08x\n",(loca->offset)[loca->numGlyphs]);
}

void ttfFreeLOCA(LOCAPtr loca)
{
    free (loca->offset);
    free (loca);
}

ULONG ttfLookUpGlyfLOCA(LOCAPtr loca,USHORT idx)
{
    /* out of bound or it is a non-glyph character */
    if (idx >= loca->numGlyphs ||
	loca->offset[idx] == loca->offset[idx+1]) 
	return (loca->offset)[0];

    return (loca->offset)[idx];
}