1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #include <stdio.h> 8 9 #include "sk_data.h" 10 #include "sk_image.h" 11 #include "sk_canvas.h" 12 #include "sk_surface.h" 13 #include "sk_paint.h" 14 #include "sk_path.h" 15 16 static sk_surface_t* make_surface(int32_t w, int32_t h) { 17 sk_imageinfo_t info; 18 info.width = w; 19 info.height = h; 20 info.colorType = sk_colortype_get_default_8888(); 21 info.alphaType = PREMUL_SK_ALPHATYPE; 22 return sk_surface_new_raster(&info, NULL); 23 } 24 25 static void emit_png(const char* path, sk_surface_t* surface) { 26 sk_image_t* image = sk_surface_new_image_snapshot(surface); 27 sk_data_t* data = sk_image_encode(image); 28 sk_image_unref(image); 29 FILE* f = fopen(path, "wb"); 30 fwrite(sk_data_get_data(data), sk_data_get_size(data), 1, f); 31 fclose(f); 32 sk_data_unref(data); 33 } 34 35 void draw(sk_canvas_t* canvas) { 36 sk_paint_t* fill = sk_paint_new(); 37 sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0x00, 0xFF)); 38 sk_canvas_draw_paint(canvas, fill); 39 40 sk_paint_set_color(fill, sk_color_set_argb(0xFF, 0x00, 0xFF, 0xFF)); 41 sk_rect_t rect; 42 rect.left = 100.0f; 43 rect.top = 100.0f; 44 rect.right = 540.0f; 45 rect.bottom = 380.0f; 46 sk_canvas_draw_rect(canvas, &rect, fill); 47 48 sk_paint_t* stroke = sk_paint_new(); 49 sk_paint_set_color(stroke, sk_color_set_argb(0xFF, 0xFF, 0x00, 0x00)); 50 sk_paint_set_antialias(stroke, true); 51 sk_paint_set_stroke(stroke, true); 52 sk_paint_set_stroke_width(stroke, 5.0f); 53 sk_path_t* path = sk_path_new(); 54 55 sk_path_move_to(path, 50.0f, 50.0f); 56 sk_path_line_to(path, 590.0f, 50.0f); 57 sk_path_cubic_to(path, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f); 58 sk_path_line_to(path, 590.0f, 430.0f); 59 sk_canvas_draw_path(canvas, path, stroke); 60 61 sk_paint_set_color(fill, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00)); 62 sk_rect_t rect2; 63 rect2.left = 120.0f; 64 rect2.top = 120.0f; 65 rect2.right = 520.0f; 66 rect2.bottom = 360.0f; 67 sk_canvas_draw_oval(canvas, &rect2, fill); 68 69 sk_path_delete(path); 70 sk_paint_delete(stroke); 71 sk_paint_delete(fill); 72 } 73 74 int main() { 75 sk_surface_t* surface = make_surface(640, 480); 76 sk_canvas_t* canvas = sk_surface_get_canvas(surface); 77 draw(canvas); 78 emit_png("skia-c-example.png", surface); 79 sk_surface_unref(surface); 80 return 0; 81 } 82