Commit 07e818170f146053c876f6138e492ee79614e000

Werner Lemberg 2009-03-11T21:29:54

Extend CID service functions to handle CID-keyed CFFs as CID fonts. * include/freetype/ftcid.h (FT_Get_CID_Is_Internally_CID_keyed, FT_Get_CID_From_Glyph_Index): New functions. * include/freetype/internal/services/svcid.h (FT_CID_GetIsInternallyCIDKeyedFunc, FT_CID_GetCIDFromGlyphIndexFunc): New function typedefs. (CID Service): Use them. * src/base/ftcid.c: Include FT_CID_H. (FT_Get_CID_Is_Internally_CID_keyed, FT_Get_CID_From_Glyph_Index): New functions. * src/cff/cffdrivr.c (cff_get_is_cid, cff_get_cid_from_glyph_index): New functions. (cff_service_cid_info): Add them. * src/cff/cffload.c (cff_font_load): Don't free `font->charset.sids' -- it is needed for access as a CID-keyed font. It gets deleted later on. * src/cid/cidriver.c (cid_get_is_cid, cid_get_cid_from_glyph_index): New functions. (cid_service_cid_info): Add them. * docs/CHANGES: Updated.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
diff --git a/ChangeLog b/ChangeLog
index 08b4a3c..33a7a8d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,32 @@
+2009-03-11  Michael Toftdal  <toftdal@gmail.com>
+
+	Extend CID service functions to handle CID-keyed CFFs as CID fonts.
+
+	* include/freetype/ftcid.h (FT_Get_CID_Is_Internally_CID_keyed,
+	FT_Get_CID_From_Glyph_Index): New functions.
+
+	* include/freetype/internal/services/svcid.h
+	(FT_CID_GetIsInternallyCIDKeyedFunc,
+	FT_CID_GetCIDFromGlyphIndexFunc): New function typedefs.
+	(CID Service): Use them.
+
+	* src/base/ftcid.c: Include FT_CID_H.
+	(FT_Get_CID_Is_Internally_CID_keyed, FT_Get_CID_From_Glyph_Index):
+	New functions.
+
+	* src/cff/cffdrivr.c (cff_get_is_cid, cff_get_cid_from_glyph_index):
+	New functions.
+	(cff_service_cid_info): Add them.
+	* src/cff/cffload.c (cff_font_load): Don't free `font->charset.sids'
+	-- it is needed for access as a CID-keyed font.  It gets deleted
+	later on.
+
+	* src/cid/cidriver.c (cid_get_is_cid, cid_get_cid_from_glyph_index):
+	New functions.
+	(cid_service_cid_info): Add them.
+
+	* docs/CHANGES: Updated.
+
 2009-03-11  Bram Tassyns  <bramt@enfocus.be>
 
 	Fix Savannah bug #25597.
diff --git a/docs/CHANGES b/docs/CHANGES
index d8b722d..9eb68c2 100644
--- a/docs/CHANGES
+++ b/docs/CHANGES
@@ -39,7 +39,15 @@ CHANGES BETWEEN 2.3.9 and 2.3.8
     - Composite SFNT bitmaps are now handled correctly.
 
 
