bool TextPainter::prepareBuffer() { if (_cairoWidth <= 0 || _cairoHeight <= 0) return false; if (_context) cairo_destroy(_context); if (_surface) cairo_surface_destroy(_surface); // ensure a valid opengl context OpenGl::Guard guard; // bind buffer for writing glCheck(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, _buf)); // discard previous buffer (so we don't have to wait for GPU until we can // map) and create new buffer glCheck(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, _cairoWidth*_cairoHeight*sizeof(cairo_pixel_t), 0, GL_DYNAMIC_DRAW)); // bind buffer for reading glCheck(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, _buf)); LOG_ALL(textpainterlog) << "determine raster position of " << _glRoi.minX + 1.0/_lastResolution.x << ", " << _glRoi.maxY - 1.0/_lastResolution.y << " [gl]" << std::endl; // get the window coordinates of the lower-left corner of the rectangle we // want to draw to int r[4]; glCheck(glRasterPos2f(_glRoi.minX + 1.0/_lastResolution.x, _glRoi.maxY - 1.0/_lastResolution.y)); glCheck(glGetIntegerv(GL_CURRENT_RASTER_POSITION, r)); _rasterPos.x = r[0]; _rasterPos.y = r[1]; LOG_ALL(textpainterlog) << "read content from raster position " << _rasterPos << std::endl; // load current content to buffer glCheck(glReadPixels( _rasterPos.x, _rasterPos.y, _cairoWidth, _cairoHeight, detail::pixel_format_traits<cairo_pixel_t>::gl_format, detail::pixel_format_traits<cairo_pixel_t>::gl_type, 0)); // map the pixel buffer object unsigned char* p = (unsigned char*)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB); // wrap the buffer in a cairo surface _surface = cairo_image_surface_create_for_data( p, CAIRO_FORMAT_ARGB32, _cairoWidth, _cairoHeight, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, _cairoWidth)); // create a context for the surface _context = cairo_create(_surface); // set antialiasing options cairo_set_font_options(_context, _fontOptions); return true; }
static gboolean on_expose_event (GtkWidget * widget, GdkEventExpose * event, gpointer *project) { cairo_t *cr; GdkWindow *gdkw; gint i; gdkw = gtk_layout_get_bin_window (GTK_LAYOUT(widget)); cr = gdk_cairo_create (gdkw); cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_line_width (cr, 1); if(!layoutexp) { for(i=0;i<=MAXLEN12;i++) { if(pertchart[i].name != NULL) { drawbutton(widget,pertchart[i].name,pertchart[i].x,pertchart[i].y,pertchart[i].type,project); } } for(i=0;i<Lin;i++) { cr = gdk_cairo_create (gdkw); cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_line_width (cr, 1); cairo_move_to (cr, arrow[i].x1,arrow[i].y1); cairo_line_to (cr, arrow[i].x2,arrow[i].y2); cairo_move_to (cr, arrow[i].x2,arrow[i].y2); cairo_line_to (cr, arrow[i].x3,arrow[i].y3); cairo_move_to (cr, arrow[i].x3,arrow[i].y3); cairo_line_to (cr, arrow[i].x4,arrow[i].y4); cairo_stroke_preserve (cr); cairo_set_source_rgb (cr, 1, 1, 1); cairo_fill (cr); cairo_destroy (cr); } } else { for(i=0;i<Lin;i++) { cr = gdk_cairo_create (gdkw); cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_line_width (cr, 1); cairo_move_to (cr, arrow[i].x1,arrow[i].y1); cairo_line_to (cr, arrow[i].x2,arrow[i].y2); cairo_move_to (cr, arrow[i].x2,arrow[i].y2); cairo_line_to (cr, arrow[i].x3,arrow[i].y3); cairo_move_to (cr, arrow[i].x3,arrow[i].y3); cairo_line_to (cr, arrow[i].x4,arrow[i].y4); cairo_stroke_preserve (cr); cairo_set_source_rgb (cr, 1, 1, 1); cairo_fill (cr); cairo_destroy (cr);} } layoutexp++; return FALSE; }
static gboolean dt_iop_levels_expose(GtkWidget *widget, GdkEventExpose *event, gpointer user_data) { dt_iop_module_t *self = (dt_iop_module_t *)user_data; dt_iop_levels_gui_data_t *c = (dt_iop_levels_gui_data_t *)self->gui_data; dt_iop_levels_params_t *p = (dt_iop_levels_params_t *)self->params; const int inset = DT_GUI_CURVE_EDITOR_INSET; int width = widget->allocation.width, height = widget->allocation.height; cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); cairo_t *cr = cairo_create(cst); float mean_picked_color = *self->picked_color / 100.0; /* we need to save the last picked color to prevent flickering when * changing from one picker to another, as the picked_color value does not * update as rapidly */ if(self->request_color_pick && self->color_picker_point[0] >= 0.0f && self->color_picker_point[1] >= 0.0f && mean_picked_color != c->last_picked_color) { float previous_color[3]; previous_color[0] = p->levels[0]; previous_color[1] = p->levels[1]; previous_color[2] = p->levels[2]; c->last_picked_color = mean_picked_color; if (BLACK == c->current_pick) { if (mean_picked_color > p->levels[1]) { p->levels[0] = p->levels[1]-FLT_EPSILON; } else { p->levels[0] = mean_picked_color; } c->pick_xy_positions[0][0] = self->color_picker_point[0]; c->pick_xy_positions[0][1] = self->color_picker_point[1]; } else if (GREY == c->current_pick) { if (mean_picked_color < p->levels[0] || mean_picked_color > p->levels[2]) { p->levels[1] = p->levels[1]; } else { p->levels[1] = mean_picked_color; } c->pick_xy_positions[1][0] = self->color_picker_point[0]; c->pick_xy_positions[1][1] = self->color_picker_point[1]; } else if (WHITE == c->current_pick) { if (mean_picked_color < p->levels[1]) { p->levels[2] = p->levels[1]+FLT_EPSILON; } else { p->levels[2] = mean_picked_color; } c->pick_xy_positions[2][0] = self->color_picker_point[0]; c->pick_xy_positions[2][1] = self->color_picker_point[1]; } if ( previous_color[0] != p->levels[0] || previous_color[1] != p->levels[1] || previous_color[2] != p->levels[2] ) { dt_dev_add_history_item(darktable.develop, self, TRUE); } } // clear bg cairo_set_source_rgb (cr, .2, .2, .2); cairo_paint(cr); cairo_translate(cr, inset, inset); width -= 2*inset; height -= 2*inset; cairo_set_line_width(cr, 1.0); cairo_set_source_rgb (cr, .1, .1, .1); cairo_rectangle(cr, 0, 0, width, height); cairo_stroke(cr); cairo_set_source_rgb (cr, .3, .3, .3); cairo_rectangle(cr, 0, 0, width, height); cairo_fill(cr); // draw grid cairo_set_line_width(cr, .4); cairo_set_source_rgb (cr, .1, .1, .1); dt_draw_vertical_lines(cr, 4, 0, 0, width, height); // Drawing the vertical line indicators cairo_set_line_width(cr, 2.); for(int k = 0; k < 3; k++) { if(k == c->handle_move && c->mouse_x > 0) cairo_set_source_rgb(cr, 1, 1, 1); else cairo_set_source_rgb(cr, .7, .7, .7); cairo_move_to(cr, width*p->levels[k], height); cairo_rel_line_to(cr, 0, -height); cairo_stroke(cr); } // draw x positions cairo_set_line_width(cr, 1.); const float arrw = 7.0f; for(int k=0; k<3; k++) { switch(k) { case 0: cairo_set_source_rgb(cr, 0, 0, 0); break; case 1: cairo_set_source_rgb(cr, 0.5, 0.5, 0.5); break; default: cairo_set_source_rgb(cr, 1, 1, 1); break; } cairo_move_to(cr, width*p->levels[k], height+inset-1); cairo_rel_line_to(cr, -arrw*.5f, 0); cairo_rel_line_to(cr, arrw*.5f, -arrw); cairo_rel_line_to(cr, arrw*.5f, arrw); cairo_close_path(cr); if(c->handle_move == k && c->mouse_x > 0) cairo_fill(cr); else cairo_stroke(cr); } cairo_translate(cr, 0, height); // draw lum histogram in background // only if the module is enabled if (self->enabled) { dt_develop_t *dev = darktable.develop; float *hist, hist_max; hist = self->histogram; hist_max = dev->histogram_linear?self->histogram_max[0]:logf(1.0 + self->histogram_max[0]); if(hist && hist_max > 0) { cairo_save(cr); cairo_scale(cr, width/63.0, -(height-5)/(float)hist_max); cairo_set_source_rgba(cr, .2, .2, .2, 0.5); dt_draw_histogram_8(cr, hist, 0); cairo_restore(cr); } } // Cleaning up cairo_destroy(cr); cairo_t *cr_pixmap = gdk_cairo_create(gtk_widget_get_window(widget)); cairo_set_source_surface (cr_pixmap, cst, 0, 0); cairo_paint(cr_pixmap); cairo_destroy(cr_pixmap); cairo_surface_destroy(cst); return TRUE; }
SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) { // build background from image GdkPixbuf* special_icon = gdk_pixbuf_new_from_file("message-mail-new.png", NULL); // GError **error); GdkPixbuf *dest = gdk_pixbuf_copy(special_icon); int w=gdk_pixbuf_get_width(special_icon); int h=gdk_pixbuf_get_height(special_icon); // prepare colors/alpha GdkColormap* cmap=gdk_screen_get_system_colormap(gdk_screen_get_default()); int screen_depth=24; GdkVisual* visual = gdk_colormap_get_visual(cmap); screen_depth = visual->depth; GdkColor fore = { 0, 0, 0, 0 }; GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; gdk_color_parse(color, &fore); if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) { alpha.red=0; // make sure alpha is different from fore } gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE); gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE); // build pixmap with rectangle GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth); cairo_t *cr = gdk_cairo_create(pm); gdk_cairo_set_source_color(cr, &alpha); /* void gdk_cairo_set_source_color (cairo_t *cr, */ /* const GdkColor *color); */ cairo_rectangle(cr, 0, 0, w, h); /* void cairo_rectangle (cairo_t *cr, */ /* double x, */ /* double y, */ /* double width, */ /* double height); */ cairo_set_source_rgb(cr, 1, 1, 1); cairo_fill(cr); // build text GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL); PangoLayout *layout = gtk_widget_create_pango_layout(scratch, NULL); gtk_widget_destroy(scratch); PangoFontDescription *fnt = pango_font_description_from_string("Sans 18"); pango_font_description_set_weight (fnt,PANGO_WEIGHT_SEMIBOLD); pango_layout_set_spacing (layout,0); pango_layout_set_font_description (layout, fnt); pango_layout_set_text (layout, (gchar *)text,-1); int tw=0; int th=0; int sz; int border=4; pango_layout_get_pixel_size(layout, &tw, &th); while( (tw>w - border || th > h - border)) //fit text to the icon by decreasing font size { sz=pango_font_description_get_size (fnt); if(sz<MIN_FONT_SIZE) { sz=MIN_FONT_SIZE; break; } sz-=PANGO_SCALE; pango_font_description_set_size (fnt,sz); pango_layout_set_font_description (layout, fnt); pango_layout_get_pixel_size(layout, &tw, &th); } pango_font_description_free (fnt); // center text int px, py; px=(w-tw)/2; py=(h-th)/2; // draw text on pixmap gdk_cairo_set_source_color(cr, &fore); cairo_move_to (cr, px, py); pango_cairo_show_layout (cr, layout); cairo_destroy(cr); g_object_unref (layout); GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h); g_object_unref (pm); GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha(buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue); g_object_unref (buf); //merge the rendered text on top gdk_pixbuf_composite(alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); g_object_unref(alpha_buf); /* gdk_pixbuf_composite(buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); */ /* g_object_unref(buf); */ gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest)); }
static void render (GtkWidget * widget) { GSMColorButton *color_button = GSM_COLOR_BUTTON (widget); GdkColor *color, tmp_color = color_button->priv->color; color = &tmp_color; cairo_t *cr = gdk_cairo_create (gtk_widget_get_window (widget)); cairo_path_t *path = NULL; gint width, height; gdouble radius, arc_start, arc_end; gint highlight_factor; if (color_button->priv->highlight > 0) { highlight_factor = 8192 * color_button->priv->highlight; if (color->red + highlight_factor > 65535) color->red = 65535; else color->red = color->red + highlight_factor; if (color->blue + highlight_factor > 65535) color->blue = 65535; else color->blue = color->blue + highlight_factor; if (color->green + highlight_factor > 65535) color->green = 65535; else color->green = color->green + highlight_factor; } gdk_cairo_set_source_color (cr, color); #if GTK_CHECK_VERSION(3, 0, 0) width = gdk_window_get_width(gtk_widget_get_window(widget)); height = gdk_window_get_height(gtk_widget_get_window(widget)); #else gdk_drawable_get_size(gtk_widget_get_window(widget), &width, &height); #endif switch (color_button->priv->type) { case GSMCP_TYPE_CPU: //gtk_widget_set_size_request (widget, GSMCP_MIN_WIDTH, GSMCP_MIN_HEIGHT); cairo_paint (cr); cairo_set_line_width (cr, 1); cairo_set_source_rgba (cr, 0, 0, 0, 0.5); cairo_rectangle (cr, 0.5, 0.5, width - 1, height - 1); cairo_stroke (cr); cairo_set_line_width (cr, 1); cairo_set_source_rgba (cr, 1, 1, 1, 0.4); cairo_rectangle (cr, 1.5, 1.5, width - 3, height - 3); cairo_stroke (cr); break; case GSMCP_TYPE_PIE: if (width < 32) // 32px minimum size gtk_widget_set_size_request (widget, 32, 32); if (width < height) radius = width / 2; else radius = height / 2; arc_start = -G_PI_2 + 2 * G_PI * color_button->priv->fraction; arc_end = -G_PI_2; cairo_set_line_width (cr, 1); // Draw external stroke and fill if (color_button->priv->fraction < 0.01) { cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, 4.5, 0, 2 * G_PI); } else if (color_button->priv->fraction > 0.99) { cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, 0, 2 * G_PI); } else { cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, arc_start, arc_end); cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, 4.5, arc_end, arc_start); cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 2.25, arc_start, arc_start); } cairo_fill_preserve (cr); cairo_set_source_rgba (cr, 0, 0, 0, 0.7); cairo_stroke (cr); // Draw internal highlight cairo_set_source_rgba (cr, 1, 1, 1, 0.45); cairo_set_line_width (cr, 1); if (color_button->priv->fraction < 0.03) { cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, 3.25, 0, 2 * G_PI); } else if (color_button->priv->fraction > 0.99) { cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, 0, 2 * G_PI); } else { cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, arc_start + (1 / (radius - 3.75)), arc_end - (1 / (radius - 3.75))); cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, 3.25, arc_end - (1 / (radius - 3.75)), arc_start + (1 / (radius - 3.75))); cairo_arc_negative (cr, (width / 2) + .5, (height / 2) + .5, radius - 3.5, arc_start + (1 / (radius - 3.75)), arc_start + (1 / (radius - 3.75))); } cairo_stroke (cr); // Draw external shape cairo_set_line_width (cr, 1); cairo_set_source_rgba (cr, 0, 0, 0, 0.2); cairo_arc (cr, (width / 2) + .5, (height / 2) + .5, radius - 1.25, 0, G_PI * 2); cairo_stroke (cr); break; case GSMCP_TYPE_NETWORK_IN: if (color_button->priv->image_buffer == NULL) color_button->priv->image_buffer = fill_image_buffer_from_file (cr, DATADIR "/pixmaps/mate-system-monitor/download.svg"); gtk_widget_set_size_request (widget, 32, 32); cairo_move_to (cr, 8.5, 1.5); cairo_line_to (cr, 23.5, 1.5); cairo_line_to (cr, 23.5, 11.5); cairo_line_to (cr, 29.5, 11.5); cairo_line_to (cr, 16.5, 27.5); cairo_line_to (cr, 15.5, 27.5); cairo_line_to (cr, 2.5, 11.5); cairo_line_to (cr, 8.5, 11.5); cairo_line_to (cr, 8.5, 1.5); cairo_close_path (cr); path = cairo_copy_path (cr); cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE); cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER); cairo_set_line_width (cr, 1); cairo_fill_preserve (cr); cairo_set_miter_limit (cr, 5.0); cairo_stroke (cr); cairo_set_source_rgba (cr, 0, 0, 0, 0.5); cairo_append_path (cr, path); cairo_path_destroy(path); cairo_stroke (cr); cairo_set_source_surface (cr, color_button->priv->image_buffer, 0.0, 0.0); cairo_paint (cr); break; case GSMCP_TYPE_NETWORK_OUT: if (color_button->priv->image_buffer == NULL) color_button->priv->image_buffer = fill_image_buffer_from_file (cr, DATADIR "/pixmaps/mate-system-monitor/upload.svg"); gtk_widget_set_size_request (widget, 32, 32); cairo_move_to (cr, 16.5, 1.5); cairo_line_to (cr, 29.5, 17.5); cairo_line_to (cr, 23.5, 17.5); cairo_line_to (cr, 23.5, 27.5); cairo_line_to (cr, 8.5, 27.5); cairo_line_to (cr, 8.5, 17.5); cairo_line_to (cr, 2.5, 17.5); cairo_line_to (cr, 15.5, 1.5); cairo_line_to (cr, 16.5, 1.5); cairo_close_path (cr); path = cairo_copy_path (cr); cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE); cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER); cairo_set_line_width (cr, 1); cairo_fill_preserve (cr); cairo_set_miter_limit (cr, 5.0); cairo_stroke (cr); cairo_set_source_rgba (cr, 0, 0, 0, 0.5); cairo_append_path (cr, path); cairo_path_destroy(path); cairo_stroke (cr); cairo_set_source_surface (cr, color_button->priv->image_buffer, 0.0, 0.0); cairo_paint (cr); break; } cairo_destroy (cr); }
void setup_tile (gint w, gint h) { cairo_status_t status; cairo_t* cr = NULL; cairo_surface_t* cr_surf = NULL; cairo_surface_t* tmp = NULL; cairo_surface_t* dummy_surf = NULL; cairo_surface_t* norm_surf = NULL; cairo_surface_t* blur_surf = NULL; gdouble width = (gdouble) w; gdouble height = (gdouble) h; raico_blur_t* blur = NULL; cr_surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 3 * BUBBLE_SHADOW_SIZE, 3 * BUBBLE_SHADOW_SIZE); status = cairo_surface_status (cr_surf); if (status != CAIRO_STATUS_SUCCESS) g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); cr = cairo_create (cr_surf); status = cairo_status (cr); if (status != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (cr_surf); g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); } // clear and render drop-shadow and bubble-background cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); if (g_composited) { draw_shadow (cr, width, height, BUBBLE_SHADOW_SIZE, CORNER_RADIUS); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); draw_round_rect (cr, 1.0f, (gdouble) BUBBLE_SHADOW_SIZE, (gdouble) BUBBLE_SHADOW_SIZE, (gdouble) CORNER_RADIUS, (gdouble) (width - 2.0f * BUBBLE_SHADOW_SIZE), (gdouble) (height - 2.0f* BUBBLE_SHADOW_SIZE)); cairo_fill (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr, BUBBLE_BG_COLOR_R, BUBBLE_BG_COLOR_G, BUBBLE_BG_COLOR_B, 0.95f); } else cairo_set_source_rgb (cr, BUBBLE_BG_COLOR_R, BUBBLE_BG_COLOR_G, BUBBLE_BG_COLOR_B); draw_round_rect (cr, 1.0f, BUBBLE_SHADOW_SIZE, BUBBLE_SHADOW_SIZE, CORNER_RADIUS, (gdouble) (width - 2.0f * BUBBLE_SHADOW_SIZE), (gdouble) (height - 2.0f * BUBBLE_SHADOW_SIZE)); cairo_fill (cr); tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (cr_surf), cairo_image_surface_get_format (cr_surf), 3 * BUBBLE_SHADOW_SIZE, 3 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (cr_surf)); dummy_surf = copy_surface (tmp); cairo_surface_destroy (tmp); tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (dummy_surf), cairo_image_surface_get_format (dummy_surf), 2 * BUBBLE_SHADOW_SIZE, 2 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (dummy_surf)); norm_surf = copy_surface (tmp); cairo_surface_destroy (tmp); blur = raico_blur_create (RAICO_BLUR_QUALITY_LOW); raico_blur_set_radius (blur, 6); raico_blur_apply (blur, dummy_surf); raico_blur_destroy (blur); tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (dummy_surf), cairo_image_surface_get_format (dummy_surf), 2 * BUBBLE_SHADOW_SIZE, 2 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (dummy_surf)); blur_surf = copy_surface (tmp); cairo_surface_destroy (tmp); cairo_surface_destroy (dummy_surf); g_tile = tile_new_for_padding (norm_surf, blur_surf); cairo_surface_destroy (norm_surf); cairo_surface_destroy (blur_surf); cairo_surface_destroy (cr_surf); cairo_destroy (cr); }
static void conting_cs_draw(ContingDrawing *self, cairo_t *cr) { ContingSymbol *symb; ContingComponent *comp; GdkColor *color; ArtPoint pw0, pw1; GdkRectangle rect; gdouble affine[6]; g_return_if_fail(self != NULL && CONTING_IS_CS(self)); symb = CONTING_SYMBOL(self); comp = CONTING_COMPONENT(self); g_object_get(self, "color", &color, NULL); pw0 = comp->p0; pw1 = comp->p1; rect.x = (pw0.x < pw1.x ? pw0.x : pw1.x); rect.y = (pw0.y < pw1.y ? pw0.y : pw1.y); rect.width = fabs(pw1.x - pw0.x); rect.height = fabs(pw1.y - pw0.y); cairo_set_antialias(cr, CAIRO_ANTIALIAS_DEFAULT); cairo_arc(cr, rect.x + ((gdouble) rect.width / 2.0), rect.y + ((gdouble) rect.height / 2.0), (gdouble) rect.width / 2.0, 0, 2 * M_PI); cairo_set_source_rgb(cr, (gdouble) color->red / (gdouble) G_MAXUINT16, (gdouble) color->green / (gdouble) G_MAXUINT16, (gdouble) color->blue / (gdouble) G_MAXUINT16); cairo_stroke(cr); cr = conting_drawing_get_cairo_absolute(self); conting_drawing_get_i2w_affine_absolute(self, affine); cairo_transform(cr, (cairo_matrix_t *) affine); cairo_set_antialias(cr, CAIRO_ANTIALIAS_DEFAULT); cairo_set_source_rgb(cr, (gdouble) color->red / (gdouble) G_MAXUINT16, (gdouble) color->green / (gdouble) G_MAXUINT16, (gdouble) color->blue / (gdouble) G_MAXUINT16); { PangoLayout *layout; PangoFontDescription *font; layout = pango_cairo_create_layout(cr); font = pango_font_description_new(); pango_font_description_set_size(font, 4 * PANGO_SCALE); /* g_print("size = %d\n", pango_font_description_get_size(font)); */ pango_font_description_set_family_static(font, "Arial"); pango_font_description_set_style(font, PANGO_STYLE_NORMAL); pango_layout_set_font_description(layout, font); pango_layout_set_text(layout, "CS", 2); cairo_move_to(cr, pw0.x + 1, pw0.y + 2); pango_cairo_update_layout(cr, layout); pango_cairo_show_layout(cr, layout); g_object_unref(layout); } cairo_stroke(cr); cairo_destroy(cr); CONTING_DRAWING_CLASS(parent_class)->draw(self, cr); }
cairo_surface_t* render_text_to_surface (gchar* text, gint width, gint height, const cairo_font_options_t* font_opts, gdouble dpi) { cairo_surface_t* surface; cairo_t* cr; PangoFontDescription* desc; PangoLayout* layout; // sanity check if (!text || width <= 0 || height <= 0) return NULL; // create surface surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) return NULL; // create context cr = cairo_create (surface); if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (surface); return NULL; } // clear context cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); // layout = pango_cairo_create_layout (cr); desc = pango_font_description_new (); pango_font_description_set_size (desc, 12 * PANGO_SCALE); pango_font_description_set_family_static (desc, "Candara"); pango_font_description_set_weight (desc, PANGO_WEIGHT_NORMAL); pango_font_description_set_style (desc, PANGO_STYLE_NORMAL); pango_layout_set_wrap (layout, PANGO_WRAP_WORD); pango_layout_set_font_description (layout, desc); pango_font_description_free (desc); pango_layout_set_width (layout, width * PANGO_SCALE); pango_layout_set_height (layout, height * PANGO_SCALE); pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END); // print and layout string (pango-wise) pango_layout_set_text (layout, text, -1); // make sure system-wide font-options like hinting, antialiasing etc. // are taken into account pango_cairo_context_set_font_options (pango_layout_get_context (layout), font_opts); pango_cairo_context_set_resolution (pango_layout_get_context (layout), dpi); pango_layout_context_changed (layout); // draw pango-text to our cairo-context cairo_move_to (cr, 0.0f, 0.0f); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr, 1.0f, 1.0f, 1.0f, 1.0f); // this call leaks 3803 bytes, I've no idea how to fix that pango_cairo_show_layout (cr, layout); // clean up g_object_unref (layout); cairo_destroy (cr); return surface; }
tile_t* setup_text_tile (const cairo_font_options_t* font_opts, gdouble dpi, gint w, gint h) { tile_t* tile = NULL; cairo_status_t status; cairo_surface_t* surface = NULL; cairo_surface_t* text = NULL; cairo_surface_t* shadow = NULL; cairo_t* cr; gdouble width = (gdouble) w; gdouble height = (gdouble) h; raico_blur_t* blur = NULL; cairo_pattern_t* pattern = NULL; surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); status = cairo_surface_status (surface); if (status != CAIRO_STATUS_SUCCESS) g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); cr = cairo_create (surface); status = cairo_status (cr); if (status != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (surface); g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); } // clear and render drop-shadow and bubble-background cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); text = render_text_to_surface ( "After an evening of hacking at the" " Fataga hotel here at GUADEC I " "can present you even text-scroll " "with blur-cache and fade-out mask" " and I dedicate this to Behdad who" " sadly burst his upper lip during " "the evening and had to go to the " "hospital. Some spanish KDE-folks " "kindly accompanied him to help out" " with translation. True collaboration!\0", width, height, font_opts, dpi); shadow = render_text_to_surface ( "After an evening of hacking at the" " Fataga hotel here at GUADEC I " "can present you even text-scroll " "with blur-cache and fade-out mask" " and I dedicate this to Behdad who" " sadly burst his upper lip during " "the evening and had to go to the " "hospital. Some spanish KDE-folks " "kindly accompanied him to help out" " with translation. True collaboration!\0", width, height, font_opts, dpi); // create and setup blur blur = raico_blur_create (RAICO_BLUR_QUALITY_LOW); raico_blur_set_radius (blur, 4); // now blur it raico_blur_apply (blur, shadow); // blur no longer needed raico_blur_destroy (blur); cairo_push_group (cr); cairo_set_source_surface (cr, shadow, 0.0f, 0.0f); cairo_paint (cr); pattern = cairo_pop_group (cr); cairo_set_source_rgba (cr, 0.0f, 0.0f, 0.0f, 1.0f); cairo_mask (cr, pattern); cairo_surface_destroy (shadow); cairo_pattern_destroy (pattern); cairo_set_source_surface (cr, text, 0.0f, 0.0f); cairo_paint (cr); cairo_destroy (cr); cairo_surface_destroy (text); tile = tile_new (surface, 6); cairo_surface_destroy (surface); return tile; }
static void sctp_graph_draw(struct sctp_udata *u_data) { int length, lwidth; guint32 distance=5, i, e, sec, w, start, a, j, b; gint label_width, label_height; char label_string[15]; gfloat dis; gboolean write_label = FALSE; PangoLayout *layout; GtkAllocation widget_alloc; cairo_t *cr; if (u_data->io->x1_tmp_sec == 0 && u_data->io->x1_tmp_usec == 0) u_data->io->offset = 0; else u_data->io->offset = 5; if (u_data->io->x2_tmp_sec - u_data->io->x1_tmp_sec > 1500) { u_data->io->min_x=u_data->io->x1_tmp_sec; u_data->io->max_x=u_data->io->x2_tmp_sec; u_data->io->uoff = TRUE; } else { u_data->io->min_x=((guint32) (u_data->io->x1_tmp_sec*1000000.0))+u_data->io->x1_tmp_usec; u_data->io->max_x=((guint32) (u_data->io->x2_tmp_sec*1000000.0))+u_data->io->x2_tmp_usec; u_data->io->uoff = FALSE; } u_data->io->tmp_width = u_data->io->max_x - u_data->io->min_x; if (u_data->dir == 1) { if (u_data->io->tmp == FALSE) { if (u_data->assoc->sort_tsn1 != NULL) u_data->io->max_y = u_data->io->tmp_max_tsn1 - u_data->io->tmp_min_tsn1; else u_data->io->max_y = 0; u_data->io->min_y = 0; } else { u_data->io->max_y = u_data->io->tmp_max_tsn1; u_data->io->min_y = u_data->io->tmp_min_tsn1; } } else if (u_data->dir == 2) { if (u_data->io->tmp == FALSE) { if (u_data->assoc->tsn2 != NULL) u_data->io->max_y = u_data->io->tmp_max_tsn2 - u_data->io->tmp_min_tsn2; else u_data->io->max_y = 0; u_data->io->min_y = 0; } else { u_data->io->max_y = u_data->io->tmp_max_tsn2; u_data->io->min_y = u_data->io->tmp_min_tsn2; } } #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_source_rgb (cr, 1, 1, 1); gtk_widget_get_allocation(u_data->io->draw_area, &widget_alloc); cairo_rectangle (cr, 0, 0, widget_alloc.width, widget_alloc.height); cairo_fill (cr); cairo_destroy (cr); /* x_axis */ #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER+u_data->io->offset+0.5, u_data->io->surface_height - BOTTOM_BORDER+0.5); cairo_line_to(cr, u_data->io->surface_width - RIGHT_BORDER + u_data->io->offset+0.5, u_data->io->surface_height - BOTTOM_BORDER+0.5); cairo_move_to(cr, u_data->io->surface_width - RIGHT_BORDER + u_data->io->offset+0.5, u_data->io->surface_height - BOTTOM_BORDER+0.5); cairo_line_to(cr, u_data->io->surface_width - RIGHT_BORDER + u_data->io->offset - 5+0.5, u_data->io->surface_height - BOTTOM_BORDER - 5+0.5); cairo_move_to(cr, u_data->io->surface_width - RIGHT_BORDER + u_data->io->offset + 0.5, u_data->io->surface_height - BOTTOM_BORDER + 0.5); cairo_line_to(cr, u_data->io->surface_width - RIGHT_BORDER + u_data->io->offset - 5.5, u_data->io->surface_height - BOTTOM_BORDER + 5.5); cairo_stroke(cr); cairo_destroy(cr); u_data->io->axis_width = u_data->io->surface_width - LEFT_BORDER - RIGHT_BORDER - u_data->io->offset; if(u_data->io->tmp_width>0){ u_data->io->x_interval = (float)((u_data->io->axis_width*1.0)/u_data->io->tmp_width); /*distance in pixels between 2 data points*/ } else { u_data->io->x_interval = (float)(u_data->io->axis_width); } e=0; if (u_data->io->x_interval < 1) { dis = 1 / u_data->io->x_interval; while (dis >1) { dis /= 10; e++; } distance = 1; for (i=0; i<=e+1; i++) distance *= 10; } else distance = 5; g_snprintf(label_string, sizeof(label_string), "%d", 0); memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), 15); layout = gtk_widget_create_pango_layout(u_data->io->draw_area, label_string); pango_layout_get_pixel_size(layout, &label_width, &label_height); if (u_data->io->x1_tmp_usec == 0) sec = u_data->io->x1_tmp_sec; else sec = u_data->io->x1_tmp_sec+1; if (u_data->io->offset != 0) { g_snprintf(label_string, sizeof(label_string), "%u", u_data->io->x1_tmp_sec); memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), sizeof(label_string)); pango_layout_set_text(layout, label_string, -1); pango_layout_get_pixel_size(layout, &lwidth, NULL); #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_move_to (cr, LEFT_BORDER - 25, u_data->io->surface_height - BOTTOM_BORDER + 20); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); cr = NULL; } w = (guint32)(500 / (guint32)(distance * u_data->io->x_interval)); if (w == 0) w = 1; if (w == 4 || w==3 || w==2) { w = 5; a = distance / 10; b = (guint32)((u_data->io->min_x/100000))%10; /* start for labels*/ } else { a = distance / 5; b = 0; } if (!u_data->io->uoff) { if (a>=1000000) { start=u_data->io->min_x/1000000*1000000; if (a==1000000) b = 0; } else { start=u_data->io->min_x/100000; if (start%2!=0) start--; start*=100000; b = (guint32)((start/100000))%10; } } else { start = u_data->io->min_x; if (start%2!=0) start--; b = 0; } for (i=start, j=b; i<=u_data->io->max_x; i+=a, j++) { if (!u_data->io->uoff) if (i >= u_data->io->min_x && i % 1000000 != 0) { length = 5; g_snprintf(label_string, sizeof(label_string), "%d", i%1000000); if (j % w == 0) { length = 10; memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), sizeof(label_string)); pango_layout_set_text(layout, label_string, -1); pango_layout_get_pixel_size(layout, &lwidth, NULL); #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_move_to (cr, LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval - lwidth / 2, u_data->io->surface_height - BOTTOM_BORDER + 10); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); cr = NULL; } #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval + 0.5, u_data->io->surface_height - BOTTOM_BORDER + 0.5); cairo_line_to(cr, LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval + 0.5, u_data->io->surface_height - BOTTOM_BORDER + length + 0.5); cairo_stroke(cr); cairo_destroy(cr); } if (!u_data->io->uoff) { if (i%1000000==0 && j%w==0) { sec=i/1000000; write_label = TRUE; } } else { if (j%w == 0) { sec = i; write_label = TRUE; } } if (write_label) { #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval + 0.5, u_data->io->surface_height - BOTTOM_BORDER + 0.5); cairo_line_to(cr, LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval + 0.5, u_data->io->surface_height - BOTTOM_BORDER + 10 + 0.5); cairo_stroke(cr); cairo_destroy(cr); g_snprintf(label_string, sizeof(label_string), "%d", sec); memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), sizeof(label_string)); pango_layout_set_text(layout, label_string, -1); pango_layout_get_pixel_size(layout, &lwidth, NULL); #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_move_to (cr, (LEFT_BORDER + u_data->io->offset + (i - u_data->io->min_x) * u_data->io->x_interval-10), u_data->io->surface_height - BOTTOM_BORDER + 20); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); cr = NULL; write_label = FALSE; } } g_strlcpy(label_string, "sec", sizeof(label_string)); memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), sizeof(label_string)); pango_layout_set_text(layout, label_string, -1); pango_layout_get_pixel_size(layout, &lwidth, NULL); #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_move_to (cr, u_data->io->surface_width - RIGHT_BORDER - 10, u_data->io->surface_height - BOTTOM_BORDER + 30); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); cr = NULL; distance = 5; /* y-axis */ #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER + 0.5, TOP_BORDER - u_data->io->offset + 0.5); cairo_line_to(cr, LEFT_BORDER + 0.5, u_data->io->surface_height - BOTTOM_BORDER - u_data->io->offset + 0.5); cairo_move_to(cr, LEFT_BORDER + 0.5, TOP_BORDER - u_data->io->offset + 0.5); cairo_line_to(cr, LEFT_BORDER - 5 + 0.5, TOP_BORDER - u_data->io->offset + 5 + 0.5); cairo_move_to(cr, LEFT_BORDER + 0.5, TOP_BORDER - u_data->io->offset + 0.5); cairo_line_to(cr, LEFT_BORDER +5 + 0.5, TOP_BORDER - u_data->io->offset + 5 + 0.5); cairo_stroke(cr); cairo_destroy(cr); u_data->io->y_interval = (float)(((u_data->io->surface_height - TOP_BORDER - BOTTOM_BORDER) * 1.0)/(u_data->io->max_y - u_data->io->min_y)); e = 0; if (u_data->io->y_interval < 1) { dis = 1 / u_data->io->y_interval; while (dis > 1) { dis /= 10; e++; } distance = 1; for (i=0; i<=e; i++) distance = distance * 10; } else if (u_data->io->y_interval<2) distance = 10; if (u_data->io->max_y > 0) { for (i=u_data->io->min_y/distance*distance; i<=u_data->io->max_y; i+=distance/5) { if (i >= u_data->io->min_y) { length = 5; g_snprintf(label_string, sizeof(label_string), "%d", i); if (i%distance == 0 || (distance <= 5 && u_data->io->y_interval > 10)) { length = 10; memcpy(label_string,(gchar *)g_locale_to_utf8(label_string, -1 , NULL, NULL, NULL), sizeof(label_string)); pango_layout_set_text(layout, label_string, -1); pango_layout_get_pixel_size(layout, &lwidth, NULL); #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_move_to (cr, LEFT_BORDER - length - lwidth - 5, u_data->io->surface_height - BOTTOM_BORDER - u_data->io->offset - (i - u_data->io->min_y) * u_data->io->y_interval - 3); pango_cairo_show_layout (cr, layout); cairo_destroy (cr); cr = NULL; } #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER - length + 0.5, u_data->io->surface_height - BOTTOM_BORDER - u_data->io->offset - (i - u_data->io->min_y) * u_data->io->y_interval + 0.5); cairo_line_to(cr, LEFT_BORDER + 0.5, u_data->io->surface_height - BOTTOM_BORDER - u_data->io->offset - (i - u_data->io->min_y) * u_data->io->y_interval + 0.5); cairo_stroke(cr); cairo_destroy(cr); } } } else simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, "No Data Chunks sent"); g_object_unref(G_OBJECT(layout)); }
static void paint_background() { cr = cairo_create(surface); cairo_set_source_rgb(cr, 0.0, 0.8, 0.0); cairo_paint(cr); cairo_destroy(cr); }
static void draw_tsn_graph(struct sctp_udata *u_data) { GPtrArray *array = NULL; guint32 i, size = 0, start, end; gint diff, width; cairo_t *cr; if (u_data->dir == 1) { array = u_data->assoc->sort_tsn1; size = u_data->assoc->n_data_chunks_ep1; if (u_data->io->tmp == FALSE) { min_tsn = 0; max_tsn = u_data->assoc->max_bytes1; } else { min_tsn = u_data->io->tmp_min_tsn1; max_tsn = u_data->io->tmp_max_tsn1; } } else if (u_data->dir == 2) { array = u_data->assoc->sort_tsn2; size = u_data->assoc->n_data_chunks_ep2; if (u_data->io->tmp == FALSE) { min_tsn = 0; max_tsn = u_data->assoc->max_bytes2; } else { min_tsn = u_data->io->tmp_min_tsn2; max_tsn = u_data->io->tmp_max_tsn2; } } width = u_data->io->max_x - u_data->io->min_x; for (i=0; i<size; i++) { if (u_data->io->uoff) diff = (gint)((struct tsn_sort*)(g_ptr_array_index(array, i)))->secs -u_data->io->min_x; else diff = (gint)((struct tsn_sort*)(g_ptr_array_index(array, i)))->secs*1000000 + ((struct tsn_sort*)(g_ptr_array_index(array, i)))->usecs-u_data->io->min_x; start = ((struct tsn_sort*)(g_ptr_array_index(array, i)))->offset; end = start + ((struct tsn_sort*)(g_ptr_array_index(array, i)))->length; if (start >= min_tsn && diff > 0 && diff <= width){ #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif cairo_set_line_width (cr, 1.0); cairo_move_to(cr, (LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff)+0.5, (u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(start,min_tsn))*u_data->io->y_interval))+0.5); cairo_line_to(cr, (LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff)+0.5, (u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(end,min_tsn))*u_data->io->y_interval))+0.5); cairo_stroke(cr); cairo_destroy(cr); } } }
static void draw_sack_graph(struct sctp_udata *u_data) { GdkRGBA red_color = {1.0, 0.0, 0.0, 1.0}; GdkRGBA green_color = {0.0, 1.0, 0.0, 1.0}; gint diff; GPtrArray *array = NULL; guint32 i, size = 0, start=0, end; gboolean more = FALSE; gint width; cairo_t *cr; if (u_data->dir == 1) { array = u_data->assoc->sort_sack1; size=u_data->assoc->n_sack_chunks_ep1; if (u_data->io->tmp == FALSE) { min_tsn = 0; max_tsn = u_data->assoc->max_bytes1; } else { min_tsn = u_data->io->tmp_min_tsn1; max_tsn = u_data->io->tmp_max_tsn1; } } else if (u_data->dir == 2) { array = u_data->assoc->sort_sack2; size = u_data->assoc->n_sack_chunks_ep2; if (u_data->io->tmp == FALSE) { min_tsn = 0; max_tsn = u_data->assoc->max_bytes2; } else { min_tsn = u_data->io->tmp_min_tsn2; max_tsn = u_data->io->tmp_max_tsn2; } } width = u_data->io->max_x - u_data->io->min_x; for (i=0; i<size; i++) { if (u_data->io->uoff) diff = (gint)((struct tsn_sort*)(g_ptr_array_index(array, i)))->secs - u_data->io->min_x; else diff = (gint)((struct tsn_sort*)(g_ptr_array_index(array, i)))->secs * 1000000 + ((struct tsn_sort*)(g_ptr_array_index(array, i)))->usecs - u_data->io->min_x; end = start + ((struct tsn_sort*)(g_ptr_array_index(array, i)))->length; if (end>max_tsn) { end = max_tsn; more = TRUE; } if (start >= min_tsn && diff > 0 && diff <= width) { #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif gdk_cairo_set_source_rgba (cr, &red_color); cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff+0.5, u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(start,min_tsn))*u_data->io->y_interval)+0.5); cairo_line_to(cr, LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff+0.5, u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(end,min_tsn))*u_data->io->y_interval)+0.5); cairo_stroke(cr); cairo_destroy(cr); if (more == TRUE) { #if GTK_CHECK_VERSION(2,22,0) cr = cairo_create (u_data->io->surface); #else cr = gdk_cairo_create (u_data->io->pixmap); #endif gdk_cairo_set_source_rgba (cr, &green_color); cairo_set_line_width (cr, 1.0); cairo_move_to(cr, LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff+0.5, u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(end,min_tsn))*u_data->io->y_interval)+0.5); cairo_line_to(cr, LEFT_BORDER+u_data->io->offset+u_data->io->x_interval*diff, u_data->io->surface_height-BOTTOM_BORDER-u_data->io->offset-((SUB_32(end+10,min_tsn))*u_data->io->y_interval)+0.5); cairo_stroke(cr); cairo_destroy(cr); more = FALSE; } } } }
void Close_Graphics(cairo_t *cr) { cairo_destroy(cr); }
/** renderer for chains */ static NftResult _render_chain( cairo_surface_t ** s, gpointer element) { if(!s || !*s || !element) NFT_LOG_NULL(NFT_FAILURE); /* get this chain */ NiftyconfChain *chain = (NiftyconfChain *) element; LedChain *c = chain_niftyled(chain); /* if dimensions changed, we need to allocate a new surface */ int width = (led_chain_get_max_x(c) + 1) * renderer_scale_factor(); int height = (led_chain_get_max_y(c) + 1) * renderer_scale_factor(); NiftyconfRenderer *r = chain_get_renderer(chain); if(!renderer_resize(r, width, height)) { g_error("Failed to resize renderer to %dx%d", width, height); return NFT_FAILURE; } /* create context for drawing */ cairo_t *cr = cairo_create(*s); /* disable antialiasing */ cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE); /* clear surface */ cairo_set_source_rgba(cr, 0, 0, 0, 1); cairo_rectangle(cr, 0, 0, (double) cairo_image_surface_get_width(*s), (double) cairo_image_surface_get_height(*s)); cairo_fill(cr); /* set line-width */ cairo_set_line_width(cr, 1); /* walk all LEDs */ LedCount i; for(i = 0; i < led_chain_get_ledcount(c); i++) { Led *l = led_chain_get_nth(c, i); NiftyconfLed *led = led_get_privdata(l); NiftyconfRenderer *lr = led_get_renderer(led); cairo_set_source_surface(cr, renderer_get_surface(lr), (double) led_get_x(l) * renderer_scale_factor(), (double) led_get_y(l) * renderer_scale_factor()); cairo_paint(cr); } cairo_destroy(cr); return NFT_SUCCESS; }
void setup_tile (gint w, gint h) { cairo_status_t status; cairo_t* cr = NULL; cairo_surface_t* cr_surf = NULL; gdouble width = (gdouble) w; gdouble height = (gdouble) h; cairo_surface_t* tmp = NULL; cairo_surface_t* dummy_surf = NULL; cairo_surface_t* norm_surf = NULL; cairo_surface_t* blur_surf = NULL; raico_blur_t* blur = NULL; tile_t* tile = NULL; // create tmp. surface for scratch cr_surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 3 * BUBBLE_SHADOW_SIZE, 3 * BUBBLE_SHADOW_SIZE); status = cairo_surface_status (cr_surf); if (status != CAIRO_STATUS_SUCCESS) g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); // create context for that tmp. scratch surface cr = cairo_create (cr_surf); status = cairo_status (cr); if (status != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (cr_surf); g_print ("Error: \"%s\"\n", cairo_status_to_string (status)); } // clear, render drop-shadow and bubble-background in scratch-surface cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); if (g_composited) { draw_shadow (cr, width, height, BUBBLE_SHADOW_SIZE, CORNER_RADIUS); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); draw_round_rect (cr, 1.0f, (gdouble) BUBBLE_SHADOW_SIZE, (gdouble) BUBBLE_SHADOW_SIZE, (gdouble) CORNER_RADIUS, (gdouble) (width - 2.0f * BUBBLE_SHADOW_SIZE), (gdouble) (height - 2.0f* BUBBLE_SHADOW_SIZE)); cairo_fill (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr, BUBBLE_BG_COLOR_R, BUBBLE_BG_COLOR_G, BUBBLE_BG_COLOR_B, 0.95f); } else cairo_set_source_rgb (cr, BUBBLE_BG_COLOR_R, BUBBLE_BG_COLOR_G, BUBBLE_BG_COLOR_B); draw_round_rect (cr, 1.0f, BUBBLE_SHADOW_SIZE, BUBBLE_SHADOW_SIZE, CORNER_RADIUS, (gdouble) (width - 2.0f * BUBBLE_SHADOW_SIZE), (gdouble) (height - 2.0f * BUBBLE_SHADOW_SIZE)); cairo_fill (cr); // create tmp. copy of scratch-surface tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (cr_surf), cairo_image_surface_get_format (cr_surf), 3 * BUBBLE_SHADOW_SIZE, 3 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (cr_surf)); dummy_surf = copy_surface (tmp); cairo_surface_destroy (tmp); // create normal-state surface for tile from copy of scratch-surface tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (dummy_surf), cairo_image_surface_get_format (dummy_surf), 2 * BUBBLE_SHADOW_SIZE, 2 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (dummy_surf)); norm_surf = copy_surface (tmp); cairo_surface_destroy (tmp); // blur tmp. copy of scratch-surface blur = raico_blur_create (RAICO_BLUR_QUALITY_LOW); raico_blur_set_radius (blur, 6); raico_blur_apply (blur, dummy_surf); raico_blur_destroy (blur); // create blurred-state surface for tile tmp = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (dummy_surf), cairo_image_surface_get_format (dummy_surf), 2 * BUBBLE_SHADOW_SIZE, 2 * BUBBLE_SHADOW_SIZE, cairo_image_surface_get_stride (dummy_surf)); blur_surf = copy_surface (tmp); cairo_surface_destroy (tmp); // actually create the tile with padding in mind tile = tile_new_for_padding (norm_surf, blur_surf); destroy_cloned_surface (norm_surf); destroy_cloned_surface (blur_surf); destroy_cloned_surface (dummy_surf); cairo_destroy (cr); cairo_surface_destroy (cr_surf); norm_surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); cr = cairo_create (norm_surf); cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); tile_paint_with_padding (tile, cr, 0.0f, 0.0f, w, h, 1.0f, 0.0f); cairo_destroy (cr); blur_surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h); cr = cairo_create (blur_surf); cairo_scale (cr, 1.0f, 1.0f); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_paint (cr); cairo_set_operator (cr, CAIRO_OPERATOR_OVER); tile_paint_with_padding (tile, cr, 0.0f, 0.0f, w, h, 0.0f, 1.0f); cairo_destroy (cr); g_tile = tile_new_for_padding (norm_surf, blur_surf); // clean up tile_destroy (tile); cairo_surface_destroy (norm_surf); cairo_surface_destroy (blur_surf); }
void draw_shadow (cairo_t* cr, gdouble width, gdouble height, gint shadow_radius, gint corner_radius) { cairo_surface_t* tmp_surface = NULL; cairo_surface_t* new_surface = NULL; cairo_pattern_t* pattern = NULL; cairo_t* cr_surf = NULL; cairo_matrix_t matrix; raico_blur_t* blur = NULL; tmp_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 4 * shadow_radius, 4 * shadow_radius); if (cairo_surface_status (tmp_surface) != CAIRO_STATUS_SUCCESS) return; cr_surf = cairo_create (tmp_surface); if (cairo_status (cr_surf) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (tmp_surface); return; } cairo_scale (cr_surf, 1.0f, 1.0f); cairo_set_operator (cr_surf, CAIRO_OPERATOR_CLEAR); cairo_paint (cr_surf); cairo_set_operator (cr_surf, CAIRO_OPERATOR_OVER); cairo_set_source_rgba (cr_surf, 0.0f, 0.0f, 0.0f, 0.75f); cairo_arc (cr_surf, 2 * shadow_radius, 2 * shadow_radius, 2.0f * corner_radius, 0.0f, 360.0f * (G_PI / 180.f)); cairo_fill (cr_surf); cairo_destroy (cr_surf); // create and setup blur blur = raico_blur_create (RAICO_BLUR_QUALITY_LOW); raico_blur_set_radius (blur, shadow_radius); // now blur it raico_blur_apply (blur, tmp_surface); // blur no longer needed raico_blur_destroy (blur); new_surface = cairo_image_surface_create_for_data ( cairo_image_surface_get_data (tmp_surface), cairo_image_surface_get_format (tmp_surface), cairo_image_surface_get_width (tmp_surface) / 2, cairo_image_surface_get_height (tmp_surface) / 2, cairo_image_surface_get_stride (tmp_surface)); pattern = cairo_pattern_create_for_surface (new_surface); if (cairo_pattern_status (pattern) != CAIRO_STATUS_SUCCESS) { cairo_surface_destroy (tmp_surface); cairo_surface_destroy (new_surface); return; } // top left cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD); cairo_set_source (cr, pattern); cairo_rectangle (cr, 0.0f, 0.0f, width - 2 * shadow_radius, 2 * shadow_radius); cairo_fill (cr); // bottom left cairo_matrix_init_scale (&matrix, 1.0f, -1.0f); cairo_matrix_translate (&matrix, 0.0f, -height); cairo_pattern_set_matrix (pattern, &matrix); cairo_rectangle (cr, 0.0f, 2 * shadow_radius, 2 * shadow_radius, height - 2 * shadow_radius); cairo_fill (cr); // top right cairo_matrix_init_scale (&matrix, -1.0f, 1.0f); cairo_matrix_translate (&matrix, -width, 0.0f); cairo_pattern_set_matrix (pattern, &matrix); cairo_rectangle (cr, width - 2 * shadow_radius, 0.0f, 2 * shadow_radius, height - 2 * shadow_radius); cairo_fill (cr); // bottom right cairo_matrix_init_scale (&matrix, -1.0f, -1.0f); cairo_matrix_translate (&matrix, -width, -height); cairo_pattern_set_matrix (pattern, &matrix); cairo_rectangle (cr, 2 * shadow_radius, height - 2 * shadow_radius, width - 2 * shadow_radius, 2 * shadow_radius); cairo_fill (cr); // clean up cairo_pattern_destroy (pattern); cairo_surface_destroy (tmp_surface); cairo_surface_destroy (new_surface); }
//called on the drawingArea's expose event gboolean drawingAreaExpose(GtkWidget *widgetDrawingArea, GdkEventExpose *event, gpointer data) { passToDrawingAreaExpose* passed = (passToDrawingAreaExpose*) data; bool* draw = passed->draw; if (*draw) { OwnRobot* ownRobotDraw = passed->ownRobotDraw; int myTeam = passed->myTeam; int numberOfRobots = passed->numberOfRobots; int *drawFactor = passed->drawFactor; int imageWidth = (VIEWDISTANCE+ROBOTDIAMETER) * (*drawFactor) * 2; int imageHeight = (VIEWDISTANCE+ROBOTDIAMETER) * (*drawFactor) * 2; //origin is the middle point where our viewed robot is located int origin[] = { imageWidth / 2, imageHeight / 2 }; cairo_t *cr = gdk_cairo_create(GTK_DRAWING_AREA(widgetDrawingArea)->widget.window); cairo_set_line_width(cr, 2); ColorObject color = colorFromTeam(myTeam); //draw our home if it is nearby (FIRST so it goes underneath) if (ownRobotDraw->homeRelX - ((HOMEDIAMETER / 2) + (ROBOTDIAMETER / 2)) < VIEWDISTANCE && ownRobotDraw->homeRelY - ((HOMEDIAMETER / 2) + (ROBOTDIAMETER / 2)) < VIEWDISTANCE) { cairo_set_source_rgb(cr, 0, 0, 0); cairo_arc(cr, origin[0] + ownRobotDraw->homeRelX * *drawFactor, origin[1] + ownRobotDraw->homeRelY * *drawFactor, HOMEDIAMETER * *drawFactor / 2, 0, 2 * M_PI); cairo_stroke_preserve(cr); cairo_set_source_rgb(cr, color.r, color.g, color.b); cairo_fill(cr); } for (unsigned int i = 0; i < ownRobotDraw->seenRobots.size(); i++) { //robot Ids start at 1, team Ids start at 0. Must remember this. THIS is where the damn //blue robot and off coloring has been coming from. color = colorFromTeam(((ownRobotDraw->seenRobots.at(i)->id)-1) / numberOfRobots); cairo_set_source_rgb(cr, 0, 0, 0); cairo_arc(cr, origin[0] + ownRobotDraw->seenRobots.at(i)->relx * *drawFactor, origin[1] + ownRobotDraw->seenRobots.at(i)->rely * *drawFactor, ROBOTDIAMETER * *drawFactor / 2, 0, 2 * M_PI); cairo_stroke_preserve(cr); cairo_set_source_rgb(cr, color.r, color.g, color.b); cairo_fill(cr); } //draw seen pucks cairo_set_source_rgb(cr, 0, 0, 0); for (unsigned int i = 0; i < ownRobotDraw->seenPucks.size(); i++) { cairo_arc(cr, origin[0] + ownRobotDraw->seenPucks.at(i)->relx * *drawFactor, origin[1] + ownRobotDraw->seenPucks.at(i)->rely * *drawFactor, PUCKDIAMETER * *drawFactor / 2, 0, 2 * M_PI); cairo_fill(cr); } //draw the currently viewed robot color = colorFromTeam(myTeam); cairo_set_source_rgb(cr, 0, 0, 0); cairo_arc(cr, origin[0], origin[1], ROBOTDIAMETER * *drawFactor / 2, 0, 2 * M_PI); cairo_stroke_preserve(cr); cairo_set_source_rgb(cr, color.r, color.g, color.b); cairo_fill(cr); //ownRobot->angle does not provide a valid angle ( it's always zero )! //draw the line showing the angle that the robot is moving at if( !(ownRobotDraw->vx == 0 && ownRobotDraw->vy == 0 )) { int endX=origin[0] + ownRobotDraw->vx * *drawFactor * 4, endY=origin[1] + ownRobotDraw->vy * *drawFactor * 4; double x1, y1, x2, y2; calcVertexes(origin[0], origin[1], endX, endY, *drawFactor, x1, y1, x2, y2); cairo_set_source_rgba(cr, 0, 0, 0, 0.6); cairo_move_to(cr, origin[0], origin[1]); cairo_line_to(cr, endX, endY); cairo_line_to(cr, x1, y1); cairo_move_to(cr, x2, y2); cairo_line_to(cr, endX, endY); cairo_stroke(cr); } //if the robot has a puck then actually draw the puck in its center if (ownRobotDraw->hasPuck) { cairo_set_source_rgb(cr, 0, 0, 0); cairo_arc(cr, origin[0], origin[1], PUCKDIAMETER * *drawFactor, 0, 2 * M_PI); cairo_stroke(cr); } //draw the viewing area - cairo angles are stupid //bonus: not moving? 360 degree view cairo_set_source_rgba(cr, 0, 0, 0, 0.75); if(ownRobotDraw->vx == 0 && ownRobotDraw->vy == 0) { //draw the circle cairo_arc(cr, origin[0], origin[1], imageWidth/2, 0, 2 * M_PI); }else{ //draw the cone cairo_move_to(cr, origin[0], origin[1]); cairo_arc_negative(cr, origin[0], origin[1], imageWidth/2, (VIEWANGLE / 2.0) - ownRobotDraw->angle, (2 * M_PI) - (VIEWANGLE/2.0) - ownRobotDraw->angle); cairo_line_to(cr, origin[0], origin[1]); } cairo_stroke(cr); cairo_destroy(cr); *draw = false; if (gtk_toggle_tool_button_get_active(passed->info)) updateInfoWindow(ownRobotDraw, passed->builder); } return FALSE; }
static gboolean nsgtk_tree_window_draw_event(GtkWidget *widget, cairo_t *cr, gpointer data) { struct nsgtk_treeview *tv = (struct nsgtk_treeview *)data; struct redraw_context ctx = { .interactive = true, .background_images = true, .plot = &nsgtk_plotters }; double x1; double y1; double x2; double y2; current_widget = widget; current_cr = cr; cairo_clip_extents(cr, &x1, &y1, &x2, &y2); if (tv->tree_flags == TREE_SSLCERT) { ssl_current_session = tv->ssl_data; } tree_draw(tv->tree, 0, 0, x1, y1, x2 - x1, y2 - y1, &ctx); current_widget = NULL; return FALSE; } #else /* signal handler functions for a tree window */ static gboolean nsgtk_tree_window_draw_event(GtkWidget *widget, GdkEventExpose *event, gpointer g) { struct nsgtk_treeview *tv = (struct nsgtk_treeview *)g; struct redraw_context ctx = { .interactive = true, .background_images = true, .plot = &nsgtk_plotters }; int x, y, width, height; x = event->area.x; y = event->area.y; width = event->area.width; height = event->area.height; current_widget = widget; current_cr = gdk_cairo_create(nsgtk_widget_get_window(widget)); if (tv->tree_flags == TREE_SSLCERT) { ssl_current_session = tv->ssl_data; } tree_draw(tv->tree, 0, 0, x, y, width, height, &ctx); current_widget = NULL; cairo_destroy(current_cr); return FALSE; } #endif void nsgtk_tree_window_hide(GtkWidget *widget, gpointer g) { } static gboolean nsgtk_tree_window_button_press_event(GtkWidget *widget, GdkEventButton *event, gpointer g) { struct nsgtk_treeview *tw = g; gtk_im_context_reset(tw->input_method); gtk_widget_grab_focus(GTK_WIDGET(tw->drawing_area)); tw->mouse_pressed = true; tw->mouse_pressed_x = event->x; tw->mouse_pressed_y = event->y; if (event->type == GDK_2BUTTON_PRESS) { tw->mouse_state = BROWSER_MOUSE_DOUBLE_CLICK; } switch (event->button) { case 1: tw->mouse_state |= BROWSER_MOUSE_PRESS_1; break; case 2: tw->mouse_state |= BROWSER_MOUSE_PRESS_2; break; } /* Handle the modifiers too */ if (event->state & GDK_SHIFT_MASK) tw->mouse_state |= BROWSER_MOUSE_MOD_1; if (event->state & GDK_CONTROL_MASK) tw->mouse_state |= BROWSER_MOUSE_MOD_2; if (event->state & GDK_MOD1_MASK) tw->mouse_state |= BROWSER_MOUSE_MOD_3; /* Record where we pressed, for use when determining whether to start * a drag in motion notify events. */ tw->last_x = event->x; tw->last_y = event->y; if (tw->tree_flags == TREE_SSLCERT) { ssl_current_session = tw->ssl_data; } tree_mouse_action(tw->tree, tw->mouse_state, event->x, event->y); return TRUE; } static gboolean nsgtk_tree_window_button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer g) { bool shift = event->state & GDK_SHIFT_MASK; bool ctrl = event->state & GDK_CONTROL_MASK; bool alt = event->state & GDK_MOD1_MASK; struct nsgtk_treeview *tw = (struct nsgtk_treeview *) g; struct tree *tree = tw->tree; /* We consider only button 1 clicks as double clicks. * If the mouse state is PRESS then we are waiting for a release to emit * a click event, otherwise just reset the state to nothing*/ if (tw->mouse_state & BROWSER_MOUSE_DOUBLE_CLICK) { if (tw->mouse_state & BROWSER_MOUSE_PRESS_1) tw->mouse_state ^= BROWSER_MOUSE_PRESS_1 | BROWSER_MOUSE_CLICK_1; else if (tw->mouse_state & BROWSER_MOUSE_PRESS_2) tw->mouse_state ^= (BROWSER_MOUSE_PRESS_2 | BROWSER_MOUSE_CLICK_2 | BROWSER_MOUSE_DOUBLE_CLICK); } else if (tw->mouse_state & BROWSER_MOUSE_PRESS_1) { tw->mouse_state ^= (BROWSER_MOUSE_PRESS_1 | BROWSER_MOUSE_CLICK_1); } else if (tw->mouse_state & BROWSER_MOUSE_PRESS_2) { tw->mouse_state ^= (BROWSER_MOUSE_PRESS_2 | BROWSER_MOUSE_CLICK_2); } else if (tw->mouse_state & BROWSER_MOUSE_HOLDING_1) { tw->mouse_state ^= (BROWSER_MOUSE_HOLDING_1 | BROWSER_MOUSE_DRAG_ON); } else if (tw->mouse_state & BROWSER_MOUSE_HOLDING_2) { tw->mouse_state ^= (BROWSER_MOUSE_HOLDING_2 | BROWSER_MOUSE_DRAG_ON); } /* Handle modifiers being removed */ if (tw->mouse_state & BROWSER_MOUSE_MOD_1 && !shift) tw->mouse_state ^= BROWSER_MOUSE_MOD_1; if (tw->mouse_state & BROWSER_MOUSE_MOD_2 && !ctrl) tw->mouse_state ^= BROWSER_MOUSE_MOD_2; if (tw->mouse_state & BROWSER_MOUSE_MOD_3 && !alt) tw->mouse_state ^= BROWSER_MOUSE_MOD_3; if (tw->tree_flags == TREE_SSLCERT) { ssl_current_session = tw->ssl_data; } if (tw->mouse_state & ~(BROWSER_MOUSE_MOD_1 | BROWSER_MOUSE_MOD_2 | BROWSER_MOUSE_MOD_3)) { tree_mouse_action(tree, tw->mouse_state, event->x, event->y); } else { tree_drag_end(tree, tw->mouse_state, tw->mouse_pressed_x, tw->mouse_pressed_y, event->x, event->y); } tw->mouse_state = 0; tw->mouse_pressed = false; return TRUE; } static gboolean nsgtk_tree_window_motion_notify_event(GtkWidget *widget, GdkEventMotion *event, gpointer g) { bool shift = event->state & GDK_SHIFT_MASK; bool ctrl = event->state & GDK_CONTROL_MASK; bool alt = event->state & GDK_MOD1_MASK; struct nsgtk_treeview *tw = (struct nsgtk_treeview *) g; struct tree *tree = tw->tree; if (tw->mouse_pressed == false) return TRUE; if ((fabs(event->x - tw->last_x) < 5.0) && (fabs(event->y - tw->last_y) < 5.0)) { /* Mouse hasn't moved far enough from press coordinate * for this to be considered a drag. */ return FALSE; } else { /* This is a drag, ensure it's always treated as such, * even if we drag back over the press location. */ tw->last_x = INT_MIN; tw->last_y = INT_MIN; } if (tw->tree_flags == TREE_SSLCERT) { ssl_current_session = tw->ssl_data; } if (tw->mouse_state & BROWSER_MOUSE_PRESS_1) { /* Start button 1 drag */ tree_mouse_action(tree, BROWSER_MOUSE_DRAG_1, tw->mouse_pressed_x, tw->mouse_pressed_y); /* Replace PRESS with HOLDING and declare drag in progress */ tw->mouse_state ^= (BROWSER_MOUSE_PRESS_1 | BROWSER_MOUSE_HOLDING_1); tw->mouse_state |= BROWSER_MOUSE_DRAG_ON; return TRUE; } else if (tw->mouse_state & BROWSER_MOUSE_PRESS_2){ /* Start button 2s drag */ tree_mouse_action(tree, BROWSER_MOUSE_DRAG_2, tw->mouse_pressed_x, tw->mouse_pressed_y); /* Replace PRESS with HOLDING and declare drag in progress */ tw->mouse_state ^= (BROWSER_MOUSE_PRESS_2 | BROWSER_MOUSE_HOLDING_2); tw->mouse_state |= BROWSER_MOUSE_DRAG_ON; return TRUE; } /* Handle modifiers being removed */ if (tw->mouse_state & BROWSER_MOUSE_MOD_1 && !shift) tw->mouse_state ^= BROWSER_MOUSE_MOD_1; if (tw->mouse_state & BROWSER_MOUSE_MOD_2 && !ctrl) tw->mouse_state ^= BROWSER_MOUSE_MOD_2; if (tw->mouse_state & BROWSER_MOUSE_MOD_3 && !alt) tw->mouse_state ^= BROWSER_MOUSE_MOD_3; if (tw->mouse_state & (BROWSER_MOUSE_HOLDING_1 | BROWSER_MOUSE_HOLDING_2)) tree_mouse_action(tree, tw->mouse_state, event->x, event->y); return TRUE; }
static cairo_surface_t * snapshot_widget (GtkWidget *widget, SnapshotMode mode) { cairo_surface_t *surface; cairo_pattern_t *bg; GMainLoop *loop; cairo_t *cr; g_assert (gtk_widget_get_realized (widget)); loop = g_main_loop_new (NULL, FALSE); /* We wait until the widget is drawn for the first time. * We can not wait for a GtkWidget::draw event, because that might not * happen if the window is fully obscured by windowed child widgets. * Alternatively, we could wait for an expose event on widget's window. * Both of these are rather hairy, not sure what's best. */ gdk_event_handler_set (check_for_draw, loop, NULL); g_main_loop_run (loop); surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget), CAIRO_CONTENT_COLOR, gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget)); cr = cairo_create (surface); switch (mode) { case SNAPSHOT_WINDOW: { GdkWindow *window = gtk_widget_get_window (widget); if (gdk_window_get_window_type (window) == GDK_WINDOW_TOPLEVEL || gdk_window_get_window_type (window) == GDK_WINDOW_FOREIGN) { /* give the WM/server some time to sync. They need it. * Also, do use popups instead of toplevls in your tests * whenever you can. */ gdk_display_sync (gdk_window_get_display (window)); g_timeout_add (500, quit_when_idle, loop); g_main_loop_run (loop); } gdk_cairo_set_source_window (cr, window, 0, 0); cairo_paint (cr); } break; case SNAPSHOT_DRAW: bg = gdk_window_get_background_pattern (gtk_widget_get_window (widget)); if (bg) { cairo_set_source (cr, bg); cairo_paint (cr); } gtk_widget_draw (widget, cr); break; default: g_assert_not_reached(); break; } cairo_destroy (cr); g_main_loop_unref (loop); gtk_widget_destroy (widget); return surface; }
/* * Draws global image with fill color onto a pixmap with the given * resolution and returns it. * */ xcb_pixmap_t draw_image(uint32_t *resolution) { xcb_pixmap_t bg_pixmap = XCB_NONE; int button_diameter_physical = ceil(scaling_factor() * BUTTON_DIAMETER); DEBUG("scaling_factor is %.f, physical diameter is %d px\n", scaling_factor(), button_diameter_physical); if (!vistype) vistype = get_root_visual_type(screen); bg_pixmap = create_bg_pixmap(conn, screen, resolution, color); /* Initialize cairo: Create one in-memory surface to render the unlock * indicator on, create one XCB surface to actually draw (one or more, * depending on the amount of screens) unlock indicators on. */ cairo_surface_t *output = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, button_diameter_physical, button_diameter_physical); cairo_t *ctx = cairo_create(output); cairo_surface_t *xcb_output = cairo_xcb_surface_create(conn, bg_pixmap, vistype, resolution[0], resolution[1]); cairo_t *xcb_ctx = cairo_create(xcb_output); if (img) { if (!tile) { cairo_set_source_surface(xcb_ctx, img, 0, 0); cairo_paint(xcb_ctx); } else { /* create a pattern and fill a rectangle as big as the screen */ cairo_pattern_t *pattern; pattern = cairo_pattern_create_for_surface(img); cairo_set_source(xcb_ctx, pattern); cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]); cairo_fill(xcb_ctx); cairo_pattern_destroy(pattern); } } else { char strgroups[3][3] = {{color[0], color[1], '\0'}, {color[2], color[3], '\0'}, {color[4], color[5], '\0'}}; uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)), (strtol(strgroups[1], NULL, 16)), (strtol(strgroups[2], NULL, 16))}; cairo_set_source_rgb(xcb_ctx, rgb16[0] / 255.0, rgb16[1] / 255.0, rgb16[2] / 255.0); cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]); cairo_fill(xcb_ctx); } if (unlock_indicator) { cairo_scale(ctx, scaling_factor(), scaling_factor()); /* Draw a (centered) circle with transparent background. */ cairo_set_line_width(ctx, 7.0); cairo_arc(ctx, BUTTON_CENTER /* x */, BUTTON_CENTER /* y */, BUTTON_RADIUS /* radius */, 0 /* start */, 2 * M_PI /* end */); /* Use the appropriate color for the different PAM states * (currently verifying, wrong password, or default) */ switch (pam_state) { case STATE_PAM_VERIFY: cairo_set_source_rgba(ctx, 0, 114.0 / 255, 255.0 / 255, 1.0); break; case STATE_PAM_WRONG: cairo_set_source_rgba(ctx, 250.0 / 255, 0, 0, 0.75); break; default: cairo_set_source_rgba(ctx, 0, 0, 0, 0); break; } cairo_fill_preserve(ctx); switch (pam_state) { case STATE_PAM_VERIFY: cairo_set_source_rgb(ctx, 51.0 / 255, 0, 250.0 / 255); break; case STATE_PAM_WRONG: cairo_set_source_rgb(ctx, 125.0 / 255, 51.0 / 255, 0); break; case STATE_PAM_IDLE: cairo_set_source_rgb(ctx, 0.66, 0.66, 0.66); break; } cairo_stroke(ctx); /* Draw an inner seperator line. */ cairo_set_source_rgb(ctx, 0, 0, 0); cairo_set_line_width(ctx, 2.0); cairo_arc(ctx, BUTTON_CENTER /* x */, BUTTON_CENTER /* y */, BUTTON_RADIUS - 5 /* radius */, 0, 2 * M_PI); cairo_stroke(ctx); cairo_set_line_width(ctx, 7.0); /* Display a (centered) text of the current PAM state. */ char *text = NULL; /* We don't want to show more than a 3-digit number. */ char buf[4]; cairo_set_source_rgb(ctx, 0, 0, 0); cairo_set_font_size(ctx, 28.0); switch (pam_state) { case STATE_PAM_VERIFY: text = "verifying…"; break; case STATE_PAM_WRONG: text = "wrong!"; break; default: if (show_failed_attempts && failed_attempts > 0) { if (failed_attempts > 999) { text = "> 999"; } else { snprintf(buf, sizeof(buf), "%d", failed_attempts); text = buf; } cairo_set_source_rgb(ctx, 1, 0, 0); cairo_set_font_size(ctx, 32.0); } break; } if (text) { cairo_text_extents_t extents; double x, y; cairo_text_extents(ctx, text, &extents); x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing); y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing); cairo_move_to(ctx, x, y); cairo_show_text(ctx, text); cairo_close_path(ctx); } if (pam_state == STATE_PAM_WRONG && (modifier_string != NULL)) { cairo_text_extents_t extents; double x, y; cairo_set_font_size(ctx, 14.0); cairo_text_extents(ctx, modifier_string, &extents); x = BUTTON_CENTER - ((extents.width / 2) + extents.x_bearing); y = BUTTON_CENTER - ((extents.height / 2) + extents.y_bearing) + 28.0; cairo_move_to(ctx, x, y); cairo_show_text(ctx, modifier_string); cairo_close_path(ctx); } /* After the user pressed any valid key or the backspace key, we * highlight a random part of the unlock indicator to confirm this * keypress. */ if (unlock_state == STATE_KEY_ACTIVE || unlock_state == STATE_BACKSPACE_ACTIVE) { cairo_new_sub_path(ctx); double highlight_start = (rand() % (int)(2 * M_PI * 100)) / 100.0; cairo_arc(ctx, BUTTON_CENTER /* x */, BUTTON_CENTER /* y */, BUTTON_RADIUS /* radius */, highlight_start, highlight_start + (M_PI / 3.0)); if (unlock_state == STATE_KEY_ACTIVE) { /* For normal keys, we use a light blue. */ cairo_set_source_rgb(ctx, 0.63, 0.83, 1.0); } else { /* For backspace, we use red. */ cairo_set_source_rgb(ctx, 0.79, 0.19, 0.19); } cairo_stroke(ctx); /* Draw two little separators for the highlighted part of the * unlock indicator. */ cairo_set_source_rgb(ctx, 0, 0, 0); cairo_arc(ctx, BUTTON_CENTER /* x */, BUTTON_CENTER /* y */, BUTTON_RADIUS /* radius */, highlight_start /* start */, highlight_start + (M_PI / 128.0) /* end */); cairo_stroke(ctx); cairo_arc(ctx, BUTTON_CENTER /* x */, BUTTON_CENTER /* y */, BUTTON_RADIUS /* radius */, highlight_start + (M_PI / 3.0) /* start */, (highlight_start + (M_PI / 3.0)) + (M_PI / 128.0) /* end */); cairo_stroke(ctx); } } if (xr_screens > 0) { /* Composite the unlock indicator in the middle of each screen. */ for (int screen = 0; screen < xr_screens; screen++) { int x = (xr_resolutions[screen].x + ((xr_resolutions[screen].width / 2) - (button_diameter_physical / 2))); int y = (xr_resolutions[screen].y + ((xr_resolutions[screen].height / 2) - (button_diameter_physical / 2))); cairo_set_source_surface(xcb_ctx, output, x, y); cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical); cairo_fill(xcb_ctx); } } else { /* We have no information about the screen sizes/positions, so we just * place the unlock indicator in the middle of the X root window and * hope for the best. */ int x = (last_resolution[0] / 2) - (button_diameter_physical / 2); int y = (last_resolution[1] / 2) - (button_diameter_physical / 2); cairo_set_source_surface(xcb_ctx, output, x, y); cairo_rectangle(xcb_ctx, x, y, button_diameter_physical, button_diameter_physical); cairo_fill(xcb_ctx); } cairo_surface_destroy(xcb_output); cairo_surface_destroy(output); cairo_destroy(ctx); cairo_destroy(xcb_ctx); return bg_pixmap; }
int main(int argc, char* argv[]) { std::string map; std::string style; std::string output; size_t width,height; double lon,lat,zoom; if (argc!=9) { std::cerr << "DrawMap <map directory> <style-file> <width> <height> <lon> <lat> <zoom> <output>" << std::endl; return 1; } map=argv[1]; style=argv[2]; if (!osmscout::StringToNumber(argv[3],width)) { std::cerr << "width is not numeric!" << std::endl; return 1; } if (!osmscout::StringToNumber(argv[4],height)) { std::cerr << "height is not numeric!" << std::endl; return 1; } if (sscanf(argv[5],"%lf",&lon)!=1) { std::cerr << "lon is not numeric!" << std::endl; return 1; } if (sscanf(argv[6],"%lf",&lat)!=1) { std::cerr << "lat is not numeric!" << std::endl; return 1; } if (sscanf(argv[7],"%lf",&zoom)!=1) { std::cerr << "zoom is not numeric!" << std::endl; return 1; } output=argv[8]; osmscout::DatabaseParameter databaseParameter; osmscout::DatabaseRef database(new osmscout::Database(databaseParameter)); osmscout::MapServiceRef mapService(new osmscout::MapService(database)); if (!database->Open(map.c_str())) { std::cerr << "Cannot open database" << std::endl; return 1; } osmscout::StyleConfigRef styleConfig(new osmscout::StyleConfig(database->GetTypeConfig())); if (!styleConfig->Load(style)) { std::cerr << "Cannot open style" << std::endl; } cairo_surface_t *surface; cairo_t *cairo; surface=cairo_image_surface_create(CAIRO_FORMAT_RGB24,width,height); if (surface!=NULL) { cairo=cairo_create(surface); if (cairo!=NULL) { osmscout::MercatorProjection projection; osmscout::MapParameter drawParameter; osmscout::AreaSearchParameter searchParameter; osmscout::MapData data; osmscout::MapPainterCairo painter(styleConfig); drawParameter.SetFontSize(3.0); projection.Set(osmscout::GeoCoord(lat,lon), osmscout::Magnification(zoom), DPI, width, height); std::list<osmscout::TileRef> tiles; mapService->LookupTiles(projection,tiles); mapService->LoadMissingTileData(searchParameter,*styleConfig,tiles); mapService->ConvertTilesToMapData(tiles,data); if (painter.DrawMap(projection, drawParameter, data, cairo)) { if (cairo_surface_write_to_png(surface,output.c_str())!=CAIRO_STATUS_SUCCESS) { std::cerr << "Cannot write PNG" << std::endl; } } cairo_destroy(cairo); } else { std::cerr << "Cannot create cairo cairo" << std::endl; } cairo_surface_destroy(surface); } else { std::cerr << "Cannot create cairo surface" << std::endl; } return 0; }
static void emPdfRender(const char * args) { unsigned char * buf, * ps, * pt, * pe; PopplerPage * page; cairo_surface_t * surface; cairo_t * cr; emPdfInst * inst; FILE * f; double srcX, srcY, srcW, srcH; int instId, pageIndex, outW, outH; unsigned int v; if ( sscanf( args,"%d %d %lg %lg %lg %lg %d %d", &instId,&pageIndex,&srcX,&srcY,&srcW,&srcH,&outW,&outH )!=8 || instId<0 || instId>=emPdfInstArraySize || !emPdfInstArray[instId] || pageIndex<0 || pageIndex>=emPdfInstArray[instId]->pageCount || srcW<=0.0 || srcH<=0.0 || outW<=0 || outH<=0 ) { printf("error: emPdfRender: illegal arguments.\n"); return; } inst=emPdfInstArray[instId]; buf=(unsigned char*)malloc(outW*outH*4); memset(buf,0xff,outW*outH*4); surface=cairo_image_surface_create_for_data( buf, CAIRO_FORMAT_RGB24, outW, outH, outW*4 ); if (cairo_surface_status(surface)!=CAIRO_STATUS_SUCCESS) { printf( "error: PDF rendering failed (bad surface status: %d).\n", (int)cairo_surface_status(surface) ); free(buf); return; } cr=cairo_create(surface); if (cairo_status(cr)!=CAIRO_STATUS_SUCCESS) { printf( "error: PDF rendering failed (bad context status: %d).\n", (int)cairo_status(cr) ); cairo_surface_destroy(surface); free(buf); return; } cairo_scale(cr,outW/srcW,outH/srcH); cairo_translate(cr,-srcX,-srcY); page = poppler_document_get_page(inst->doc,pageIndex); if (page) { poppler_page_render(page,cr); g_object_unref(page); } else { memset(buf,0x88,outW*outH*4); } cairo_destroy(cr); cairo_surface_destroy(surface); #if defined(EM_PDF_DEBUG_RENDER_TO_FILE) f=fopen("/tmp/emPdfTest.ppm","wb"); #else f=stdout; #endif fprintf(f,"P6\n%d\n%d\n255\n",outW,outH); ps=buf; pt=buf; pe=buf+outW*outH*4; while (ps<pe) { v=*(unsigned int*)ps; pt[0]=(unsigned char)(v>>16); pt[1]=(unsigned char)(v>>8); pt[2]=(unsigned char)v; ps+=4; pt+=3; } fwrite(buf,outW*outH,3,f); if (f!=stdout) fclose(f); free(buf); }
/* TODO remove redundant code */ static void i7_cell_renderer_transcript_render(GtkCellRenderer *self, GdkWindow *window, GtkWidget *widget, GdkRectangle *background_area, GdkRectangle *cell_area, GdkRectangle *expose_area, GtkCellRendererState flags) { I7_CELL_RENDERER_TRANSCRIPT_USE_PRIVATE; int x, y, width, height; unsigned xpad, ypad, transcript_width; GtkStateType state; cairo_t *cr; PangoRectangle command_rect; PangoLayout *layout; GtkStyle *style = gtk_widget_get_style(widget); /* Get the size we calculated earlier and then take the padding into account */ g_object_get(self, "xpad", &xpad, "ypad", &ypad, NULL); gtk_cell_renderer_get_size(self, widget, cell_area, &x, &y, &width, &height); x += cell_area->x + (int)xpad; y += cell_area->y + (int)ypad; width -= (int)xpad * 2; height -= (int)ypad * 2; /* Decide what state to draw the widget components in */ switch(flags) { case GTK_CELL_RENDERER_PRELIT: state = GTK_STATE_PRELIGHT; break; case GTK_CELL_RENDERER_INSENSITIVE: state = GTK_STATE_INSENSITIVE; break; default: state = GTK_STATE_NORMAL; } /* Get a cairo context to draw the rectangles on directly; use GTK themed drawing to draw everything else */ cr = gdk_cairo_create(GDK_DRAWABLE(window)); /* Draw the command */ layout = gtk_widget_create_pango_layout(widget, priv->command); pango_layout_get_pixel_extents(layout, NULL, &command_rect); set_rgb_style(cr, STYLE_COMMAND); cairo_rectangle(cr, (double)x, (double)y, (double)width, (double)(command_rect.height + priv->text_padding * 2)); cairo_fill(cr); gtk_paint_layout(style, window, state, TRUE, cell_area, widget, NULL, x + priv->text_padding, y + priv->text_padding, layout); g_object_unref(layout); /* Draw the transcript text */ transcript_width = priv->default_width / 2 - xpad; if(priv->changed) set_rgb_style(cr, STYLE_CHANGED); else set_rgb_style(cr, STYLE_UNCHANGED); cairo_rectangle(cr, (double)x, (double)(y + command_rect.height + priv->text_padding * 2), (double)(width / 2), (double)(height - command_rect.height - priv->text_padding * 2)); cairo_fill(cr); layout = gtk_widget_create_pango_layout(widget, NULL); pango_layout_set_markup(layout, priv->transcript_text, -1); pango_layout_set_width(layout, (int)(transcript_width - priv->text_padding * 2) * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); gtk_paint_layout(style, window, state, TRUE, cell_area, widget, NULL, x + (int)priv->text_padding, y + command_rect.height + (int)priv->text_padding * 3, layout); g_object_unref(layout); /* Draw the expected text */ switch(priv->match_type) { case CANT_COMPARE: set_rgb_style(cr, STYLE_NO_EXPECTED); break; case NO_MATCH: set_rgb_style(cr, STYLE_NO_MATCH); break; case NEAR_MATCH: set_rgb_style(cr, STYLE_NEAR_MATCH); break; case EXACT_MATCH: default: set_rgb_style(cr, STYLE_EXACT_MATCH); break; } cairo_rectangle(cr, (double)(x + width / 2), (double)(y + command_rect.height + priv->text_padding * 2), (double)(width / 2), (double)(height - command_rect.height - priv->text_padding * 2)); cairo_fill(cr); layout = gtk_widget_create_pango_layout(widget, NULL); pango_layout_set_markup(layout, priv->expected_text, -1); pango_layout_set_width(layout, (int)(transcript_width - priv->text_padding * 2) * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); gtk_paint_layout(style, window, state, TRUE, cell_area, widget, NULL, x + width / 2 + (int)priv->text_padding, y + command_rect.height + (int)priv->text_padding * 3, layout); g_object_unref(layout); /* Draw some lines */ gtk_paint_hline(style, window, state, cell_area, widget, NULL, x, x + width, y + command_rect.height + priv->text_padding * 2); gtk_paint_vline(style, window, state, cell_area, widget, NULL, y + command_rect.height + priv->text_padding * 2, y + height, x + width / 2); /* Draw a border around the highlighted node */ if(priv->current) { cairo_set_line_width(cr, 4.0); set_rgb_style(cr, STYLE_HIGHLIGHT); cairo_rectangle(cr, (double)x + 2.0, (double)y + 2.0, (double)width - 4.0, (double)height - 4.0); cairo_stroke(cr); } /* Draw a border around the active node */ if(priv->played) { cairo_set_line_width(cr, 2.0); set_rgb_style(cr, STYLE_ACTIVE); cairo_rectangle(cr, (double)x + 1.0, (double)y + 1.0, (double)width - 2.0, (double)height - 2.0); cairo_stroke(cr); } cairo_destroy(cr); }
static void on_screenshot_finished (GObject *source, GAsyncResult *res, gpointer user_data) { ScreenshotData *data = user_data; CcBackgroundPanel *panel = data->panel; CcBackgroundPanelPrivate *priv; GError *error; GdkPixbuf *pixbuf; cairo_surface_t *surface; cairo_t *cr; GVariant *result; error = NULL; result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), res, &error); if (result == NULL) { if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { g_error_free (error); g_free (data); return; } g_debug ("Unable to get screenshot: %s", error->message); g_error_free (error); /* fallback? */ priv = panel->priv; goto out; } g_variant_unref (result); priv = panel->priv; pixbuf = gdk_pixbuf_new_from_file (panel->priv->screenshot_path, &error); if (pixbuf == NULL) { g_debug ("Unable to use GNOME Shell's builtin screenshot interface: %s", error->message); g_error_free (error); goto out; } surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, data->monitor_rect.width, data->monitor_rect.height); cr = cairo_create (surface); gdk_cairo_set_source_pixbuf (cr, pixbuf, data->capture_rect.x - data->monitor_rect.x, data->capture_rect.y - data->monitor_rect.y); cairo_paint (cr); g_object_unref (pixbuf); if (data->whole_monitor) { /* clear the workarea */ cairo_save (cr); cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); cairo_rectangle (cr, data->workarea_rect.x - data->monitor_rect.x, data->workarea_rect.y - data->monitor_rect.y, data->workarea_rect.width, data->workarea_rect.height); cairo_fill (cr); cairo_restore (cr); } g_clear_object (&panel->priv->display_screenshot); panel->priv->display_screenshot = gdk_pixbuf_get_from_surface (surface, 0, 0, data->monitor_rect.width, data->monitor_rect.height); /* remove the temporary file created by the shell */ g_unlink (panel->priv->screenshot_path); g_clear_pointer (&priv->screenshot_path, g_free); cairo_destroy (cr); cairo_surface_destroy (surface); out: update_display_preview (panel, WID ("background-desktop-drawingarea"), priv->current_background); g_free (data); }
/** * Sends the given character to the terminal at the given row and column, * rendering the character immediately. This bypasses the guac_terminal_display * mechanism and is intended for flushing of updates only. */ int __guac_terminal_set(guac_terminal_display* display, int row, int col, int codepoint) { int width; int bytes; char utf8[4]; /* Use foreground color */ const guac_terminal_color* color = &display->glyph_foreground; /* Use background color */ const guac_terminal_color* background = &display->glyph_background; cairo_surface_t* surface; cairo_t* cairo; int surface_width, surface_height; PangoLayout* layout; int layout_width, layout_height; int ideal_layout_width, ideal_layout_height; /* Calculate width in columns */ width = wcwidth(codepoint); if (width < 0) width = 1; /* Do nothing if glyph is empty */ if (width == 0) return 0; /* Convert to UTF-8 */ bytes = guac_terminal_encode_utf8(codepoint, utf8); surface_width = width * display->char_width; surface_height = display->char_height; ideal_layout_width = surface_width * PANGO_SCALE; ideal_layout_height = surface_height * PANGO_SCALE; /* Prepare surface */ surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, surface_width, surface_height); cairo = cairo_create(surface); /* Fill background */ cairo_set_source_rgb(cairo, background->red / 255.0, background->green / 255.0, background->blue / 255.0); cairo_rectangle(cairo, 0, 0, surface_width, surface_height); cairo_fill(cairo); /* Get layout */ layout = pango_cairo_create_layout(cairo); pango_layout_set_font_description(layout, display->font_desc); pango_layout_set_text(layout, utf8, bytes); pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); pango_layout_get_size(layout, &layout_width, &layout_height); /* If layout bigger than available space, scale it back */ if (layout_width > ideal_layout_width || layout_height > ideal_layout_height) { double scale = fmin(ideal_layout_width / (double) layout_width, ideal_layout_height / (double) layout_height); cairo_scale(cairo, scale, scale); /* Update layout to reflect scaled surface */ pango_layout_set_width(layout, ideal_layout_width / scale); pango_layout_set_height(layout, ideal_layout_height / scale); pango_cairo_update_layout(cairo, layout); } /* Draw */ cairo_set_source_rgb(cairo, color->red / 255.0, color->green / 255.0, color->blue / 255.0); cairo_move_to(cairo, 0.0, 0.0); pango_cairo_show_layout(cairo, layout); /* Draw */ guac_common_surface_draw(display->display_surface, display->char_width * col, display->char_height * row, surface); /* Free all */ g_object_unref(layout); cairo_destroy(cairo); cairo_surface_destroy(surface); return 0; }
int main(int argc, const char* argv[]) { size_t tex_side =(argc>1)? std::atoi(argv[1]) : 512; const char* font_desc_str = (argc>2)? argv[2] : "Sans 18"; unsigned plane = (argc>3)? std::atoi(argv[3]) : 0; cairo_surface_t *surface = cairo_image_surface_create( CAIRO_FORMAT_A8, tex_side, tex_side ); cairo_t *cr = cairo_create(surface); PangoFontDescription *font_desc = pango_font_description_from_string( font_desc_str ); PangoFontMap* font_map = pango_cairo_font_map_get_default(); PangoContext* context = pango_font_map_create_context(font_map); PangoFont* font = pango_font_map_load_font( font_map, context, font_desc ); PangoFontMetrics* font_metrics = pango_font_get_metrics(font, nullptr); // The Bitmap Glyph Metrics file std::ofstream bgm((argc>5) ? argv[5] : "out.bgm"); unsigned step = tex_side / 16; for(unsigned y=0; y!=16; ++y) { for(unsigned x=0; x!=16; ++x) { render_glyph( cr, font_desc, font, 256*plane + y*16 + x, x, y, step, tex_side, pango_font_metrics_get_ascent(font_metrics), pango_font_metrics_get_descent(font_metrics), bgm ); } } bgm.close(); pango_font_metrics_unref(font_metrics); pango_font_description_free(font_desc); g_object_unref(context); cairo_destroy(cr); cairo_status_t status = cairo_surface_write_to_png( surface, (argc>4) ? argv[4] : "out.png" ); cairo_surface_destroy(surface); return 0; }
static void gnm_so_path_set_property (GObject *obj, guint param_id, GValue const *value, GParamSpec *pspec) { GnmSOPath *sop = GNM_SO_PATH (obj); switch (param_id) { case SOP_PROP_STYLE: { GOStyle *style = go_style_dup (g_value_get_object (value)); style->interesting_fields = GO_STYLE_OUTLINE | GO_STYLE_FILL; g_object_unref (sop->style); sop->style = style; break; } case SOP_PROP_PATH: { GOPath *path = g_value_get_boxed (value); if (sop->path) go_path_free (sop->path); else if (sop->paths) g_ptr_array_unref (sop->paths); sop->path = NULL; sop->paths = NULL; if (path) { cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1); cairo_t *cr = cairo_create (surface); sop->path = go_path_ref (path); /* evaluates the bounding rectangle */ go_path_to_cairo (path, GO_PATH_DIRECTION_FORWARD, cr); cairo_fill_extents (cr, &sop->x_offset, &sop->y_offset, &sop->width, &sop->height); sop->width -= sop->x_offset; sop->height -= sop->y_offset; cairo_destroy (cr); cairo_surface_destroy (surface); } break; } case SOP_PROP_PATHS: { GPtrArray *paths = g_value_get_boxed (value); unsigned i; for (i = 0; i < paths->len; i++) /* we can only check that the path is not NULL */ g_return_if_fail (g_ptr_array_index (paths, i) != NULL); if (sop->path) go_path_free (sop->path); else if (sop->paths) g_ptr_array_unref (sop->paths); sop->path = NULL; sop->paths = NULL; if (paths) { cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1); cairo_t *cr = cairo_create (surface); sop->paths = g_ptr_array_ref (paths); /* evaluates the bounding rectangle */ for (i = 0; i < paths->len; i++) go_path_to_cairo ((GOPath *) g_ptr_array_index (paths, i), GO_PATH_DIRECTION_FORWARD, cr); cairo_fill_extents (cr, &sop->x_offset, &sop->y_offset, &sop->width, &sop->height); sop->width -= sop->x_offset; sop->height -= sop->y_offset; cairo_destroy (cr); cairo_surface_destroy (surface); } break; } case SOP_PROP_TEXT: { char const *str = g_value_get_string (value); g_free (sop->text); sop->text = g_strdup (str == NULL ? "" : str); break; } case SOP_PROP_MARKUP: if (sop->markup != NULL) pango_attr_list_unref (sop->markup); sop->markup = g_value_peek_pointer (value); if (sop->markup != NULL) pango_attr_list_ref (sop->markup); break; case SOP_PROP_VIEWBOX: /* not settable */ default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, param_id, pspec); return; } }
/** generate a mikaGlImage from a jpeg file */ mikaGlImage::mikaGlImage( unsigned char *filename ) { #ifdef _GLDEBUG printf("mikaGlImage::mikaGlImage\n"); #endif csurface = NULL; struct jpeg_decompress_struct cinfo; struct jpeg_decompress_struct *ciptr; struct jpeg_error_mgr *emgr = NULL; FILE *fd = NULL; JSAMPLE *lb; ciptr = &cinfo; if ((fd=fopen((char*)filename,"rb"))==NULL) { printf("failed to open jpeg (%s)\n",(char*)filename); throw "failed to open jpeg"; } else { #ifdef _GLDEBUG printf("opened '%s'\n",(char*)filename); #endif } if (fd) { emgr = (struct jpeg_error_mgr*)malloc(sizeof(struct jpeg_error_mgr)); ciptr->err = jpeg_std_error(emgr); emgr->error_exit = __jpeg_cb_error_exit; jpeg_create_decompress(ciptr); jpeg_stdio_src(ciptr,fd); jpeg_read_header(ciptr,1); ciptr->out_color_space = JCS_RGB; jpeg_start_decompress(ciptr); static int h, w, cc, xs, yn, xn; static unsigned char pixel[4]; pixel[0] = 0x10; pixel[1] = 0x10; pixel[2] = 0x10; pixel[3] = 0xff; w = ciptr->output_width; h = ciptr->output_height; cc = ciptr->output_components; mw = w; mh = h; pixels = (unsigned char*)malloc(mw*4*mh); for(yn = 0; yn < mh; yn++) { for(xn = 0; xn < mw; xn++) { memcpy( pixels+(yn*(mw*4))+(xn*4), (void*)&pixel,4 ); } } #ifdef _GLDEBUG printf("jpeg_image_attr h(%d) w(%d)\n",w,h); #endif lb = (JSAMPLE *)(*ciptr->mem->alloc_small)( (j_common_ptr)ciptr, JPOOL_PERMANENT, cc*w); yn = 0; while(ciptr->output_scanline < ciptr->output_height) { jpeg_read_scanlines(ciptr,&lb,1); for (xs = 0; xs < w; xs++) { pixel[2] = *(lb+3*xs); pixel[1] = *(lb+3*xs+1); pixel[0] = *(lb+3*xs+2); memcpy( pixels+(yn*(mw*4))+(xs*4), (void*)&pixel,4 ); } yn++; } jpeg_finish_decompress(ciptr); jpeg_destroy_decompress(ciptr); fclose(fd); csurface = cairo_image_surface_create_for_data( pixels, CAIRO_FORMAT_ARGB32, mw,mh, 4*mw ); if (!csurface) { #ifdef _GLDEBUG printf("couldn't create cairo surface from jpeg image\n"); #endif } // testing scaling #ifdef _TEST_SCALING unsigned char *newPixels = (unsigned char*)malloc(mw*mh*4); memset(newPixels,0x00,mw*mh*4); cairo_surface_t *newSurface = cairo_image_surface_create_for_data( newPixels, CAIRO_FORMAT_ARGB32, mw,mh, 4*mw ); cairo_t *cnew = cairo_create(newSurface); cairo_scale(cnew,(double)0.3,(double)0.3); cairo_rotate(cnew,12.0*3.14/180); cairo_set_source_surface(cnew,csurface,0,0); // the source is the original image cairo_paint(cnew); cairo_surface_flush(csurface); cairo_surface_flush(newSurface); cairo_surface_finish(csurface); cairo_surface_finish(newSurface); memcpy(pixels,newPixels,mw*mh*4); free(newPixels); cairo_destroy(cnew); cairo_surface_destroy(newSurface); #endif } }
static gboolean dt_iop_levels_expose(GtkWidget *widget, GdkEventExpose *event, gpointer user_data) { dt_iop_module_t *self = (dt_iop_module_t *)user_data; dt_iop_levels_gui_data_t *c = (dt_iop_levels_gui_data_t *)self->gui_data; dt_iop_levels_params_t *p = (dt_iop_levels_params_t *)self->params; const int inset = DT_GUI_CURVE_EDITOR_INSET; int width = widget->allocation.width, height = widget->allocation.height; cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); cairo_t *cr = cairo_create(cst); // clear bg cairo_set_source_rgb (cr, .2, .2, .2); cairo_paint(cr); cairo_translate(cr, inset, inset); width -= 2*inset; height -= 2*inset; cairo_set_line_width(cr, 1.0); cairo_set_source_rgb (cr, .1, .1, .1); cairo_rectangle(cr, 0, 0, width, height); cairo_stroke(cr); cairo_set_source_rgb (cr, .3, .3, .3); cairo_rectangle(cr, 0, 0, width, height); cairo_fill(cr); // draw grid cairo_set_line_width(cr, .4); cairo_set_source_rgb (cr, .1, .1, .1); dt_draw_vertical_lines(cr, 4, 0, 0, width, height); // Drawing the vertical line indicators cairo_set_line_width(cr, 2.); for(int k = 0; k < 3; k++) { if(k == c->handle_move && c->mouse_x > 0) cairo_set_source_rgb(cr, 1, 1, 1); else cairo_set_source_rgb(cr, .7, .7, .7); cairo_move_to(cr, width*p->levels[k], height); cairo_rel_line_to(cr, 0, -height); cairo_stroke(cr); } // draw x positions cairo_set_line_width(cr, 1.); const float arrw = 7.0f; for(int k=0; k<3; k++) { switch(k) { case 0: cairo_set_source_rgb(cr, 0, 0, 0); break; case 1: cairo_set_source_rgb(cr, 0.5, 0.5, 0.5); break; default: cairo_set_source_rgb(cr, 1, 1, 1); break; } cairo_move_to(cr, width*p->levels[k], height+inset-1); cairo_rel_line_to(cr, -arrw*.5f, 0); cairo_rel_line_to(cr, arrw*.5f, -arrw); cairo_rel_line_to(cr, arrw*.5f, arrw); cairo_close_path(cr); if(c->handle_move == k && c->mouse_x > 0) cairo_fill(cr); else cairo_stroke(cr); } cairo_translate(cr, 0, height); // draw lum histogram in background // only if the module is enabled if (self->enabled) { dt_develop_t *dev = darktable.develop; float *hist, hist_max; hist = dev->histogram_pre_levels; hist_max = dev->histogram_pre_levels_max; if(hist_max > 0) { cairo_save(cr); cairo_scale(cr, width/63.0, -(height-5)/(float)hist_max); cairo_set_source_rgba(cr, .2, .2, .2, 0.5); dt_draw_histogram_8(cr, hist, 3); cairo_restore(cr); } } // Cleaning up cairo_destroy(cr); cairo_t *cr_pixmap = gdk_cairo_create(gtk_widget_get_window(widget)); cairo_set_source_surface (cr_pixmap, cst, 0, 0); cairo_paint(cr_pixmap); cairo_destroy(cr_pixmap); cairo_surface_destroy(cst); return TRUE; }