Skip to content

Commit 6118fe0

Browse files
committed
Get tests passing and implement more Context functions
1 parent 8a34592 commit 6118fe0

File tree

9 files changed

+191
-12
lines changed

9 files changed

+191
-12
lines changed

lib/xairo/context.ex

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ defmodule Xairo.Context do
3838
end
3939

4040
def stroke_preserve(%__MODULE__{context: ctx} = this) do
41-
with {:ok, _} <- Xairo.Native.context_stroke(ctx), do: this
41+
with {:ok, _} <- Xairo.Native.context_stroke_preserve(ctx), do: this
4242
end
4343

4444
def fill(%__MODULE__{context: ctx} = this) do
4545
with {:ok, _} <- Xairo.Native.context_fill(ctx), do: this
4646
end
4747

4848
def fill_preserve(%__MODULE__{context: ctx} = this) do
49-
with {:ok, _} <- Xairo.Native.context_fill(ctx), do: this
49+
with {:ok, _} <- Xairo.Native.context_fill_preserve(ctx), do: this
5050
end
5151

5252
def move_to(%__MODULE__{context: ctx} = this, x, y) do
@@ -59,6 +59,46 @@ defmodule Xairo.Context do
5959
this
6060
end
6161

62+
def curve_to(%__MODULE__{context: ctx} = this, x1, y1, x2, y2, x3, y3) do
63+
Xairo.Native.context_curve_to(ctx, x1 / 1, y1 / 1, x2 / 1, y2 / 1, x3 / 1, y3 / 1)
64+
this
65+
end
66+
67+
def rel_move_to(%__MODULE__{context: ctx} = this, x, y) do
68+
Xairo.Native.context_rel_move_to(ctx, x / 1, y / 1)
69+
this
70+
end
71+
72+
def rel_line_to(%__MODULE__{context: ctx} = this, x, y) do
73+
Xairo.Native.context_rel_line_to(ctx, x / 1, y / 1)
74+
this
75+
end
76+
77+
def rel_curve_to(%__MODULE__{context: ctx} = this, x1, y1, x2, y2, x3, y3) do
78+
Xairo.Native.context_rel_curve_to(ctx, x1 / 1, y1 / 1, x2 / 1, y2 / 1, x3 / 1, y3 / 1)
79+
this
80+
end
81+
82+
def arc(%__MODULE__{context: ctx} = this, cx, cy, r, a1, a2) do
83+
Xairo.Native.context_arc(ctx, cx / 1, cy / 1, r / 1, a1 / 1, a2 / 1)
84+
this
85+
end
86+
87+
def arc_negative(%__MODULE__{context: ctx} = this, cx, cy, r, a1, a2) do
88+
Xairo.Native.context_arc_negative(ctx, cx / 1, cy / 1, r / 1, a1 / 1, a2 / 1)
89+
this
90+
end
91+
92+
def new_path(%__MODULE__{context: ctx} = this) do
93+
Xairo.Native.context_new_path(ctx)
94+
this
95+
end
96+
97+
def new_sub_path(%__MODULE__{context: ctx} = this) do
98+
Xairo.Native.context_new_sub_path(ctx)
99+
this
100+
end
101+
62102
def rectangle(%__MODULE__{context: ctx} = this, x, y, w, h) do
63103
Xairo.Native.context_rectangle(ctx, x / 1, y / 1, w / 1, h / 1)
64104
this

lib/xairo/native.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,16 @@ defmodule Xairo.Native do
139139
def context_paint_with_alpha(_c, _a), do: error()
140140
def context_move_to(_c, _x, _y), do: error()
141141
def context_line_to(_c, _x, _y), do: error()
142+
def context_rel_move_to(_c, _x, _y), do: error()
143+
def context_rel_line_to(_c, _x, _y), do: error()
142144
def context_rectangle(_c, _x, _y, _w, _h), do: error()
143145
def context_close_path(_c), do: error()
146+
def context_curve_to(_c, _x1, _y1, _x2, _y2, _x3, _y3), do: error()
147+
def context_rel_curve_to(_c, _x1, _y1, _x2, _y2, _x3, _y3), do: error()
148+
def context_arc(_c, _x, _y, _r, _a1, _a2), do: error()
149+
def context_arc_negative(_c, _x, _y, _r, _a1, _a2), do: error()
150+
def context_new_path(_c), do: error()
151+
def context_new_sub_path(_c), do: error()
144152

145153
def stride_for_width(_f, _w), do: error()
146154

native/xairo/src/context.rs

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::image_surface::ImageSurface;
21
use crate::error::XairoError;
2+
use crate::image_surface::ImageSurface;
33
use rustler::ResourceArc;
44

