summaryrefslogtreecommitdiff
path: root/pango
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2020-08-03 03:13:35 +0000
committerMatthias Clasen <mclasen@redhat.com>2020-08-03 03:13:35 +0000
commitc33e272822020833a15b09bf15f59a2e078b6953 (patch)
tree48f86a0d589927b0ee388c0e24ce3185f4576774 /pango
parent98b3ff0b967c7ecb53e119e914d6e8cb65584ad3 (diff)
parent77c8408675c6e8aaded386932aad66004004060a (diff)
downloadpango-c33e272822020833a15b09bf15f59a2e078b6953.tar.gz
Merge branch 'parse-color-with-alpha' into 'master'
Export pango_parse_color_with_alpha See merge request GNOME/pango!215
Diffstat (limited to 'pango')
-rw-r--r--pango/pango-attributes.h7
-rw-r--r--pango/pango-color.c89
-rw-r--r--pango/pango-markup.c6
-rw-r--r--pango/pango-utils-internal.h4
4 files changed, 64 insertions, 42 deletions
diff --git a/pango/pango-attributes.h b/pango/pango-attributes.h
index 6f6622ab..dd38aee2 100644
--- a/pango/pango-attributes.h
+++ b/pango/pango-attributes.h
@@ -65,7 +65,12 @@ PANGO_AVAILABLE_IN_ALL
void pango_color_free (PangoColor *color);
PANGO_AVAILABLE_IN_ALL
gboolean pango_color_parse (PangoColor *color,
- const char *spec);
+ const char *spec);
+PANGO_AVAILABLE_IN_1_46
+gboolean pango_color_parse_with_alpha
+ (PangoColor *color,
+ guint16 *alpha,
+ const char *spec);
PANGO_AVAILABLE_IN_1_16
gchar *pango_color_to_string(const PangoColor *color);
diff --git a/pango/pango-color.c b/pango/pango-color.c
index 3c37c3d0..9c044810 100644
--- a/pango/pango-color.c
+++ b/pango/pango-color.c
@@ -207,20 +207,40 @@ hex (const char *spec,
}
-/* Like pango_color_parse, but allow strings of the form
+/**
+ * pango_color_parse_with_alpha:
+ * @color: (nullable): a #PangoColor structure in which to store the
+ * result, or %NULL
+ * @alpha: (nullable): return location for alpha, or %NULL
+ * @spec: a string specifying the new color
+ *
+ * Fill in the fields of a color from a string specification. The
+ * string can either one of a large set of standard names. (Taken
+ * from the CSS <ulink url="http://dev.w3.org/csswg/css-color/#named-colors">specification</ulink>), or it can be a hexadecimal
+ * value in the
+ * form '&num;rgb' '&num;rrggbb' '&num;rrrgggbbb' or '&num;rrrrggggbbbb' where
+ * 'r', 'g' and 'b' are hex digits of the red, green, and blue
+ * components of the color, respectively. (White in the four
+ * forms is '&num;fff' '&num;ffffff' '&num;fffffffff' and '&num;ffffffffffff')
+ *
+ * Additionally, parse strings of the form
* '&num;rgba', '&num;rrggbbaa', '&num;rrrrggggbbbbaaaa',
- * if alpha is not NULL. If no alpha component is found
- * in the string, *alpha is set to 0.
+ * if @alpha is not %NULL, and set @alpha to the value specified
+ * by the hex digits for 'a'. If no alpha component is found
+ * in @spec, @alpha is set to 0xffff (for a solid color).
+ *
+ * Return value: %TRUE if parsing of the specifier succeeded,
+ * otherwise false.
*/
gboolean
-_pango_color_parse_with_alpha (PangoColor *color,
- guint16 *alpha,
- const char *spec)
+pango_color_parse_with_alpha (PangoColor *color,
+ guint16 *alpha,
+ const char *spec)
{
g_return_val_if_fail (spec != NULL, FALSE);
if (alpha)
- *alpha = 0;
+ *alpha = 0xffff;
if (spec[0] == '#')
{
@@ -248,52 +268,53 @@ _pango_color_parse_with_alpha (PangoColor *color,
has_alpha = TRUE;
break;
default:
- return FALSE;
+ return FALSE;
}
if (!hex (spec, len, &r) ||
- !hex (spec + len, len, &g) ||
- !hex (spec + len * 2, len, &b) ||
+ !hex (spec + len, len, &g) ||
+ !hex (spec + len * 2, len, &b) ||
(has_alpha && !hex (spec + len * 3, len, &a)))
- return FALSE;
+ return FALSE;
if (color)
- {
- int bits = len * 4;
- r <<= 16 - bits;
- g <<= 16 - bits;
- b <<= 16 - bits;
- while (bits < 16)
- {
- r |= (r >> bits);
- g |= (g >> bits);
- b |= (b >> bits);
- bits *= 2;
- }
- color->red = r;
- color->green = g;
- color->blue = b;
- }
+ {
+ int bits = len * 4;
+ r <<= 16 - bits;
+ g <<= 16 - bits;
+ b <<= 16 - bits;
+ while (bits < 16)
+ {
+ r |= (r >> bits);
+ g |= (g >> bits);
+ b |= (b >> bits);
+ bits *= 2;
+ }
+ color->red = r;
+ color->green = g;
+ color->blue = b;
+ }
if (alpha && has_alpha)
{
- int bits = len * 4;
+ int bits = len * 4;
a <<= 16 - bits;
- while (bits < 16)
- {
+ while (bits < 16)
+ {
a |= (a >> bits);
- bits *= 2;
- }
+ bits *= 2;
+ }
*alpha = a;
}
}
else
{
if (!find_color (spec, color))
- return FALSE;
+ return FALSE;
}
return TRUE;
}
+
/**
* pango_color_parse:
* @color: (nullable): a #PangoColor structure in which to store the
@@ -316,5 +337,5 @@ gboolean
pango_color_parse (PangoColor *color,
const char *spec)
{
- return _pango_color_parse_with_alpha (color, NULL, spec);
+ return pango_color_parse_with_alpha (color, NULL, spec);
}
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index a67e10fd..b74c1ad4 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -1199,7 +1199,7 @@ span_parse_color (const char *attr_name,
int line_number,
GError **error)
{
- if (!_pango_color_parse_with_alpha (color, alpha, attr_val))
+ if (!pango_color_parse_with_alpha (color, alpha, attr_val))
{
g_set_error (error,
G_MARKUP_ERROR,
@@ -1622,7 +1622,7 @@ span_parse_func (MarkupData *md G_GNUC_UNUSED,
goto error;
add_attribute (tag, pango_attr_foreground_new (color.red, color.green, color.blue));
- if (alpha != 0)
+ if (alpha != 0xffff)
add_attribute (tag, pango_attr_foreground_alpha_new (alpha));
}
@@ -1635,7 +1635,7 @@ span_parse_func (MarkupData *md G_GNUC_UNUSED,
goto error;
add_attribute (tag, pango_attr_background_new (color.red, color.green, color.blue));
- if (alpha != 0)
+ if (alpha != 0xffff)
add_attribute (tag, pango_attr_background_alpha_new (alpha));
}
diff --git a/pango/pango-utils-internal.h b/pango/pango-utils-internal.h
index 56340215..0bc355e0 100644
--- a/pango/pango-utils-internal.h
+++ b/pango/pango-utils-internal.h
@@ -43,10 +43,6 @@ gboolean pango_parse_flags (GType type,
char *_pango_trim_string (const char *str);
-gboolean _pango_color_parse_with_alpha (PangoColor *color,
- guint16 *alpha,
- const char *spec);
-
G_END_DECLS