From 5939c3203d50dba8f7cd60a798fa818f5953a444 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 11 Mar 2018 19:32:08 -0400 Subject: [PATCH] Created a new file for the font matching code. --- unix/CMakeLists.txt | 1 + unix/attrstr.c | 6 ++--- unix/attrstr.h | 9 +++++--- unix/drawtext.c | 33 ++------------------------- unix/fontmatch.c | 55 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 37 deletions(-) create mode 100644 unix/fontmatch.c diff --git a/unix/CMakeLists.txt b/unix/CMakeLists.txt index c20cf266..eba09ad9 100644 --- a/unix/CMakeLists.txt +++ b/unix/CMakeLists.txt @@ -24,6 +24,7 @@ list(APPEND _LIBUI_SOURCES unix/editablecombo.c unix/entry.c unix/fontbutton.c + unix/fontmatch.c unix/form.c unix/future.c unix/graphemes.c diff --git a/unix/attrstr.c b/unix/attrstr.c index 0ffb0b56..0fc9de89 100644 --- a/unix/attrstr.c +++ b/unix/attrstr.c @@ -68,15 +68,15 @@ static uiForEach processAttribute(const uiAttributedString *s, const uiAttribute case uiAttributeTypeWeight: // TODO reverse the misalignment from drawtext.c if it is corrected addattr(p, start, end, - pango_attr_weight_new((PangoWeight) uiAttributeWeight(attr))); + pango_attr_weight_new(uiprivWeightToPangoWeight(uiAttributeWeight(attr)))); break; case uiAttributeTypeItalic: addattr(p, start, end, - pango_attr_style_new(pangoItalics[uiAttributeItalic(attr)])); + pango_attr_style_new(uiprivItalicToPangoStyle(uiAttributeItalic(attr)))); break; case uiAttributeTypeStretch: addattr(p, start, end, - pango_attr_stretch_new(pangoStretches[uiAttributeStretch(attr)])); + pango_attr_stretch_new(uiprivStretchToPangoStretch(uiAttributeStretch(attr)))); break; case uiAttributeTypeColor: uiAttributeColor(attr, &r, &g, &b, &a); diff --git a/unix/attrstr.h b/unix/attrstr.h index 53acc496..5fd072c2 100644 --- a/unix/attrstr.h +++ b/unix/attrstr.h @@ -9,6 +9,12 @@ // opentype.c extern GString *uiprivOpenTypeFeaturesToPangoCSSFeaturesString(const uiOpenTypeFeatures *otf); +// fontmatch.c +extern PangoWeight uiprivWeightToPangoWeight(uiTextWeight w); +extern PangoStyle uiprivItalicToPangoStyle(uiTextItalic i); +extern PangoStretch uiprivStretchToPangoStretch(uiTextStretch s); +extern PangoFontDescription *uiprivFontDescriptorToPangoFontDescription(const uiFontDescriptor *uidesc); + // attrstr.c extern PangoAttrList *uiprivAttributedStringToPangoAttrList(uiDrawTextLayoutParams *p, GPtrArray **backgroundParams); @@ -23,6 +29,3 @@ struct uiprivDrawTextBackgroundParams { double b; double a; }; -// TODO move this to a fontmatch.c -extern const PangoStyle uiprivPangoItalics[]; -extern const PangoStretch uiprivPangoStretches[]; diff --git a/unix/drawtext.c b/unix/drawtext.c index d0866929..47d4362d 100644 --- a/unix/drawtext.c +++ b/unix/drawtext.c @@ -1,4 +1,4 @@ -xx 11 march 2018 +// 11 march 2018 #import "uipriv_unix.h" #import "draw.h" #import "attrstr.h" @@ -14,24 +14,6 @@ struct uiDrawTextLayout { // so let's use gdk_pango_context_get() instead; even though it's for the default screen only, it's good enough for us #define mkGenericPangoCairoContext() (gdk_pango_context_get()) -const PangoStyle uiprivPangoItalics[] = { - [uiTextItalicNormal] = PANGO_STYLE_NORMAL, - [uiTextItalicOblique] = PANGO_STYLE_OBLIQUE, - [uiTextItalicItalic] = PANGO_STYLE_ITALIC, -}; - -const PangoStretch uiprivPangoStretches[] = { - [uiTextStretchUltraCondensed] = PANGO_STRETCH_ULTRA_CONDENSED, - [uiTextStretchExtraCondensed] = PANGO_STRETCH_EXTRA_CONDENSED, - [uiTextStretchCondensed] = PANGO_STRETCH_CONDENSED, - [uiTextStretchSemiCondensed] = PANGO_STRETCH_SEMI_CONDENSED, - [uiTextStretchNormal] = PANGO_STRETCH_NORMAL, - [uiTextStretchSemiExpanded] = PANGO_STRETCH_SEMI_EXPANDED, - [uiTextStretchExpanded] = PANGO_STRETCH_EXPANDED, - [uiTextStretchExtraExpanded] = PANGO_STRETCH_EXTRA_EXPANDED, - [uiTextStretchUltraExpanded] = PANGO_STRETCH_ULTRA_EXPANDED, -}; - static const PangoAlignment pangoAligns[] = { [uiDrawTextAlignLeft] = PANGO_ALIGN_LEFT, [uiDrawTextAlignCenter] = PANGO_ALIGN_CENTER, @@ -57,18 +39,7 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *p) // this is safe; pango_layout_set_text() copies the string pango_layout_set_text(tl->layout, uiAttributedStringString(p->String), -1); - desc = pango_font_description_new(); - pango_font_description_set_family(desc, p->DefaultFont->Family); - pango_font_description_set_style(desc, uiprivPangoItalics[p->DefaultFont->Italic]); - // for the most part, pango weights correlate to ours - // the differences: - // - Book — libui: 350, Pango: 380 - // - Ultra Heavy — libui: 950, Pango: 1000 - // TODO figure out what to do about this misalignment - pango_font_description_set_weight(desc, p->DefaultFont->Weight); - pango_font_description_set_stretch(desc, uiprivPangoStretches[p->DefaultFont->Stretch]); - // see https://developer.gnome.org/pango/1.30/pango-Fonts.html#pango-font-description-set-size and https://developer.gnome.org/pango/1.30/pango-Glyph-Storage.html#pango-units-from-double - pango_font_description_set_size(desc, pango_units_from_double(p->DefaultFont->Size)); + desc = uiprivFontDescriptorToPangoFontDescription(p->DefaultFont); pango_layout_set_font_description(tl->layout, desc); // this is safe; the description is copied pango_font_description_free(desc); diff --git a/unix/fontmatch.c b/unix/fontmatch.c new file mode 100644 index 00000000..05523c24 --- /dev/null +++ b/unix/fontmatch.c @@ -0,0 +1,55 @@ +// 11 march 2018 +#include "uipriv_unix.h" +#include "attrstr.h" + +static const PangoStyle pangoItalics[] = { + [uiTextItalicNormal] = PANGO_STYLE_NORMAL, + [uiTextItalicOblique] = PANGO_STYLE_OBLIQUE, + [uiTextItalicItalic] = PANGO_STYLE_ITALIC, +}; + +static const PangoStretch pangoStretches[] = { + [uiTextStretchUltraCondensed] = PANGO_STRETCH_ULTRA_CONDENSED, + [uiTextStretchExtraCondensed] = PANGO_STRETCH_EXTRA_CONDENSED, + [uiTextStretchCondensed] = PANGO_STRETCH_CONDENSED, + [uiTextStretchSemiCondensed] = PANGO_STRETCH_SEMI_CONDENSED, + [uiTextStretchNormal] = PANGO_STRETCH_NORMAL, + [uiTextStretchSemiExpanded] = PANGO_STRETCH_SEMI_EXPANDED, + [uiTextStretchExpanded] = PANGO_STRETCH_EXPANDED, + [uiTextStretchExtraExpanded] = PANGO_STRETCH_EXTRA_EXPANDED, + [uiTextStretchUltraExpanded] = PANGO_STRETCH_ULTRA_EXPANDED, +}; + +// for the most part, pango weights correlate to ours +// the differences: +// - Book — libui: 350, Pango: 380 +// - Ultra Heavy — libui: 950, Pango: 1000 +// TODO figure out what to do about this misalignment +PangoWeight uiprivWeightToPangoWeight(uiTextWeight w) +{ + return (PangoWeight) w; +} + +PangoStyle uiprivItalicToPangoStyle(uiTextItalic i) +{ + return pangoItalics[i]; +} + +PangoStretch uiprivStretchToPangoStretch(uiTextStretch s) +{ + return pangoStretches[s]; +} + +PangoFontDescription *uiprivFontDescriptorToPangoFontDescription(const uiFontDescriptor *uidesc) +{ + PangoFontDescriptor *desc; + + desc = pango_font_description_new(); + pango_font_description_set_family(desc, uidesc->Family); + // see https://developer.gnome.org/pango/1.30/pango-Fonts.html#pango-font-description-set-size and https://developer.gnome.org/pango/1.30/pango-Glyph-Storage.html#pango-units-from-double + pango_font_description_set_size(desc, pango_units_from_double(uidesc->Size)); + pango_font_description_set_weight(desc, uiprivWeightToPangoWeight(uidesc->Weight)); + pango_font_description_set_style(desc, uiprivItalicToPangoStyle(uidesc->Italic)); + pango_font_description_set_stretch(desc, uiprivStretchToPangoStretch(uidesc->Stretch)); + return desc; +}