55
pub struct ContextRaw {
@@ -15,15 +15,15 @@ pub type Context = ResourceArc<ContextRaw>;
1515
fn context_new_from_image_surface(surface: ImageSurface) -> Result<Context, XairoError> {
1616
match cairo::Context::new(&surface.surface) {
1717
Ok(context) => Ok(ResourceArc::new(ContextRaw { context })),
18-
Err(err) => Err(err.into())
18+
Err(err) => Err(err.into()),
1919
}
2020
}
2121

2222
#[rustler::nif]
2323
fn context_status(context: Context) -> Result<(), XairoError> {
2424
match context.context.status() {
2525
Ok(_) => Ok(()),
26-
Err(err) => Err(err.into())
26+
Err(err) => Err(err.into()),
2727
}
2828
}
2929

@@ -32,27 +32,56 @@ fn context_set_source_rgb(context: Context, r: f64, g: f64, b: f64) {
3232
context.context.set_source_rgb(r, g, b);
3333
}
3434

35+
#[rustler::nif]
36+
fn context_set_source_rgba(context: Context, r: f64, g: f64, b: f64, a: f64) {
37+
context.context.set_source_rgba(r, g, b, a);
38+
}
39+
3540
#[rustler::nif]
3641
fn context_paint(context: Context) -> Result<(), XairoError> {
3742
match context.context.paint() {
3843
Ok(_) => Ok(()),
39-
Err(err) => Err(err.into())
44+
Err(err) => Err(err.into()),
45+
}
46+
}
47+
48+
#[rustler::nif]
49+
fn context_paint_with_alpha(context: Context, alpha: f64) -> Result<(), XairoError> {
50+
match context.context.paint_with_alpha(alpha) {
51+
Ok(_) => Ok(()),
52+
Err(err) => Err(err.into()),
4053
}
4154
}
4255

4356
#[rustler::nif]
4457
fn context_fill(context: Context) -> Result<(), XairoError> {
4558
match context.context.fill() {
4659
Ok(_) => Ok(()),
47-
Err(err) => Err(err.into())
60+
Err(err) => Err(err.into()),
61+
}
62+
}
63+
64+
#[rustler::nif]
65+
fn context_fill_preserve(context: Context) -> Result<(), XairoError> {
66+
match context.context.fill_preserve() {
67+
Ok(_) => Ok(()),
68+
Err(err) => Err(err.into()),
4869
}
4970
}
5071

5172
#[rustler::nif]
5273
fn context_stroke(context: Context) -> Result<(), XairoError> {
5374
match context.context.stroke() {
5475
Ok(_) => Ok(()),
55-
Err(err) => Err(err.into())
76+
Err(err) => Err(err.into()),
77+
}
78+
}
79+
80+
#[rustler::nif]
81+
fn context_stroke_preserve(context: Context) -> Result<(), XairoError> {
82+
match context.context.stroke_preserve() {
83+
Ok(_) => Ok(()),
84+
Err(err) => Err(err.into()),
5685
}
5786
}
5887

@@ -61,12 +90,57 @@ fn context_move_to(context: Context, x: f64, y: f64) {
6190
context.context.move_to(x, y);
6291
}
6392

93+
#[rustler::nif]
94+
fn context_rel_move_to(context: Context, x: f64, y: f64) {
95+
context.context.rel_move_to(x, y);
96+
}
97+
6498
#[rustler::nif]
6599
fn context_line_to(context: Context, x: f64, y: f64) {
66100
context.context.line_to(x, y);
67101
}
68102

103+
#[rustler::nif]
104+
fn context_rel_line_to(context: Context, x: f64, y: f64) {
105+
context.context.rel_line_to(x, y);
106+
}
107+
108+
#[rustler::nif]
109+
fn context_rectangle(context: Context, x: f64, y: f64, w: f64, h: f64) {
110+
context.context.rectangle(x, y, w, h);
111+
}
112+
69113
#[rustler::nif]
70114
fn context_close_path(context: Context) {
71115
context.context.close_path();
72116
}
117+
118+
#[rustler::nif]
119+
fn context_curve_to(context: Context, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
120+
context.context.curve_to(x1, y1, x2, y2, x3, y3);
121+
}
122+
123+
#[rustler::nif]
124+
fn context_rel_curve_to(context: Context, x1: f64, y1: f64, x2: f64, y2: f64, x3: f64, y3: f64) {
125+
context.context.rel_curve_to(x1, y1, x2, y2, x3, y3);
126+
}
127+
128+
#[rustler::nif]
129+
fn context_arc(context: Context, cx: f64, cy: f64, r: f64, a1: f64, a2: f64) {
130+
context.context.arc(cx, cy, r, a1, a2);
131+
}
132+
133+
#[rustler::nif]
134+
fn context_arc_negative(context: Context, cx: f64, cy: f64, r: f64, a1: f64, a2: f64) {
135+
context.context.arc_negative(cx, cy, r, a1, a2);
136+
}
137+
138+
#[rustler::nif]
139+
fn context_new_path(context: Context) {
140+
context.context.new_path();
141+
}
142+
143+
#[rustler::nif]
144+
fn context_new_sub_path(context: Context) {
145+
context.context.new_sub_path();
146+
}

native/xairo/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,25 @@ rustler::init!(
150150
context::context_new_from_image_surface,
151151
context::context_status,
152152
context::context_set_source_rgb,
153+
context::context_set_source_rgba,
153154
context::context_paint,
155+
context::context_paint_with_alpha,
154156
context::context_fill,
157+
context::context_fill_preserve,
155158
context::context_stroke,
159+
context::context_stroke_preserve,
156160
context::context_move_to,
161+
context::context_rel_move_to,
157162
context::context_line_to,
163+
context::context_rel_line_to,
164+
context::context_rectangle,
158165
context::context_close_path,
166+
context::context_curve_to,
167+
context::context_rel_curve_to,
168+
context::context_arc,
169+
context::context_arc_negative,
170+
context::context_new_path,
171+
context::context_new_sub_path,
159172
// enums
160173
enums::stride_for_width,
161174
],

priv/native/libxairo.so

-20.2 MB
Binary file not shown.
File renamed without changes.

test/images/basic_drawing3.png

2.15 KB
Loading

test/images/basic_drawing4.png

995 Bytes
Loading

test/xairo/context_test.exs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ defmodule Xairo.ContextTest do
4242
|> Context.close_path()
4343
|> Context.fill()
4444

45-
ImageSurface.write_to_png(surface, "basic_lines.png")
45+
ImageSurface.write_to_png(surface, "basic_drawing1.png")
4646

47-
assert File.exists?("basic_lines.png")
47+
assert File.exists?("basic_drawing1.png")
4848

49-
assert hash("basic_lines.png") == hash("test/images/basic_lines.png")
49+
assert hash("basic_drawing1.png") == hash("test/images/basic_drawing1.png")
5050

51-
:ok = File.rm("basic_lines.png")
51+
:ok = File.rm("basic_drawing1.png")
5252
end
5353

5454
test "set_source_rgba, fill_preserve, stroke_preserve, paint_with_alpha, rectangle", %{
@@ -74,5 +74,49 @@ defmodule Xairo.ContextTest do
7474

7575
:ok = File.rm("basic_drawing2.png")
7676
end
77+
78+
test "arc, arc_negative, curve, new_sub_path", %{context: ctx, surface: sfc} do
79+
ctx
80+
|> Context.set_source_rgb(1, 1, 1)
81+
|> Context.paint()
82+
|> Context.set_source_rgb(0, 0, 1)
83+
|> Context.move_to(10, 10)
84+
|> Context.curve_to(20, 20, 50, 30, 30, 80)
85+
|> Context.arc(60, 40, 10, 0, 3.1)
86+
|> Context.new_sub_path()
87+
|> Context.arc_negative(70, 80, 20, 0, 3.1)
88+
|> Context.stroke()
89+
90+
ImageSurface.write_to_png(sfc, "basic_drawing3.png")
91+
92+
assert File.exists?("basic_drawing3.png")
93+
94+
assert hash("basic_drawing3.png") == hash("test/images/basic_drawing3.png")
95+
96+
:ok = File.rm("basic_drawing3.png")
97+
end
98+
99+
test "new_path, rel_line_to, rel_move_to, rel_curve_to", %{context: ctx, surface: sfc} do
100+
ctx
101+
|> Context.set_source_rgb(1, 1, 1)
102+
|> Context.paint()
103+
|> Context.set_source_rgb(0.5, 0.5, 1)
104+
|> Context.move_to(10, 10)
105+
|> Context.rel_line_to(30, 30)
106+
|> Context.new_path()
107+
|> Context.move_to(10, 15)
108+
|> Context.rel_line_to(20, 25)
109+
|> Context.rel_move_to(30, 25)
110+
|> Context.rel_curve_to(10, 15, 20, -20, 30, 20)
111+
|> Context.stroke()
112+
113+
ImageSurface.write_to_png(sfc, "basic_drawing4.png")
114+
115+
assert File.exists?("basic_drawing4.png")
116+
117+
assert hash("basic_drawing4.png") == hash("test/images/basic_drawing4.png")
118+
119+
:ok = File.rm("basic_drawing4.png")
120+
end
77121
end
78122
end

0 commit comments

Comments
 (0)