--- ./nx-X11/lib/Xrender/Glyph.c.X.original	2015-02-13 14:03:44.652443320 +0100
+++ ./nx-X11/lib/Xrender/Glyph.c	2015-02-10 19:13:12.580731749 +0100
@@ -27,6 +27,26 @@
 #endif
 #include "Xrenderint.h"
 
+/* 
+ * NX_RENDER_CLEANUP enables cleaning of padding bytes
+ */
+
+#define NX_RENDER_CLEANUP
+
+#define PANIC
+#define WARNING
+#undef  TEST
+#undef  DEBUG
+#undef  DUMP
+
+#ifdef NX_RENDER_CLEANUP
+
+#include <stdio.h>
+
+#define ROUNDUP(nbits, pad) ((((nbits) + ((pad)-1)) / (pad)) * ((pad)>>3))
+
+#endif /* NX_RENDER_CLEANUP */
+
 GlyphSet
 XRenderCreateGlyphSet (Display *dpy, _Xconst XRenderPictFormat *format)
 {
@@ -81,6 +101,248 @@
     SyncHandle();
 }
 
+#ifdef NX_RENDER_CLEANUP
+
+void
+XRenderCleanGlyphs(xGlyphInfo  *gi,
+                        int         nglyphs,
+                        CARD8       *images,
+                        int         depth,
+                        Display     *dpy)
+{
+
+  int widthInBits;
+  int bytesPerLine;
+  int bytesToClean;
+  int bitsToClean;
+  int widthInBytes;
+  int height = gi -> height;
+  register int i;
+  int j;
+
+  #ifdef DEBUG
+  fprintf(stderr, "nxagentCleanGlyphs: Found a Glyph with Depth %d, width %d, pad %d.\n",
+          depth, gi -> width, dpy -> bitmap_pad);
+  #endif
+
+  while (nglyphs > 0)
+  {
+    if (depth == 24)
+    {
+      widthInBits = gi -> width * 32;
+
+      bytesPerLine = ROUNDUP(widthInBits, dpy -> bitmap_pad);
+
+      bytesToClean = bytesPerLine * height;
+
+      #ifdef DUBUG
+      fprintf(stderr, "nxagentCleanGlyphs: Found glyph with depth 24, bytes to clean is %d"
+              "width in bits is %d bytes per line [%d] height [%d].\n", bytesToClean,
+                      widthInBits, bytesPerLine, height);
+      #endif
+
+      if (dpy -> byte_order == LSBFirst)
+      {
+        for (i = 3; i < bytesToClean; i += 4)
+        {
+          images[i] = 0x00;
+        }
+      }
+      else
+      {
+        for (i = 0; i < bytesToClean; i += 4)
+        {
+          images[i] = 0x00;
+        }
+      }
+
+      #ifdef DUMP
+      fprintf(stderr, "nxagentCleanGlyphs: depth %d, bytesToClean %d, scanline: ", depth, bytesToClean);
+      for (i = 0; i < bytesPerLine; i++)
+      {
+        fprintf(stderr, "[%d]", images[i]);
+      }
+      fprintf(stderr,"\n");
+      #endif
+
+      images += bytesToClean;
+
+      gi++;
+
+      nglyphs--;
+    }
+    else if (depth == 1)
+    {
+      widthInBits = gi -> width;
+
+      bytesPerLine = ROUNDUP(widthInBits, dpy -> bitmap_pad);
+
+      bitsToClean = (bytesPerLine << 3) - (gi -> width);
+
+      #ifdef DEBUG
+      fprintf(stderr, "nxagentCleanGlyphs: Found glyph with depth 1, width [%d], height [%d], bitsToClean [%d]," 
+              " bytesPerLine [%d].\n", gi -> width, height, bitsToClean, bytesPerLine);
+      #endif
+
+      bytesToClean = bitsToClean >> 3;
+
+      bitsToClean &= 7;
+
+      #ifdef DEBUG
+      fprintf(stderr, "nxagentCleanGlyphs: bitsToClean &=7 is %d, bytesToCLean is %d."
+              " byte_order is %d, bitmap_bit_order is %d.\n", bitsToClean, bytesToClean,
+              dpy -> byte_order, dpy -> bitmap_bit_order);
+      #endif
+
+      for (i = 1; i <= height; i++)
+      {
+        if (dpy -> byte_order == dpy -> bitmap_bit_order)
+        {
+          for (j = 1; j <= bytesToClean; j++)
+          {
+            images[i * bytesPerLine - j] = 0x00;
+
+            #ifdef DEBUG
+            fprintf(stderr, "nxagentCleanGlyphs: byte_order = bitmap_bit_orde, cleaning %d, i=%d, j=%d.\n"
+                    , (i * bytesPerLine - j), i, j);
+            #endif
+
+          }
+        }
+        else
+        {
+          for (j = bytesToClean; j >= 1; j--)
+          {
+            images[i * bytesPerLine - j] = 0x00;
+
+            #ifdef DEBUG
+            fprintf(stderr, "nxagentCleanGlyphs: byte_order %d, bitmap_bit_order %d, cleaning %d, i=%d, j=%d.\n"
+                    , dpy -> byte_order, dpy -> bitmap_bit_order, (i * bytesPerLine - j), i, j);
+            #endif
+
+          }
+        }
+
+        if (dpy -> bitmap_bit_order == MSBFirst)
+        {
+          images[i * bytesPerLine - j] &= 0xff << bitsToClean;
+
+          #ifdef DEBUG
+          fprintf(stderr, "nxagentCleanGlyphs: byte_order MSBFirst, cleaning %d, i=%d, j=%d.\n"
+                  , (i * bytesPerLine - j), i, j);
+          #endif
+        }
+        else
+        {
+          images[i * bytesPerLine - j] &= 0xff >> bitsToClean;
+
+          #ifdef DEBUG
+          fprintf(stderr, "nxagentCleanGlyphs: byte_order LSBFirst, cleaning %d, i=%d, j=%d.\n"
+                  , (i * bytesPerLine - j), i, j);
+          #endif
+        }
+      }
+  
+      #ifdef DUMP
+      fprintf(stderr, "nxagentCleanGlyphs: depth %d, bytesToClean %d, scanline: ", depth, bytesToClean);
+      for (i = 0; i < bytesPerLine; i++)
+      {
+        fprintf(stderr, "[%d]", images[i]);
+      }
+      fprintf(stderr,"\n");
+      #endif
+
+      images += bytesPerLine * height;
+
+      gi++;
+
+      nglyphs--;
+    }
+    else if ((depth == 8) || (depth == 16) )
+    {
+      widthInBits = gi -> width * depth;
+
+      bytesPerLine = ROUNDUP(widthInBits, dpy -> bitmap_pad);
+
+      widthInBytes = (widthInBits >> 3);
+
+      bytesToClean = bytesPerLine - widthInBytes;
+
+      #ifdef DEBUG
+      fprintf(stderr, "nxagentCleanGlyphs: nglyphs is %d, width of glyph in bits is %d, in bytes is %d.\n",
+              nglyphs, widthInBits, widthInBytes);
+
+      fprintf(stderr, "nxagentCleanGlyphs: bytesPerLine is %d bytes, there are %d scanlines.\n", bytesPerLine, height);
+
+      fprintf(stderr, "nxagentCleanGlyphs: Bytes to clean for each scanline are %d.\n", bytesToClean);
+      #endif
+
+      if (bytesToClean > 0)
+      {
+        while (height > 0)
+        {
+          i = bytesToClean;
+
+          while (i > 0)
+          {
+            *(images + (bytesPerLine - i)) = 0;
+
+            #ifdef DEBUG
+            fprintf(stderr, "nxagentCleanGlyphs: cleaned a byte.\n");
+            #endif
+
+            i--;
+          }
+
+          #ifdef DUMP
+          fprintf(stderr, "nxagentCleanGlyphs: depth %d, bytesToClean %d, scanline: ", depth, bytesToClean);
+          for (i = 0; i < bytesPerLine; i++)
+          {
+            fprintf(stderr, "[%d]", images[i]);
+          }
+          fprintf(stderr,"\n");
+          #endif
+
+          images += bytesPerLine;
+
+          height--;
+        }
+      }
+
+      gi++;
+
+      nglyphs--;
+
+      #ifdef DEBUG
+      fprintf(stderr, "nxagentCleanGlyphs: Breaking Out.\n");
+      #endif
+    }
+    else if (depth == 32)
+    {
+      #ifdef DEBUG
+      fprintf(stderr, "nxagentCleanGlyphs: Found glyph with depth 32.\n");
+      #endif
+
+      gi++;
+
+      nglyphs--;
+    }
+    else
+    {
+      #ifdef WARNING
+      fprintf(stderr, "nxagentCleanGlyphs: Unrecognized glyph, depth is not 8/16/24/32, it appears to be %d.\n",
+              depth);
+      #endif
+
+      gi++;
+
+      nglyphs--;
+    }
+  }
+}
+
+#endif /* #ifdef NX_RENDER_CLEANUP */
+
 void
 XRenderAddGlyphs (Display	*dpy,
 		  GlyphSet	glyphset,
@@ -404,6 +666,14 @@
     _Xconst char		*chars;
     int				nchars;
 
+    #ifdef NX_RENDER_CLEANUP
+
+    char 			tmpChar[4];
+    int				bytes_to_clean;
+    int				bytes_to_write;
+
+    #endif /* NX_RENDER_CLEANUP */
+
     if (!nelt)
 	return;
     
@@ -464,6 +734,14 @@
 	{
 	    glyphset = elts[i].glyphset;
 	    BufAlloc (xGlyphElt *, elt, SIZEOF (xGlyphElt));
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = 0xff;
 	    elt->deltax = 0;
 	    elt->deltay = 0;
@@ -478,11 +756,88 @@
 	    int this_chars = nchars > MAX_8 ? MAX_8 : nchars;
 
 	    BufAlloc (xGlyphElt *, elt, SIZEOF(xGlyphElt))
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = this_chars;
 	    elt->deltax = xDst;
 	    elt->deltay = yDst;
 	    xDst = 0;
 	    yDst = 0;
+
+            #ifdef NX_RENDER_CLEANUP
+
+            bytes_to_write = this_chars & ~3;
+
+            bytes_to_clean = ((this_chars + 3) & ~3) - this_chars;
+
+            #ifdef DEBUG
+            fprintf(stderr, "XRenderCompositeText8: bytes_to_write %d, bytes_to_clean are %d,"
+                        " this_chars %d.\n", bytes_to_write, bytes_to_clean, this_chars);
+            #endif
+
+            if (bytes_to_clean > 0)
+            {
+              if (bytes_to_write > 0)
+               {
+                #ifdef DEBUG
+                fprintf(stderr, "XRenderCompositeText8: found %d clean bytes, bytes_to_clean are %d,"
+                        " this_chars %d.\n", bytes_to_write, bytes_to_clean, this_chars);
+                #endif
+
+                Data (dpy, chars, bytes_to_write);
+                chars += bytes_to_write;
+              }
+
+              bytes_to_write = this_chars % 4;
+              memcpy (tmpChar, chars, bytes_to_write);
+              chars += bytes_to_write;
+
+              #ifdef DEBUG
+              fprintf(stderr, "XRenderCompositeText8: last 32 bit, bytes_to_write are %d,"
+                      " bytes_to_clean are %d, this_chars are %d.\n", bytes_to_write, bytes_to_clean, this_chars);
+              #endif
+
+              #ifdef DUMP
+              fprintf(stderr, "XRenderCompositeText8: bytes_to_clean %d, ", bytes_to_clean);
+              #endif
+
+              while (bytes_to_clean > 0)
+              {
+                tmpChar[4 - bytes_to_clean] = 0;
+                bytes_to_clean--;
+
+                #ifdef DEBUG
+                fprintf(stderr, "XRenderCompositeText8: Cleaned %d byte.\n", 4 - bytes_to_clean);
+                #endif
+              }
+
+              Data (dpy, tmpChar, 4);
+              nchars -= this_chars;
+
+              #ifdef DUMP
+              fprintf(stderr, "Data: ");
+              for (i = 0; i < 4; i++)
+              {
+                fprintf(stderr, "[%d]", tmpChar[i]);
+              }
+              fprintf(stderr,"\n");
+              #endif
+
+              #ifdef DEBUG
+              fprintf(stderr, "XRenderCompositeText8: nchars now is %d.\n", nchars);
+              #endif
+
+              continue;
+            }
+
+            #endif /* NX_RENDER_CLEANUP */
+
 	    Data (dpy, chars, this_chars);
 	    nchars -= this_chars;
 	    chars += this_chars;
@@ -517,6 +872,14 @@
     _Xconst unsigned short    	*chars;
     int				nchars;
 
+    #ifdef NX_RENDER_CLEANUP
+
+    int				bytes_to_write;
+    int				bytes_to_clean;
+    char			tmpChar[4];
+
+    #endif /* NX_RENDER_CLEANUP */
+
     if (!nelt)
 	return;
     
@@ -574,6 +937,14 @@
 	{
 	    glyphset = elts[i].glyphset;
 	    BufAlloc (xGlyphElt *, elt, SIZEOF (xGlyphElt));
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = 0xff;
 	    elt->deltax = 0;
 	    elt->deltay = 0;
@@ -587,13 +958,77 @@
 	{
 	    int this_chars = nchars > MAX_16 ? MAX_16 : nchars;
 	    int this_bytes = this_chars * 2;
-    
+
 	    BufAlloc (xGlyphElt *, elt, SIZEOF(xGlyphElt))
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = this_chars;
 	    elt->deltax = xDst;
 	    elt->deltay = yDst;
 	    xDst = 0;
 	    yDst = 0;
+	    
+            #ifdef NX_RENDER_CLEANUP
+
+            bytes_to_write = this_bytes & ~3;
+            bytes_to_clean = ((this_bytes + 3) & ~3) - this_bytes;
+
+            #ifdef DEBUG
+            fprintf(stderr, "XRenderCompositeText16: this_chars %d, this_bytes %d.\n"
+                    "bytes_to_write %d, bytes_to_clean are %d.\n", this_chars, this_bytes,
+                    bytes_to_write, bytes_to_clean);
+            #endif
+
+            if (bytes_to_clean > 0)
+            {
+              if (bytes_to_write > 0)
+              {
+                Data16 (dpy, chars, bytes_to_write);
+
+                /*
+                 * Cast chars to avoid errors with pointer arithmetic.
+                 */
+
+                chars = (unsigned short *) ((char *) chars + bytes_to_write);
+              }
+
+              bytes_to_write = this_bytes % 4;
+              memcpy (tmpChar, (char *) chars, bytes_to_write);
+              chars = (unsigned short *) ((char *) chars + bytes_to_write);
+
+              #ifdef DEBUG
+              fprintf(stderr, "XRenderCompositeText16: last 32 bit, bytes_to_write are %d,"
+                      " bytes_to_clean are %d.\n", bytes_to_write, bytes_to_clean);
+              #endif
+
+              while (bytes_to_clean > 0)
+              {
+                tmpChar[4 - bytes_to_clean] = 0;
+                bytes_to_clean--;
+
+                #ifdef DEBUG
+                fprintf(stderr, "XRenderCompositeText16: Cleaned %d byte.\n", 4 - bytes_to_clean);
+                #endif
+              }
+
+              Data16 (dpy, tmpChar, 4);
+              nchars -= this_chars;
+
+              #ifdef DEBUG
+              fprintf(stderr, "XRenderCompositeText16: nchars now is %d.\n", nchars);
+              #endif
+
+              continue;
+            }
+
+            #endif /* NX_RENDER_CLEANUP */
+
 	    Data16 (dpy, chars, this_bytes);
 	    nchars -= this_chars;
 	    chars += this_chars;
@@ -681,6 +1116,14 @@
 	{
 	    glyphset = elts[i].glyphset;
 	    BufAlloc (xGlyphElt *, elt, SIZEOF (xGlyphElt));
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = 0xff;
 	    elt->deltax = 0;
 	    elt->deltay = 0;
@@ -695,11 +1138,25 @@
 	    int this_chars = nchars > MAX_32 ? MAX_32 : nchars;
 	    int this_bytes = this_chars * 4;
 	    BufAlloc (xGlyphElt *, elt, SIZEOF(xGlyphElt))
+
+	    #ifdef NX_RENDER_CLEANUP
+
+	    elt->pad1 = 0;
+	    elt->pad2 = 0;
+
+	    #endif /* NX_RENDER_CLEANUP */
+
 	    elt->len = this_chars;
 	    elt->deltax = xDst;
 	    elt->deltay = yDst;
 	    xDst = 0;
 	    yDst = 0;
+
+            #ifdef TEST
+            fprintf(stderr, "XRenderCompositeText32: this_chars %d, this_bytes %d.\n",
+                    this_chars, this_bytes);
+            #endif
+
 	    DataInt32 (dpy, chars, this_bytes);
 	    nchars -= this_chars;
 	    chars += this_chars;
