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
|
|
|
|
2024-12-10 23:44:57 +01:00
|
|
|
#include "include/c/sk_font.h"
|
2024-04-08 23:15:05 +02:00
|
|
|
#include "include/c/sk_paint.h"
|
|
|
|
|
#include "include/c/sk_path.h"
|
|
|
|
|
|
2024-03-24 21:01:47 +01:00
|
|
|
#include "../defines.h"
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
|
|
|
|
|
#include "../generated_headers/android_graphics_GskCanvas.h"
|
|
|
|
|
|
2024-12-13 22:35:40 +01:00
|
|
|
#define STYLE_FILL 0
|
|
|
|
|
#define STYLE_STROKE 1
|
|
|
|
|
#define STYLE_FILL_AND_STROKE 2
|
|
|
|
|
|
2024-05-25 23:57:31 +02: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, jint color)
|
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-03-24 21:12:13 +01:00
|
|
|
if (color) { // use only alpha from pixbuf, color is fixed
|
|
|
|
|
graphene_matrix_t color_matrix;
|
|
|
|
|
graphene_vec4_t color_offset;
|
|
|
|
|
graphene_matrix_init_from_float(&color_matrix, (float[]){
|
|
|
|
|
0, 0, 0, 0,
|
|
|
|
|
0, 0, 0, 0,
|
|
|
|
|
0, 0, 0, 0,
|
|
|
|
|
0, 0, 0, 1,
|
|
|
|
|
});
|
|
|
|
|
graphene_vec4_init(&color_offset, ((color >> 16) & 0xFF) / 255.f, ((color >> 8) & 0xFF) / 255.f, ((color >> 0) & 0xFF) / 255.f, 0);
|
|
|
|
|
gtk_snapshot_push_color_matrix(snapshot, &color_matrix, &color_offset);
|
|
|
|
|
}
|
2024-03-24 21:01:47 +01:00
|
|
|
gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(x, y, width, height));
|
2024-03-24 21:12:13 +01:00
|
|
|
if (color)
|
|
|
|
|
gtk_snapshot_pop(snapshot);
|
2024-03-24 21:01:47 +01:00
|
|
|
}
|
2024-04-07 21:49:40 +02: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, jint color)
|
|
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
|
|
|
|
|
GdkRGBA gdk_color = {
|
|
|
|
|
(float)((color >> 16) & 0xff) / 0xff,
|
|
|
|
|
(float)((color >> 8) & 0xff) / 0xff,
|
|
|
|
|
(float)((color >> 0) & 0xff) / 0xff,
|
|
|
|
|
(float)((color >> 24) & 0xff) / 0xff,
|
|
|
|
|
};
|
|
|
|
|
graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top);
|
|
|
|
|
gtk_snapshot_append_color(snapshot, &gdk_color, &bounds);
|
|
|
|
|
}
|
2024-04-08 23:15:05 +02:00
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong path_ptr, jlong paint_ptr)
|
|
|
|
|
{
|
|
|
|
|
sk_path_t *path = (sk_path_t *)_PTR(path_ptr);
|
|
|
|
|
sk_path_iterator_t *iterator = sk_path_create_iter(path, 0);
|
|
|
|
|
sk_path_verb_t verb;
|
|
|
|
|
sk_point_t line[4];
|
|
|
|
|
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) {
|
2024-05-25 16:32:01 +02:00
|
|
|
Java_android_graphics_GskCanvas_native_1drawLine(env, this_class, snapshot_ptr, line[0].x, line[0].y, line[1].x, line[1].y, paint_ptr);
|
2024-04-08 23:15:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-05-25 16:32:01 +02: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-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));
|
|
|
|
|
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
|
|
|
|
|
GdkRGBA gdk_color;
|
|
|
|
|
sk_paint_get_color4f(paint, (sk_color4f_t *)&gdk_color);
|
|
|
|
|
float width = sk_paint_get_stroke_width(paint);
|
|
|
|
|
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));
|
|
|
|
|
gtk_snapshot_append_color(snapshot, &gdk_color, &GRAPHENE_RECT_INIT(0, -width / 2, length, width));
|
|
|
|
|
gtk_snapshot_restore(snapshot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extern GtkWidget *window;
|
|
|
|
|
|
2024-12-10 23:44:57 +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, jlong font_ptr)
|
2024-05-25 16:32:01 +02:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
|
|
|
|
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
|
2024-12-10 23:44:57 +01:00
|
|
|
sk_font_t *font = (sk_font_t *)_PTR(font_ptr);
|
2024-05-25 16:32:01 +02:00
|
|
|
GdkRGBA gdk_color;
|
|
|
|
|
sk_paint_get_color4f(paint, (sk_color4f_t *)&gdk_color);
|
|
|
|
|
PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(window));
|
2024-12-10 23:44:57 +01:00
|
|
|
PangoFontDescription *description = pango_font_description_new();
|
|
|
|
|
pango_font_description_set_size(description, sk_font_get_size(font) * .8f * PANGO_SCALE);
|
|
|
|
|
pango_layout_set_font_description(layout, description);
|
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);
|
|
|
|
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(x, y));
|
|
|
|
|
gtk_snapshot_append_layout(snapshot, layout, &gdk_color);
|
|
|
|
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(-x, -y));
|
|
|
|
|
g_object_unref(layout);
|
2024-12-10 23:44:57 +01:00
|
|
|
g_object_unref(description);
|
2024-05-25 16:32:01 +02:00
|
|
|
}
|
2024-11-30 17:58:31 +01:00
|
|
|
|
2024-12-13 22:35:40 +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, jint color, jfloat width, int style)
|
2024-11-30 17:58:31 +01:00
|
|
|
{
|
|
|
|
|
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
|
|
|
|
|
GdkRGBA gdk_color[4];
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
|
gdk_color[i].red = (float)((color >> 16) & 0xff) / 0xff;
|
|
|
|
|
gdk_color[i].green = (float)((color >> 8) & 0xff) / 0xff;
|
|
|
|
|
gdk_color[i].blue = (float)((color >> 0) & 0xff) / 0xff;
|
|
|
|
|
gdk_color[i].alpha = (float)((color >> 24) & 0xff) / 0xff;
|
|
|
|
|
}
|
|
|
|
|
GskRoundedRect round_rect = {
|
|
|
|
|
.bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top),
|
|
|
|
|
.corner = {{rx, ry}, {rx, ry}, {rx, ry}, {rx, ry}},
|
|
|
|
|
};
|
|
|
|
|
const float widths[4] = {width, width, width, width};
|
2024-12-13 22:35:40 +01:00
|
|
|
if (style == STYLE_FILL || style == STYLE_FILL_AND_STROKE) {
|
|
|
|
|
gtk_snapshot_push_rounded_clip(snapshot, &round_rect);
|
|
|
|
|
gtk_snapshot_append_color(snapshot, gdk_color, &round_rect.bounds);
|
|
|
|
|
gtk_snapshot_pop(snapshot);
|
|
|
|
|
}
|
|
|
|
|
if (style == STYLE_STROKE || style == STYLE_FILL_AND_STROKE) {
|
|
|
|
|
gtk_snapshot_append_border(snapshot, &round_rect, widths, gdk_color);
|
|
|
|
|
}
|
2024-11-30 17:58:31 +01:00
|
|
|
}
|