Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a C API #8

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
39 changes: 39 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,44 @@ function(add_example name)

endfunction()

function(add_c_example name)
if (APPLE)
add_executable(
${name}-c MACOSX_BUNDLE
${name}.c
${artist_fonts}
${${name}_images}
)

message(STATUS ${CMAKE_CURRENT_SOURCE_DIR})

target_include_directories(
${name}-c
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/../lib/include
)

set_target_properties(
${name}
PROPERTIES
MACOSX_BUNDLE_INFO_PLIST
${CMAKE_CURRENT_LIST_DIR}/../resources/macos/plist.in
)

target_link_options(${name} PRIVATE -framework AppKit)

set_source_files_properties(
${artist_fonts}
${${name}_images}
PROPERTIES
MACOSX_PACKAGE_LOCATION Resources
)
else()
message(FATAL_ERROR "Unsupported platform for artist C API!")
endif()

endfunction()

set(space_images
${CMAKE_CURRENT_LIST_DIR}/images/space.jpg
)
Expand All @@ -159,3 +197,4 @@ add_example(paths)
add_example(shadow)
add_example(chessboard)

add_c_example(paths)
79 changes: 79 additions & 0 deletions examples/paths.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <artist/c/canvas.h>

void stroke_fill(canvas* cnv, path* p, color fill_c, color stroke_c) {
artist_canvas_add_path(cnv, p);
artist_canvas_fill_color(cnv, fill_c);
artist_canvas_stroke_color(cnv, stroke_c);
artist_canvas_fill(cnv);
artist_canvas_line_width(cnv, 5);
artist_canvas_add_path(cnv, p);
artist_canvas_stroke(cnv);
}

void stroke(canvas* cnv, path* p, color stroke_c) {
artist_canvas_add_path(cnv, p);
artist_canvas_stroke_color(cnv, stroke_c);
artist_canvas_line_width(cnv, 5);
artist_canvas_stroke(cnv);
}

void dot(canvas* cnv, float x, float y) {
artist_canvas_add_circle(cnv, x, y, 10);
artist_canvas_fill_color(cnv, artist_color_opacity(COLOR_WHITE, 0.5));
artist_canvas_fill(cnv);
}

int main(int argc, char const *argv[])
{
image* buffer = artist_image_create(400, 300);
offscreen_image* surface = artist_offscreen_image_create(buffer);
canvas* cnv = artist_offscreen_image_context(surface);

// These paths are defined using SVG-style strings lifted off the
// W3C SVG documentation: https://www.w3.org/TR/SVG/paths.html

// TODO: auto save = cnv.new_state();
artist_canvas_scale(cnv, 0.5, 0.5);

artist_canvas_translate(cnv, -40, 0);
path* p1 = artist_path_create_from_svg("M 100 100 L 300 100 L 200 300 z");
stroke_fill(cnv, p1, artist_color_opacity(COLOR_GREEN, 0.5), COLOR_IVORY);

artist_canvas_translate(cnv, 220, 0);
path* p2 = artist_path_create_from_svg("M100,200 C100,100 250,100 250,200 S400,300 400,200");
stroke(cnv, p2, COLOR_LIGHT_SKY_BLUE);

artist_canvas_translate(cnv, -150, 250);
path* p3 = artist_path_create_from_svg("M200,300 Q400,50 600,300 T1000,300");
stroke(cnv, p3, COLOR_LIGHT_SKY_BLUE);

path* p4 = artist_path_create_from_svg("M200,300 L400,50 L600,300 L800,550 L1000,300");
stroke(cnv, p4, artist_color_opacity(COLOR_LIGHT_GRAY, 0.5));
dot(cnv, 200, 300);
dot(cnv, 600, 300);
dot(cnv, 1000, 300);
dot(cnv, 400, 50);
dot(cnv, 800, 550);
artist_canvas_translate(cnv, 150, -250);

artist_canvas_translate(cnv, 350, 0);
path* p5 = artist_path_create_from_svg("M300,200 h-150 a150,150 0 1,0 150,-150 z");
stroke_fill(cnv, p5, artist_color_opacity(COLOR_RED, 0.8), COLOR_IVORY);

path* p6 = artist_path_create_from_svg("M275,175 v-150 a150,150 0 0,0 -150,150 z");
stroke_fill(cnv, p6, artist_color_opacity(COLOR_BLUE, 0.8), COLOR_IVORY);

artist_canvas_translate(cnv, -350, 200);
path* p7 = artist_path_create_from_svg(
"M600,350 l 50,-25"
"a25,25 -30 0,1 50,-25 l 50,-25"
"a25,50 -30 0,1 50,-25 l 50,-25"
"a25,75 -30 0,1 50,-25 l 50,-25"
"a25,100 -30 0,1 50,-25 l 50,-25"
);
stroke(p7, COLOR_IVORY);

artist_image_save_png(buffer, create);

return 0;
}
10 changes: 10 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ set(ARTIST_SOURCES
src/artist/rect.cpp
src/artist/resources.cpp
src/artist/svg_path.cpp
src/artist/c/affine_transform.cpp
src/artist/c/canvas.cpp
src/artist/c/circle.cpp
src/artist/c/color.cpp
src/artist/c/font.cpp
src/artist/c/image.cpp
src/artist/c/path.cpp
src/artist/c/rect.cpp
src/artist/c/strings.cpp
src/artist/c/text_layout.cpp
)

