Code

remove antediluvian cruft
[inkscape.git] / src / libnrtype / FontFactory.cpp
1 /*
2  *  FontFactory.cpp
3  *  testICU
4  *
5  *   Authors:
6  *     fred
7  *     bulia byak <buliabyak@users.sf.net>
8  *
9  */
11 #include "FontFactory.h"
12 #include <libnrtype/font-instance.h>
14 #include <glibmm.h>
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <glib/gmem.h>
22 #include <glibmm/i18n.h> // _()
24 /* Freetype2 */
25 # include <pango/pangoft2.h>
28 // need to avoid using the size field
29 size_t font_descr_hash::operator()( PangoFontDescription *const &x) const {
30     int h = 0;
31     h *= 1128467;
32     char const *theF = pango_font_description_get_family(x);
33     h += (theF)?g_str_hash(theF):0;
34     h *= 1128467;
35     h += (int)pango_font_description_get_style(x);
36     h *= 1128467;
37     h += (int)pango_font_description_get_variant(x);
38     h *= 1128467;
39     h += (int)pango_font_description_get_weight(x);
40     h *= 1128467;
41     h += (int)pango_font_description_get_stretch(x);
42     return h;
43 }
44 bool  font_descr_equal::operator()( PangoFontDescription *const&a, PangoFontDescription *const &b) {
45     //if ( pango_font_description_equal(a,b) ) return true;
46     char const *fa = pango_font_description_get_family(a);
47     char const *fb = pango_font_description_get_family(b);
48     if ( ( fa && fb == NULL ) || ( fb && fa == NULL ) ) return false;
49     if ( fa && fb && strcmp(fa,fb) != 0 ) return false;
50     if ( pango_font_description_get_style(a) != pango_font_description_get_style(b) ) return false;
51     if ( pango_font_description_get_variant(a) != pango_font_description_get_variant(b) ) return false;
52     if ( pango_font_description_get_weight(a) != pango_font_description_get_weight(b) ) return false;
53     if ( pango_font_description_get_stretch(a) != pango_font_description_get_stretch(b) ) return false;
54     return true;
55 }
57 /////////////////// helper functions
59 /**
60  * A wrapper for strcasestr that also provides an implementation for Win32.
61  */
62 static bool
63 ink_strstr(char const *haystack, char const *pneedle)
64 {
65     // windows has no strcasestr implementation, so here is ours...
66     // stolen from nmap
67     /* FIXME: This is broken for e.g. ink_strstr("aab", "ab").  Report to nmap.
68      *
69      * Also, suggest use of g_ascii_todown instead of buffer stuff, and g_ascii_tolower instead
70      * of tolower.  Given that haystack is a font name (i.e. fairly short), it should be ok to
71      * do g_ascii_strdown on both haystack and pneedle, and do normal strstr.
72      *
73      * Rather than fixing in inkscape, consider getting rid of this routine, instead using
74      * strdown and plain strstr at caller.  We have control over the needle values, so we can
75      * modify the callers rather than calling strdown there.
76      */
77     char buf[512];
78     register char const *p;
79     char *needle, *q, *foundto;
80     if (!*pneedle) return true;
81     if (!haystack) return false;
82         
83     needle = buf;
84     p = pneedle; q = needle;
85     while ((*q++ = tolower(*p++)))
86         ;
87     p = haystack - 1; foundto = needle;
88     while (*++p) {
89         if (tolower(*p) == *foundto) {
90             if (!*++foundto) {
91                 /* Yeah, we found it */
92                 return true;
93             }
94         } else foundto = needle;
95     }
96     return false;
97 }
99 /**
100  * Regular fonts are 'Regular', 'Roman', 'Normal', or 'Plain'
101  */
102 // FIXME: make this UTF8, add non-English style names
103 static bool
104 is_regular(char const *s)
106     if (ink_strstr(s, "Regular")) return true;
107     if (ink_strstr(s, "Roman")) return true;
108     if (ink_strstr(s, "Normal")) return true;
109     if (ink_strstr(s, "Plain")) return true;
110     return false;
113 /**
114  * Non-bold fonts are 'Medium' or 'Book'
115  */
116 static bool
117 is_nonbold(char const *s)
119     if (ink_strstr(s, "Medium")) return true;
120     if (ink_strstr(s, "Book")) return true;
121     return false;
124 /**
125  * Italic fonts are 'Italic', 'Oblique', or 'Slanted'
126  */
127 static bool
128 is_italic(char const *s)
130     if (ink_strstr(s, "Italic")) return true;
131     if (ink_strstr(s, "Oblique")) return true;
132     if (ink_strstr(s, "Slanted")) return true;
133     return false;
136 /**
137  * Bold fonts are 'Bold'
138  */
139 static bool
140 is_bold(char const *s)
142     if (ink_strstr(s, "Bold")) return true;
143     return false;
146 /**
147  * Caps fonts are 'Caps'
148  */
149 static bool
150 is_caps(char const *s)
152     if (ink_strstr(s, "Caps")) return true;
153     return false;
156 #if 0 /* FIXME: These are all unused.  Please delete them or use them (presumably in
157 * style_name_compare). */
158 /**
159  * Monospaced fonts are 'Mono'
160  */
161 static bool
162 is_mono(char const *s)
164     if (ink_strstr(s, "Mono")) return true;
165     return false;
168 /**
169  * Rounded fonts are 'Round'
170  */
171 static bool
172 is_round(char const *s)
174     if (ink_strstr(s, "Round")) return true;
175     return false;
178 /**
179  * Outline fonts are 'Outline'
180  */
181 static bool
182 is_outline(char const *s)
184     if (ink_strstr(s, "Outline")) return true;
185     return false;
188 /**
189  * Swash fonts are 'Swash'
190  */
191 static bool
192 is_swash(char const *s)
194     if (ink_strstr(s, "Swash")) return true;
195     return false;
197 #endif
199 /**
200  * Determines if two style names match.  This allows us to match
201  * based on the type of style rather than simply doing string matching,
202  * because for instance 'Plain' and 'Normal' mean the same thing.
203  * 
204  * Q:  Shouldn't this include the other tests such as is_outline, etc.?
205  * Q:  Is there a problem with strcasecmp on Win32?  Should it use stricmp?
206  */
207 int
208 style_name_compare(char const *aa, char const *bb)
210     char const *a = (char const *) aa;
211     char const *b = (char const *) bb;
212         
213     if (is_regular(a) && !is_regular(b)) return -1;
214     if (is_regular(b) && !is_regular(a)) return 1;
215         
216     if (is_bold(a) && !is_bold(b)) return 1;
217     if (is_bold(b) && !is_bold(a)) return -1;
218         
219     if (is_italic(a) && !is_italic(b)) return 1;
220     if (is_italic(b) && !is_italic(a)) return -1;
221         
222     if (is_nonbold(a) && !is_nonbold(b)) return 1;
223     if (is_nonbold(b) && !is_nonbold(a)) return -1;
224         
225     if (is_caps(a) && !is_caps(b)) return 1;
226     if (is_caps(b) && !is_caps(a)) return -1;
227         
228     return strcasecmp(a, b);
231 static int
232 style_record_compare(void const *aa, void const *bb)
234     NRStyleRecord const *a = (NRStyleRecord const *) aa;
235     NRStyleRecord const *b = (NRStyleRecord const *) bb;
236         
237     return (style_name_compare(a->name, b->name));
240 static void font_factory_name_list_destructor(NRNameList *list) 
242     for (unsigned int i = 0; i < list->length; i++) 
243         g_free(list->names[i]);
244     if ( list->names ) g_free(list->names);
247 static void font_factory_style_list_destructor(NRStyleList *list) 
249     for (unsigned int i = 0; i < list->length; i++) {
250         g_free((void *) (list->records)[i].name);
251         g_free((void *) (list->records)[i].descr);
252     }
253     if ( list->records ) g_free(list->records);
256 /**
257  * On Win32 performs a stricmp(a,b), otherwise does a strcasecmp(a,b)
258  */
259 int
260 family_name_compare(char const *a, char const *b)
262 #ifndef WIN32
263     return strcasecmp((*((char const **) a)), (*((char const **) b)));
264 #else
265     return stricmp((*((char const **) a)), (*((char const **) b)));
266 #endif
269 void noop(...) {}
270 //#define PANGO_DEBUG g_print
271 #define PANGO_DEBUG noop
275 ///////////////////// FontFactory
276 #ifndef USE_PANGO_WIN32
277 // the substitute function to tell fontconfig to enforce outline fonts
278 void FactorySubstituteFunc(FcPattern *pattern,gpointer /*data*/)
280     FcPatternAddBool(pattern, "FC_OUTLINE",FcTrue);
281     //char *fam = NULL;
282     //FcPatternGetString(pattern, "FC_FAMILY",0, &fam);
283     //printf("subst_f on %s\n",fam);
285 #endif
288 font_factory *font_factory::lUsine = NULL;
290 font_factory *font_factory::Default(void)
292     if ( lUsine == NULL ) lUsine = new font_factory;
293     return lUsine;
296 font_factory::font_factory(void)
298     fontSize = 512;
299     nbEnt = 0;
300     maxEnt = 32;
301     ents = (font_entry*)g_malloc(maxEnt*sizeof(font_entry));
303 #ifdef USE_PANGO_WIN32
304     hScreenDC = pango_win32_get_dc();
305     fontServer = pango_win32_font_map_for_display();
306     fontContext = pango_win32_get_context();
307     pangoFontCache = pango_win32_font_map_get_font_cache(fontServer);
308 #else
309     fontServer = pango_ft2_font_map_new();
310     pango_ft2_font_map_set_resolution((PangoFT2FontMap*)fontServer, 72, 72);
311     fontContext = pango_ft2_font_map_create_context((PangoFT2FontMap*)fontServer);
312     pango_ft2_font_map_set_default_substitute((PangoFT2FontMap*)fontServer,FactorySubstituteFunc,this,NULL);
313 #endif
316 font_factory::~font_factory(void)
318     for (int i = 0;i < nbEnt;i++) ents[i].f->Unref();
319     if ( ents ) g_free(ents);
321     g_object_unref(fontServer);
322 #ifdef USE_PANGO_WIN32
323     pango_win32_shutdown_display();
324 #else
325     //pango_ft2_shutdown_display();
326 #endif
327     //g_object_unref(fontContext);
328     
329     // Delete the pango font pointers in the string to instance map
330     PangoStringToDescrMap::iterator it = fontInstanceMap.begin();
331     while (it != fontInstanceMap.end()) {
332         pango_font_description_free((*it).second);
333         it++;
334     }
338 Glib::ustring font_factory::ConstructFontSpecification(PangoFontDescription *font)
340     Glib::ustring pangoString;
341     
342     g_assert(font);
343     
344     if (font) {
345         // Once the format for the font specification is decided, it must be
346         // kept.. if it is absolutely necessary to change it, the attribute
347         // it is written to needs to have a new version so the legacy files
348         // can be read.
349         
350         PangoFontDescription *copy = pango_font_description_copy(font);
351         
352         pango_font_description_unset_fields (copy, PANGO_FONT_MASK_SIZE);
353         pangoString = Glib::ustring(pango_font_description_to_string(copy));
354         
355         pango_font_description_free(copy);
356         
357     }
358     
359     return pangoString;
362 Glib::ustring font_factory::ConstructFontSpecification(font_instance *font)
364     Glib::ustring pangoString;
365     
366     g_assert(font);
367     
368     if (font) {
369         pangoString = ConstructFontSpecification(font->descr);
370     }
371     
372     return pangoString;
375 Glib::ustring font_factory::GetUIFamilyString(PangoFontDescription const *fontDescr)
377     Glib::ustring family;
378     
379     g_assert(fontDescr);
380     
381     if (fontDescr) {
382         // For now, keep it as family name taken from pango
383         family = pango_font_description_get_family(fontDescr);
384     }
385     
386     return family;
389 Glib::ustring font_factory::GetUIStyleString(PangoFontDescription const *fontDescr)
391     Glib::ustring style;
392     
393     g_assert(fontDescr);
394     
395     if (fontDescr) {
396         PangoFontDescription *fontDescrCopy = pango_font_description_copy(fontDescr);
397         
398         pango_font_description_unset_fields(fontDescrCopy, PANGO_FONT_MASK_FAMILY);
399         pango_font_description_unset_fields(fontDescrCopy, PANGO_FONT_MASK_SIZE);
400         
401         // For now, keep it as style name taken from pango
402         style = pango_font_description_to_string(fontDescrCopy);
403         
404         pango_font_description_free(fontDescrCopy);
405     }
406     
407     return style; 
410 Glib::ustring font_factory::ReplaceFontSpecificationFamily(const Glib::ustring & fontSpec, const Glib::ustring & newFamily)
412     Glib::ustring newFontSpec;
413     
414     // Although we are using the string from pango_font_description_to_string for the
415     // font specification, we definitely cannot just set the new family in the
416     // PangoFontDescription structure and ask for a new string.  This is because
417     // what constitutes a "family" in our own UI may be different from how Pango
418     // sees it.
419     
420     // Find the PangoFontDescription associated to this fontSpec
421     PangoStringToDescrMap::iterator it = fontInstanceMap.find(fontSpec);
422     
423     if (it != fontInstanceMap.end()) {
424         PangoFontDescription *descr = pango_font_description_copy((*it).second);
425         
426         // Grab the UI Family string from the descr
427         Glib::ustring uiFamily = GetUIFamilyString(descr);
428         
429         // Replace the UI Family name with the new family name
430         std::size_t found = fontSpec.find(uiFamily);
431         if (found != Glib::ustring::npos) {
432             newFontSpec = fontSpec;
433             newFontSpec.erase(found, uiFamily.size());
434             newFontSpec.insert(found, newFamily);
435             
436             // If the new font specification does not exist in the reference maps,
437             // search for the next best match for the faces in that style 
438             it = fontInstanceMap.find(newFontSpec);
439             if (it == fontInstanceMap.end()) {
440                 
441                 PangoFontDescription *newFontDescr = pango_font_description_from_string(newFontSpec.c_str());
442                 
443                 PangoFontDescription *bestMatchForNewDescr = NULL;
444                 Glib::ustring bestMatchFontDescription;
445                 
446                 bool setFirstFamilyMatch = false;
447                 for (it = fontInstanceMap.begin(); it != fontInstanceMap.end(); it++) {
448                     
449                     Glib::ustring currentFontSpec = (*it).first;
450                     
451                     // Save some time by only looking at the right family
452                     if (currentFontSpec.find(newFamily) != Glib::ustring::npos) {
453                         if (!setFirstFamilyMatch) {
454                                 // This ensures that the closest match is at least within the correct
455                                 // family rather than the first font in the list
456                             bestMatchForNewDescr = pango_font_description_copy((*it).second);
457                             bestMatchFontDescription = currentFontSpec;
458                             setFirstFamilyMatch = true;
459                         } else {
460                             // Get the font description that corresponds, and
461                             // then see if we've found a better match
462                             PangoFontDescription *possibleMatch = pango_font_description_copy((*it).second);
463                             
464                             if (pango_font_description_better_match(
465                                     newFontDescr, bestMatchForNewDescr, possibleMatch)) {
466                                 
467                                 pango_font_description_free(bestMatchForNewDescr);
468                                 bestMatchForNewDescr = possibleMatch;
469                                 bestMatchFontDescription = currentFontSpec;
470                             } else {
471                                 pango_font_description_free(possibleMatch);
472                             }
473                         }
474                     }
475                 }
476                 
477                 newFontSpec = bestMatchFontDescription;
478                 
479                 pango_font_description_free(newFontDescr);
480                 pango_font_description_free(bestMatchForNewDescr);
481             }
482         }
483         
484         pango_font_description_free(descr);
485     }
486     
487     return newFontSpec;
490 Glib::ustring font_factory::FontSpecificationSetItalic(const Glib::ustring & fontSpec, bool turnOn)
492     Glib::ustring newFontSpec;
493     
494     // Find the PangoFontDesecription that goes with this font specification string
495     PangoStringToDescrMap::iterator it = fontInstanceMap.find(fontSpec);
496     
497     if (it != fontInstanceMap.end()) {
498         // If we did find one, make a copy and set/unset the italic as needed
499         PangoFontDescription *descr = pango_font_description_copy((*it).second);
500         
501         PangoStyle style;
502         if (turnOn) {
503             style = PANGO_STYLE_ITALIC;
504         } else {
505             style = PANGO_STYLE_NORMAL;
506         }
507         pango_font_description_set_style(descr, style);
508         
509         newFontSpec = ConstructFontSpecification(descr);
510         if (fontInstanceMap.find(newFontSpec) == fontInstanceMap.end()) {
511             // If the new font does not have an italic face, don't
512             // allow italics to be set!
513             newFontSpec = fontSpec;
514         }
515         
516         pango_font_description_free(descr);
517     }
518     
519     return newFontSpec;    
522 Glib::ustring font_factory::FontSpecificationSetBold(const Glib::ustring & fontSpec, bool turnOn)
524     Glib::ustring newFontSpec;
525     
526     // Find the PangoFontDesecription that goes with this font specification string
527     PangoStringToDescrMap::iterator it = fontInstanceMap.find(fontSpec);
528     
529     if (it != fontInstanceMap.end()) {
530         // If we did find one, make a copy and set/unset the bold as needed
531         PangoFontDescription *descr = pango_font_description_copy((*it).second);
532         
533         PangoWeight weight;
534         if (turnOn) {
535             weight = PANGO_WEIGHT_BOLD;
536         } else {
537             weight = PANGO_WEIGHT_NORMAL;
538         }
539         pango_font_description_set_weight(descr, weight);
540         
541         newFontSpec = ConstructFontSpecification(descr);
542         if (fontInstanceMap.find(newFontSpec) == fontInstanceMap.end()) {
543             // If the new font does not have a bold face, don't
544             // allow bold to be set!
545             newFontSpec = fontSpec;
546         }
547         
548         pango_font_description_free(descr);
549     }
550     
551     return newFontSpec;  
554 /////
556 static bool StyleNameCompareInternal(Glib::ustring style1, Glib::ustring style2)
558     return (style_name_compare(style1.c_str(), style2.c_str()) < 0);
561 void font_factory::GetUIFamiliesAndStyles(FamilyToStylesMap *map)
563     g_assert(map);
564     
565     if (map) {
566         
567         // Gather the family names as listed by Pango
568         PangoFontFamily**  families = NULL;
569         int numFamilies = 0;
570         pango_font_map_list_families(fontServer, &families, &numFamilies);
571            
572         for (int currentFamily=0; currentFamily < numFamilies; currentFamily++) {
573             
574             // Gather the styles for this family
575             PangoFontFace** faces = NULL;
576             int numFaces = 0;
577             pango_font_family_list_faces(families[currentFamily], &faces, &numFaces);
578             
579             for (int currentFace=0; currentFace < numFaces; currentFace++) {
580                 
581                 // If the face has a name, describe it, and then use the 
582                 // description to get the UI family and face strings
583                 
584                 if (pango_font_face_get_face_name(faces[currentFace]) == NULL) {
585                     continue;
586                 }
587                 
588                 PangoFontDescription *faceDescr = pango_font_face_describe(faces[currentFace]);
589                 if (faceDescr) {
590                     Glib::ustring familyUIName = GetUIFamilyString(faceDescr);
591                     Glib::ustring styleUIName = GetUIStyleString(faceDescr);
592                     
593                     if (!familyUIName.empty() && !styleUIName.empty()) {
594                         // Find the right place to put the style information, adding
595                         // a map entry for the family name if it doesn't yet exist
596                         
597                         FamilyToStylesMap::iterator iter = map->find(familyUIName);
598                         
599                         if (iter == map->end()) {
600                             map->insert(std::make_pair(familyUIName, std::list<Glib::ustring>()));
601                         }
602                         
603                         // Insert into the style list and save the info in the reference maps
604                         // only if the style does not yet exist
605                         
606                         bool exists = false;
607                         std::list<Glib::ustring> &styleList = (*map)[familyUIName];
608                         
609                         for (std::list<Glib::ustring>::iterator it=styleList.begin();
610                                  it != styleList.end();
611                                  it++) {
612                             if (*it == styleUIName) {
613                                 exists = true;
614                                 break;
615                             }
616                         }
617                         
618                         if (!exists) {
619                             styleList.push_back(styleUIName);
620                             
621                             // Add the string info needed in the reference maps
622                             fontStringMap.insert(
623                                     std::make_pair(
624                                             Glib::ustring(familyUIName) + Glib::ustring(styleUIName),
625                                             ConstructFontSpecification(faceDescr)));
626                             fontInstanceMap.insert(
627                                     std::make_pair(ConstructFontSpecification(faceDescr), faceDescr));
628                         } else {
629                             pango_font_description_free(faceDescr);
630                         }
631                     } else {
632                         pango_font_description_free(faceDescr);
633                     }
634                 }
635             }
636         }
637        
638         // Sort the style lists
639         for (FamilyToStylesMap::iterator iter = map->begin() ; iter != map->end(); iter++) {
640             (*iter).second.sort(StyleNameCompareInternal);
641         }
642     }
645 font_instance* font_factory::FaceFromStyle(SPStyle const *style)
647     font_instance *font = NULL;
648     
649     g_assert(style);
650     
651     if (style) {
652         //  First try to use the font specification if it is set
653         if (style->text->font_specification.set
654             && style->text->font_specification.value
655             && *style->text->font_specification.value) {
656             
657             font = FaceFromFontSpecification(style->text->font_specification.value);
658         }
659         
660         // If that failed, try using the CSS information in the style
661         if (!font) {
662             font = Face(style->text->font_family.value, font_style_to_pos(*style));
663         }
664     }
665     
666     return font;
669 font_instance *font_factory::FaceFromDescr(char const *family, char const *style)
671     PangoFontDescription *temp_descr = pango_font_description_from_string(style);
672     pango_font_description_set_family(temp_descr,family);
673     font_instance *res = Face(temp_descr);
674     pango_font_description_free(temp_descr);
675     return res;
678 font_instance* font_factory::FaceFromUIStrings(char const *uiFamily, char const *uiStyle)
680     font_instance *fontInstance = NULL;
681     
682     g_assert(uiFamily && uiStyle);
683     if (uiFamily && uiStyle) {
684         Glib::ustring uiString = Glib::ustring(uiFamily) + Glib::ustring(uiStyle);
685         
686         UIStringToPangoStringMap::iterator uiToPangoIter = fontStringMap.find(uiString);
687         
688         if (uiToPangoIter != fontStringMap.end ()) {
689             PangoStringToDescrMap::iterator pangoToDescrIter = fontInstanceMap.find((*uiToPangoIter).second);
690             if (pangoToDescrIter != fontInstanceMap.end()) {
691                 // We found the pango description - now we can make a font_instance
692                 PangoFontDescription *tempDescr = pango_font_description_copy((*pangoToDescrIter).second);
693                 fontInstance = Face(tempDescr);
694                 pango_font_description_free(tempDescr);
695             }
696         }
697     }
698     
699     return fontInstance;
702 font_instance* font_factory::FaceFromPangoString(char const *pangoString)
704     font_instance *fontInstance = NULL;
705         
706     g_assert(pangoString);
707     
708     if (pangoString) {
709         PangoFontDescription *descr = NULL;
710          
711         // First attempt to find the font specification in the reference map
712         PangoStringToDescrMap::iterator it = fontInstanceMap.find(Glib::ustring(pangoString));
713         if (it != fontInstanceMap.end()) {
714             descr = pango_font_description_copy((*it).second);
715         }
716          
717         // Or create a font description from the string - this may fail or
718         // produce unexpected results if the string does not have a good format
719         if (!descr) {
720             descr = pango_font_description_from_string(pangoString);
721         }
722         
723         if (descr && (pango_font_description_get_family(descr) != NULL)) {
724             fontInstance = Face(descr);
725         }
726         
727         if (descr) {
728             pango_font_description_free(descr);
729         }
730     }
731      
732     return fontInstance;
735 font_instance* font_factory::FaceFromFontSpecification(char const *fontSpecification)
737     font_instance *font = NULL;
738     
739     g_assert(fontSpecification);
740     
741     if (fontSpecification) {
742         // How the string is used to reconstruct a font depends on how it
743         // was constructed in ConstructFontSpecification.  As it stands,
744         // the font specification is a pango-created string
745         font = FaceFromPangoString(fontSpecification);
746     }
747     
748     return font;
753 font_instance *font_factory::Face(PangoFontDescription *descr, bool canFail)
755 #ifdef USE_PANGO_WIN32
756     // damn Pango fudges the size, so we need to unfudge. See source of pango_win32_font_map_init()
757     pango_font_description_set_size(descr, (int) (fontSize*PANGO_SCALE*72/GetDeviceCaps(pango_win32_get_dc(),LOGPIXELSY))); // mandatory huge size (hinting workaround)
758 #else
759     pango_font_description_set_size(descr, (int) (fontSize*PANGO_SCALE)); // mandatory huge size (hinting workaround)
760 #endif
761         
762     font_instance *res = NULL;
763         
764     if ( loadedFaces.find(descr) == loadedFaces.end() ) {
765         // not yet loaded
766         PangoFont *nFace = NULL;
768         // workaround for bug #1025565.
769         // fonts without families blow up Pango.
770         if (pango_font_description_get_family(descr) != NULL) {
771             nFace = pango_font_map_load_font(fontServer,fontContext,descr);
772         }
773         else {
774             g_warning(_("Ignoring font without family that will crash Pango"));
775         }
777         if ( nFace ) {
778             // duplicate FcPattern, the hard way
779             res = new font_instance();
780             // store the descr of the font we asked for, since this is the key where we intend to put the font_instance at
781             // in the hash_map.  the descr of the returned pangofont may differ from what was asked, so we don't know (at this 
782             // point) whether loadedFaces[that_descr] is free or not (and overwriting an entry will bring deallocation problems)
783             res->descr = pango_font_description_copy(descr);
784             res->daddy = this;
785             res->InstallFace(nFace);
786             if ( res->pFont == NULL ) {
787                 // failed to install face -> bitmap font
788                 // printf("face failed\n");
789                 res->daddy = NULL;
790                 delete res;
791                 res = NULL;
792                 if ( canFail ) {
793                     char *tc = pango_font_description_to_string(descr);
794                     PANGO_DEBUG("falling back from %s to Sans because InstallFace failed\n",tc);
795                     g_free(tc);
796                     pango_font_description_set_family(descr,"Sans");
797                     res = Face(descr,false);
798                 }
799             } else {
800                 loadedFaces[res->descr]=res;
801                 res->Ref();
802                 AddInCache(res);
803             }
804         } else {
805             // no match
806             if ( canFail ) {
807                 PANGO_DEBUG("falling back to Sans\n");
808                 descr = pango_font_description_new();
809                 pango_font_description_set_family(descr,"Sans");
810                 res = Face(descr,false);
811                 pango_font_description_free(descr);
812             }
813         }
814     } else {
815         // already here
816         res = loadedFaces[descr];
817         res->Ref();
818         AddInCache(res);
819     }
820     res->InitTheFace();
821     return res;
824 font_instance *font_factory::Face(char const *family, int variant, int style, int weight, int stretch, int /*size*/, int /*spacing*/)
826     PangoFontDescription *temp_descr = pango_font_description_new();
827     pango_font_description_set_family(temp_descr,family);
828     pango_font_description_set_weight(temp_descr,(PangoWeight)weight);
829     pango_font_description_set_stretch(temp_descr,(PangoStretch)stretch);
830     pango_font_description_set_style(temp_descr,(PangoStyle)style);
831     pango_font_description_set_variant(temp_descr,(PangoVariant)variant);
832     font_instance *res = Face(temp_descr);
833     pango_font_description_free(temp_descr);
834     return res;
837 font_instance *font_factory::Face(char const *family, NRTypePosDef apos)
839     PangoFontDescription *temp_descr = pango_font_description_new();
840         
841     pango_font_description_set_family(temp_descr, family);
842         
843     if ( apos.variant == NR_POS_VARIANT_SMALLCAPS ) {
844         pango_font_description_set_variant(temp_descr, PANGO_VARIANT_SMALL_CAPS);
845     } else {
846         pango_font_description_set_variant(temp_descr, PANGO_VARIANT_NORMAL);
847     }
848         
849     if ( apos.italic ) {
850         pango_font_description_set_style(temp_descr, PANGO_STYLE_ITALIC);
851     } else if ( apos.oblique ) {
852         pango_font_description_set_style(temp_descr, PANGO_STYLE_OBLIQUE);
853     } else {
854         pango_font_description_set_style(temp_descr, PANGO_STYLE_NORMAL);
855     }
856         
857     if ( apos.weight <= NR_POS_WEIGHT_ULTRA_LIGHT ) {
858         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_ULTRALIGHT);
859     } else if ( apos.weight <= NR_POS_WEIGHT_LIGHT ) {
860         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_LIGHT);
861     } else if ( apos.weight <= NR_POS_WEIGHT_NORMAL ) {
862         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_NORMAL);
863     } else if ( apos.weight <= NR_POS_WEIGHT_BOLD ) {
864         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_BOLD);
865     } else if ( apos.weight <= NR_POS_WEIGHT_ULTRA_BOLD ) {
866         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_ULTRABOLD);
867     } else {
868         pango_font_description_set_weight(temp_descr, PANGO_WEIGHT_HEAVY);
869     }
870         
871     if ( apos.stretch <= NR_POS_STRETCH_ULTRA_CONDENSED ) {
872         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXTRA_CONDENSED);
873     } else if ( apos.stretch <= NR_POS_STRETCH_CONDENSED ) {
874         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_CONDENSED);
875     } else if ( apos.stretch <= NR_POS_STRETCH_SEMI_CONDENSED ) {
876         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_SEMI_CONDENSED);
877     } else if ( apos.stretch <= NR_POS_WEIGHT_NORMAL ) {
878         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_NORMAL);
879     } else if ( apos.stretch <= NR_POS_STRETCH_SEMI_EXPANDED ) {
880         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_SEMI_EXPANDED);
881     } else if ( apos.stretch <= NR_POS_STRETCH_EXPANDED ) {
882         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXPANDED);
883     } else {
884         pango_font_description_set_stretch(temp_descr, PANGO_STRETCH_EXTRA_EXPANDED);
885     }
886         
887     font_instance *res = Face(temp_descr);
888     pango_font_description_free(temp_descr);
889     return res;
892 void font_factory::UnrefFace(font_instance *who)
894     if ( who == NULL ) return;
895     if ( loadedFaces.find(who->descr) == loadedFaces.end() ) {
896         // not found
897         char *tc = pango_font_description_to_string(who->descr);
898         g_warning("unrefFace %p=%s: failed\n",who,tc);
899         g_free(tc);
900     } else {
901         loadedFaces.erase(loadedFaces.find(who->descr));
902         //                      printf("unrefFace %p: success\n",who);
903     }
906 void font_factory::AddInCache(font_instance *who)
908     if ( who == NULL ) return;
909     for (int i = 0;i < nbEnt;i++) ents[i].age *= 0.9;
910     for (int i = 0;i < nbEnt;i++) {
911         if ( ents[i].f == who ) {
912             //                  printf("present\n");
913             ents[i].age += 1.0;
914             return;
915         }
916     }
917     if ( nbEnt > maxEnt ) {
918         printf("cache sur-plein?\n");
919         return;
920     }
921     who->Ref();
922     if ( nbEnt == maxEnt ) {
923         int    bi = 0;
924         double ba = ents[bi].age;
925         for (int i = 1;i < nbEnt;i++) {
926             if ( ents[i].age < ba ) {
927                 bi = i;
928                 ba = ents[bi].age;
929             }
930         }
931         ents[bi].f->Unref();
932         ents[bi]=ents[--nbEnt];
933     }
934     ents[nbEnt].f = who;
935     ents[nbEnt].age = 1.0;
936     nbEnt++;
940 /*
941   Local Variables:
942   mode:c++
943   c-file-style:"stroustrup"
944   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
945   indent-tabs-mode:nil
946   fill-column:99
947   End:
948 */
949 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :