Fix color order regression.
[libguac-client-rdp.git] / src / rdp_glyph.c
old mode 100755 (executable)
new mode 100644 (file)
index c6fbce0..603a104
@@ -20,6 +20,7 @@
  * the Initial Developer. All Rights Reserved.
  *
  * Contributor(s):
+ * Matt Hortman
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
 #include <freerdp/freerdp.h>
 
 #include <guacamole/client.h>
+#include <guacamole/error.h>
 
 #include "client.h"
 #include "rdp_glyph.h"
 
 void guac_rdp_glyph_new(rdpContext* context, rdpGlyph* glyph) {
 
-    /* Allocate buffer */
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
-    guac_socket* socket = client->socket;
-    guac_layer* layer = guac_client_alloc_buffer(client);
-
     int x, y, i;
     int stride;
     unsigned char* image_buffer;
@@ -58,8 +55,6 @@ void guac_rdp_glyph_new(rdpContext* context, rdpGlyph* glyph) {
     int width  = glyph->cx;
     int height = glyph->cy;
 
-    cairo_surface_t* surface;
-
     /* Init Cairo buffer */
     stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
     image_buffer = malloc(height*stride);
@@ -96,16 +91,9 @@ void guac_rdp_glyph_new(rdpContext* context, rdpGlyph* glyph) {
         }
     }
 
-    surface = cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride);
-    guac_protocol_send_png(socket, GUAC_COMP_SRC, layer, 0, 0, surface);
-    guac_socket_flush(socket);
-
-    /* Free surface */
-    cairo_surface_destroy(surface);
-    free(image_buffer);
-
-    /* Store layer */
-    ((guac_rdp_glyph*) glyph)->layer = layer;
+    /* Store glyph surface */
+    ((guac_rdp_glyph*) glyph)->surface = cairo_image_surface_create_for_data(
+            image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride);
 
 }
 
