1 /************************************************************************/
2 /*									*/
3 /*  Map Unicode values to glyph numbers.				*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"appUtilConfig.h"
8 
9 #   include	<stddef.h>
10 #   include	<stdlib.h>
11 
12 #   include	"utilIndexMapping.h"
13 
14 #   include	<appDebugon.h>
15 
16 /************************************************************************/
17 /*									*/
18 /*  Bookkeeping.							*/
19 /*									*/
20 /************************************************************************/
21 
utilInitIndexMapping(IndexMapping * im)22 void utilInitIndexMapping(	IndexMapping *	im )
23     {
24     im->imPages= (int **)0;
25     im->imPageCount= 0;
26     }
27 
utilCleanIndexMapping(IndexMapping * im)28 void utilCleanIndexMapping(	IndexMapping *	im )
29     {
30     int		i;
31 
32     for ( i= 0; i < im->imPageCount; i++ )
33 	{
34 	if  ( im->imPages[i] )
35 	    { free( im->imPages[i] );	}
36 	}
37 
38     if  ( im->imPages )
39 	{ free( im->imPages );	}
40     }
41 
utilClearIndexMapping(IndexMapping * im)42 void utilClearIndexMapping(	IndexMapping *	im )
43     {
44     utilCleanIndexMapping( im );
45     utilInitIndexMapping( im );
46     }
47 
48 /************************************************************************/
49 /*									*/
50 /*  Map a unicode value to a glyph index.				*/
51 /*									*/
52 /************************************************************************/
53 
utilIndexMappingPut(IndexMapping * im,int from,int to)54 int  utilIndexMappingPut(	IndexMapping *		im,
55 				int			from,
56 				int			to )
57     {
58     int		page;
59     int		idx;
60 
61     if  ( from < 0 || to < 0 || from >= 256* 256 || to >= 256* 256 )
62 	{ XLDEB(from,to); return -1;	}
63 
64     page= from/ TGM_PSZ;
65     idx=  from% TGM_PSZ;
66 
67     if  ( page >= im->imPageCount )
68 	{
69 	int **		pages;
70 
71 	pages= (int **)realloc( im->imPages, (page+ 1)* sizeof(int *) );
72 	if  ( ! pages )
73 	    { LXDEB(page,pages); return -1;	}
74 	im->imPages= pages;
75 
76 	while( im->imPageCount <= page )
77 	    { im->imPages[im->imPageCount++]= (int *)0; }
78 	}
79 
80     if  ( ! im->imPages[page] )
81 	{
82 	int		i;
83 	int *		fresh;
84 
85 	fresh= (int *)malloc( TGM_PSZ* sizeof(int) );
86 	if  ( ! fresh )
87 	    { XDEB(fresh); return -1;	}
88 
89 	for ( i= 0; i < TGM_PSZ; i++ )
90 	    { fresh[i]= -1;	}
91 
92 	im->imPages[page]= fresh;
93 	}
94 
95     im->imPages[page][idx]= to;
96 
97     return 0;
98     }
99 
100 /************************************************************************/
101 /*									*/
102 /*  Add a mapping at the end of the mapping.				*/
103 /*									*/
104 /************************************************************************/
105 
utilIndexMappingAppend(IndexMapping * im,int * pFrom,int to)106 int utilIndexMappingAppend(	IndexMapping *		im,
107 				int *			pFrom,
108 				int			to )
109     {
110     int		from= TGM_PSZ* im->imPageCount;
111     int		page;
112 
113     for ( page= im->imPageCount- 1; page >= 0; page-- )
114 	{
115 	const int *	ints= im->imPages[page];
116 	int		idx= 0;
117 
118 	if  ( ! ints )
119 	    { from= TGM_PSZ* page; continue;	}
120 
121 	for ( idx= TGM_PSZ- 1; idx >= 0; idx-- )
122 	    {
123 	    if  ( ints[idx] >= 0 )
124 		{ break;	}
125 
126 	    from= TGM_PSZ* page+ idx;
127 	    }
128 
129 	if  ( idx >= 0 )
130 	    { break;	}
131 	}
132 
133     if  ( utilIndexMappingPut( im, from, to ) )
134 	{ LLDEB(from,to); return -1;	}
135 
136     if  ( pFrom )
137 	{ *pFrom= from;	}
138 
139     return 0;
140     }
141 
142 /************************************************************************/
143 /*									*/
144 /*  Construct a backward mapping for an array of unicode code points.	*/
145 /*									*/
146 /************************************************************************/
147 
utilIndexMappingBuildBackward(IndexMapping * im,const int * forward,int forwardCount)148 int utilIndexMappingBuildBackward(	IndexMapping *		im,
149 					const int *		forward,
150 					int			forwardCount )
151     {
152     int		g;
153 
154     for ( g= 0; g < forwardCount; g++ )
155 	{
156 	if  ( forward[g] >= 0				&&
157 	      utilIndexMappingPut( im, forward[g], g )	)
158 	    { LLDEB(g,forward[g]);	}
159 	}
160 
161     return 0;
162     }
163 
utilIndexMappingForAll(const IndexMapping * im,IndexMappingForOne forOne,void * through)164 int utilIndexMappingForAll(	const IndexMapping *	im,
165 				IndexMappingForOne	forOne,
166 				void *			through )
167     {
168     int		page;
169 
170     for ( page= 0; page < im->imPageCount; page++ )
171 	{
172 	const int *	ints= im->imPages[page];
173 	int		idx;
174 
175 	if  ( ! ints )
176 	    { continue;	}
177 
178 	for ( idx= 0; idx < TGM_PSZ; ints++, idx++ )
179 	    {
180 	    if  ( *ints >= 0 && (*forOne)( TGM_PSZ*page+ idx, *ints, through ) )
181 		{ return -1;	}
182 	    }
183 	}
184 
185     return 0;
186     }
187