-  II. MISCELLANEOUS
+  II. IMPORTANT CHANGES
+
+    - The   new  functions   `FT_Get_CID_Is_Internally_CID_keyed'  and
+      `FT_Get_CID_From_Glyph_Index'  can be  used to  access CID-keyed
+      CFF fonts  via CID  values.  This code  has been  contributed by
+      Michael Toftdal.
+
+
+  III. MISCELLANEOUS
 
     - `FT_Outline_Get_InsideBorder'  returns   FT_STROKER_BORDER_RIGHT
       for empty outlines.  This was incorrectly documented.
diff --git a/include/freetype/ftcid.h b/include/freetype/ftcid.h
index 02df2a0..b38e5d2 100644
--- a/include/freetype/ftcid.h
+++ b/include/freetype/ftcid.h
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    FreeType API for accessing CID font information (specification).     */
 /*                                                                         */
-/*  Copyright 2007 by Dereg Clegg.                                         */
+/*  Copyright 2007, 2009 by Dereg Clegg, Michael Toftdal.                  */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
 /*  modified, and distributed under the terms of the FreeType project      */
@@ -88,6 +88,74 @@ FT_BEGIN_HEADER
                                            const char*  *ordering,
                                            FT_Int       *supplement);
 
+
+  /**********************************************************************
+   *
+   * @function:
+   *    FT_Get_CID_Is_Internally_CID_Keyed
+   *
+   * @description:
+   *    Retrieve the type of the input face, CID keyed or not.  In
+   *    constrast to the @FT_IS_CID_KEYED macro this function returns
+   *    successfully also for CID-keyed fonts in an SNFT wrapper.
+   *
+   * @input:
+   *    face ::
+   *       A handle to the input face.
+   *
+   * @output:
+   *    is_cid ::
+   *       The type of the face as an @FT_Bool.
+   *
+   * @return:
+   *    FreeType error code.  0~means success.
+   *
+   * @note:
+   *    This function only works with CID faces and OpenType fonts,
+   *    returning an error otherwise.
+   *
+   * @since:
+   *    2.3.9
+   */
+  FT_EXPORT( FT_Error )
+  FT_Get_CID_Is_Internally_CID_Keyed( FT_Face   face,
+                                      FT_Bool  *is_cid );
+
+
+  /**********************************************************************
+   *
+   * @function:
+   *    FT_Get_CID_From_Glyph_Index
+   *
+   * @description:
+   *    Retrieve the CID of the input glyph index.
+   *
+   * @input:
+   *    face ::
+   *       A handle to the input face.
+   *
+   *    glyph_index ::
+   *       The input glyph index.
+   *
+   * @output:
+   *    cid ::
+   *       The CID as an FT_UInt.
+   *
+   * @return:
+   *    FreeType error code.  0~means success.
+   *
+   * @note:
+   *    This function only works with CID faces and OpenType fonts,
+   *    returning an error otherwise.
+   *
+   * @since:
+   *    2.3.9
+   */
+  FT_EXPORT( FT_Error )
+  FT_Get_CID_From_Glyph_Index( FT_Face   face,
+                               FT_UInt   glyph_index,
+                               FT_UInt  *cid );
+
  /* */
 
 FT_END_HEADER
diff --git a/include/freetype/internal/services/svcid.h b/include/freetype/internal/services/svcid.h
index 47fef62..2e391f2 100644
--- a/include/freetype/internal/services/svcid.h
+++ b/include/freetype/internal/services/svcid.h
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    The FreeType CID font services (specification).                      */
 /*                                                                         */
-/*  Copyright 2007 by Derek Clegg.                                         */
+/*  Copyright 2007, 2009 by Derek Clegg, Michael Toftdal.                  */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
 /*  modified, and distributed under the terms of the FreeType project      */
@@ -31,10 +31,19 @@ FT_BEGIN_HEADER
                                                const char*  *registry,
                                                const char*  *ordering,
                                                FT_Int       *supplement );
+  typedef FT_Error
+  (*FT_CID_GetIsInternallyCIDKeyedFunc)( FT_Face   face,
+                                         FT_Bool  *is_cid );
+  typedef FT_Error
+  (*FT_CID_GetCIDFromGlyphIndexFunc)( FT_Face   face,
+                                      FT_UInt   glyph_index,
+                                      FT_UInt  *cid );
 
   FT_DEFINE_SERVICE( CID )
   {
     FT_CID_GetRegistryOrderingSupplementFunc  get_ros;
+    FT_CID_GetIsInternallyCIDKeyedFunc        get_is_cid;
+    FT_CID_GetCIDFromGlyphIndexFunc           get_cid_from_glyph_index;
   };
 
   /* */
diff --git a/src/base/ftcid.c b/src/base/ftcid.c
index 5a8b50f..733aae1 100644
--- a/src/base/ftcid.c
+++ b/src/base/ftcid.c
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    FreeType API for accessing CID font information.                     */
 /*                                                                         */
-/*  Copyright 2007 by Derek Clegg.                                         */
+/*  Copyright 2007, 2009 by Derek Clegg, Michael Toftdal.                  */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
 /*  modified, and distributed under the terms of the FreeType project      */
@@ -16,6 +16,7 @@
 
 
 #include <ft2build.h>
+#include FT_CID_H
 #include FT_INTERNAL_OBJECTS_H
 #include FT_SERVICE_CID_H
 
@@ -60,4 +61,57 @@
   }
 
 
+  FT_EXPORT_DEF( FT_Error )
+  FT_Get_CID_Is_Internally_CID_Keyed( FT_Face   face,
+                                      FT_Bool  *is_cid )
+  {
+    FT_Error  error = FT_Err_Invalid_Argument;
+    FT_Bool   ic = 0;
+
+
+    if ( face )
+    {
+      FT_Service_CID  service;
+
+
+      FT_FACE_FIND_SERVICE( face, service, CID );
+
+      if ( service && service->get_is_cid )
+        error = service->get_is_cid( face, &ic);
+    }
+
+    if ( is_cid )
+      *is_cid = ic;
+
+    return error;
+  }
+
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_Get_CID_From_Glyph_Index( FT_Face   face,
+                               FT_UInt   glyph_index,
+                               FT_UInt  *cid )
+  {
+    FT_Error  error = FT_Err_Invalid_Argument;
+    FT_UInt   c = 0;
+
+
+    if ( face )
+    {
+      FT_Service_CID  service;
+
+
+      FT_FACE_FIND_SERVICE( face, service, CID );
+
+      if ( service && service->get_cid_from_glyph_index )
+        error = service->get_cid_from_glyph_index( face, glyph_index, &c);
+    }
+
+    if ( cid )
+      *cid = c;
+
+    return error;
+  }
+
+
 /* END */
diff --git a/src/cff/cffdrivr.c b/src/cff/cffdrivr.c
index 286c0b3..8d385f7 100644
--- a/src/cff/cffdrivr.c
+++ b/src/cff/cffdrivr.c
@@ -506,9 +506,74 @@
   }
 
 
+  static FT_Error
+  cff_get_is_cid( CFF_Face  face,
+                  FT_Bool  *is_cid )
+  {
+    FT_Error  error = CFF_Err_Ok;
+    CFF_Font  cff   = (CFF_Font)face->extra.data;
+
+
+    *is_cid = 0;
+
+    if ( cff )
+    {
+      CFF_FontRecDict  dict = &cff->top_font.font_dict;
+
+
+      if ( dict->cid_registry != 0xFFFFU )
+        *is_cid = 1;
+    }
+
+    return error;
+  }
+
+
+  static FT_Error
+  cff_get_cid_from_glyph_index( CFF_Face  face,
+                                FT_UInt   glyph_index,
+                                FT_UInt  *cid )
+  {
+    FT_Error  error = CFF_Err_Ok;
+    CFF_Font  cff;
+
+
+    cff = (CFF_Font)face->extra.data;
+
+    if ( cff )
+    {
+      FT_UInt          c;
+      CFF_FontRecDict  dict = &cff->top_font.font_dict;
+
+
+      if ( dict->cid_registry == 0xFFFFU )
+      {
+        error = CFF_Err_Invalid_Argument;
+        goto Fail;
+      }
+
+      if ( glyph_index > cff->num_glyphs )
+      {
+        error = CFF_Err_Invalid_Argument;
+        goto Fail;
+      }
+
+      c = cff->charset.sids[glyph_index];
+
+      if ( cid )
+        *cid = c;
+    }
+
+  Fail:
+    return error;
+  }
+
+
   static const FT_Service_CIDRec  cff_service_cid_info =
   {
-    (FT_CID_GetRegistryOrderingSupplementFunc)cff_get_ros
+    (FT_CID_GetRegistryOrderingSupplementFunc)cff_get_ros,
+    (FT_CID_GetIsInternallyCIDKeyedFunc)      cff_get_is_cid,
+    (FT_CID_GetCIDFromGlyphIndexFunc)         cff_get_cid_from_glyph_index
   };
 
 
diff --git a/src/cff/cffload.c b/src/cff/cffload.c
index 0178c81..4a58189 100644
--- a/src/cff/cffload.c
+++ b/src/cff/cffload.c
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    OpenType and CFF data/program tables loader (body).                  */
 /*                                                                         */
-/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by       */
+/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -1540,9 +1540,6 @@
         if ( error )
           goto Exit;
       }
-      else
-        /* CID-keyed fonts only need CIDs */
-        FT_FREE( font->charset.sids );
     }
 
     /* get the font name (/CIDFontName for CID-keyed fonts, */
diff --git a/src/cid/cidriver.c b/src/cid/cidriver.c
index f650ee1..b41d5d6 100644
--- a/src/cid/cidriver.c
+++ b/src/cid/cidriver.c
@@ -122,9 +122,42 @@
   }
 
 
+  static FT_Error
+  cid_get_is_cid( CID_Face  face,
+                  FT_Bool  *is_cid )
+  {
+    FT_Error  error = CID_Err_Ok;
+    FT_UNUSED( face );
+
+
+    if ( is_cid )
+      *is_cid = 1; /* cid driver is only used for CID keyed fonts */
+
+    return error;
+  }
+
+
+  static FT_Error
+  cid_get_cid_from_glyph_index( CID_Face  face,
+                                FT_UInt   glyph_index,
+                                FT_UInt  *cid )
+  {
+    FT_Error  error = CID_Err_Ok;
+    FT_UNUSED( face );
+
+
+    if ( cid )
+      *cid = glyph_index; /* identity mapping */
+
+    return error;
+  }
+
+
   static const FT_Service_CIDRec  cid_service_cid_info =
   {
-    (FT_CID_GetRegistryOrderingSupplementFunc)cid_get_ros
+     (FT_CID_GetRegistryOrderingSupplementFunc)cid_get_ros,
+     (FT_CID_GetIsInternallyCIDKeyedFunc)      cid_get_is_cid,
+     (FT_CID_GetCIDFromGlyphIndexFunc)         cid_get_cid_from_glyph_index
   };