[cairo-commit] src/cairo-atsui-font.c

Brian Ewins brianewins at kemper.freedesktop.org
Sun Jan 7 16:57:28 PST 2007


 src/cairo-atsui-font.c |   56 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 40 insertions(+), 16 deletions(-)

New commits:
diff-tree 58265f3508959298eabab55ec28dc6d9516eedc3 (from 22e271d687ad871a4e8a069ee2945c42062c1cb4)
Author: Brian Ewins <Brian.Ewins at gmail.com>
Date:   Thu Jan 4 05:54:52 2007 +0000

    [ATSUI] Select Bitstream Vera and CSS font families reliably. 
    
    Previously the code selected using the family name; this intermittently
    selected a bold or italic face instead of the regular one. The new approach
    is to select the desired font instance directly if possible, and only use
    the family lookup if that fails. This isn't 100% correct but should always
    provide the correct font instance for CSS generic font families. The
    bug was sometimes reproducible with the select-font-face test.

diff --git a/src/cairo-atsui-font.c b/src/cairo-atsui-font.c
index c5e2d36..43d4e50 100644
--- a/src/cairo-atsui-font.c
+++ b/src/cairo-atsui-font.c
@@ -277,6 +277,7 @@ _cairo_atsui_font_create_toy(cairo_toy_f
     OSStatus err;
     Boolean isItalic, isBold;
     const char *family = toy_face->family;
+    const char *full_name;
 
     err = ATSUCreateStyle(&style);
 
@@ -303,33 +304,56 @@ _cairo_atsui_font_create_toy(cairo_toy_f
         break;
     }
 
+    /* The APIs for resolving a font family to a regular
+     * font face are all broken or deprecated in 10.4, so
+     * just try our best to find a sensible font.
+     * 
+     * First we try to map the CSS generic font families
+     * to fonts that should always be available. 
+     * If the family isn't a CSS family, guess that the 
+     * font family name is the same as the full name of the 
+     * regular font instance, which works in many cases. 
+     * 'Times' is one common exception to this rule, so it's
+     * handled specially.
+     */
+    if (!strcmp(family, "serif") || !strcmp(family, "Times"))
+	full_name = "Times Roman";
+    else if (!strcmp(family, "sans-serif") || !strcmp(family, "sans"))
+	full_name = "Helvetica";
+    else if (!strcmp(family, "cursive"))
+	full_name = "Apple Chancery";
+    else if (!strcmp(family, "fantasy"))
+	full_name = "American Typewriter";
+    else if (!strcmp(family, "monospace") || !strcmp(family, "mono"))
+	full_name = "Courier";
+    else
+	full_name = family;
+
     err = ATSUFindFontFromName(family, strlen(family),
-                               kFontFamilyName,
+                               kFontFullName,
                                kFontNoPlatformCode,
                                kFontRomanScript,
                                kFontNoLanguageCode, &fontID);
 
     if (err != noErr) {
-	/* couldn't get the font - remap css names and try again */
-
-	if (!strcmp(family, "serif"))
-	    family = "Times";
-	else if (!strcmp(family, "sans-serif"))
-	    family = "Helvetica";
-	else if (!strcmp(family, "cursive"))
-	    family = "Apple Chancery";
-	else if (!strcmp(family, "fantasy"))
-	    family = "Gadget";
-	else if (!strcmp(family, "monospace"))
-	    family = "Courier";
-	else /* anything else - return error instead? */
-	    family = "Courier";
-
+	/* Look for any font instance in the family. This may 
+	 * succeed, but select the bold or italic face. It only
+	 * selects the first loaded font instance in the family.
+	 */
 	err = ATSUFindFontFromName(family, strlen(family),
 				   kFontFamilyName,
 				   kFontNoPlatformCode,
 				   kFontRomanScript,
 				   kFontNoLanguageCode, &fontID);
+	if (err != noErr) {
+	    /* Last chance - try Courier. */
+	    full_name = "Courier";
+	    err = ATSUFindFontFromName(full_name, strlen(full_name),
+				       kFontFullName,
+				       kFontNoPlatformCode,
+				       kFontRomanScript,
+				       kFontNoLanguageCode, &fontID);
+	}
     }
 
     {


More information about the cairo-commit mailing list