[OS/2] Improve weight matching when creating cairo font, part of work on Bug 381333

This commit is contained in:
mozilla@weilbacher.org 2007-06-24 03:00:50 -07:00
Родитель 499b336247
Коммит 25a1daae9b
1 изменённых файлов: 46 добавлений и 17 удалений

Просмотреть файл

@ -180,6 +180,26 @@ const gfxFont::Metrics& gfxOS2Font::GetMetrics()
return *mMetrics; return *mMetrics;
} }
// weight list copied from fontconfig.h
// unfortunately, the OS/2 version so far only supports regular and bold
static const PRInt8 nFcWeight = 2; // 10; // length of weight list
static const int fcWeight[] = {
//FC_WEIGHT_THIN,
//FC_WEIGHT_EXTRALIGHT, // == FC_WEIGHT_ULTRALIGHT
//FC_WEIGHT_LIGHT,
//FC_WEIGHT_BOOK,
FC_WEIGHT_REGULAR, // == FC_WEIGHT_NORMAL
//FC_WEIGHT_MEDIUM,
//FC_WEIGHT_DEMIBOLD, // == FC_WEIGHT_SEMIBOLD
FC_WEIGHT_BOLD,
//FC_WEIGHT_EXTRABOLD, // == FC_WEIGHT_ULTRABOLD
//FC_WEIGHT_BLACK // == FC_WEIGHT_HEAVY
};
// gfxOS2Font::CairoFontFace()
// return a font face usable by cairo for font rendering
// if none was created yet, use FontConfig patterns based on the current style
// to create a new font face
cairo_font_face_t *gfxOS2Font::CairoFontFace() cairo_font_face_t *gfxOS2Font::CairoFontFace()
{ {
#ifdef DEBUG_thebes_2 #ifdef DEBUG_thebes_2
@ -197,20 +217,34 @@ cairo_font_face_t *gfxOS2Font::CairoFontFace()
FcPatternAddString(fcPattern, FC_FAMILY, FcPatternAddString(fcPattern, FC_FAMILY,
(FcChar8 *)NS_LossyConvertUTF16toASCII(mName).get()); (FcChar8 *)NS_LossyConvertUTF16toASCII(mName).get());
PRUint8 fcProperty; // adjust font weight using the offset
// add weight to pattern // The requirements outlined in gfxFont.h are difficult to meet without
switch (mStyle.weight) { // having a table of available font weights, so we map the gfxFont
case FONT_WEIGHT_NORMAL: // weight to possible FontConfig weights.
fcProperty = FC_WEIGHT_NORMAL; PRInt8 weight, offset;
break; mStyle.ComputeWeightAndOffset(&weight, &offset);
case FONT_WEIGHT_BOLD: // gfxFont weight FC weight
fcProperty = FC_WEIGHT_BOLD; // 400 80
break; // 700 200
default: PRInt16 fcW = 40 * weight - 80; // match gfxFont weight to base FC weight
fcProperty = FC_WEIGHT_MEDIUM; // find the correct weight in the list
PRInt8 i = 0;
while (i < nFcWeight && fcWeight[i] < fcW) {
i++;
} }
FcPatternAddInteger(fcPattern, FC_WEIGHT, fcProperty); // add the offset, but observe the available number of weights
i += offset;
if (i < 0) {
i = 0;
} else if (i >= nFcWeight) {
i = nFcWeight - 1;
}
fcW = fcWeight[i];
// add weight to pattern
FcPatternAddInteger(fcPattern, FC_WEIGHT, fcW);
PRUint8 fcProperty;
// add style to pattern // add style to pattern
switch (mStyle.style) { switch (mStyle.style) {
case FONT_STYLE_ITALIC: case FONT_STYLE_ITALIC:
@ -253,11 +287,6 @@ cairo_font_face_t *gfxOS2Font::CairoFontFace()
mFontFace = cairo_ft_font_face_create_for_pattern(fcMatch); mFontFace = cairo_ft_font_face_create_for_pattern(fcMatch);
FcPatternDestroy(fcMatch); FcPatternDestroy(fcMatch);
} }
#ifdef DEBUG_thebes_2
printf(" cairo_font_face_type=%s (%d)\n",
cairo_font_face_get_type(mFontFace) == CAIRO_FONT_TYPE_FT ? "FT" : "??",
cairo_font_face_get_type(mFontFace));
#endif
NS_ASSERTION(mFontFace, "Failed to make font face"); NS_ASSERTION(mFontFace, "Failed to make font face");
return mFontFace; return mFontFace;