GskCanvas: implement drawPath, rotate, translate, save, restore

This is enough to make DrawerArrowDrawable functional.

drawPath() only draws line segments for now.
This commit is contained in:
Julian Winkler
2024-04-08 23:15:05 +02:00
parent 74bedcc93c
commit 3d9eb9611f
10 changed files with 168 additions and 12 deletions

View File

@@ -1,6 +1,9 @@
#include <gtk/gtk.h>
#include <graphene.h>
#include "include/c/sk_paint.h"
#include "include/c/sk_path.h"
#include "../defines.h"
#include "../util.h"
@@ -41,3 +44,53 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect(JNIEnv *
graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top);
gtk_snapshot_append_color(snapshot, &gdk_color, &bounds);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong path_ptr, jlong paint_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
sk_path_t *path = (sk_path_t *)_PTR(path_ptr);
GdkRGBA gdk_color;
sk_path_iterator_t *iterator = sk_path_create_iter(path, 0);
sk_path_verb_t verb;
sk_point_t line[4];
sk_paint_get_color4f(paint, (sk_color4f_t *)&gdk_color);
float width = sk_paint_get_stroke_width(paint);
while ((verb = sk_path_iter_next(iterator, line)) != DONE_SK_PATH_VERB) {
// TODO: use GskPath to support other verbs
if (verb == LINE_SK_PATH_VERB) {
gtk_snapshot_save(snapshot);
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(line[0].x, line[0].y));
float rotation = atan2(line[1].y - line[0].y, line[1].x - line[0].x);
gtk_snapshot_rotate(snapshot, rotation * 180 / M_PI);
float length = sqrt((line[1].x - line[0].x) * (line[1].x - line[0].x) + (line[1].y - line[0].y) * (line[1].y - line[0].y));
gtk_snapshot_append_color(snapshot, &gdk_color, &GRAPHENE_RECT_INIT(0, -width / 2, length, width));
gtk_snapshot_restore(snapshot);
}
}
sk_path_iter_destroy(iterator);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1translate(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat dx, jfloat dy)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(dx, dy));
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1rotate(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat angle)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_rotate(snapshot, angle);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1save(JNIEnv *env, jclass this_class, jlong snapshot_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_save(snapshot);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore(JNIEnv *env, jclass this_class, jlong snapshot_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_restore(snapshot);
}