From bffe311afe126d335b3efe6fdbfdc95dde9eb079 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 11 Mar 2018 20:23:18 -0400 Subject: [PATCH] Switched to using Pango background color attributes. Unix code done for now. --- darwin/attrstr.h | 1 + unix/attrstr.c | 35 ++++++++++------------------------- unix/attrstr.h | 14 +------------- unix/drawtext.c | 9 +-------- unix/future.c | 9 +++++++++ unix/uipriv_unix.h | 1 + 6 files changed, 23 insertions(+), 46 deletions(-) diff --git a/darwin/attrstr.h b/darwin/attrstr.h index f77b4d86..02a3418d 100644 --- a/darwin/attrstr.h +++ b/darwin/attrstr.h @@ -77,6 +77,7 @@ extern void uiprivUninitUnderlineColors(void); extern CFAttributedStringRef uiprivAttributedStringToCFAttributedString(uiDrawTextLayoutParams *p, NSArray **backgroundParams); // drawtext.m +// TODO figure out where this type should *really* go in all the headers... @interface uiprivDrawTextBackgroundParams : NSObject { size_t start; size_t end; diff --git a/unix/attrstr.c b/unix/attrstr.c index fe554c48..a378e452 100644 --- a/unix/attrstr.c +++ b/unix/attrstr.c @@ -7,24 +7,8 @@ // TODO make this name less generic? struct foreachParams { PangoAttrList *attrs; - // TODO use pango's built-in background attribute - GPtrArray *backgroundParams; }; -static void addBackgroundAttribute(struct foreachParams *p, size_t start, size_t end, double r, double g, double b, double a) -{ - uiprivDrawTextBackgroundParams *dtb; - - dtb = uiprivNew(uiprivDrawTextBackgroundParams); - dtb->start = start; - dtb->end = end; - dtb->r = r; - dtb->g = g; - dtb->b = b; - dtb->a = a; - g_ptr_array_add(p->backgroundParams, dtb); -} - static void addattr(struct foreachParams *p, size_t start, size_t end, PangoAttribute *attr) { if (attr == NULL) // in case of a future attribute @@ -77,8 +61,16 @@ static uiForEach processAttribute(const uiAttributedString *s, const uiAttribute (guint16) (a * 65535.0))); break; case uiAttributeTypeBackground: + // TODO make sure this works properly with line paragraph spacings (after figuring out what that means, of course) uiAttributeColor(attr, &r, &g, &b, &a); - addBackgroundAttribute(p, start, end, r, g, b, a); + addattr(p, start, end, + pango_attr_background_new( + (guint16) (r * 65535.0), + (guint16) (g * 65535.0), + (guint16) (b * 65535.0))); + addattr(p, start, end, + FUTURE_pango_attr_background_alpha_new( + (guint16) (a * 65535.0))); break; case uiAttributeTypeUnderline: switch (uiAttributeUnderline(attr)) { @@ -143,18 +135,11 @@ static uiForEach processAttribute(const uiAttributedString *s, const uiAttribute return uiForEachContinue; } -static void freeBackgroundParams(gpointer data) -{ - uiprivFree((uiprivDrawTextBackgroundParams *) data); -} - -PangoAttrList *uiprivAttributedStringToPangoAttrList(uiDrawTextLayoutParams *p, GPtrArray **backgroundParams) +PangoAttrList *uiprivAttributedStringToPangoAttrList(uiDrawTextLayoutParams *p) { struct foreachParams fep; fep.attrs = pango_attr_list_new(); - fep.backgroundParams = g_ptr_array_new_with_free_func(freeBackgroundParams); uiAttributedStringForEachAttribute(p->String, processAttribute, &fep); - *backgroundParams = fep.backgroundParams; return fep.attrs; } diff --git a/unix/attrstr.h b/unix/attrstr.h index 5dff27b3..984ac1f5 100644 --- a/unix/attrstr.h +++ b/unix/attrstr.h @@ -17,16 +17,4 @@ extern PangoFontDescription *uiprivFontDescriptorToPangoFontDescription(const ui extern void uiprivFontDescriptorFromPangoFontDescription(PangoFontDescription *pdesc, uiFontDescriptor *uidesc); // attrstr.c -extern PangoAttrList *uiprivAttributedStringToPangoAttrList(uiDrawTextLayoutParams *p, GPtrArray **backgroundParams); - -// drawtext.c -// TODO figure out where this type should *really* go in all the headers... -typedef struct uiprivDrawTextBackgroundParams uiprivDrawTextBackgroundParams; -struct uiprivDrawTextBackgroundParams { - size_t start; - size_t end; - double r; - double g; - double b; - double a; -}; +extern PangoAttrList *uiprivAttributedStringToPangoAttrList(uiDrawTextLayoutParams *p); diff --git a/unix/drawtext.c b/unix/drawtext.c index b64a9999..5792f9e0 100644 --- a/unix/drawtext.c +++ b/unix/drawtext.c @@ -5,7 +5,6 @@ struct uiDrawTextLayout { PangoLayout *layout; - GPtrArray *backgroundParams; }; // we need a context for a few things @@ -51,7 +50,7 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *p) pango_layout_set_alignment(tl->layout, pangoAligns[p->Align]); - attrs = uiprivAttributedStringToPangoAttrList(p, &(tl->backgroundParams)); + attrs = uiprivAttributedStringToPangoAttrList(p); pango_layout_set_attributes(tl->layout, attrs); pango_attr_list_unref(attrs); @@ -60,18 +59,12 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *p) void uiDrawFreeTextLayout(uiDrawTextLayout *tl) { - g_ptr_array_unref(tl->backgroundParams); g_object_unref(tl->layout); uiprivFree(tl); } void uiDrawText(uiDrawContext *c, uiDrawTextLayout *tl, double x, double y) { - guint i; - - for (i = 0; i < tl->backgroundParams->len; i++) { - // TODO - } // TODO have an implicit save/restore on each drawing functions instead? and is this correct? cairo_set_source_rgb(c->cr, 0.0, 0.0, 0.0); cairo_move_to(c->cr, x, y); diff --git a/unix/future.c b/unix/future.c index 3d63e9e0..68730ead 100644 --- a/unix/future.c +++ b/unix/future.c @@ -8,6 +8,7 @@ // added in pango 1.38; we need 1.36 static PangoAttribute *(*newFeaturesAttr)(const gchar *features) = NULL; static PangoAttribute *(*newFGAlphaAttr)(guint16 alpha) = NULL; +static PangoAttribute *(*newBGAlphaAttr)(guint16 alpha) = NULL; // added in GTK+ 3.20; we need 3.10 static void (*gwpIterSetObjectName)(GtkWidgetPath *path, gint pos, const char *name) = NULL; @@ -24,6 +25,7 @@ void loadFutures(void) #define GET(var, fn) *((void **) (&var)) = dlsym(handle, #fn) GET(newFeaturesAttr, pango_attr_font_features_new); GET(newFGAlphaAttr, pango_attr_foreground_alpha_new); + GET(newBGAlphaAttr, pango_attr_background_alpha_new); GET(gwpIterSetObjectName, gtk_widget_path_iter_set_object_name); dlclose(handle); } @@ -42,6 +44,13 @@ PangoAttribute *FUTURE_pango_attr_foreground_alpha_new(guint16 alpha) return (*newFGAlphaAttr)(alpha); } +PangoAttribute *FUTURE_pango_attr_background_alpha_new(guint16 alpha) +{ + if (newBGAlphaAttr == NULL) + return NULL; + return (*newBGAlphaAttr)(alpha); +} + gboolean FUTURE_gtk_widget_path_iter_set_object_name(GtkWidgetPath *path, gint pos, const char *name) { if (gwpIterSetObjectName == NULL) diff --git a/unix/uipriv_unix.h b/unix/uipriv_unix.h index debcb0b2..43ed144b 100644 --- a/unix/uipriv_unix.h +++ b/unix/uipriv_unix.h @@ -57,4 +57,5 @@ extern GtkCellRenderer *newCellRendererButton(void); extern void loadFutures(void); extern PangoAttribute *FUTURE_pango_attr_font_features_new(const gchar *features); extern PangoAttribute *FUTURE_pango_attr_foreground_alpha_new(guint16 alpha); +extern PangoAttribute *FUTURE_pango_attr_background_alpha_new(guint16 alpha); extern gboolean FUTURE_gtk_widget_path_iter_set_object_name(GtkWidgetPath *path, gint pos, const char *name);