Fix color order regression.
[libguac-client-rdp.git] / src / rdp_glyph.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is libguac-client-rdp.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2011
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Matt Hortman
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #include <freerdp/freerdp.h>
40
41 #include <guacamole/client.h>
42 #include <guacamole/error.h>
43
44 #include "client.h"
45 #include "rdp_glyph.h"
46
47 void guac_rdp_glyph_new(rdpContext* context, rdpGlyph* glyph) {
48
49     int x, y, i;
50     int stride;
51     unsigned char* image_buffer;
52     unsigned char* image_buffer_row;
53
54     unsigned char* data = glyph->aj;
55     int width  = glyph->cx;
56     int height = glyph->cy;
57
58     /* Init Cairo buffer */
59     stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
60     image_buffer = malloc(height*stride);
61     image_buffer_row = image_buffer;
62
63     /* Copy image data from image data to buffer */
64     for (y = 0; y<height; y++) {
65
66         unsigned int*  image_buffer_current;
67         
68         /* Get current buffer row, advance to next */
69         image_buffer_current  = (unsigned int*) image_buffer_row;
70         image_buffer_row     += stride;
71
72         for (x = 0; x<width;) {
73
74             /* Get byte from image data */
75             unsigned int v = *(data++);
76
77             /* Read bits, write pixels */
78             for (i = 0; i<8 && x<width; i++, x++) {
79
80                 /* Output RGB */
81                 if (v & 0x80)
82                     *(image_buffer_current++) = 0xFF000000;
83                 else
84                     *(image_buffer_current++) = 0x00000000;
85
86                 /* Next bit */
87                 v <<= 1;
88
89             }
90
91         }
92     }
93
94     /* Store glyph surface */
95     ((guac_rdp_glyph*) glyph)->surface = cairo_image_surface_create_for_data(
96             image_buffer, CAIRO_FORMAT_ARGB32, width, height, stride);
97
98 }
99
100 void guac_rdp_glyph_draw(rdpContext* context, rdpGlyph* glyph, int x, int y) {
101
102     guac_client* client = ((rdp_freerdp_context*) context)->client;
103     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
104
105     /* Do not attempt to draw glyphs if glyph drawing is not begun */
106     if (guac_client_data->glyph_cairo == NULL)
107         return;
108
109     /* Use glyph as mask */
110     cairo_mask_surface(
111             guac_client_data->glyph_cairo,
112             ((guac_rdp_glyph*) glyph)->surface, x, y);
113
114 }
115
116 void guac_rdp_glyph_free(rdpContext* context, rdpGlyph* glyph) {
117
118     unsigned char* image_buffer = cairo_image_surface_get_data(
119             ((guac_rdp_glyph*) glyph)->surface);
120
121     /* Free surface */
122     cairo_surface_destroy(((guac_rdp_glyph*) glyph)->surface);
123     free(image_buffer);
124
125 }
126
127 void guac_rdp_glyph_begindraw(rdpContext* context,
128         int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
129
130     guac_client* client = ((rdp_freerdp_context*) context)->client;
131     rdp_guac_client_data* guac_client_data =
132         (rdp_guac_client_data*) client->data;
133
134     /* Convert foreground color */
135     fgcolor = freerdp_color_convert_var(fgcolor,
136             context->instance->settings->color_depth, 32,
137             ((rdp_freerdp_context*) context)->clrconv);
138
139     /* Fill background with color if specified */
140     if (width != 0 && height != 0) {
141
142         /* Prepare for opaque glyphs */
143         guac_client_data->glyph_surface = 
144             guac_client_data->opaque_glyph_surface;
145
146         /* Create cairo instance */
147         guac_client_data->glyph_cairo = cairo_create(
148             guac_client_data->glyph_surface);
149
150         /* Convert background color */
151         bgcolor = freerdp_color_convert_var(bgcolor,
152                 context->instance->settings->color_depth, 32,
153                 ((rdp_freerdp_context*) context)->clrconv);
154
155         /* Fill background */
156         cairo_rectangle(guac_client_data->glyph_cairo,
157                 x, y, width, height);
158
159         cairo_set_source_rgb(guac_client_data->glyph_cairo,
160                 ((bgcolor & 0xFF0000) >> 16) / 255.0,
161                 ((bgcolor & 0x00FF00) >> 8 ) / 255.0,
162                 ( bgcolor & 0x0000FF       ) / 255.0);
163
164         cairo_fill(guac_client_data->glyph_cairo);
165
166     }
167
168     /* Otherwise, prepare for transparent glyphs  */
169     else {
170
171         /* Select transparent glyph surface */
172         guac_client_data->glyph_surface = 
173             guac_client_data->trans_glyph_surface;
174
175         guac_client_data->glyph_cairo = cairo_create(
176             guac_client_data->glyph_surface);
177
178         /* Clear surface */
179         cairo_set_operator(guac_client_data->glyph_cairo,
180             CAIRO_OPERATOR_SOURCE);
181
182         cairo_set_source_rgba(guac_client_data->glyph_cairo, 0, 0, 0, 0);
183         cairo_paint(guac_client_data->glyph_cairo);
184
185         /* Restore operator */
186         cairo_set_operator(guac_client_data->glyph_cairo,
187             CAIRO_OPERATOR_OVER);
188
189     }
190
191     /* Prepare for glyph drawing */
192     cairo_set_source_rgb(guac_client_data->glyph_cairo,
193             ((fgcolor & 0xFF0000) >> 16) / 255.0,
194             ((fgcolor & 0x00FF00) >> 8 ) / 255.0,
195             ( fgcolor & 0x0000FF       ) / 255.0);
196
197 }
198
199 void guac_rdp_glyph_enddraw(rdpContext* context,
200         int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
201
202     guac_client* client = ((rdp_freerdp_context*) context)->client;
203     rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
204     const guac_layer* current_layer = ((rdp_guac_client_data*) client->data)->current_surface;
205
206     /* Use glyph surface to provide image data for glyph rectangle */
207     cairo_surface_t* glyph_surface = guac_client_data->glyph_surface;
208     int stride = cairo_image_surface_get_stride(glyph_surface);
209
210     /* Ensure data is ready */
211     cairo_surface_flush(glyph_surface);
212
213     /* Create surface for subsection with text */
214     cairo_surface_t* surface = cairo_image_surface_create_for_data(
215             cairo_image_surface_get_data(glyph_surface) + 4*x + y*stride,
216             cairo_image_surface_get_format(glyph_surface),
217             width, height, stride);
218
219     /* Send surface with all glyphs to layer */
220     guac_protocol_send_png(client->socket,
221             GUAC_COMP_OVER, current_layer, x, y,
222             surface);
223
224     /* Destroy surface */
225     cairo_surface_destroy(surface);
226
227     /* Destroy cairo instance */
228     cairo_destroy(guac_client_data->glyph_cairo);
229
230 }
231