pango_color_parse()



I've just committed the follow patch that publically exports
pango_color_parse(). Making gdk_color_parse() not based on
XParseColor() gives a number of advantages:

 - same code can be used on Windows, meaning that we don't
   need separate copies of this in pango and in the windows
   (fb, etc.) backend for GDK. 

 - same code can be used on all platforms, preventing subtle
   incompatibilies.

 - gdk_color_parse() will be display independent when we 
   support multihead, avoiding one area that app code would
   need fixing.

Regards,
                                        Owen

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/pango/ChangeLog,v
retrieving revision 1.303
diff -u -r1.303 ChangeLog
--- ChangeLog	2001/08/24 16:23:06	1.303
+++ ChangeLog	2001/08/30 21:43:59
@@ -1,3 +1,12 @@
+Thu Aug 30 17:34:52 2001  Owen Taylor  <otaylor redhat com>
+
+	* pango/pango-color.c: New file, move color handling
+	here.
+
+	* pango/pango-attributes.[ch] pango/pango-markup.c
+	pango/pango-color.c: Export pango_color_parse, change
+	it to take a PangoColor as its argument.
+
 Fri Aug 24 12:21:50 2001  Owen Taylor  <otaylor redhat com>
 
 	* pango/pango-layout.c (pango_layout_set_text): Handle
Index: pango/Makefile.am
===================================================================
RCS file: /cvs/gnome/pango/pango/Makefile.am,v
retrieving revision 1.53
diff -u -r1.53 Makefile.am
--- pango/Makefile.am	2001/08/12 14:34:27	1.53
+++ pango/Makefile.am	2001/08/30 21:43:59
@@ -65,6 +65,7 @@
 	mapping.c 	   \
 	modules.c 	   \
 	pango-attributes.c \
+	pango-color.c	   \
 	pango-context.c    \
 	pango-coverage.c   \
 	pango-fontmap.c	   \
Index: pango/pango-attributes.c
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-attributes.c,v
retrieving revision 1.28
diff -u -r1.28 pango-attributes.c
--- pango/pango-attributes.c	2001/07/12 16:34:40	1.28
+++ pango/pango-attributes.c	2001/08/30 21:43:59
@@ -1539,57 +1539,3 @@
 	}
     }
 }
-
-GType
-pango_color_get_type (void)
-{
-  static GType our_type = 0;
-  
-  if (our_type == 0)
-    our_type = g_boxed_type_register_static ("PangoColor",
-					     NULL,
-					     (GBoxedCopyFunc) pango_color_copy,
-					     (GBoxedFreeFunc) pango_color_free,
-					     FALSE);
-
-  return our_type;
-}
-
-/**
- * pango_color_copy:
- * @src: color to copy
- * 
- * Creates a copy of @src, which should be freed with
- * pango_color_free(). Primarily used by language bindings,
- * not that useful otherwise (since colors can just be copied
- * by assignment in C).
- * 
- * Return value: an allocated #PangoColor
- **/
-PangoColor*
-pango_color_copy (const PangoColor *src)
-{
-  PangoColor *ret;
-
-  g_return_val_if_fail (src != NULL, NULL);
-  
-  ret = g_new (PangoColor, 1);
-
-  *ret = *src;
-
-  return ret;
-}
-
-/**
- * pango_color_free:
- * @color: an allocated #PangoColor
- * 
- * Frees a color allocated by pango_color_copy().
- **/
-void
-pango_color_free (PangoColor *color)
-{
-  g_return_if_fail (color != NULL);
-
-  g_free (color);
-}
Index: pango/pango-attributes.h
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-attributes.h,v
retrieving revision 1.16
diff -u -r1.16 pango-attributes.h
--- pango/pango-attributes.h	2001/06/14 20:38:25	1.16
+++ pango/pango-attributes.h	2001/08/30 21:43:59
@@ -45,6 +45,8 @@
 
 PangoColor *pango_color_copy     (const PangoColor *src);
 void        pango_color_free     (PangoColor       *color);
