2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include <freerdp/freerdp.h>
41 #include <guacamole/client.h>
42 #include <guacamole/error.h>
45 #include "rdp_glyph.h"
47 void guac_rdp_glyph_new(rdpContext* context, rdpGlyph* glyph) {
51 unsigned char* image_buffer;
52 unsigned char* image_buffer_row;
54 unsigned char* data = glyph->aj;
55 int width = glyph->cx;
56 int height = glyph->cy;
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;
63 /* Copy image data from image data to buffer */
64 for (y = 0; y<height; y++) {
66 unsigned int* image_buffer_current;
68 /* Get current buffer row, advance to next */
69 image_buffer_current = (unsigned int*) image_buffer_row;
70 image_buffer_row += stride;
72 for (x = 0; x<width;) {
74 /* Get byte from image data */
75 unsigned int v = *(data++);
77 /* Read bits, write pixels */
78 for (i = 0; i<8 && x<width; i++, x++) {
82 *(image_buffer_current++) = 0xFF000000;
84 *(image_buffer_current++) = 0x00000000;
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);
100 void guac_rdp_glyph_draw(rdpContext* context, rdpGlyph* glyph, int x, int y) {
102 guac_client* client = ((rdp_freerdp_context*) context)->client;
103 rdp_guac_client_data* guac_client_data = (rdp_guac_client_data*) client->data;
105 /* Do not attempt to draw glyphs if glyph drawing is not begun */
106 if (guac_client_data->glyph_cairo == NULL)
109 /* Use glyph as mask */
111 guac_client_data->glyph_cairo,
112 ((guac_rdp_glyph*) glyph)->surface, x, y);
116 void guac_rdp_glyph_free(rdpContext* context, rdpGlyph* glyph) {
118 unsigned char* image_buffer = cairo_image_surface_get_data(
119 ((guac_rdp_glyph*) glyph)->surface);
122 cairo_surface_destroy(((guac_rdp_glyph*) glyph)->surface);
127 void guac_rdp_glyph_begindraw(rdpContext* context,
128 int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
130 guac_client* client = ((rdp_freerdp_context*) context)->client;
131 rdp_guac_client_data* guac_client_data =
132 (rdp_guac_client_data*) client->data;
134 /* Convert foreground color */
135 fgcolor = freerdp_color_convert_var(fgcolor,
136 context->instance->settings->color_depth, 32,
137 ((rdp_freerdp_context*) context)->clrconv);
139 /* Fill background with color if specified */
140 if (width != 0 && height != 0) {
142 /* Prepare for opaque glyphs */
143 guac_client_data->glyph_surface =
144 guac_client_data->opaque_glyph_surface;
146 /* Create cairo instance */
147 guac_client_data->glyph_cairo = cairo_create(
148 guac_client_data->glyph_surface);
150 /* Convert background color */
151 bgcolor = freerdp_color_convert_var(bgcolor,
152 context->instance->settings->color_depth, 32,
153 ((rdp_freerdp_context*) context)->clrconv);
155 /* Fill background */
156 cairo_rectangle(guac_client_data->glyph_cairo,
157 x, y, width, height);
159 cairo_set_source_rgb(guac_client_data->glyph_cairo,
160 ( bgcolor & 0x0000FF ) / 255.0,
161 ((bgcolor & 0x00FF00) >> 8 ) / 255.0,
162 ((bgcolor & 0xFF0000) >> 16) / 255.0);
164 cairo_fill(guac_client_data->glyph_cairo);
168 /* Otherwise, prepare for transparent glyphs */
171 /* Select transparent glyph surface */
172 guac_client_data->glyph_surface =
173 guac_client_data->trans_glyph_surface;
175 guac_client_data->glyph_cairo = cairo_create(
176 guac_client_data->glyph_surface);
179 cairo_set_operator(guac_client_data->glyph_cairo,
180 CAIRO_OPERATOR_SOURCE);
182 cairo_set_source_rgba(guac_client_data->glyph_cairo, 0, 0, 0, 0);
183 cairo_paint(guac_client_data->glyph_cairo);
185 /* Restore operator */
186 cairo_set_operator(guac_client_data->glyph_cairo,
187 CAIRO_OPERATOR_OVER);
191 /* Prepare for glyph drawing */
192 cairo_set_source_rgb(guac_client_data->glyph_cairo,
193 ( fgcolor & 0x0000FF ) / 255.0,
194 ((fgcolor & 0x00FF00) >> 8 ) / 255.0,
195 ((fgcolor & 0xFF0000) >> 16) / 255.0);
199 void guac_rdp_glyph_enddraw(rdpContext* context,
200 int x, int y, int width, int height, uint32 fgcolor, uint32 bgcolor) {
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;
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);
210 /* Ensure data is ready */
211 cairo_surface_flush(glyph_surface);
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);
219 /* Send surface with all glyphs to layer */
220 guac_protocol_send_png(client->socket,
221 GUAC_COMP_OVER, current_layer, x, y,
224 /* Destroy surface */
225 cairo_surface_destroy(surface);
227 /* Destroy cairo instance */
228 cairo_destroy(guac_client_data->glyph_cairo);