Debian Codegraph @

texk/web2c/xetexdir/XeTeXFontMgr.cpp

raw
1 /****************************************************************************\
2 Part of the XeTeX typesetting system
3 Copyright (c) 1994-2008 by SIL International
4 Copyright (c) 2009-2014 by Jonathan Kew
5
6 SIL Author(s): Jonathan Kew
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
23 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 Except as contained in this notice, the name of the copyright holders
28 shall not be used in advertising or otherwise to promote the sale,
29 use or other dealings in this Software without prior written
30 authorization from the copyright holders.
31 \****************************************************************************/
32
33 #include <w2c/config.h>
34
35 #include "XeTeX_web.h"
36
37 #ifdef XETEX_MAC
38 #include "XeTeXFontMgr_Mac.h"
39 #else
40 #include "XeTeXFontMgr_FC.h"
41 #endif
42 #include "XeTeXFontInst.h"
43
44 #include <hb-ot.h>
45
46 // see cpascal.h
47 #define printcstring(STR) \
48 do { \
49 const char* ch_ptr = (STR); \
50 while (*ch_ptr) \
51 zprintchar(*(ch_ptr++)); \
52 } while (0)
53
54 XeTeXFontMgr* XeTeXFontMgr::sFontManager = NULL;
55 char XeTeXFontMgr::sReqEngine = 0;
56
57 /* use our own fmax function because it seems to be missing on certain platforms
58 (solaris2.9, at least) */
59 static inline double
60 my_fmax(double x, double y)
61 {
62 return (x > y) ? x : y;
63 }
64
65 XeTeXFontMgr*
66 XeTeXFontMgr::GetFontManager()
67 {
68 if (sFontManager == NULL) {
69 #ifdef XETEX_MAC
70 sFontManager = new XeTeXFontMgr_Mac;
71 #else
72 sFontManager = new XeTeXFontMgr_FC;
73 #endif
74 sFontManager->initialize();
75 }
76
77 return sFontManager;
78 }
79
80 void
81 XeTeXFontMgr::Terminate()
82 {
83 if (sFontManager != NULL) {
84 sFontManager->terminate();
85 // we don't actually deallocate the manager, just ask it to clean up
86 // any auxiliary data such as the cocoa pool or freetype/fontconfig stuff
87 // as we still need to access font names after this is called
88 }
89 }
90
91 PlatformFontRef
92 XeTeXFontMgr::findFont(const char* name, char* variant, double ptSize)
93 // ptSize is in TeX points, or negative for 'scaled' factor
94 // "variant" string will be shortened (in-place) by removal of /B and /I if present
95 {
96 std::string nameStr(name);
97 Font* font = NULL;
98 double dsize = 10.0;
99 loadedfontdesignsize = 655360L;
100
101 for (int pass = 0; pass < 2; ++pass) {
102 // try full name as given
103 std::map<std::string,Font*>::iterator i = m_nameToFont.find(nameStr);
104 if (i != m_nameToFont.end()) {
105 font = i->second;
106 if (font->opSizeInfo.designSize != 0.0)
107 dsize = font->opSizeInfo.designSize;
108 break;
109 }
110
111 // if there's a hyphen, split there and try Family-Style
112 int hyph = nameStr.find('-');
113 if (hyph > 0 && hyph < nameStr.length() - 1) {
114 std::string family(nameStr.begin(), nameStr.begin() + hyph);
115 std::map<std::string,Family*>::iterator f = m_nameToFamily.find(family);
116 if (f != m_nameToFamily.end()) {
117 std::string style(nameStr.begin() + hyph + 1, nameStr.end());
118 i = f->second->styles->find(style);
119 if (i != f->second->styles->end()) {
120 font = i->second;
121 if (font->opSizeInfo.designSize != 0.0)
122 dsize = font->opSizeInfo.designSize;
123 break;
124 }
125 }
126 }
127
128 // try as PostScript name
129 i = m_psNameToFont.find(nameStr);
130 if (i != m_psNameToFont.end()) {
131 font = i->second;
132 if (font->opSizeInfo.designSize != 0.0)
133 dsize = font->opSizeInfo.designSize;
134 break;
135 }
136
137 // try for the name as a family name
138 std::map<std::string,Family*>::iterator f = m_nameToFamily.find(nameStr);
139
140 if (f != m_nameToFamily.end()) {
141 // look for a family member with the "regular" bit set in OS/2
142 int regFonts = 0;
143 for (i = f->second->styles->begin(); i != f->second->styles->end(); ++i)
144 if (i->second->isReg) {
145 if (regFonts == 0)
146 font = i->second;
147 ++regFonts;
148 }
149
150 // families with Ornament or similar fonts may flag those as Regular,
151 // which confuses the search above... so try some known names
152 if (font == NULL || regFonts > 1) {
153 // try for style "Regular", "Plain", "Normal", "Roman"
154 i = f->second->styles->find("Regular");
155 if (i != f->second->styles->end())
156 font = i->second;
157 else {
158 i = f->second->styles->find("Plain");
159 if (i != f->second->styles->end())
160 font = i->second;
161 else {
162 i = f->second->styles->find("Normal");
163 if (i != f->second->styles->end())
164 font = i->second;
165 else {
166 i = f->second->styles->find("Roman");
167 if (i != f->second->styles->end())
168 font = i->second;
169 }
170 }
171 }
172 }
173
174 if (font == NULL) {
175 // look through the family for the (weight, width, slant) nearest to (80, 100, 0)
176 font = bestMatchFromFamily(f->second, 80, 100, 0);
177 }
178
179 if (font != NULL)
180 break;
181 }
182
183 if (pass == 0) {
184 // didn't find it in our caches, so do a platform search (may be relatively expensive);
185 // this will update the caches with any fonts that seem to match the name given,
186 // so that the second pass might find it
187 searchForHostPlatformFonts(nameStr);
188 }
189 }
190
191 if (font == NULL)
192 return 0;
193
194 Family* parent = font->parent;
195
196 // if there are variant requests, try to apply them
197 // and delete B, I, and S=... codes from the string, just retain /engine option
198 sReqEngine = 0;
199 bool reqBold = false;
200 bool reqItal = false;
201 if (variant != NULL) {
202 std::string varString;
203 char* cp = variant;
204 while (*cp) {
205 if (strncmp(cp, "AAT", 3) == 0) {
206 sReqEngine = 'A';
207 cp += 3;
208 if (varString.length() > 0 && *(varString.end() - 1) != '/')
209 varString.append("/");
210 varString.append("AAT");
211 goto skip_to_slash;
212 }
213 if (strncmp(cp, "ICU", 3) == 0) { // for backword compatability
214 sReqEngine = 'O';
215 cp += 3;
216 if (varString.length() > 0 && *(varString.end() - 1) != '/')
217 varString.append("/");
218 varString.append("OT");
219 goto skip_to_slash;
220 }
221 if (strncmp(cp, "OT", 2) == 0) {
222 sReqEngine = 'O';
223 cp += 2;
224 if (varString.length() > 0 && *(varString.end() - 1) != '/')
225 varString.append("/");
226 varString.append("OT");
227 goto skip_to_slash;
228 }
229 if (strncmp(cp, "GR", 2) == 0) {
230 sReqEngine = 'G';
231 cp += 2;
232 if (varString.length() > 0 && *(varString.end() - 1) != '/')
233 varString.append("/");
234 varString.append("GR");
235 goto skip_to_slash;
236 }
237 if (*cp == 'S') {
238 ++cp;
239 if (*cp == '=')
240 ++cp;
241 ptSize = 0.0;
242 while (*cp >= '0' && *cp <= '9') {
243 ptSize = ptSize * 10 + *cp - '0';
244 ++cp;
245 }
246 if (*cp == '.') {
247 double dec = 1.0;
248 ++cp;
249 while (*cp >= '0' && *cp <= '9') {
250 dec = dec * 10.0;
251 ptSize = ptSize + (*cp - '0') / dec;
252 ++cp;
253 }
254 }
255 goto skip_to_slash;
256 }
257
258 /* if the code is "B" or "I", we skip putting it in varString */
259 while (1) {
260 if (*cp == 'B') {
261 reqBold = true;
262 ++cp;
263 continue;
264 }
265 if (*cp == 'I') {
266 reqItal = true;
267 ++cp;
268 continue;
269 }
270 break;
271 }
272
273 skip_to_slash:
274 while (*cp && *cp != '/')
275 ++cp;
276 if (*cp == '/')
277 ++cp;
278 }
279 strcpy(variant, varString.c_str());
280
281 std::map<std::string,Font*>::iterator i;
282 if (reqItal) {
283 Font* bestMatch = font;
284 if (font->slant < parent->maxSlant)
285 // try for a face with more slant
286 bestMatch = bestMatchFromFamily(parent, font->weight, font->width, parent->maxSlant);
287
288 if (bestMatch == font && font->slant > parent->minSlant)
289 // maybe the slant is negated, or maybe this was something like "Times-Italic/I"
290 bestMatch = bestMatchFromFamily(parent, font->weight, font->width, parent->minSlant);
291
292 if (parent->minWeight == parent->maxWeight && bestMatch->isBold != font->isBold) {
293 // try again using the bold flag, as we can't trust weight values
294 Font* newBest = NULL;
295 for (i = parent->styles->begin(); i != parent->styles->end(); ++i) {
296 if (i->second->isBold == font->isBold) {
297 if (newBest == NULL && i->second->isItalic != font->isItalic) {
298 newBest = i->second;
299 break;
300 }
301 }
302 }
303 if (newBest != NULL)
304 bestMatch = newBest;
305 }
306
307 if (bestMatch == font) {
308 // maybe slant values weren't present; try the style bits as a fallback
309 bestMatch = NULL;
310 for (i = parent->styles->begin(); i != parent->styles->end(); ++i) {
311 if (i->second->isItalic == !font->isItalic) {
312 if (parent->minWeight != parent->maxWeight) {
313 // weight info was available, so try to match that
314 if (bestMatch == NULL || weightAndWidthDiff(i->second, font) < weightAndWidthDiff(bestMatch, font))
315 bestMatch = i->second;
316 } else {
317 // no weight info, so try matching style bits
318 if (bestMatch == NULL && i->second->isBold == font->isBold) {
319 bestMatch = i->second;
320 break; // found a match, no need to look further as we can't distinguish!
321 }
322 }
323 }
324 }
325 }
326 if (bestMatch != NULL)
327 font = bestMatch;
328 }
329
330 if (reqBold) {
331 // try for more boldness, with the same width and slant
332 Font* bestMatch = font;
333 if (font->weight < parent->maxWeight) {
334 // try to increase weight by 1/2 x (max - min), rounding up
335 bestMatch = bestMatchFromFamily(parent,
336 font->weight + (parent->maxWeight - parent->minWeight) / 2 + 1,
337 font->width, font->slant);
338 if (parent->minSlant == parent->maxSlant) {
339 // double-check the italic flag, as we can't trust slant values
340 Font* newBest = NULL;
341 for (i = parent->styles->begin(); i != parent->styles->end(); ++i) {
342 if (i->second->isItalic == font->isItalic) {
343 if (newBest == NULL || weightAndWidthDiff(i->second, bestMatch) < weightAndWidthDiff(newBest, bestMatch))
344 newBest = i->second;
345 }
346 }
347 if (newBest != NULL)
348 bestMatch = newBest;
349 }
350 }
351 if (bestMatch == font && !font->isBold) {
352 for (i = parent->styles->begin(); i != parent->styles->end(); ++i) {
353 if (i->second->isItalic == font->isItalic && i->second->isBold) {
354 bestMatch = i->second;
355 break;
356 }
357 }
358 }
359 font = bestMatch;
360 }
361 }
362
363 // if there's optical size info, try to apply it
364 if (ptSize < 0.0)
365 ptSize = dsize;
366 if (font != NULL && font->opSizeInfo.subFamilyID != 0 && ptSize > 0.0) {
367 double bestMismatch = my_fmax(font->opSizeInfo.minSize - ptSize, ptSize - font->opSizeInfo.maxSize);
368 if (bestMismatch > 0.0) {
369 Font* bestMatch = font;
370 for (std::map<std::string,Font*>::iterator i = parent->styles->begin(); i != parent->styles->end(); ++i) {
371 if (i->second->opSizeInfo.subFamilyID != font->opSizeInfo.subFamilyID)
372 continue;
373 double mismatch = my_fmax(i->second->opSizeInfo.minSize - ptSize, ptSize - i->second->opSizeInfo.maxSize);
374 if (mismatch < bestMismatch) {
375 bestMatch = i->second;
376 bestMismatch = mismatch;
377 }
378 if (bestMismatch <= 0.0)
379 break;
380 }
381 font = bestMatch;
382 }
383 }
384
385 if (font != NULL && font->opSizeInfo.designSize != 0.0)
386 loadedfontdesignsize = unsigned(font->opSizeInfo.designSize * 65536.0 + 0.5);
387
388 if (gettracingfontsstate() > 0) {
389 begindiagnostic();
390 zprintnl(' ');
391 printcstring("-> ");
392 printcstring(getPlatformFontDesc(font->fontRef).c_str());
393 zenddiagnostic(0);
394 }
395
396 return font->fontRef;
397 }
398
399 const char*
400 XeTeXFontMgr::getFullName(PlatformFontRef font) const
401 {
402 std::map<PlatformFontRef,Font*>::const_iterator i = m_platformRefToFont.find(font);
403 if (i == m_platformRefToFont.end())
404 die("internal error %d in XeTeXFontMgr", 2);
405 if (i->second->m_fullName != NULL)
406 return i->second->m_fullName->c_str();
407 else
408 return i->second->m_psName->c_str();
409 }
410
411 int
412 XeTeXFontMgr::weightAndWidthDiff(const Font* a, const Font* b) const
413 {
414 if (a->weight == 0 && a->width == 0) {
415 // assume there was no OS/2 info
416 if (a->isBold == b->isBold)
417 return 0;
418 else
419 return 10000;
420 }
421
422 int widDiff = labs(a->width - b->width);
423 if (widDiff < 10)
424 widDiff *= 50;
425
426 return labs(a->weight - b->weight) + widDiff;
427 }
428
429 int
430 XeTeXFontMgr::styleDiff(const Font* a, int wt, int wd, int slant) const
431 {
432 int widDiff = labs(a->width - wd);
433 if (widDiff < 10)
434 widDiff *= 200;
435
436 return labs(labs(a->slant) - labs(slant)) * 2 + labs(a->weight - wt) + widDiff;
437 }
438
439 XeTeXFontMgr::Font*
440 XeTeXFontMgr::bestMatchFromFamily(const Family* fam, int wt, int wd, int slant) const
441 {
442 Font* bestMatch = NULL;
443 for (std::map<std::string,Font*>::iterator s = fam->styles->begin(); s != fam->styles->end(); ++s)
444 if (bestMatch == NULL || styleDiff(s->second, wt, wd, slant) < styleDiff(bestMatch, wt, wd, slant))
445 bestMatch = s->second;
446 return bestMatch;
447 }
448
449 const XeTeXFontMgr::OpSizeRec*
450 XeTeXFontMgr::getOpSize(XeTeXFont font)
451 {
452 hb_font_t* hbFont = ((XeTeXFontInst*)font)->getHbFont();
453 if (hbFont != NULL) {
454 hb_face_t* face = hb_font_get_face(hbFont);
455 OpSizeRec* pSizeRec = (OpSizeRec*) xmalloc(sizeof(OpSizeRec));
456
457 unsigned int designSize, minSize, maxSize;
458 bool ok = hb_ot_layout_get_size_params(face,
459 &designSize,
460 &pSizeRec->subFamilyID,
461 &pSizeRec->nameCode,
462 &minSize,
463 &maxSize);
464
465 if (ok) {
466 // Convert sizes from PostScript deci-points to TeX points
467 pSizeRec->designSize = designSize * 72.27 / 72.0 / 10.0;
468 pSizeRec->minSize = minSize * 72.27 / 72.0 / 10.0;
469 pSizeRec->maxSize = maxSize * 72.27 / 72.0 / 10.0;
470 return pSizeRec;
471 }
472
473 free(pSizeRec);
474 return NULL;
475 }
476
477 return NULL;
478 }
479
480 double
481 XeTeXFontMgr::getDesignSize(XeTeXFont font)
482 {
483 const OpSizeRec* pSizeRec = getOpSize(font);
484 if (pSizeRec != NULL)
485 return pSizeRec->designSize;
486 else
487 return 10.0;
488 }
489
490 void
491 XeTeXFontMgr::getOpSizeRecAndStyleFlags(Font* theFont)
492 {
493 XeTeXFont font = createFont(theFont->fontRef, 655360);
494 XeTeXFontInst* fontInst = (XeTeXFontInst*) font;
495 if (font != 0) {
496 const OpSizeRec* pSizeRec = getOpSize(font);
497 if (pSizeRec != NULL) {
498 theFont->opSizeInfo.designSize = pSizeRec->designSize;
499 if (pSizeRec->subFamilyID == 0
500 && pSizeRec->nameCode == 0
501 && pSizeRec->minSize == 0.0
502 && pSizeRec->maxSize == 0.0)
503 goto done_size; // feature is valid, but no 'size' range
504 theFont->opSizeInfo.subFamilyID = pSizeRec->subFamilyID;
505 theFont->opSizeInfo.nameCode = pSizeRec->nameCode;
506 theFont->opSizeInfo.minSize = pSizeRec->minSize;
507 theFont->opSizeInfo.maxSize = pSizeRec->maxSize;
508 }
509 done_size:
510
511 const TT_OS2* os2Table = (TT_OS2*) fontInst->getFontTable(ft_sfnt_os2);
512 if (os2Table != NULL) {
513 theFont->weight = os2Table->usWeightClass;
514 theFont->width = os2Table->usWidthClass;
515 uint16_t sel = os2Table->fsSelection;
516 theFont->isReg = (sel & (1 << 6)) != 0;
517 theFont->isBold = (sel & (1 << 5)) != 0;
518 theFont->isItalic = (sel & (1 << 0)) != 0;
519 }
520
521 const TT_Header* headTable = (TT_Header*) fontInst->getFontTable(ft_sfnt_head);
522 if (headTable != NULL) {
523 uint16_t ms = headTable->Mac_Style;
524 if ((ms & (1 << 0)) != 0)
525 theFont->isBold = true;
526 if ((ms & (1 << 1)) != 0)
527 theFont->isItalic = true;
528 }
529
530 const TT_Postscript* postTable = (const TT_Postscript*) fontInst->getFontTable(ft_sfnt_post);
531 if (postTable != NULL) {
532 theFont->slant = (int)(1000 * (tan(Fix2D(-postTable->italicAngle) * M_PI / 180.0)));
533 }
534 deleteFont(font);
535 }
536 }
537
538 // append a name but only if it's not already in the list
539 void
540 XeTeXFontMgr::appendToList(std::list<std::string>* list, const char* str)
541 {
542 for (std::list<std::string>::const_iterator i = list->begin(); i != list->end(); ++i)
543 if (*i == str)
544 return;
545 list->push_back(str);
546 }
547
548 // prepend a name, removing it from later in the list if present
549 void
550 XeTeXFontMgr::prependToList(std::list<std::string>* list, const char* str)
551 {
552 for (std::list<std::string>::iterator i = list->begin(); i != list->end(); ++i)
553 if (*i == str) {
554 list->erase(i);
555 break;
556 }
557 list->push_front(str);
558 }
559
560 void
561 XeTeXFontMgr::addToMaps(PlatformFontRef platformFont, const NameCollection* names)
562 {
563 if (m_platformRefToFont.find(platformFont) != m_platformRefToFont.end())
564 return; // this font has already been cached
565
566 if (names->m_psName.length() == 0)
567 return; // can't use a font that lacks a PostScript name
568
569 if (m_psNameToFont.find(names->m_psName) != m_psNameToFont.end())
570 return; // duplicates an earlier PS name, so skip
571
572 Font* thisFont = new Font(platformFont);
573 thisFont->m_psName = new std::string(names->m_psName);
574 getOpSizeRecAndStyleFlags(thisFont);
575
576 m_psNameToFont[names->m_psName] = thisFont;
577 m_platformRefToFont[platformFont] = thisFont;
578
579 if (names->m_fullNames.size() > 0)
580 thisFont->m_fullName = new std::string(*(names->m_fullNames.begin()));
581
582 if (names->m_familyNames.size() > 0)
583 thisFont->m_familyName = new std::string(*(names->m_familyNames.begin()));
584 else
585 thisFont->m_familyName = new std::string(names->m_psName);
586
587 if (names->m_styleNames.size() > 0)
588 thisFont->m_styleName = new std::string(*(names->m_styleNames.begin()));
589 else
590 thisFont->m_styleName = new std::string;
591
592 std::list<std::string>::const_iterator i;
593 for (i = names->m_familyNames.begin(); i != names->m_familyNames.end(); ++i) {
594 std::map<std::string,Family*>::iterator iFam = m_nameToFamily.find(*i);
595 Family* family;
596 if (iFam == m_nameToFamily.end()) {
597 family = new Family;
598 m_nameToFamily[*i] = family;
599 family->minWeight = thisFont->weight;
600 family->maxWeight = thisFont->weight;
601 family->minWidth = thisFont->width;
602 family->maxWidth = thisFont->width;
603 family->minSlant = thisFont->slant;
604 family->maxSlant = thisFont->slant;
605 } else {
606 family = iFam->second;
607 if (thisFont->weight < family->minWeight)
608 family->minWeight = thisFont->weight;
609 if (thisFont->weight > family->maxWeight)
610 family->maxWeight = thisFont->weight;
611 if (thisFont->width < family->minWidth)
612 family->minWidth = thisFont->width;
613 if (thisFont->width > family->maxWidth)
614 family->maxWidth = thisFont->width;
615 if (thisFont->slant < family->minSlant)
616 family->minSlant = thisFont->slant;
617 if (thisFont->slant > family->maxSlant)
618 family->maxSlant = thisFont->slant;
619 }
620
621 if (thisFont->parent == NULL)
622 thisFont->parent = family;
623
624 // ensure all style names in the family point to thisFont
625 for (std::list<std::string>::const_iterator j = names->m_styleNames.begin(); j != names->m_styleNames.end(); ++j) {
626 std::map<std::string,Font*>::iterator iFont = family->styles->find(*j);
627 if (iFont == family->styles->end())
628 (*family->styles)[*j] = thisFont;
629 /*
630 else if (iFont->second != thisFont)
631 fprintf(stderr, "# Font name warning: ambiguous Style \"%s\" in Family \"%s\" (PSNames \"%s\" and \"%s\")\n",
632 j->c_str(), i->c_str(), iFont->second->m_psName->c_str(), thisFont->m_psName->c_str());
633 */
634 }
635 }
636
637 for (i = names->m_fullNames.begin(); i != names->m_fullNames.end(); ++i) {
638 std::map<std::string,Font*>::iterator iFont = m_nameToFont.find(*i);
639 if (iFont == m_nameToFont.end())
640 m_nameToFont[*i] = thisFont;
641 /*
642 else if (iFont->second != thisFont)
643 fprintf(stderr, "# Font name warning: ambiguous FullName \"%s\" (PSNames \"%s\" and \"%s\")\n",
644 i->c_str(), iFont->second->m_psName->c_str(), thisFont->m_psName->c_str());
645 */
646 }
647 }
648
649 void
650 XeTeXFontMgr::die(const char*s, int i) const
651 {
652 fprintf(stderr, s, i);
653 fprintf(stderr, " - exiting\n");
654 exit(3);
655 }
656
657 void
658 XeTeXFontMgr::terminate()
659 {
660 }
661