+void RrAppearanceFree(RrAppearance *a)
+{
+ gint i;
+
+ if (a) {
+ RrSurface *p;
+ if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
+ if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
+ for (i = 0; i < a->textures; ++i)
+ if (a->texture[i].type == RR_TEXTURE_RGBA) {
+ g_free(a->texture[i].data.rgba.cache);
+ a->texture[i].data.rgba.cache = NULL;
+ }
+ if (a->textures)
+ g_free(a->texture);
+ p = &a->surface;
+ RrColorFree(p->primary);
+ RrColorFree(p->secondary);
+ RrColorFree(p->border_color);
+ RrColorFree(p->interlace_color);
+ RrColorFree(p->bevel_dark);
+ RrColorFree(p->bevel_light);
+ g_free(p->pixel_data);
+ p->pixel_data = NULL;
+ g_free(a);
+ }
+}
+
+
+static void pixel_data_to_pixmap(RrAppearance *l,
+ gint x, gint y, gint w, gint h)
+{
+ RrPixel32 *in, *scratch;
+ Pixmap out;
+ XImage *im = NULL;
+ im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
+ ZPixmap, 0, NULL, w, h, 32, 0);
+ g_assert(im != NULL);
+
+ in = l->surface.pixel_data;
+ out = l->pixmap;
+
+/* this malloc is a complete waste of time on normal 32bpp
+ as reduce_depth just sets im->data = data and returns
+*/
+ scratch = g_new(RrPixel32, im->width * im->height);
+ im->data = (gchar*) scratch;
+ RrReduceDepth(l->inst, in, im);
+ XPutImage(RrDisplay(l->inst), out,
+ DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
+ im, 0, 0, x, y, w, h);
+ im->data = NULL;
+ XDestroyImage(im);
+ g_free(scratch);
+}
+
+void RrMargins (RrAppearance *a, gint *l, gint *t, gint *r, gint *b)
+{
+ *l = *t = *r = *b = 0;
+
+ if (a->surface.grad != RR_SURFACE_PARENTREL) {
+ if (a->surface.relief != RR_RELIEF_FLAT) {
+ switch (a->surface.bevel) {
+ case RR_BEVEL_1:
+ *l = *t = *r = *b = 1;
+ break;
+ case RR_BEVEL_2:
+ *l = *t = *r = *b = 2;
+ break;
+ }
+ } else if (a->surface.border) {
+ *l = *t = *r = *b = 1;
+ }
+ }
+}
+
+void RrMinSize(RrAppearance *a, gint *w, gint *h)
+{
+ *w = RrMinWidth(a);
+ *h = RrMinHeight(a);
+}
+
+gint RrMinWidth(RrAppearance *a)
+{
+ gint i;
+ RrSize *m;
+ gint l, t, r, b;
+ gint w = 0;
+
+ for (i = 0; i < a->textures; ++i) {
+ switch (a->texture[i].type) {
+ case RR_TEXTURE_NONE:
+ break;
+ case RR_TEXTURE_MASK:
+ w = MAX(w, a->texture[i].data.mask.mask->width);
+ break;
+ case RR_TEXTURE_TEXT:
+ m = RrFontMeasureString(a->texture[i].data.text.font,
+ a->texture[i].data.text.string,
+ a->texture[i].data.text.shadow_offset_x,
+ a->texture[i].data.text.shadow_offset_y);
+ w = MAX(w, m->width + 4);
+ g_free(m);
+ break;
+ case RR_TEXTURE_RGBA:
+ w += MAX(w, a->texture[i].data.rgba.width);
+ break;
+ case RR_TEXTURE_LINE_ART:
+ w += MAX(w, MAX(a->texture[i].data.lineart.x1,
+ a->texture[i].data.lineart.x2));
+ break;
+ }
+ }
+
+ RrMargins(a, &l, &t, &r, &b);
+
+ w += l + r;
+
+ if (w < 1) w = 1;
+ return w;
+}
+
+gint RrMinHeight(RrAppearance *a)
+{
+ gint i;
+ gint l, t, r, b;
+ gint h = 0;
+
+ for (i = 0; i < a->textures; ++i) {
+ switch (a->texture[i].type) {
+ case RR_TEXTURE_NONE:
+ break;
+ case RR_TEXTURE_MASK:
+ h = MAX(h, a->texture[i].data.mask.mask->height);
+ break;
+ case RR_TEXTURE_TEXT:
+ h += MAX(h, RrFontHeight(a->texture[i].data.text.font,
+ a->texture[i].data.text.shadow_offset_y));
+ break;
+ case RR_TEXTURE_RGBA:
+ h += MAX(h, a->texture[i].data.rgba.height);
+ break;
+ case RR_TEXTURE_LINE_ART:
+ h += MAX(h, MAX(a->texture[i].data.lineart.y1,
+ a->texture[i].data.lineart.y2));
+ break;
+ }
+ }
+
+ RrMargins(a, &l, &t, &r, &b);
+
+ h += t + b;
+
+ if (h < 1) h = 1;
+ return h;
+}
+
+static void reverse_bits(gchar *c, gint n)