2024-03-24 21:01:47 +01:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
#include <graphene.h>
|
2024-05-25 16:32:01 +02:00
|
|
|
#include <pango/pango.h>
|
2024-03-24 21:01:47 +01:00
|
|
|
|
|
|
|
|
#include "../defines.h"
|
2024-12-21 14:24:33 +01:00
|
|
|
#include "AndroidPaint.h"
|
2024-03-24 21:01:47 +01:00
|
|
|
|
|
|
|
|
#include "../generated_headers/android_graphics_GskCanvas.h"
|
|
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong texture_ptr, jint x, jint y, jint width, jint height, jlong paint_ptr)
|
2024-03-24 21:01:47 +01:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
|
2024-05-25 23:57:31 +02:00
|
|
|
GdkTexture *texture = GDK_TEXTURE(_PTR(texture_ptr));
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
|
|
|
|
if (paint->use_color_filter) {
|
|
|
|
|
gtk_snapshot_push_color_matrix(snapshot, &paint->color_matrix, &paint->color_offset);
|
2024-03-24 21:12:13 +01:00
|
|
|
}
|
2024-03-24 21:01:47 +01:00
|
|
|
gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(x, y, width, height));
|
2024-12-21 14:24:33 +01:00
|
|
|
if (paint->use_color_filter)
|
2024-03-24 21:12:13 +01:00
|
|
|
gtk_snapshot_pop(snapshot);
|
2024-03-24 21:01:47 +01:00
|
|
|
}
|
2024-04-07 21:49:40 +02:00
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat left, jfloat top, jfloat right, jfloat bottom, jlong paint_ptr)
|
2024-04-07 21:49:40 +02:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
2024-04-07 21:49:40 +02:00
|
|
|
graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top);
|
2024-12-21 14:24:33 +01:00
|
|
|
gtk_snapshot_append_color(snapshot, &paint->color, &bounds);
|
2024-04-07 21:49:40 +02:00
|
|
|
}
|
2024-04-08 23:15:05 +02:00
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong path_ptr, jlong paint_ptr)
|
2024-04-08 23:15:05 +02:00
|
|
|
{
|
2024-12-19 20:20:40 +01:00
|
|
|
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
|
|
|
|
GskPath *path = _PTR(path_ptr);
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
|
|
|
|
if (paint->is_stroke) {
|
|
|
|
|
gtk_snapshot_append_stroke(snapshot, path, paint->gsk_stroke, &paint->color);
|
2024-12-19 20:20:40 +01:00
|
|
|
}
|
2024-12-21 14:24:33 +01:00
|
|
|
if (paint->is_fill) {
|
|
|
|
|
gtk_snapshot_append_fill(snapshot, path, GSK_FILL_RULE_WINDING, &paint->color);
|
2024-04-08 23:15:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-05-25 16:32:01 +02:00
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLine(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat x0, jfloat y0, jfloat x1, jfloat y1, jlong paint_ptr)
|
2024-05-25 16:32:01 +02:00
|
|
|
{
|
2024-11-27 14:59:37 +01:00
|
|
|
if (isnan(x0) || isnan(y0) || isnan(x1) || isnan(y1)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-25 16:32:01 +02:00
|
|
|
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
2024-05-25 16:32:01 +02:00
|
|
|
gtk_snapshot_save(snapshot);
|
|
|
|
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(x0, y0));
|
|
|
|
|
float rotation = atan2(y1 - y0, x1 - x0);
|
|
|
|
|
gtk_snapshot_rotate(snapshot, rotation * 180 / M_PI);
|
|
|
|
|
float length = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
|
2024-12-21 14:24:33 +01:00
|
|
|
float stroke_width = gsk_stroke_get_line_width(paint->gsk_stroke);
|
|
|
|
|
gtk_snapshot_append_color(snapshot, &paint->color, &GRAPHENE_RECT_INIT(0, -stroke_width / 2, length, stroke_width));
|
2024-05-25 16:32:01 +02:00
|
|
|
gtk_snapshot_restore(snapshot);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-15 21:36:03 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLines(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloatArray points_arr, jint offset, jint count, jlong paint_ptr)
|
|
|
|
|
{
|
|
|
|
|
jfloat *points = (*env)->GetFloatArrayElements(env, points_arr, NULL);
|
|
|
|
|
for(int i = offset; i < count; i++)
|
|
|
|
|
Java_android_graphics_GskCanvas_native_1drawLine(env, this_class, snapshot_ptr, points[i + 0], points[i + 1], points[i + 2], points[i + 3], paint_ptr);
|
|
|
|
|
(*env)->ReleaseFloatArrayElements(env, points_arr, points, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-25 16:32:01 +02:00
|
|
|
extern GtkWidget *window;
|
|
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jstring text, jfloat x, jfloat y, jlong paint_ptr)
|
2024-05-25 16:32:01 +02:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
2024-05-25 16:32:01 +02:00
|
|
|
PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(window));
|
2024-12-21 14:24:33 +01:00
|
|
|
pango_layout_set_font_description(layout, paint->font);
|
2024-05-25 16:32:01 +02:00
|
|
|
const char *str = (*env)->GetStringUTFChars(env, text, NULL);
|
|
|
|
|
pango_layout_set_text(layout, str, -1);
|
|
|
|
|
(*env)->ReleaseStringUTFChars(env, text, str);
|
2025-02-18 18:58:58 +01:00
|
|
|
PangoRectangle rect;
|
|
|
|
|
pango_layout_get_pixel_extents(layout, NULL, &rect);
|
|
|
|
|
y -= rect.height;
|
|
|
|
|
if (paint->alignment == PANGO_ALIGN_CENTER)
|
|
|
|
|
x -= rect.width / 2.f;
|
|
|
|
|
else if (paint->alignment == PANGO_ALIGN_RIGHT)
|
|
|
|
|
x -= rect.width;
|
2024-05-25 16:32:01 +02:00
|
|
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(x, y));
|
2024-12-21 14:24:33 +01:00
|
|
|
gtk_snapshot_append_layout(snapshot, layout, &paint->color);
|
2024-05-25 16:32:01 +02:00
|
|
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(-x, -y));
|
|
|
|
|
g_object_unref(layout);
|
|
|
|
|
}
|
2024-11-30 17:58:31 +01:00
|
|
|
|
2024-12-21 14:24:33 +01:00
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRoundRect(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat left, jfloat top, jfloat right, jfloat bottom, jfloat rx, jfloat ry, jlong paint_ptr)
|
2024-11-30 17:58:31 +01:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
|
2024-12-21 14:24:33 +01:00
|
|
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
2024-11-30 17:58:31 +01:00
|
|
|
GdkRGBA gdk_color[4];
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2024-12-21 14:24:33 +01:00
|
|
|
gdk_color[i].red = paint->color.red;
|
|
|
|
|
gdk_color[i].green = paint->color.green;
|
|
|
|
|
gdk_color[i].blue = paint->color.blue;
|
|
|
|
|
gdk_color[i].alpha = paint->color.alpha;
|
2024-11-30 17:58:31 +01:00
|
|
|
}
|
|
|
|
|
GskRoundedRect round_rect = {
|
|
|
|
|
.bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top),
|
|
|
|
|
.corner = {{rx, ry}, {rx, ry}, {rx, ry}, {rx, ry}},
|
|
|
|
|
};
|
2024-12-21 14:24:33 +01:00
|
|
|
float width = gsk_stroke_get_line_width(paint->gsk_stroke);
|
2024-11-30 17:58:31 +01:00
|
|
|
const float widths[4] = {width, width, width, width};
|
2024-12-21 14:24:33 +01:00
|
|
|
if (paint->is_fill) {
|
2024-12-13 22:35:40 +01:00
|
|
|
gtk_snapshot_push_rounded_clip(snapshot, &round_rect);
|
|
|
|
|
gtk_snapshot_append_color(snapshot, gdk_color, &round_rect.bounds);
|
|
|
|
|
gtk_snapshot_pop(snapshot);
|
|
|
|
|
}
|
2024-12-21 14:24:33 +01:00
|
|
|
if (paint->is_stroke) {
|
2024-12-13 22:35:40 +01:00
|
|
|
gtk_snapshot_append_border(snapshot, &round_rect, widths, gdk_color);
|
|
|
|
|
}
|
2024-11-30 17:58:31 +01:00
|
|
|
}
|
2024-12-13 23:10:28 +01:00
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1scale(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat x, jfloat y)
|
|
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
|
|
|
|
gtk_snapshot_scale(snapshot, x, y);
|
|
|
|
|
}
|
2025-02-10 18:06:49 +01:00
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1concat(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong matrix_ptr)
|
|
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
|
|
|
|
graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr);
|
|
|
|
|
gtk_snapshot_transform_matrix(snapshot, matrix);
|
|
|
|
|
}
|