| 1 |
/****************************************************************************\ |
| 2 |
Part of the XeTeX typesetting system |
| 3 |
Copyright (c) 1994-2008 by SIL International |
| 4 |
Copyright (c) 2009 by Jonathan Kew |
| 5 |
Copyright (c) 2012, 2013 by Jiang Jiang |
| 6 |
Copyright (c) 2012-2015 by Khaled Hosny |
| 7 |
|
| 8 |
SIL Author(s): Jonathan Kew |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining |
| 11 |
a copy of this software and associated documentation files (the |
| 12 |
"Software"), to deal in the Software without restriction, including |
| 13 |
without limitation the rights to use, copy, modify, merge, publish, |
| 14 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 15 |
permit persons to whom the Software is furnished to do so, subject to |
| 16 |
the following conditions: |
| 17 |
|
| 18 |
The above copyright notice and this permission notice shall be |
| 19 |
included in all copies or substantial portions of the Software. |
| 20 |
|
| 21 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 |
NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
| 25 |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 26 |
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 27 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 |
|
| 29 |
Except as contained in this notice, the name of the copyright holders |
| 30 |
shall not be used in advertising or otherwise to promote the sale, |
| 31 |
use or other dealings in this Software without prior written |
| 32 |
authorization from the copyright holders. |
| 33 |
\****************************************************************************/ |
| 34 |
|
| 35 |
/* XeTeX_mac.c |
| 36 |
* additional plain C extensions for XeTeX - MacOS-specific routines |
| 37 |
*/ |
| 38 |
|
| 39 |
#define EXTERN extern |
| 40 |
#include "xetexd.h" |
| 41 |
|
| 42 |
#include <ApplicationServices/ApplicationServices.h> |
| 43 |
|
| 44 |
#include <teckit/TECkit_Engine.h> |
| 45 |
#include "XeTeX_ext.h" |
| 46 |
#include "XeTeXLayoutInterface.h" |
| 47 |
|
| 48 |
static inline double |
| 49 |
TeXtoPSPoints(double pts) |
| 50 |
{ |
| 51 |
return pts * 72.0 / 72.27; |
| 52 |
} |
| 53 |
|
| 54 |
static inline double |
| 55 |
PStoTeXPoints(double pts) |
| 56 |
{ |
| 57 |
return pts * 72.27 / 72.0; |
| 58 |
} |
| 59 |
|
| 60 |
static inline Fixed |
| 61 |
FixedPStoTeXPoints(double pts) |
| 62 |
{ |
| 63 |
return D2Fix(PStoTeXPoints(pts)); |
| 64 |
} |
| 65 |
|
| 66 |
CTFontRef |
| 67 |
fontFromAttributes(CFDictionaryRef attributes) |
| 68 |
{ |
| 69 |
return CFDictionaryGetValue(attributes, kCTFontAttributeName); |
| 70 |
} |
| 71 |
|
| 72 |
CTFontRef |
| 73 |
fontFromInteger(integer font) |
| 74 |
{ |
| 75 |
CFDictionaryRef attributes = (CFDictionaryRef) fontlayoutengine[font]; |
| 76 |
return fontFromAttributes(attributes); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
DoAATLayout(void* p, int justify) |
| 81 |
{ |
| 82 |
CFArrayRef glyphRuns; |
| 83 |
CFIndex i, j, runCount; |
| 84 |
CFIndex totalGlyphCount = 0; |
| 85 |
UInt16* glyphIDs; |
| 86 |
Fixed* glyphAdvances; |
| 87 |
void* glyph_info = NULL; |
| 88 |
FixedPoint* locations; |
| 89 |
CGFloat width; |
| 90 |
|
| 91 |
long txtLen; |
| 92 |
const UniChar* txtPtr; |
| 93 |
|
| 94 |
CFDictionaryRef attributes; |
| 95 |
CFStringRef string; |
| 96 |
CFAttributedStringRef attrString; |
| 97 |
CTTypesetterRef typesetter; |
| 98 |
CTLineRef line; |
| 99 |
|
| 100 |
memoryword* node = (memoryword*)p; |
| 101 |
|
| 102 |
unsigned f = native_font(node); |
| 103 |
if (fontarea[f] != AAT_FONT_FLAG) { |
| 104 |
fprintf(stderr, "internal error: DoAATLayout called for non-AAT font\n"); |
| 105 |
exit(1); |
| 106 |
} |
| 107 |
|
| 108 |
txtLen = native_length(node); |
| 109 |
txtPtr = (UniChar*)(node + native_node_size); |
| 110 |
|
| 111 |
attributes = fontlayoutengine[native_font(node)]; |
| 112 |
string = CFStringCreateWithCharactersNoCopy(NULL, txtPtr, txtLen, kCFAllocatorNull); |
| 113 |
attrString = CFAttributedStringCreate(NULL, string, attributes); |
| 114 |
CFRelease(string); |
| 115 |
|
| 116 |
typesetter = CTTypesetterCreateWithAttributedString(attrString); |
| 117 |
CFRelease(attrString); |
| 118 |
line = CTTypesetterCreateLine(typesetter, CFRangeMake(0, txtLen)); |
| 119 |
if (justify) { |
| 120 |
CGFloat lineWidth = TeXtoPSPoints(Fix2D(node_width(node))); |
| 121 |
CTLineRef justifiedLine = CTLineCreateJustifiedLine(line, TeXtoPSPoints(Fix2D(fract1)), lineWidth); |
| 122 |
// TODO(jjgod): how to handle the case when justification failed? for |
| 123 |
// now we just fallback to use the original line. |
| 124 |
if (justifiedLine) { |
| 125 |
CFRelease(line); |
| 126 |
line = justifiedLine; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
glyphRuns = CTLineGetGlyphRuns(line); |
| 131 |
runCount = CFArrayGetCount(glyphRuns); |
| 132 |
totalGlyphCount = CTLineGetGlyphCount(line); |
| 133 |
|
| 134 |
if (totalGlyphCount > 0) { |
| 135 |
glyph_info = xmalloc(totalGlyphCount * native_glyph_info_size); |
| 136 |
locations = (FixedPoint*)glyph_info; |
| 137 |
glyphIDs = (UInt16*)(locations + totalGlyphCount); |
| 138 |
glyphAdvances = xmalloc(totalGlyphCount * sizeof(Fixed)); |
| 139 |
totalGlyphCount = 0; |
| 140 |
|
| 141 |
width = 0; |
| 142 |
for (i = 0; i < runCount; i++) { |
| 143 |
CTRunRef run = CFArrayGetValueAtIndex(glyphRuns, i); |
| 144 |
CFIndex count = CTRunGetGlyphCount(run); |
| 145 |
CFDictionaryRef runAttributes = CTRunGetAttributes(run); |
| 146 |
CFBooleanRef vertical = CFDictionaryGetValue(runAttributes, kCTVerticalFormsAttributeName); |
| 147 |
// TODO(jjgod): Avoid unnecessary allocation with CTRunGetFoosPtr(). |
| 148 |
CGGlyph* glyphs = (CGGlyph*) xmalloc(count * sizeof(CGGlyph)); |
| 149 |
CGPoint* positions = (CGPoint*) xmalloc(count * sizeof(CGPoint)); |
| 150 |
CGSize* advances = (CGSize*) xmalloc(count * sizeof(CGSize)); |
| 151 |
CGFloat runWidth = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), NULL, NULL, NULL); |
| 152 |
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs); |
| 153 |
CTRunGetPositions(run, CFRangeMake(0, 0), positions); |
| 154 |
CTRunGetAdvances(run, CFRangeMake(0, 0), advances); |
| 155 |
for (j = 0; j < count; j++) { |
| 156 |
// XXX Core Text has that font cascading thing that will do |
| 157 |
// font substitution for missing glyphs, which we do not want |
| 158 |
// but I can not find a way to disable it yet, so if the font |
| 159 |
// of the resulting run is not the same font we asked for, use |
| 160 |
// the glyph at index 0 (usually .notdef) instead or we will be |
| 161 |
// showing garbage or even invalid glyphs |
| 162 |
if (!CFEqual(fontFromAttributes(attributes), fontFromAttributes(runAttributes))) |
| 163 |
glyphIDs[totalGlyphCount] = 0; |
| 164 |
else |
| 165 |
glyphIDs[totalGlyphCount] = glyphs[j]; |
| 166 |
|
| 167 |
// Swap X and Y when doing vertical layout |
| 168 |
if (vertical == kCFBooleanTrue) { |
| 169 |
locations[totalGlyphCount].x = -FixedPStoTeXPoints(positions[j].y); |
| 170 |
locations[totalGlyphCount].y = FixedPStoTeXPoints(positions[j].x); |
| 171 |
} else { |
| 172 |
locations[totalGlyphCount].x = FixedPStoTeXPoints(positions[j].x); |
| 173 |
locations[totalGlyphCount].y = -FixedPStoTeXPoints(positions[j].y); |
| 174 |
} |
| 175 |
glyphAdvances[totalGlyphCount] = advances[j].width; |
| 176 |
totalGlyphCount++; |
| 177 |
} |
| 178 |
width += FixedPStoTeXPoints(runWidth); |
| 179 |
free(glyphs); |
| 180 |
free(positions); |
| 181 |
free(advances); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
native_glyph_count(node) = totalGlyphCount; |
| 186 |
native_glyph_info_ptr(node) = glyph_info; |
| 187 |
|
| 188 |
if (!justify) { |
| 189 |
node_width(node) = width; |
| 190 |
|
| 191 |
if (totalGlyphCount > 0) { |
| 192 |
/* this is essentially a copy from similar code in XeTeX_ext.c, easier |
| 193 |
* to be done here */ |
| 194 |
if (fontletterspace[f] != 0) { |
| 195 |
Fixed lsDelta = 0; |
| 196 |
Fixed lsUnit = fontletterspace[f]; |
| 197 |
int i; |
| 198 |
for (i = 0; i < totalGlyphCount; ++i) { |
| 199 |
if (glyphAdvances[i] == 0 && lsDelta != 0) |
| 200 |
lsDelta -= lsUnit; |
| 201 |
locations[i].x += lsDelta; |
| 202 |
lsDelta += lsUnit; |
| 203 |
} |
| 204 |
if (lsDelta != 0) { |
| 205 |
lsDelta -= lsUnit; |
| 206 |
node_width(node) += lsDelta; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
free(glyphAdvances); |
| 213 |
CFRelease(line); |
| 214 |
CFRelease(typesetter); |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
getGlyphBBoxFromCTFont(CTFontRef font, UInt16 gid, GlyphBBox* bbox) |
| 219 |
{ |
| 220 |
CGRect rect; |
| 221 |
|
| 222 |
bbox->xMin = 65536.0; |
| 223 |
bbox->yMin = 65536.0; |
| 224 |
bbox->xMax = -65536.0; |
| 225 |
bbox->yMax = -65536.0; |
| 226 |
|
| 227 |
rect = CTFontGetBoundingRectsForGlyphs(font, |
| 228 |
0, /* Use default orientation for now, handle vertical later */ |
| 229 |
(const CGGlyph *) &gid, NULL, 1); |
| 230 |
|
| 231 |
if (CGRectIsNull(rect)) |
| 232 |
bbox->xMin = bbox->yMin = bbox->xMax = bbox->yMax = 0; |
| 233 |
else { |
| 234 |
bbox->yMin = PStoTeXPoints(rect.origin.y); |
| 235 |
bbox->yMax = PStoTeXPoints(rect.origin.y + rect.size.height); |
| 236 |
bbox->xMin = PStoTeXPoints(rect.origin.x); |
| 237 |
bbox->xMax = PStoTeXPoints(rect.origin.x + rect.size.width); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void |
| 242 |
GetGlyphBBox_AAT(CFDictionaryRef attributes, UInt16 gid, GlyphBBox* bbox) |
| 243 |
/* returns glyph bounding box in TeX points */ |
| 244 |
{ |
| 245 |
CTFontRef font = fontFromAttributes(attributes); |
| 246 |
return getGlyphBBoxFromCTFont(font, gid, bbox); |
| 247 |
} |
| 248 |
|
| 249 |
static double |
| 250 |
getGlyphWidthFromCTFont(CTFontRef font, UInt16 gid) |
| 251 |
{ |
| 252 |
return PStoTeXPoints(CTFontGetAdvancesForGlyphs(font, kCTFontHorizontalOrientation, &gid, NULL, 1)); |
| 253 |
} |
| 254 |
|
| 255 |
double |
| 256 |
GetGlyphWidth_AAT(CFDictionaryRef attributes, UInt16 gid) |
| 257 |
/* returns TeX points */ |
| 258 |
{ |
| 259 |
CTFontRef font = fontFromAttributes(attributes); |
| 260 |
return getGlyphWidthFromCTFont(font, gid); |
| 261 |
} |
| 262 |
|
| 263 |
void |
| 264 |
GetGlyphHeightDepth_AAT(CFDictionaryRef attributes, UInt16 gid, float* ht, float* dp) |
| 265 |
/* returns TeX points */ |
| 266 |
{ |
| 267 |
GlyphBBox bbox; |
| 268 |
|
| 269 |
GetGlyphBBox_AAT(attributes, gid, &bbox); |
| 270 |
|
| 271 |
*ht = bbox.yMax; |
| 272 |
*dp = -bbox.yMin; |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
GetGlyphSidebearings_AAT(CFDictionaryRef attributes, UInt16 gid, float* lsb, float* rsb) |
| 277 |
/* returns TeX points */ |
| 278 |
{ |
| 279 |
CTFontRef font = fontFromAttributes(attributes); |
| 280 |
CGSize advances[1] = { CGSizeMake(0, 0) }; |
| 281 |
double advance = CTFontGetAdvancesForGlyphs(font, 0, &gid, advances, 1); |
| 282 |
GlyphBBox bbox; |
| 283 |
getGlyphBBoxFromCTFont(font, gid, &bbox); |
| 284 |
*lsb = bbox.xMin; |
| 285 |
*rsb = PStoTeXPoints(advance) - bbox.xMax; |
| 286 |
} |
| 287 |
|
| 288 |
double |
| 289 |
GetGlyphItalCorr_AAT(CFDictionaryRef attributes, UInt16 gid) |
| 290 |
{ |
| 291 |
CTFontRef font = fontFromAttributes(attributes); |
| 292 |
CGSize advances[1] = { CGSizeMake(0, 0) }; |
| 293 |
double advance = CTFontGetAdvancesForGlyphs(font, 0, &gid, advances, 1); |
| 294 |
|
| 295 |
GlyphBBox bbox; |
| 296 |
getGlyphBBoxFromCTFont(font, gid, &bbox); |
| 297 |
|
| 298 |
if (bbox.xMax > PStoTeXPoints(advance)) |
| 299 |
return bbox.xMax - PStoTeXPoints(advance); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
static int |
| 304 |
mapCharToGlyphFromCTFont(CTFontRef font, UInt32 ch) |
| 305 |
{ |
| 306 |
CGGlyph glyphs[2] = { 0 }; |
| 307 |
UniChar txt[2]; |
| 308 |
int len = 1; |
| 309 |
|
| 310 |
if (ch > 0xffff) { |
| 311 |
ch -= 0x10000; |
| 312 |
txt[0] = 0xd800 + ch / 1024; |
| 313 |
txt[1] = 0xdc00 + ch % 1024; |
| 314 |
len = 2; |
| 315 |
} else { |
| 316 |
txt[0] = ch; |
| 317 |
} |
| 318 |
|
| 319 |
if (CTFontGetGlyphsForCharacters(font, txt, glyphs, len)) |
| 320 |
return glyphs[0]; |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
int |
| 326 |
MapCharToGlyph_AAT(CFDictionaryRef attributes, UInt32 ch) |
| 327 |
{ |
| 328 |
CTFontRef font = fontFromAttributes(attributes); |
| 329 |
return mapCharToGlyphFromCTFont(font, ch); |
| 330 |
} |
| 331 |
|
| 332 |
static int |
| 333 |
GetGlyphIDFromCTFont(CTFontRef ctFontRef, const char* glyphName) |
| 334 |
{ |
| 335 |
CFStringRef glyphname = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, |
| 336 |
glyphName, |
| 337 |
kCFStringEncodingUTF8, |
| 338 |
kCFAllocatorNull); |
| 339 |
int rval = CTFontGetGlyphWithName(ctFontRef, glyphname); |
| 340 |
CFRelease(glyphname); |
| 341 |
return rval; |
| 342 |
} |
| 343 |
|
| 344 |
int |
| 345 |
MapGlyphToIndex_AAT(CFDictionaryRef attributes, const char* glyphName) |
| 346 |
{ |
| 347 |
CTFontRef font = fontFromAttributes(attributes); |
| 348 |
return GetGlyphIDFromCTFont(font, glyphName); |
| 349 |
} |
| 350 |
|
| 351 |
char* |
| 352 |
GetGlyphNameFromCTFont(CTFontRef ctFontRef, UInt16 gid, int* len) |
| 353 |
{ |
| 354 |
CGFontRef cgfont; |
| 355 |
static char buffer[256]; |
| 356 |
buffer[0] = 0; |
| 357 |
*len = 0; |
| 358 |
|
| 359 |
cgfont = CTFontCopyGraphicsFont(ctFontRef, 0); |
| 360 |
if (cgfont && gid < CGFontGetNumberOfGlyphs(cgfont)) { |
| 361 |
CFStringRef glyphname = CGFontCopyGlyphNameForGlyph(cgfont, gid); |
| 362 |
if (glyphname) { |
| 363 |
if (CFStringGetCString(glyphname, buffer, 256, kCFStringEncodingUTF8)) { |
| 364 |
*len = strlen(buffer); |
| 365 |
} |
| 366 |
CFRelease(glyphname); |
| 367 |
} |
| 368 |
CGFontRelease(cgfont); |
| 369 |
} |
| 370 |
|
| 371 |
return &buffer[0]; |
| 372 |
} |
| 373 |
|
| 374 |
int |
| 375 |
GetFontCharRange_AAT(CFDictionaryRef attributes, int reqFirst) |
| 376 |
{ |
| 377 |
if (reqFirst) { |
| 378 |
int ch = 0; |
| 379 |
while (MapCharToGlyph_AAT(attributes, ch) == 0 && ch < 0x10ffff) |
| 380 |
++ch; |
| 381 |
return ch; |
| 382 |
} else { |
| 383 |
int ch = 0x10ffff; |
| 384 |
while (MapCharToGlyph_AAT(attributes, ch) == 0 && ch > 0) |
| 385 |
--ch; |
| 386 |
return ch; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
char* |
| 391 |
getNameFromCTFont(CTFontRef ctFontRef, CFStringRef nameKey) |
| 392 |
{ |
| 393 |
char *buf; |
| 394 |
CFStringRef name = CTFontCopyName(ctFontRef, nameKey); |
| 395 |
CFIndex len = CFStringGetLength(name); |
| 396 |
len = len * 6 + 1; |
| 397 |
buf = xmalloc(len); |
| 398 |
if (CFStringGetCString(name, buf, len, kCFStringEncodingUTF8)) |
| 399 |
return buf; |
| 400 |
free(buf); |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
char* |
| 405 |
getFileNameFromCTFont(CTFontRef ctFontRef, uint32_t *index) |
| 406 |
{ |
| 407 |
char *ret = NULL; |
| 408 |
CFURLRef url = NULL; |
| 409 |
|
| 410 |
#if !defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6 |
| 411 |
/* kCTFontURLAttribute was not avialable before 10.6 */ |
| 412 |
ATSFontRef atsFont; |
| 413 |
FSRef fsref; |
| 414 |
OSStatus status; |
| 415 |
atsFont = CTFontGetPlatformFont(ctFontRef, NULL); |
| 416 |
status = ATSFontGetFileReference(atsFont, &fsref); |
| 417 |
if (status == noErr) |
| 418 |
url = CFURLCreateFromFSRef(NULL, &fsref); |
| 419 |
#else |
| 420 |
url = (CFURLRef) CTFontCopyAttribute(ctFontRef, kCTFontURLAttribute); |
| 421 |
#endif |
| 422 |
if (url) { |
| 423 |
UInt8 pathname[PATH_MAX]; |
| 424 |
if (CFURLGetFileSystemRepresentation(url, true, pathname, PATH_MAX)) { |
| 425 |
FT_Error error; |
| 426 |
FT_Face face; |
| 427 |
|
| 428 |
*index = 0; |
| 429 |
|
| 430 |
if (!gFreeTypeLibrary) { |
| 431 |
error = FT_Init_FreeType(&gFreeTypeLibrary); |
| 432 |
if (error) { |
| 433 |
fprintf(stderr, "FreeType initialization failed! (%d)\n", error); |
| 434 |
exit(1); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
error = FT_New_Face(gFreeTypeLibrary, (char *) pathname, 0, &face); |
| 439 |
if (!error) { |
| 440 |
if (face->num_faces > 1) { |
| 441 |
int num_faces = face->num_faces; |
| 442 |
char *ps_name1 = getNameFromCTFont(ctFontRef, kCTFontPostScriptNameKey); |
| 443 |
int i; |
| 444 |
*index = -1; |
| 445 |
FT_Done_Face (face); |
| 446 |
for (i = 0; i < num_faces; i++) { |
| 447 |
error = FT_New_Face (gFreeTypeLibrary, (char *) pathname, i, &face); |
| 448 |
if (!error) { |
| 449 |
const char *ps_name2 = FT_Get_Postscript_Name(face); |
| 450 |
if (strcmp(ps_name1, ps_name2) == 0) { |
| 451 |
*index = i; |
| 452 |
break; |
| 453 |
} |
| 454 |
FT_Done_Face (face); |
| 455 |
} |
| 456 |
} |
| 457 |
free(ps_name1); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
if (*index != -1) |
| 462 |
ret = strdup((char *) pathname); |
| 463 |
} |
| 464 |
CFRelease(url); |
| 465 |
} |
| 466 |
|
| 467 |
return ret; |
| 468 |
} |
| 469 |
|
| 470 |
CFDictionaryRef |
| 471 |
findDictionaryInArrayWithIdentifier(CFArrayRef array, const void* identifierKey, int identifier) |
| 472 |
{ |
| 473 |
CFDictionaryRef dict = NULL; |
| 474 |
|
| 475 |
if (array) { |
| 476 |
int value = -1; |
| 477 |
CFIndex i; |
| 478 |
for (i = 0; i < CFArrayGetCount(array); i++) { |
| 479 |
CFDictionaryRef item = CFArrayGetValueAtIndex(array, i); |
| 480 |
CFNumberRef itemId = CFDictionaryGetValue(item, identifierKey); |
| 481 |
if (itemId) { |
| 482 |
CFNumberGetValue(itemId, kCFNumberIntType, &value); |
| 483 |
if (value == identifier) { |
| 484 |
dict = item; |
| 485 |
break; |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
return dict; |
| 491 |
} |
| 492 |
|
| 493 |
CFDictionaryRef |
| 494 |
findDictionaryInArray(CFArrayRef array, const void* nameKey, const char* name, int nameLength) |
| 495 |
{ |
| 496 |
CFDictionaryRef dict = NULL; |
| 497 |
|
| 498 |
if (array) { |
| 499 |
CFStringRef itemName; |
| 500 |
CFIndex i; |
| 501 |
itemName = CFStringCreateWithBytes(NULL, (UInt8 *) name, nameLength, |
| 502 |
kCFStringEncodingUTF8, false); |
| 503 |
for (i = 0; i < CFArrayGetCount(array); i++) { |
| 504 |
CFDictionaryRef item = CFArrayGetValueAtIndex(array, i); |
| 505 |
CFStringRef iName = CFDictionaryGetValue(item, nameKey); |
| 506 |
if (iName && !CFStringCompare(itemName, iName, kCFCompareCaseInsensitive)) { |
| 507 |
dict = item; |
| 508 |
break; |
| 509 |
} |
| 510 |
} |
| 511 |
CFRelease(itemName); |
| 512 |
} |
| 513 |
return dict; |
| 514 |
} |
| 515 |
|
| 516 |
CFNumberRef |
| 517 |
findSelectorByName(CFDictionaryRef feature, const char* name, int nameLength) |
| 518 |
{ |
| 519 |
CFNumberRef selector = NULL; |
| 520 |
CFArrayRef selectors = CFDictionaryGetValue(feature, kCTFontFeatureTypeSelectorsKey); |
| 521 |
if (selectors) { |
| 522 |
CFDictionaryRef s = findDictionaryInArray(selectors, kCTFontFeatureSelectorNameKey, name, nameLength); |
| 523 |
if (s) |
| 524 |
selector = CFDictionaryGetValue(s, kCTFontFeatureSelectorIdentifierKey); |
| 525 |
} |
| 526 |
return selector; |
| 527 |
} |
| 528 |
|
| 529 |
static CFDictionaryRef |
| 530 |
createFeatureSettingDictionary(CFNumberRef featureTypeIdentifier, CFNumberRef featureSelectorIdentifier) |
| 531 |
{ |
| 532 |
const void* settingKeys[] = { kCTFontFeatureTypeIdentifierKey, kCTFontFeatureSelectorIdentifierKey }; |
| 533 |
const void* settingValues[] = { featureTypeIdentifier, featureSelectorIdentifier }; |
| 534 |
|
| 535 |
return CFDictionaryCreate(kCFAllocatorDefault, settingKeys, settingValues, 2, |
| 536 |
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
| 537 |
} |
| 538 |
|
| 539 |
const CFStringRef kXeTeXEmboldenAttributeName = CFSTR("XeTeXEmbolden"); |
| 540 |
|
| 541 |
void* |
| 542 |
loadAATfont(CTFontDescriptorRef descriptor, integer scaled_size, const char* cp1) |
| 543 |
{ |
| 544 |
CTFontRef font, actualFont; |
| 545 |
CGFloat ctSize; |
| 546 |
CFMutableDictionaryRef stringAttributes, attributes; |
| 547 |
CGAffineTransform matrix; |
| 548 |
CFMutableArrayRef cascadeList; |
| 549 |
CTFontDescriptorRef lastResort; |
| 550 |
double tracking = 0.0; |
| 551 |
float extend = 1.0; |
| 552 |
float slant = 0.0; |
| 553 |
float embolden = 0.0; |
| 554 |
float letterspace = 0.0; |
| 555 |
uint32_t rgbValue; |
| 556 |
|
| 557 |
// create a base font instance for applying further attributes |
| 558 |
ctSize = TeXtoPSPoints(Fix2D(scaled_size)); |
| 559 |
font = CTFontCreateWithFontDescriptor(descriptor, ctSize, NULL); |
| 560 |
if (!font) |
| 561 |
return NULL; |
| 562 |
|
| 563 |
stringAttributes = CFDictionaryCreateMutable(NULL, 0, |
| 564 |
&kCFTypeDictionaryKeyCallBacks, |
| 565 |
&kCFTypeDictionaryValueCallBacks); |
| 566 |
attributes = CFDictionaryCreateMutable(NULL, 0, |
| 567 |
&kCFTypeDictionaryKeyCallBacks, |
| 568 |
&kCFTypeDictionaryValueCallBacks); |
| 569 |
if (cp1) { |
| 570 |
CFArrayRef features = CTFontCopyFeatures(font); |
| 571 |
CFMutableArrayRef featureSettings = |
| 572 |
CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 573 |
|
| 574 |
// interpret features following ":" |
| 575 |
while (*cp1) { |
| 576 |
CFDictionaryRef feature; |
| 577 |
int ret; |
| 578 |
const char* cp2; |
| 579 |
const char* cp3; |
| 580 |
// locate beginning of name=value pair |
| 581 |
if (*cp1 == ':' || *cp1 == ';') // skip over separator |
| 582 |
++cp1; |
| 583 |
while (*cp1 == ' ' || *cp1 == '\t') // skip leading whitespace |
| 584 |
++cp1; |
| 585 |
if (*cp1 == 0) // break if end of string |
| 586 |
break; |
| 587 |
|
| 588 |
// scan to end of pair |
| 589 |
cp2 = cp1; |
| 590 |
while (*cp2 && (*cp2 != ';') && (*cp2 != ':')) |
| 591 |
++cp2; |
| 592 |
|
| 593 |
// look for the '=' separator |
| 594 |
cp3 = cp1; |
| 595 |
while ((cp3 < cp2) && (*cp3 != '=')) |
| 596 |
++cp3; |
| 597 |
if (cp3 == cp2) |
| 598 |
goto bad_option; |
| 599 |
|
| 600 |
// now cp1 points to option name, cp3 to '=', cp2 to ';' or null |
| 601 |
|
| 602 |
// first try for a feature by this name |
| 603 |
feature = findDictionaryInArray(features, kCTFontFeatureTypeNameKey, cp1, cp3 - cp1); |
| 604 |
if (feature) { |
| 605 |
// look past the '=' separator for setting names |
| 606 |
int featLen = cp3 - cp1; |
| 607 |
int zeroInteger = 0; |
| 608 |
CFNumberRef zero = CFNumberCreate(NULL, kCFNumberIntType, &zeroInteger); |
| 609 |
++cp3; |
| 610 |
while (cp3 < cp2) { |
| 611 |
CFNumberRef selector; |
| 612 |
int disable = 0; |
| 613 |
const char* cp4; |
| 614 |
// skip leading whitespace |
| 615 |
while (*cp3 == ' ' || *cp3 == '\t') |
| 616 |
++cp3; |
| 617 |
|
| 618 |
// possibly multiple settings... |
| 619 |
if (*cp3 == '!') { // check for negation |
| 620 |
disable = 1; |
| 621 |
++cp3; |
| 622 |
} |
| 623 |
|
| 624 |
// scan for end of setting name |
| 625 |
cp4 = cp3; |
| 626 |
while (cp4 < cp2 && *cp4 != ',') |
| 627 |
++cp4; |
| 628 |
|
| 629 |
// now cp3 points to name, cp4 to ',' or ';' or null |
| 630 |
selector = findSelectorByName(feature, cp3, cp4 - cp3); |
| 631 |
if (selector && CFNumberCompare(selector, zero, NULL) >= 0) { |
| 632 |
CFNumberRef featureType = CFDictionaryGetValue(feature, kCTFontFeatureTypeIdentifierKey); |
| 633 |
CFDictionaryRef featureSetting = createFeatureSettingDictionary(featureType, selector); |
| 634 |
CFArrayAppendValue(featureSettings, featureSetting); |
| 635 |
CFRelease(featureSetting); |
| 636 |
} else { |
| 637 |
fontfeaturewarning(cp1, featLen, cp3, cp4 - cp3); |
| 638 |
} |
| 639 |
|
| 640 |
// point beyond setting name terminator |
| 641 |
cp3 = cp4 + 1; |
| 642 |
} |
| 643 |
CFRelease(zero); |
| 644 |
|
| 645 |
goto next_option; |
| 646 |
} |
| 647 |
|
| 648 |
// didn't find feature, try other options... |
| 649 |
ret = readCommonFeatures(cp1, cp2, &extend, &slant, &embolden, &letterspace, &rgbValue); |
| 650 |
if (ret == 1) |
| 651 |
goto next_option; |
| 652 |
else if (ret == -1) |
| 653 |
goto bad_option; |
| 654 |
|
| 655 |
if (strncmp(cp1, "tracking", 8) == 0) { |
| 656 |
CFNumberRef trackingNumber; |
| 657 |
cp3 = cp1 + 8; |
| 658 |
if (*cp3 != '=') |
| 659 |
goto bad_option; |
| 660 |
++cp3; |
| 661 |
tracking = read_double(&cp3); |
| 662 |
trackingNumber = CFNumberCreate(NULL, kCFNumberDoubleType, &tracking); |
| 663 |
CFDictionaryAddValue(stringAttributes, kCTKernAttributeName, trackingNumber); |
| 664 |
CFRelease(trackingNumber); |
| 665 |
goto next_option; |
| 666 |
} |
| 667 |
|
| 668 |
bad_option: |
| 669 |
// not a name=value pair, or not recognized.... |
| 670 |
// check for plain "vertical" before complaining |
| 671 |
if (strncmp(cp1, "vertical", 8) == 0) { |
| 672 |
cp3 = cp2; |
| 673 |
if (*cp3 == ';' || *cp3 == ':') |
| 674 |
--cp3; |
| 675 |
while (*cp3 == '\0' || *cp3 == ' ' || *cp3 == '\t') |
| 676 |
--cp3; |
| 677 |
if (*cp3) |
| 678 |
++cp3; |
| 679 |
if (cp3 == cp1 + 8) { |
| 680 |
int orientation = kCTFontVerticalOrientation; |
| 681 |
CFNumberRef orientationNumber = CFNumberCreate(NULL, kCFNumberIntType, &orientation); |
| 682 |
CFDictionaryAddValue(attributes, kCTFontOrientationAttribute, orientationNumber); |
| 683 |
CFRelease(orientationNumber); |
| 684 |
CFDictionaryAddValue(stringAttributes, kCTVerticalFormsAttributeName, kCFBooleanTrue); |
| 685 |
goto next_option; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
fontfeaturewarning(cp1, cp2 - cp1, 0, 0); |
| 690 |
|
| 691 |
next_option: |
| 692 |
// go to next name=value pair |
| 693 |
cp1 = cp2; |
| 694 |
} |
| 695 |
|
| 696 |
if (features) |
| 697 |
CFRelease(features); |
| 698 |
|
| 699 |
if (CFArrayGetCount(featureSettings)) |
| 700 |
CFDictionaryAddValue(attributes, kCTFontFeatureSettingsAttribute, featureSettings); |
| 701 |
CFRelease(featureSettings); |
| 702 |
} |
| 703 |
|
| 704 |
if ((loadedfontflags & FONT_FLAGS_COLORED) != 0) { |
| 705 |
CGFloat red = ((rgbValue & 0xFF000000) >> 24) / 255.0; |
| 706 |
CGFloat green = ((rgbValue & 0x00FF0000) >> 16) / 255.0; |
| 707 |
CGFloat blue = ((rgbValue & 0x0000FF00) >> 8 ) / 255.0; |
| 708 |
CGFloat alpha = ((rgbValue & 0x000000FF) ) / 255.0; |
| 709 |
CGColorRef color = CGColorCreateGenericRGB(red, green, blue, alpha); |
| 710 |
CFDictionaryAddValue(stringAttributes, kCTForegroundColorAttributeName, color); |
| 711 |
CGColorRelease(color); |
| 712 |
} |
| 713 |
|
| 714 |
matrix = CGAffineTransformIdentity; |
| 715 |
if (extend != 1.0 || slant != 0.0) |
| 716 |
matrix = CGAffineTransformMake(extend, 0, slant, 1.0, 0, 0); |
| 717 |
|
| 718 |
if (embolden != 0.0) { |
| 719 |
CFNumberRef emboldenNumber; |
| 720 |
embolden = embolden * Fix2D(scaled_size) / 100.0; |
| 721 |
emboldenNumber = CFNumberCreate(NULL, kCFNumberFloatType, &embolden); |
| 722 |
CFDictionaryAddValue(stringAttributes, kXeTeXEmboldenAttributeName, emboldenNumber); |
| 723 |
CFRelease(emboldenNumber); |
| 724 |
} |
| 725 |
|
| 726 |
if (letterspace != 0.0) |
| 727 |
loadedfontletterspace = (letterspace / 100.0) * scaled_size; |
| 728 |
|
| 729 |
// Disable Core Text font fallback (cascading) with only the last resort font |
| 730 |
// in the cascade list. |
| 731 |
cascadeList = CFArrayCreateMutable(NULL, 1, &kCFTypeArrayCallBacks); |
| 732 |
lastResort = CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0); |
| 733 |
CFArrayAppendValue(cascadeList, lastResort); |
| 734 |
CFRelease(lastResort); |
| 735 |
CFDictionaryAddValue(attributes, kCTFontCascadeListAttribute, cascadeList); |
| 736 |
CFRelease(cascadeList); |
| 737 |
|
| 738 |
descriptor = CTFontDescriptorCreateWithAttributes(attributes); |
| 739 |
CFRelease(attributes); |
| 740 |
actualFont = CTFontCreateCopyWithAttributes(font, ctSize, &matrix, descriptor); |
| 741 |
CFRelease(font); |
| 742 |
CFDictionaryAddValue(stringAttributes, kCTFontAttributeName, actualFont); |
| 743 |
CFRelease(actualFont); |
| 744 |
|
| 745 |
nativefonttypeflag = AAT_FONT_FLAG; |
| 746 |
return (void *) stringAttributes; |
| 747 |
} |