• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sk_surface_t* result = sk_surface_new_raster(info, NULL);
21     sk_imageinfo_delete(info);
22     return result;
23 }
24 
emit_png(const char * path,sk_surface_t * surface)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 
draw(sk_canvas_t * canvas)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 
54     sk_pathbuilder_t* path_builder = sk_pathbuilder_new();
55     sk_pathbuilder_move_to(path_builder, 50.0f, 50.0f);
56     sk_pathbuilder_line_to(path_builder, 590.0f, 50.0f);
57     sk_pathbuilder_cubic_to(path_builder, -490.0f, 50.0f, 1130.0f, 430.0f, 50.0f, 430.0f);
58     sk_pathbuilder_line_to(path_builder, 590.0f, 430.0f);
59 
60     sk_path_t* path = sk_pathbuilder_detach_path(path_builder);
61     sk_canvas_draw_path(canvas, path, stroke);
62 
63     sk_paint_set_color(fill, sk_color_set_argb(0x80, 0x00, 0xFF, 0x00));
64     sk_rect_t rect2;
65     rect2.left = 120.0f;
66     rect2.top = 120.0f;
67     rect2.right = 520.0f;
68     rect2.bottom = 360.0f;
69     sk_canvas_draw_oval(canvas, &rect2, fill);
70 
71     sk_pathbuilder_delete(path_builder);
72     sk_path_delete(path);
73     sk_paint_delete(stroke);
74     sk_paint_delete(fill);
75 }
76 
main()77 int main() {
78     sk_surface_t* surface = make_surface(640, 480);
79     sk_canvas_t* canvas = sk_surface_get_canvas(surface);
80     draw(canvas);
81     emit_png("skia-c-example.png", surface);
82     sk_surface_unref(surface);
83     return 0;
84 }
85