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