Skip to content

Commit b1c9995

Browse files
committed
Raster backend (#18)
1 parent 955a72f commit b1c9995

33 files changed

Lines changed: 3749 additions & 563 deletions

examples/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,3 @@ if (NOT MSVC)
2929
target_compile_options(font-example PRIVATE -fsanitize=address,undefined)
3030
target_link_options(font-example PRIVATE -fsanitize=address,undefined)
3131
endif()
32-
33-
add_executable(tessellation-example tessellation.c)
34-
target_link_libraries(tessellation-example PRIVATE canvas)
35-
target_compile_definitions(tessellation-example PRIVATE DEBUG TEST)
36-
if (NOT MSVC)
37-
target_compile_options(tessellation-example PRIVATE -fsanitize=address,undefined)
38-
target_link_options(tessellation-example PRIVATE -fsanitize=address,undefined)
39-
endif()

examples/font.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ int main(int argc, char** argv) {
2626
SfntGlyph glyph;
2727
REQUIRE(sfnt_get_glyph_for_cid(&font, '%', &glyph));
2828

29-
Canvas* canvas = canvas_new_scalable(arena, 2000, 2000, 0xffffffff, 1.0);
29+
Canvas* canvas = canvas_new_scalable(
30+
arena,
31+
2000,
32+
2000,
33+
rgba_new(1.0, 1.0, 1.0, 1.0),
34+
1.0
35+
);
3036
GeomMat3 transform =
3137
geom_mat3_new(1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 500.0, 1500.0, 1.0);
3238
sfnt_glyph_render(
@@ -35,7 +41,7 @@ int main(int argc, char** argv) {
3541
transform,
3642
(CanvasBrush) {.enable_fill = true,
3743
.enable_stroke = false,
38-
.fill_rgba = 0x000000ff}
44+
.fill_rgba = rgba_new(0.0, 0.0, 0.0, 1.0)}
3945
);
4046
canvas_write_file(canvas, "glyph.svg");
4147

examples/pdf.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ int main(int argc, char** argv) {
4343
}
4444

4545
Canvas* canvas = NULL;
46-
REQUIRE(render_page(arena, resolver, &page, &canvas));
46+
REQUIRE(render_page(
47+
arena,
48+
resolver,
49+
&page,
50+
RENDER_CANVAS_TYPE_SCALABLE,
51+
&canvas
52+
));
4753
canvas_write_file(canvas, "test.svg");
4854
};
4955

examples/tessellation.c

Lines changed: 0 additions & 44 deletions
This file was deleted.

libs/canvas/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
add_library(canvas
22
src/canvas.c
33
src/path_builder.c
4-
src/dcel_builder.c
54
src/dcel.c
65
src/raster_canvas.c
76
src/scalable_canvas.c)
87
target_include_directories(canvas PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
9-
target_link_libraries(canvas PUBLIC arena geom str)
10-
target_link_libraries(canvas PRIVATE logger $<$<NOT:$<PLATFORM_ID:Windows>>:m>)
8+
target_link_libraries(canvas PUBLIC arena color geom str)
9+
target_link_libraries(canvas PRIVATE logger pdf-test $<$<NOT:$<PLATFORM_ID:Windows>>:m>)
1110
target_compile_features(canvas PUBLIC c_std_11)
1211
if (NOT MSVC)
1312
target_compile_options(canvas PRIVATE -fsanitize=address,undefined)

libs/canvas/include/canvas/canvas.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "arena/arena.h"
88
#include "canvas/path_builder.h"
9+
#include "color/rgb.h"
910

1011
typedef enum CanvasLineCap {
1112
CANVAS_LINECAP_BUTT,
@@ -21,10 +22,11 @@ typedef enum CanvasLineJoin {
2122

2223
typedef struct CanvasBrush {
2324
bool enable_fill;
25+
bool even_odd_fill;
2426
bool enable_stroke;
2527

26-
uint32_t fill_rgba;
27-
uint32_t stroke_rgba;
28+
Rgba fill_rgba;
29+
Rgba stroke_rgba;
2830

2931
double stroke_width;
3032
CanvasLineCap line_cap;
@@ -38,15 +40,14 @@ Canvas* canvas_new_raster(
3840
Arena* arena,
3941
uint32_t width,
4042
uint32_t height,
41-
uint32_t rgba,
42-
double coordinate_scale
43+
Rgba rgba
4344
);
4445

4546
Canvas* canvas_new_scalable(
4647
Arena* arena,
4748
uint32_t width,
4849
uint32_t height,
49-
uint32_t rgba,
50+
Rgba rgba,
5051
double raster_res
5152
);
5253

@@ -58,7 +59,7 @@ void canvas_draw_circle(
5859
double x,
5960
double y,
6061
double radius,
61-
uint32_t rgba
62+
Rgba rgba
6263
);
6364

6465
void canvas_draw_line(
@@ -68,7 +69,7 @@ void canvas_draw_line(
6869
double x2,
6970
double y2,
7071
double radius,
71-
uint32_t rgba
72+
Rgba rgba
7273
);
7374

7475
void canvas_draw_bezier(
@@ -81,7 +82,7 @@ void canvas_draw_bezier(
8182
double cy,
8283
double flatness,
8384
double radius,
84-
uint32_t rgba
85+
Rgba rgba
8586
);
8687

8788
void canvas_draw_path(
@@ -98,7 +99,7 @@ void canvas_push_clip_path(
9899

99100
void canvas_pop_clip_paths(Canvas* canvas, size_t count);
100101

101-
void canvas_draw_pixel(Canvas* canvas, GeomVec2 position, uint32_t rgba);
102+
void canvas_draw_pixel(Canvas* canvas, GeomVec2 position, Rgba rgba);
102103

103104
/// Writes the canvas to a file. Returns `true` on success.
104105
bool canvas_write_file(Canvas* canvas, const char* path);

libs/canvas/include/canvas/dcel_builder.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

libs/canvas/include/canvas/path_builder.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
#pragma once
22

3+
#include <stdbool.h>
4+
35
#include "arena/arena.h"
46
#include "geom/mat3.h"
57
#include "geom/vec2.h"
68

79
typedef struct PathBuilder PathBuilder;
10+
typedef struct PathBuilderOptions {
11+
bool flatten_curves;
12+
double quad_flatness;
13+
int quad_max_depth;
14+
double cubic_flatness;
15+
int cubic_max_depth;
16+
} PathBuilderOptions;
17+
18+
PathBuilderOptions path_builder_options_default(void);
19+
PathBuilderOptions path_builder_options_flattened(void);
820

921
PathBuilder* path_builder_new(Arena* arena);
22+
PathBuilder*
23+
path_builder_new_with_options(Arena* arena, PathBuilderOptions options);
1024
PathBuilder* path_builder_clone(Arena* arena, const PathBuilder* to_clone);
25+
void path_builder_set_options(PathBuilder* builder, PathBuilderOptions options);
1126

1227
void path_builder_new_contour(PathBuilder* builder, GeomVec2 point);
1328
void path_builder_close_contour(PathBuilder* builder);
@@ -24,4 +39,5 @@ void path_builder_cubic_bezier_to(
2439
GeomVec2 control_a,
2540
GeomVec2 control_b
2641
);
42+
2743
void path_builder_apply_transform(PathBuilder* path, GeomMat3 transform);

libs/canvas/src/canvas.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ Canvas* canvas_new_raster(
1919
Arena* arena,
2020
uint32_t width,
2121
uint32_t height,
22-
uint32_t rgba,
23-
double coordinate_scale
22+
Rgba rgba
2423
) {
2524
Canvas* canvas = arena_alloc(arena, sizeof(Canvas));
26-
canvas->data.raster =
27-
raster_canvas_new(arena, width, height, rgba, coordinate_scale);
25+
canvas->data.raster = raster_canvas_new(arena, width, height, rgba);
2826
canvas->type = CANVAS_TYPE_RASTER;
2927

3028
return canvas;
@@ -42,7 +40,7 @@ Canvas* canvas_new_scalable(
4240
Arena* arena,
4341
uint32_t width,
4442
uint32_t height,
45-
uint32_t rgba,
43+
Rgba rgba,
4644
double raster_res
4745
) {
4846
Canvas* canvas = arena_alloc(arena, sizeof(Canvas));
@@ -61,7 +59,7 @@ bool canvas_is_raster(Canvas* canvas) {
6159
double canvas_raster_res(Canvas* canvas) {
6260
RELEASE_ASSERT(canvas);
6361
if (canvas->type == CANVAS_TYPE_RASTER) {
64-
return 1.0;
62+
return raster_canvas_raster_res(canvas->data.raster);
6563
} else {
6664
return scalable_canvas_raster_res(canvas->data.scalable);
6765
}
@@ -72,7 +70,7 @@ void canvas_draw_circle(
7270
double x,
7371
double y,
7472
double radius,
75-
uint32_t rgba
73+
Rgba rgba
7674
) {
7775
switch (canvas->type) {
7876
case CANVAS_TYPE_RASTER: {
@@ -102,7 +100,7 @@ void canvas_draw_line(
102100
double x2,
103101
double y2,
104102
double radius,
105-
uint32_t rgba
103+
Rgba rgba
106104
) {
107105
switch (canvas->type) {
108106
case CANVAS_TYPE_RASTER: {
@@ -145,7 +143,7 @@ void canvas_draw_bezier(
145143
double cy,
146144
double flatness,
147145
double radius,
148-
uint32_t rgba
146+
Rgba rgba
149147
) {
150148
switch (canvas->type) {
151149
case CANVAS_TYPE_RASTER: {
@@ -255,12 +253,13 @@ void canvas_pop_clip_paths(Canvas* canvas, size_t count) {
255253
}
256254
}
257255

258-
void canvas_draw_pixel(Canvas* canvas, GeomVec2 position, uint32_t rgba) {
256+
void canvas_draw_pixel(Canvas* canvas, GeomVec2 position, Rgba rgba) {
259257
RELEASE_ASSERT(canvas);
260258

261259
switch (canvas->type) {
262260
case CANVAS_TYPE_RASTER: {
263-
LOG_TODO();
261+
raster_canvas_draw_pixel(canvas->data.raster, position, rgba);
262+
break;
264263
}
265264
case CANVAS_TYPE_SCALABLE: {
266265
scalable_canvas_draw_pixel(canvas->data.scalable, position, rgba);

0 commit comments

Comments
 (0)