- Remove svn:needs-lock, svn:eol-type, and svn:eol-tyle properties.
[reactos.git] / reactos / lib / 3rdparty / freetype / src / pfr / pfrcmap.c
1 /***************************************************************************/
2 /* */
3 /* pfrcmap.c */
4 /* */
5 /* FreeType PFR cmap handling (body). */
6 /* */
7 /* Copyright 2002, 2007 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
15 /* */
16 /***************************************************************************/
17
18
19 #include "pfrcmap.h"
20 #include "pfrobjs.h"
21 #include FT_INTERNAL_DEBUG_H
22
23
24 FT_CALLBACK_DEF( FT_Error )
25 pfr_cmap_init( PFR_CMap cmap )
26 {
27 FT_Error error = PFR_Err_Ok;
28 PFR_Face face = (PFR_Face)FT_CMAP_FACE( cmap );
29
30
31 cmap->num_chars = face->phy_font.num_chars;
32 cmap->chars = face->phy_font.chars;
33
34 /* just for safety, check that the character entries are correctly */
35 /* sorted in increasing character code order */
36 {
37 FT_UInt n;
38
39
40 for ( n = 1; n < cmap->num_chars; n++ )
41 {
42 if ( cmap->chars[n - 1].char_code >= cmap->chars[n].char_code )
43 {
44 error = PFR_Err_Invalid_Table;
45 goto Exit;
46 }
47 }
48 }
49
50 Exit:
51 return error;
52 }
53
54
55 FT_CALLBACK_DEF( void )
56 pfr_cmap_done( PFR_CMap cmap )
57 {
58 cmap->chars = NULL;
59 cmap->num_chars = 0;
60 }
61
62
63 FT_CALLBACK_DEF( FT_UInt )
64 pfr_cmap_char_index( PFR_CMap cmap,
65 FT_UInt32 char_code )
66 {
67 FT_UInt min = 0;
68 FT_UInt max = cmap->num_chars;
69 FT_UInt mid;
70 PFR_Char gchar;
71
72
73 while ( min < max )
74 {
75 mid = min + ( max - min ) / 2;
76 gchar = cmap->chars + mid;
77
78 if ( gchar->char_code == char_code )
79 return mid + 1;
80
81 if ( gchar->char_code < char_code )
82 min = mid + 1;
83 else
84 max = mid;
85 }
86 return 0;
87 }
88
89
90 FT_CALLBACK_DEF( FT_UInt )
91 pfr_cmap_char_next( PFR_CMap cmap,
92 FT_UInt32 *pchar_code )
93 {
94 FT_UInt result = 0;
95 FT_UInt32 char_code = *pchar_code + 1;
96
97
98 Restart:
99 {
100 FT_UInt min = 0;
101 FT_UInt max = cmap->num_chars;
102 FT_UInt mid;
103 PFR_Char gchar;
104
105
106 while ( min < max )
107 {
108 mid = min + ( ( max - min ) >> 1 );
109 gchar = cmap->chars + mid;
110
111 if ( gchar->char_code == char_code )
112 {
113 result = mid;
114 if ( result != 0 )
115 {
116 result++;
117 goto Exit;
118 }
119
120 char_code++;
121 goto Restart;
122 }
123
124 if ( gchar->char_code < char_code )
125 min = mid+1;
126 else
127 max = mid;
128 }
129
130 /* we didn't find it, but we have a pair just above it */
131 char_code = 0;
132
133 if ( min < cmap->num_chars )
134 {
135 gchar = cmap->chars + min;
136 result = min;
137 if ( result != 0 )
138 {
139 result++;
140 char_code = gchar->char_code;
141 }
142 }
143 }
144
145 Exit:
146 *pchar_code = char_code;
147 return result;
148 }
149
150
151 FT_CALLBACK_TABLE_DEF const FT_CMap_ClassRec
152 pfr_cmap_class_rec =
153 {
154 sizeof ( PFR_CMapRec ),
155
156 (FT_CMap_InitFunc) pfr_cmap_init,
157 (FT_CMap_DoneFunc) pfr_cmap_done,
158 (FT_CMap_CharIndexFunc)pfr_cmap_char_index,
159 (FT_CMap_CharNextFunc) pfr_cmap_char_next
160 };
161
162
163 /* END */