4 * Copyright (C) 2011 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 /* originally file `afglobal.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
31 #ifdef FT_OPTION_AUTOFIT2
36 /* populate this list when you add new scripts */
37 static TA_ScriptClass
const ta_script_classes
[] =
39 &ta_dummy_script_class
,
40 #ifdef FT_OPTION_AUTOFIT2
41 &ta_latin2_script_class
,
43 &ta_latin_script_class
,
46 &ta_indic_script_class
,
48 NULL
/* do not remove */
52 /* a bit mask indicating an uncovered glyph */
53 #define TA_SCRIPT_LIST_NONE 0x7F
54 /* if this flag is set, we have an ASCII digit */
58 /* note that glyph_scripts[] is used to map each glyph into */
59 /* an index into the `ta_script_classes' array. */
60 typedef struct TA_FaceGlobalsRec_
63 FT_Long glyph_count
; /* same as face->num_glyphs */
64 FT_Byte
* glyph_scripts
;
66 TA_ScriptMetrics metrics
[TA_SCRIPT_MAX
];
70 /* Compute the script index of each glyph within a given face. */
73 ta_face_globals_compute_script_coverage(TA_FaceGlobals globals
,
74 FT_UInt fallback_script
)
76 FT_Error error
= FT_Err_Ok
;
77 FT_Face face
= globals
->face
;
78 FT_CharMap old_charmap
= face
->charmap
;
79 FT_Byte
* gscripts
= globals
->glyph_scripts
;
84 /* the value TA_SCRIPT_LIST_NONE means `uncovered glyph' */
85 memset(globals
->glyph_scripts
, TA_SCRIPT_LIST_NONE
, globals
->glyph_count
);
87 error
= FT_Select_Charmap(face
, FT_ENCODING_UNICODE
);
90 /* ignore this error; we simply use the fallback script */
91 /* XXX: Shouldn't we rather disable hinting? */
96 /* scan each script in a Unicode charmap */
97 for (ss
= 0; ta_script_classes
[ss
]; ss
++)
99 TA_ScriptClass clazz
= ta_script_classes
[ss
];
100 TA_Script_UniRange range
;
103 if (clazz
->script_uni_ranges
== NULL
)
106 /* scan all Unicode points in the range and */
107 /* set the corresponding glyph script index */
108 for (range
= clazz
->script_uni_ranges
; range
->first
!= 0; range
++)
110 FT_ULong charcode
= range
->first
;
114 gindex
= FT_Get_Char_Index(face
, charcode
);
117 && gindex
< (FT_ULong
)globals
->glyph_count
118 && gscripts
[gindex
] == TA_SCRIPT_LIST_NONE
)
119 gscripts
[gindex
] = (FT_Byte
)ss
;
123 charcode
= FT_Get_Next_Char(face
, charcode
, &gindex
);
125 if (gindex
== 0 || charcode
> range
->last
)
128 if (gindex
< (FT_ULong
)globals
->glyph_count
129 && gscripts
[gindex
] == TA_SCRIPT_LIST_NONE
)
130 gscripts
[gindex
] = (FT_Byte
)ss
;
135 /* mark ASCII digits */
136 for (i
= 0x30; i
<= 0x39; i
++)
138 FT_UInt gindex
= FT_Get_Char_Index(face
, i
);
142 && gindex
< (FT_ULong
)globals
->glyph_count
)
143 gscripts
[gindex
] |= TA_DIGIT
;
147 /* by default, all uncovered glyphs are set to the fallback script */
148 /* XXX: Shouldn't we disable hinting or do something similar? */
153 for (nn
= 0; nn
< globals
->glyph_count
; nn
++)
155 if ((gscripts
[nn
] & ~TA_DIGIT
) == TA_SCRIPT_LIST_NONE
)
157 gscripts
[nn
] &= ~TA_SCRIPT_LIST_NONE
;
158 gscripts
[nn
] |= fallback_script
;
163 FT_Set_Charmap(face
, old_charmap
);
169 ta_face_globals_new(FT_Face face
,
170 TA_FaceGlobals
*aglobals
,
171 FT_UInt fallback_script
)
174 TA_FaceGlobals globals
;
177 globals
= (TA_FaceGlobals
)calloc(1, sizeof (TA_FaceGlobalsRec
) +
178 face
->num_glyphs
* sizeof (FT_Byte
));
181 error
= FT_Err_Out_Of_Memory
;
185 globals
->face
= face
;
186 globals
->glyph_count
= face
->num_glyphs
;
187 globals
->glyph_scripts
= (FT_Byte
*)(globals
+ 1);
189 error
= ta_face_globals_compute_script_coverage(globals
, fallback_script
);
192 ta_face_globals_free(globals
);
203 ta_face_globals_free(TA_FaceGlobals globals
)
210 for (nn
= 0; nn
< TA_SCRIPT_MAX
; nn
++)
212 if (globals
->metrics
[nn
])
214 TA_ScriptClass clazz
= ta_script_classes
[nn
];
218 FT_ASSERT(globals
->metrics
[nn
]->clazz
== clazz
);
221 if (clazz
->script_metrics_done
)
222 clazz
->script_metrics_done(globals
->metrics
[nn
]);
224 free(globals
->metrics
[nn
]);
225 globals
->metrics
[nn
] = NULL
;
229 globals
->glyph_count
= 0;
230 globals
->glyph_scripts
= NULL
; /* no need to free this one! */
231 globals
->face
= NULL
;
240 ta_face_globals_get_metrics(TA_FaceGlobals globals
,
243 TA_ScriptMetrics
*ametrics
)
245 TA_ScriptMetrics metrics
= NULL
;
247 TA_ScriptClass clazz
;
248 FT_UInt script
= options
& 15;
249 const FT_Offset script_max
= sizeof (ta_script_classes
)
250 / sizeof (ta_script_classes
[0]);
251 FT_Error error
= FT_Err_Ok
;
254 if (gindex
>= (FT_ULong
)globals
->glyph_count
)
256 error
= FT_Err_Invalid_Argument
;
262 || gidx
+ 1 >= script_max
)
263 gidx
= globals
->glyph_scripts
[gindex
] & TA_SCRIPT_LIST_NONE
;
265 clazz
= ta_script_classes
[gidx
];
267 script
= clazz
->script
;
269 metrics
= globals
->metrics
[clazz
->script
];
272 /* create the global metrics object if necessary */
273 metrics
= (TA_ScriptMetrics
)calloc(1, clazz
->script_metrics_size
);
276 error
= FT_Err_Out_Of_Memory
;
280 metrics
->clazz
= clazz
;
282 if (clazz
->script_metrics_init
)
284 error
= clazz
->script_metrics_init(metrics
, globals
->face
);
287 if (clazz
->script_metrics_done
)
288 clazz
->script_metrics_done(metrics
);
296 globals
->metrics
[clazz
->script
] = metrics
;
307 ta_face_globals_is_digit(TA_FaceGlobals globals
,
310 if (gindex
< (FT_ULong
)globals
->glyph_count
)
311 return (FT_Bool
)(globals
->glyph_scripts
[gindex
] & TA_DIGIT
);
316 /* end of taglobal.c */