Created a new file for the font matching code.

This commit is contained in:
Pietro Gagliardi 2018-03-11 19:32:08 -04:00
parent 24d2220fe5
commit 5939c3203d
5 changed files with 67 additions and 37 deletions

View File

@ -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

View File

@ -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);

View File

@ -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[];

View File

@ -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);

55
unix/fontmatch.c Normal file
View File

@ -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;
}