set(ARTIST_HEADERS
Expand Down
7 changes: 5 additions & 2 deletions lib/include/artist/affine_transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

#include <cmath>
#include <artist/point.hpp>
#include <artist/c/affine_transform.h>

typedef affine_transform c_affine_transform;

namespace cycfi::artist
{
struct affine_transform
struct affine_transform : c_affine_transform
{
constexpr bool is_identity() const;
constexpr affine_transform translate(double tx, double ty) const;
Expand All @@ -36,7 +39,7 @@ namespace cycfi::artist
double ty = 0.0;
};

constexpr affine_transform affine_identity;
constexpr affine_transform affine_identity = { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 };

constexpr affine_transform make_translation(double tx, double ty);
constexpr affine_transform make_scale(double sx, double sy);
Expand Down
52 changes: 52 additions & 0 deletions lib/include/artist/c/affine_transform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*=============================================================================
Copyright (c) 2021 Chance Snow, Joel de Guzman

Distributed under the MIT License [ https://opensource.org/licenses/MIT ]
=============================================================================*/
#ifndef __ARTIST_AFFINE_TRANSFORM_H
#define __ARTIST_AFFINE_TRANSFORM_H

#include <stdbool.h>
#include <stddef.h>

#include "point.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
double a;
double b;
double c;
double d;
double tx;
double ty;
} affine_transform;

affine_transform artist_affine_transform_identity();
bool artist_affine_transform_is_identity(affine_transform t);
affine_transform artist_affine_transform_translate(affine_transform t, double tx, double ty);
affine_transform artist_affine_transform_scale(affine_transform t, double sx, double sy);
affine_transform artist_affine_transform_scale_square(affine_transform t, double sc);
inline affine_transform artist_affine_transform_rotate(affine_transform t, double rad);
inline affine_transform artist_affine_transform_skew(affine_transform t, double sx, double sy);
affine_transform artist_affine_transform_invert(affine_transform t);

point artist_affine_transform_apply_pt(affine_transform t, point p);
point artist_affine_transform_apply(affine_transform t, float x, float y);

// template <std::size_t N>
// void apply(point p[N]) const;
void artist_affine_transform_apply_points(affine_transform t, point* p, size_t n);

affine_transform artist_affine_transform_make_translation(double tx, double ty);
affine_transform artist_affine_transform_make_scale(double sx, double sy);
affine_transform artist_affine_transform_make_scale_square(double sc);
inline affine_transform artist_affine_transform_make_rotation(double rad);
inline affine_transform artist_affine_transform_make_skew(double sx, double sy);

#ifdef __cplusplus
}
#endif
#endif
25 changes: 25 additions & 0 deletions lib/include/artist/c/artist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*=============================================================================
Copyright (c) 2021 Chance Snow, Joel de Guzman

Distributed under the MIT License [ https://opensource.org/licenses/MIT ]
=============================================================================*/
#ifndef __ARTIST_H
#define __ARTIST_H

////////////////////////////////////////////////////////////////////////////
// Amalgamated C API
////////////////////////////////////////////////////////////////////////////

#include "canvas.h"
#include "color.h"
#include "circle.h"
#include "image.h"
#include "font.h"
#include "path.h"
#include "point.h"
#include "rect.h"
#include "strings.h"
#include "text_layout.h"
#include "affine_transform.h"

#endif
Loading