+gboolean    pango_color_parse    (PangoColor       *color,
+				  const char       *spec);
 
 /* Attributes */
 
Index: pango/pango-markup.c
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-markup.c,v
retrieving revision 1.9
diff -u -r1.9 pango-markup.c
--- pango/pango-markup.c	2001/07/02 03:00:10	1.9
+++ pango/pango-markup.c	2001/08/30 21:43:59
@@ -22,7 +22,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <stdio.h>
 #include <ctype.h>
 
 #include <pango/pango-attributes.h>
@@ -32,11 +31,6 @@
 /* FIXME */
 #define _(x) x
 
-static gboolean pango_color_parse (const char *spec,
-                                   guint16    *red,
-                                   guint16    *green,
-                                   guint16    *blue);
-
 /* CSS size levels */
 typedef enum
 {
@@ -1088,9 +1082,9 @@
 
   if (foreground)
     {
-      guint16 red, green, blue;
+      PangoColor color;
 
-      if (!pango_color_parse (foreground, &red, &green, &blue))
+      if (!pango_color_parse (&color, foreground))
         {
           g_set_error (error,
                        G_MARKUP_ERROR,
@@ -1101,14 +1095,14 @@
           goto error;
         }
 
-      add_attribute (tag, pango_attr_foreground_new (red, green, blue));
+      add_attribute (tag, pango_attr_foreground_new (color.red, color.green, color.blue));
     }
 
   if (background)
     {
-      guint16 red, green, blue;
-
-      if (!pango_color_parse (background, &red, &green, &blue))
+      PangoColor color;
+      
+      if (!pango_color_parse (&color, background))
         {
           g_set_error (error,
                        G_MARKUP_ERROR,
@@ -1119,7 +1113,7 @@
           goto error;
         }
 
-      add_attribute (tag, pango_attr_background_new (red, green, blue));
+      add_attribute (tag, pango_attr_background_new (color.red, color.green, color.blue));
     }
 
   if (underline)
============
/* pango
 * pango-color.c: Color handling
 *
 * Copyright (C) 2000 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pango-attributes.h"

GType
pango_color_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
    our_type = g_boxed_type_register_static ("PangoColor",
					     NULL,
					     (GBoxedCopyFunc) pango_color_copy,
					     (GBoxedFreeFunc) pango_color_free,
					     FALSE);

  return our_type;
}

/**
 * pango_color_copy:
 * @src: color to copy
 * 
 * Creates a copy of @src, which should be freed with
 * pango_color_free(). Primarily used by language bindings,
 * not that useful otherwise (since colors can just be copied
 * by assignment in C).
 * 
 * Return value: an allocated #PangoColor
 **/
PangoColor*
pango_color_copy (const PangoColor *src)
{
  PangoColor *ret;

  g_return_val_if_fail (src != NULL, NULL);
  
  ret = g_new (PangoColor, 1);

  *ret = *src;

  return ret;
}

/**
 * pango_color_free:
 * @color: an allocated #PangoColor
 * 
 * Frees a color allocated by pango_color_copy().
 **/
void
pango_color_free (PangoColor *color)
{
  g_return_if_fail (color != NULL);

  g_free (color);
}

/* Color parsing
 */

/* The following 2 routines (parse_color, find_color) come from Tk, via the Win32
 * port of GDK. The licensing terms on these (longer than the functions) is:
 *
 * This software is copyrighted by the Regents of the University of
 * California, Sun Microsystems, Inc., and other parties.  The following
 * terms apply to all files associated with the software unless explicitly
 * disclaimed in individual files.
 *
 * The authors hereby grant permission to use, copy, modify, distribute,
 * and license this software and its documentation for any purpose, provided
 * that existing copyright notices are retained in all copies and that this
 * notice is included verbatim in any distributions. No written agreement,
 * license, or royalty fee is required for any of the authorized uses.
 * Modifications to this software may be copyrighted by their authors
 * and need not follow the licensing terms described here, provided that
 * the new terms are clearly indicated on the first page of each file where
 * they apply.
 *
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 * MODIFICATIONS.
 *
 * GOVERNMENT USE: If you are acquiring this software on behalf of the
 * U.S. government, the Government shall have only "Restricted Rights"
 * in the software and related documentation as defined in the Federal
 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
 * are acquiring the software on behalf of the Department of Defense, the
 * software shall be classified as "Commercial Computer Software" and the
 * Government shall have only "Restricted Rights" as defined in Clause
 * 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
 * authors grant the U.S. Government and others acting in its behalf
 * permission to use and distribute the software in accordance with the
 * terms specified in this license.
 */

typedef struct {
    const char *name;
    unsigned char red;
    unsigned char green;
    unsigned char blue;
} ColorEntry;

static ColorEntry xColors[] = {
  { "alice blue", 240, 248, 255 },
  { "AliceBlue", 240, 248, 255 },
  { "antique white", 250, 235, 215 },
  { "AntiqueWhite", 250, 235, 215 },
  { "AntiqueWhite1", 255, 239, 219 },
  { "AntiqueWhite2", 238, 223, 204 },
  { "AntiqueWhite3", 205, 192, 176 },
  { "AntiqueWhite4", 139, 131, 120 },
[...]
  { "WhiteSmoke", 245, 245, 245 },
  { "yellow", 255, 255, 0 },
  { "yellow green", 154, 205, 50 },
  { "yellow1", 255, 255, 0 },
  { "yellow2", 238, 238, 0 },
  { "yellow3", 205, 205, 0 },
  { "yellow4", 139, 139, 0 },
  { "YellowGreen", 154, 205, 50 }
};

static int
compare_xcolor_entries (const void *a, const void *b)
{
  return g_strcasecmp ((const char *) a, ((const ColorEntry *) b)->name);
}

static gboolean
find_color(const char *name,
	   PangoColor *color)
{
  ColorEntry *found;

  found = bsearch (name, xColors, G_N_ELEMENTS (xColors),
                   sizeof (ColorEntry),
                   compare_xcolor_entries);
  if (found == NULL)
    return FALSE;

  if (color)
    {
      color->red = (found->red * 65535) / 255;
      color->blue = (found->green * 65535) / 255;
      color->green = (found->blue * 65535) / 255;
    }

  return TRUE;
}

/**
 * pango_color_parse:
 * @color: a #PangoColor structure in which to store the result
 * @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 X11 rgb.txt file), or it can be a hex value in the
 * form '#rgb' '#rrggbb' '#rrrgggbbb' or '#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 '#fff' '#ffffff' '#fffffffff' and '#ffffffffffff')
 *
 * Return value: TRUE if parsing of the specifier succeeded,
 *   otherwise false.
 **/
gboolean
pango_color_parse (PangoColor *color,
		   const char *spec)
{
  if (spec[0] == '#')
    {
      char fmt[16];
      int i, r, g, b;

      if ((i = strlen (spec+1)) % 3)
        return FALSE;

      i /= 3;

      sprintf (fmt, "%%%dx%%%dx%%%dx", i, i, i);
      if (sscanf (spec+1, fmt, &r, &g, &b) != 3)
        return FALSE;

      if (i == 4)
        {
          if (color)
	    {
	      color->red = r;
	      color->green = g;
	      color->blue = b;
	    }
        }
      else if (i == 1)
        {
          if (color)
	    {
	      color->red = (r * 65535) / 15;
	      color->green = (g * 65535) / 15;
	      color->blue = (b * 65535) / 15;
	    }
        }
      else if (i == 2)
        {
          if (color)
	    {
	      color->red = (r * 65535) / 255;
	      color->green = (g * 65535) / 255;
	      color->blue = (b * 65535) / 255;
	    }
        }
      else /* if (i == 3) */
        {
	  if (color)
	    {
	      color->red = (r * 65535) / 4095;
	      color->green = (g * 65535) / 4095;
	      color->blue = (b * 65535) / 4095;
	    }
        }
    }
  else
    {
      if (!find_color (spec, color))
        return FALSE;
    }

  return TRUE;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]