@@ -113,51 +101,131 @@ void guac_rdp_glyph_draw(rdpContext* context, rdpGlyph* glyph, int x, int y) {
 
     guac_client* client = ((rdp_freerdp_context*) context)->client;
     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
-    const guac_layer* current_layer = ((rdp_guac_client_data*) client->data)->current_surface;
-  
-    /* Colorize glyph */
-    guac_protocol_send_rect(client->socket,
-            GUAC_COMP_ATOP, ((guac_rdp_glyph*) glyph)->layer,
-            0, 0, glyph->cx, glyph->cy,
-            guac_client_data->foreground.red,
-            guac_client_data->foreground.green,
-            guac_client_data->foreground.blue,
-            255);
-
-    /* Draw glyph */
-    guac_protocol_send_copy(client->socket,
-           ((guac_rdp_glyph*) glyph)->layer, 0, 0, glyph->cx, glyph->cy,
-           GUAC_COMP_OVER, current_layer, x, y); 
+
+    /* Do not attempt to draw glyphs if glyph drawing is not begun */
+    if (guac_client_data->glyph_cairo == NULL)
+        return;
+
+    /* Use glyph as mask */
+    cairo_mask_surface(
+            guac_client_data->glyph_cairo,
+            ((guac_rdp_glyph*) glyph)->surface, x, y);
 
 }
 
 void guac_rdp_glyph_free(rdpContext* context, rdpGlyph* glyph) {
-    guac_client* client = ((rdp_freerdp_context*) context)->client;
-    guac_client_free_buffer(client, ((guac_rdp_glyph*) glyph)->layer);
+
+    unsigned char* image_buffer = cairo_image_surface_get_data(
+            ((guac_rdp_glyph*) glyph)->surface);
+
+    /* Free surface */
+    cairo_surface_destroy(((guac_rdp_glyph*) glyph)->surface);
+    free(image_buffer);
+
 }
 
 void guac_rdp_glyph_begindraw(rdpContext* context,
         int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
 
     guac_client* client = ((rdp_freerdp_context*) context)->client;
-    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
-
-    bgcolor = freerdp_color_convert_var(bgcolor,
-            context->instance->settings->color_depth, 32,
-            ((rdp_freerdp_context*) context)->clrconv);
+    rdp_guac_client_data* guac_client_data =
+        (rdp_guac_client_data*) client->data;
 
+    /* Convert foreground color */
     fgcolor = freerdp_color_convert_var(fgcolor,
             context->instance->settings->color_depth, 32,
             ((rdp_freerdp_context*) context)->clrconv);
 
-    guac_client_data->foreground.red   =  fgcolor & 0x0000FF;
-    guac_client_data->foreground.green = (fgcolor & 0x00FF00) >> 8;
-    guac_client_data->foreground.blue  = (fgcolor & 0xFF0000) >> 16;
+    /* Fill background with color if specified */
+    if (width != 0 && height != 0) {
+
+        /* Prepare for opaque glyphs */
+        guac_client_data->glyph_surface = 
+            guac_client_data->opaque_glyph_surface;
+
+        /* Create cairo instance */
+        guac_client_data->glyph_cairo = cairo_create(
+            guac_client_data->glyph_surface);
+
+        /* Convert background color */
+        bgcolor = freerdp_color_convert_var(bgcolor,
+                context->instance->settings->color_depth, 32,
+                ((rdp_freerdp_context*) context)->clrconv);
+
+        /* Fill background */
+        cairo_rectangle(guac_client_data->glyph_cairo,
+                x, y, width, height);
+
+        cairo_set_source_rgb(guac_client_data->glyph_cairo,
+                ((bgcolor & 0xFF0000) >> 16) / 255.0,
+                ((bgcolor & 0x00FF00) >> 8 ) / 255.0,
+                ( bgcolor & 0x0000FF       ) / 255.0);
+
+        cairo_fill(guac_client_data->glyph_cairo);
+
+    }
+
+    /* Otherwise, prepare for transparent glyphs  */
+    else {
+
+        /* Select transparent glyph surface */
+        guac_client_data->glyph_surface = 
+            guac_client_data->trans_glyph_surface;
+
+        guac_client_data->glyph_cairo = cairo_create(
+            guac_client_data->glyph_surface);
+
+        /* Clear surface */
+        cairo_set_operator(guac_client_data->glyph_cairo,
+            CAIRO_OPERATOR_SOURCE);
+
+        cairo_set_source_rgba(guac_client_data->glyph_cairo, 0, 0, 0, 0);
+        cairo_paint(guac_client_data->glyph_cairo);
+
+        /* Restore operator */
+        cairo_set_operator(guac_client_data->glyph_cairo,
+            CAIRO_OPERATOR_OVER);
+
+    }
+
+    /* Prepare for glyph drawing */
+    cairo_set_source_rgb(guac_client_data->glyph_cairo,
+            ((fgcolor & 0xFF0000) >> 16) / 255.0,
+            ((fgcolor & 0x00FF00) >> 8 ) / 255.0,
+            ( fgcolor & 0x0000FF       ) / 255.0);
 
 }
 
 void guac_rdp_glyph_enddraw(rdpContext* context,
         int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
-    /* UNUSED */
+
+    guac_client* client = ((rdp_freerdp_context*) context)->client;
+    rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
+    const guac_layer* current_layer = ((rdp_guac_client_data*) client->data)->current_surface;
+
+    /* Use glyph surface to provide image data for glyph rectangle */
+    cairo_surface_t* glyph_surface = guac_client_data->glyph_surface;
+    int stride = cairo_image_surface_get_stride(glyph_surface);
+
+    /* Ensure data is ready */
+    cairo_surface_flush(glyph_surface);
+
+    /* Create surface for subsection with text */
+    cairo_surface_t* surface = cairo_image_surface_create_for_data(
+            cairo_image_surface_get_data(glyph_surface) + 4*x + y*stride,
+            cairo_image_surface_get_format(glyph_surface),
+            width, height, stride);
+
+    /* Send surface with all glyphs to layer */
+    guac_protocol_send_png(client->socket,
+            GUAC_COMP_OVER, current_layer, x, y,
+            surface);
+
+    /* Destroy surface */
+    cairo_surface_destroy(surface);
+
+    /* Destroy cairo instance */
+    cairo_destroy(guac_client_data->glyph_cairo);
+
 }