From 3c995c9357c4168c737dce1e8663ce6fe8df88e8 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 6 Nov 2020 11:24:47 -0500 Subject: fontconfig: Try harder to find a default face Fonts are amazing, and not in a good way. My system has fonts with 0, 1, 2 "Regular" faces. It also has fonts where the "Regular" face is, in fact, bold. So, we need to work even harder to return a reasonable face when asked about the default. We already make a determination of faces that we consider 'regular' when we create the faces initially. Just keep that information for later reuse. --- pango/pangofc-fontmap.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c index 3b7f1915..764336ae 100644 --- a/pango/pangofc-fontmap.c +++ b/pango/pangofc-fontmap.c @@ -182,6 +182,7 @@ struct _PangoFcFace FcPattern *pattern; guint fake : 1; + guint regular : 1; }; struct _PangoFcFamily @@ -2852,6 +2853,7 @@ ensure_faces (PangoFcFamily *fcfamily) fcfamily->faces[i++] = create_face (fcfamily, "Bold", NULL, TRUE); fcfamily->faces[i++] = create_face (fcfamily, "Italic", NULL, TRUE); fcfamily->faces[i++] = create_face (fcfamily, "Bold Italic", NULL, TRUE); + fcfamily->faces[0]->regular = 1; } else { @@ -2865,12 +2867,17 @@ ensure_faces (PangoFcFamily *fcfamily) gboolean has_face [4] = { FALSE, FALSE, FALSE, FALSE }; PangoFcFace **faces; gint num = 0; + int regular_weight; + int regular_idx; fontset = fcfamily->patterns; /* at most we have 3 additional artifical faces */ faces = g_new (PangoFcFace *, fontset->nfont + 3); + regular_weight = 0; + regular_idx = -1; + for (i = 0; i < fontset->nfont; i++) { const char *style, *font_style = NULL; @@ -2895,12 +2902,23 @@ ensure_faces (PangoFcFamily *fcfamily) if (FcPatternGetString (fontset->fonts[i], FC_STYLE, 0, (FcChar8 **)(void*)&font_style) != FcResultMatch) font_style = NULL; + if (font_style && strcmp (font_style, "Regular") == 0) + { + regular_weight = FC_WEIGHT_MEDIUM; + regular_idx = num; + } + if (weight <= FC_WEIGHT_MEDIUM) { if (slant == FC_SLANT_ROMAN) { has_face[REGULAR] = TRUE; style = "Regular"; + if (weight > regular_weight) + { + regular_weight = weight; + regular_idx = num; + } } else { @@ -2938,6 +2956,9 @@ ensure_faces (PangoFcFamily *fcfamily) if ((has_face[REGULAR] || has_face[ITALIC] || has_face[BOLD]) && !has_face[BOLD_ITALIC]) faces[num++] = create_face (fcfamily, "Bold Italic", NULL, TRUE); + if (regular_idx != -1) + faces[regular_idx]->regular = 1; + faces = g_renew (PangoFcFace *, faces, num); qsort (faces, num, sizeof (PangoFcFace *), compare_face); @@ -2979,24 +3000,18 @@ pango_fc_family_get_face (PangoFontFamily *family, { PangoFcFamily *fcfamily = PANGO_FC_FAMILY (family); int i; - const char *style = name; ensure_faces (fcfamily); - if (style == NULL) - style = "Regular"; - for (i = 0; i < fcfamily->n_faces; i++) { PangoFontFace *face = PANGO_FONT_FACE (fcfamily->faces[i]); - if (strcmp (style, pango_font_face_get_face_name (face)) == 0) + if ((name != NULL && strcmp (name, pango_font_face_get_face_name (face)) == 0) || + (name == NULL && PANGO_FC_FACE (face)->regular)) return face; } - if (name == NULL && fcfamily->n_faces > 0) - return PANGO_FONT_FACE (fcfamily->faces[0]); - return NULL; } -- cgit v1.2.1