From 7695aadf9125c39db0499fca13103ab3f7fb0aa2 Mon Sep 17 00:00:00 2001 From: Julian Winkler Date: Sat, 21 Dec 2024 14:24:33 +0100 Subject: [PATCH] implement Paint as combination of GDK, GSK and Pango attributes --- meson.build | 1 + .../android_graphics_GskCanvas.h | 24 ++-- .../android_graphics_Paint.h | 120 +++++++++------- src/api-impl-jni/graphics/AndroidPaint.h | 14 ++ .../graphics/android_graphics_GskCanvas.c | 81 +++++------ .../graphics/android_graphics_Paint.c | 133 ++++++++++++++++++ src/api-impl/android/graphics/Canvas.java | 4 + src/api-impl/android/graphics/GskCanvas.java | 30 ++-- src/api-impl/android/graphics/Paint.java | 113 +++++++++------ 9 files changed, 350 insertions(+), 170 deletions(-) create mode 100644 src/api-impl-jni/graphics/AndroidPaint.h create mode 100644 src/api-impl-jni/graphics/android_graphics_Paint.c diff --git a/meson.build b/meson.build index 88c5e87a..f1609891 100644 --- a/meson.build +++ b/meson.build @@ -109,6 +109,7 @@ libtranslationlayer_so = shared_library('translation_layer_main', [ 'src/api-impl-jni/graphics/android_graphics_BitmapFactory.c', 'src/api-impl-jni/graphics/android_graphics_GskCanvas.c', 'src/api-impl-jni/graphics/android_graphics_Matrix.c', + 'src/api-impl-jni/graphics/android_graphics_Paint.c', 'src/api-impl-jni/graphics/android_graphics_Path.c', 'src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c', 'src/api-impl-jni/graphics/android_graphics_drawable_DrawableContainer.c', diff --git a/src/api-impl-jni/generated_headers/android_graphics_GskCanvas.h b/src/api-impl-jni/generated_headers/android_graphics_GskCanvas.h index d09e1960..c3c0635a 100644 --- a/src/api-impl-jni/generated_headers/android_graphics_GskCanvas.h +++ b/src/api-impl-jni/generated_headers/android_graphics_GskCanvas.h @@ -12,26 +12,26 @@ extern "C" { /* * Class: android_graphics_GskCanvas * Method: native_drawBitmap - * Signature: (JJIIIII)V + * Signature: (JJIIIIJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap - (JNIEnv *, jobject, jlong, jlong, jint, jint, jint, jint, jint); + (JNIEnv *, jobject, jlong, jlong, jint, jint, jint, jint, jlong); /* * Class: android_graphics_GskCanvas * Method: native_drawRect - * Signature: (JFFFFI)V + * Signature: (JFFFFJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect - (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jint); + (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jlong); /* * Class: android_graphics_GskCanvas * Method: native_drawPath - * Signature: (JJII)V + * Signature: (JJJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath - (JNIEnv *, jobject, jlong, jlong, jint, jint); + (JNIEnv *, jobject, jlong, jlong, jlong); /* * Class: android_graphics_GskCanvas @@ -68,26 +68,26 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore /* * Class: android_graphics_GskCanvas * Method: native_drawLine - * Signature: (JFFFFIF)V + * Signature: (JFFFFJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLine - (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jint, jfloat); + (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jlong); /* * Class: android_graphics_GskCanvas * Method: native_drawText - * Signature: (JLjava/lang/String;FFIF)V + * Signature: (JLjava/lang/String;FFJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText - (JNIEnv *, jobject, jlong, jstring, jfloat, jfloat, jint, jfloat); + (JNIEnv *, jobject, jlong, jstring, jfloat, jfloat, jlong); /* * Class: android_graphics_GskCanvas * Method: native_drawRoundRect - * Signature: (JFFFFFFIFI)V + * Signature: (JFFFFFFJ)V */ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRoundRect - (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jfloat, jfloat, jint, jfloat, jint); + (JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jfloat, jfloat, jlong); /* * Class: android_graphics_GskCanvas diff --git a/src/api-impl-jni/generated_headers/android_graphics_Paint.h b/src/api-impl-jni/generated_headers/android_graphics_Paint.h index ea9f7ff7..16fd234c 100644 --- a/src/api-impl-jni/generated_headers/android_graphics_Paint.h +++ b/src/api-impl-jni/generated_headers/android_graphics_Paint.h @@ -35,19 +35,27 @@ extern "C" { #define android_graphics_Paint_VERTICAL_TEXT_FLAG 4096L /* * Class: android_graphics_Paint - * Method: native_constructor + * Method: native_create * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1constructor - (JNIEnv *, jobject); +JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1create + (JNIEnv *, jclass); /* * Class: android_graphics_Paint - * Method: native_set_antialias - * Signature: (JZ)V + * Method: native_clone + * Signature: (J)J */ -JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1antialias - (JNIEnv *, jobject, jlong, jboolean); +JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1clone + (JNIEnv *, jclass, jlong); + +/* + * Class: android_graphics_Paint + * Method: native_recycle + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1recycle + (JNIEnv *, jclass, jlong); /* * Class: android_graphics_Paint @@ -55,7 +63,7 @@ JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1antialias * Signature: (JI)V */ JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color - (JNIEnv *, jobject, jlong, jint); + (JNIEnv *, jclass, jlong, jint); /* * Class: android_graphics_Paint @@ -63,47 +71,23 @@ JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color * Signature: (J)I */ JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1color - (JNIEnv *, jobject, jlong); - -/* - * Class: android_graphics_Paint - * Method: native_create_font - * Signature: ()J - */ -JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1create_1font - (JNIEnv *, jclass); - -/* - * Class: android_graphics_Paint - * Method: native_ascent - * Signature: (J)F - */ -JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1ascent (JNIEnv *, jclass, jlong); /* * Class: android_graphics_Paint - * Method: native_set_typeface - * Signature: (JJ)V + * Method: native_set_style + * Signature: (JI)V */ -JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1typeface - (JNIEnv *, jclass, jlong, jlong); +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1style + (JNIEnv *, jclass, jlong, jint); /* * Class: android_graphics_Paint - * Method: native_set_text_size - * Signature: (JF)V + * Method: native_get_style + * Signature: (J)I */ -JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1size - (JNIEnv *, jclass, jlong, jfloat); - -/* - * Class: android_graphics_Paint - * Method: native_measure_text - * Signature: (JLjava/lang/CharSequence;IIJ)F - */ -JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1measure_1text - (JNIEnv *, jclass, jlong, jobject, jint, jint, jlong); +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1style + (JNIEnv *, jclass, jlong); /* * Class: android_graphics_Paint @@ -123,28 +107,60 @@ JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1stroke_1width /* * Class: android_graphics_Paint - * Method: native_set_style + * Method: native_set_stroke_cap * Signature: (JI)V */ -JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1style +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1cap (JNIEnv *, jclass, jlong, jint); /* * Class: android_graphics_Paint - * Method: native_set_blendmode - * Signature: (JI)V - */ -JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1blendmode - (JNIEnv *, jclass, jlong, jint); - -/* - * Class: android_graphics_Paint - * Method: native_get_style + * Method: native_get_stroke_cap * Signature: (J)I */ -JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1style +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1stroke_1cap (JNIEnv *, jclass, jlong); +/* + * Class: android_graphics_Paint + * Method: native_set_stroke_join + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1join + (JNIEnv *, jclass, jlong, jint); + +/* + * Class: android_graphics_Paint + * Method: native_get_stroke_join + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1stroke_1join + (JNIEnv *, jclass, jlong); + +/* + * Class: android_graphics_Paint + * Method: native_set_text_size + * Signature: (JF)V + */ +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1size + (JNIEnv *, jclass, jlong, jfloat); + +/* + * Class: android_graphics_Paint + * Method: native_get_text_size + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1text_1size + (JNIEnv *, jclass, jlong); + +/* + * Class: android_graphics_Paint + * Method: native_set_color_filter + * Signature: (JII)V + */ +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter + (JNIEnv *, jclass, jlong, jint, jint); + #ifdef __cplusplus } #endif diff --git a/src/api-impl-jni/graphics/AndroidPaint.h b/src/api-impl-jni/graphics/AndroidPaint.h new file mode 100644 index 00000000..429aa867 --- /dev/null +++ b/src/api-impl-jni/graphics/AndroidPaint.h @@ -0,0 +1,14 @@ +#include +#include +#include + +struct AndroidPaint { + GdkRGBA color; + GskStroke *gsk_stroke; + PangoFontDescription *font; + graphene_matrix_t color_matrix; + graphene_vec4_t color_offset; + bool is_fill:1; + bool is_stroke:1; + bool use_color_filter:1; +}; diff --git a/src/api-impl-jni/graphics/android_graphics_GskCanvas.c b/src/api-impl-jni/graphics/android_graphics_GskCanvas.c index 25ebddff..87b011cd 100644 --- a/src/api-impl-jni/graphics/android_graphics_GskCanvas.c +++ b/src/api-impl-jni/graphics/android_graphics_GskCanvas.c @@ -3,58 +3,41 @@ #include #include "../defines.h" +#include "AndroidPaint.h" #include "../generated_headers/android_graphics_GskCanvas.h" -#define STYLE_FILL 0 -#define STYLE_STROKE 1 -#define STYLE_FILL_AND_STROKE 2 - -#define GDK_RGBA_INIT(color) ((GdkRGBA){ \ - .red = ((color >> 16) & 0xFF) / 255.f, \ - .green = ((color >> 8) & 0xFF) / 255.f, \ - .blue = ((color >> 0) & 0xFF) / 255.f, \ - .alpha = ((color >> 24) & 0xFF) / 255.f}) - -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) +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) { GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr); GdkTexture *texture = GDK_TEXTURE(_PTR(texture_ptr)); - 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); + struct AndroidPaint *paint = _PTR(paint_ptr); + if (paint->use_color_filter) { + gtk_snapshot_push_color_matrix(snapshot, &paint->color_matrix, &paint->color_offset); } gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(x, y, width, height)); - if (color) + if (paint->use_color_filter) gtk_snapshot_pop(snapshot); } -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) +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) { GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr); - GdkRGBA gdk_color = GDK_RGBA_INIT(color); + struct AndroidPaint *paint = _PTR(paint_ptr); graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top); - gtk_snapshot_append_color(snapshot, &gdk_color, &bounds); + gtk_snapshot_append_color(snapshot, &paint->color, &bounds); } -JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong path_ptr, jint color, jint style) +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)); GskPath *path = _PTR(path_ptr); - GdkRGBA gdk_color = GDK_RGBA_INIT(color); - if (style == STYLE_STROKE || style == STYLE_FILL_AND_STROKE) { - gtk_snapshot_append_stroke(snapshot, path, gsk_stroke_new(2), &gdk_color); + struct AndroidPaint *paint = _PTR(paint_ptr); + if (paint->is_stroke) { + gtk_snapshot_append_stroke(snapshot, path, paint->gsk_stroke, &paint->color); } - if (style == STYLE_FILL || style == STYLE_FILL_AND_STROKE) { - gtk_snapshot_append_fill(snapshot, path, GSK_FILL_RULE_WINDING, &gdk_color); + if (paint->is_fill) { + gtk_snapshot_append_fill(snapshot, path, GSK_FILL_RULE_WINDING, &paint->color); } } @@ -82,63 +65,63 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore(JNIEnv *e gtk_snapshot_restore(snapshot); } -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, jint color, jfloat stroke_width) +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) { if (isnan(x0) || isnan(y0) || isnan(x1) || isnan(y1)) { return; } GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr)); - GdkRGBA gdk_color = GDK_RGBA_INIT(color); + struct AndroidPaint *paint = _PTR(paint_ptr); 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, -stroke_width / 2, length, stroke_width)); + 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)); gtk_snapshot_restore(snapshot); } extern GtkWidget *window; -JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jstring text, jfloat x, jfloat y, jint color, jfloat text_size) +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) { GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr)); - GdkRGBA gdk_color = GDK_RGBA_INIT(color); + struct AndroidPaint *paint = _PTR(paint_ptr); PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(window)); - PangoFontDescription *description = pango_font_description_new(); - pango_font_description_set_size(description, text_size * .8f * PANGO_SCALE); - pango_layout_set_font_description(layout, description); + pango_layout_set_font_description(layout, paint->font); 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_append_layout(snapshot, layout, &paint->color); gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(-x, -y)); g_object_unref(layout); - g_object_unref(description); } -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) +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) { GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr); + struct AndroidPaint *paint = _PTR(paint_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; + 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; } GskRoundedRect round_rect = { .bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top), .corner = {{rx, ry}, {rx, ry}, {rx, ry}, {rx, ry}}, }; + float width = gsk_stroke_get_line_width(paint->gsk_stroke); const float widths[4] = {width, width, width, width}; - if (style == STYLE_FILL || style == STYLE_FILL_AND_STROKE) { + if (paint->is_fill) { 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) { + if (paint->is_stroke) { gtk_snapshot_append_border(snapshot, &round_rect, widths, gdk_color); } } diff --git a/src/api-impl-jni/graphics/android_graphics_Paint.c b/src/api-impl-jni/graphics/android_graphics_Paint.c new file mode 100644 index 00000000..84cb59b2 --- /dev/null +++ b/src/api-impl-jni/graphics/android_graphics_Paint.c @@ -0,0 +1,133 @@ +#include +#include + +#include "AndroidPaint.h" +#include "../defines.h" +#include "pango/pango-font.h" + +#include "../generated_headers/android_graphics_Paint.h" + +JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1create(JNIEnv *env, jclass clazz) +{ + struct AndroidPaint *paint = g_new0(struct AndroidPaint, 1); + paint->color.alpha = 1.f; + paint->gsk_stroke = gsk_stroke_new(1); + paint->font = pango_font_description_new(); + paint->is_fill = true; + return _INTPTR(paint); +} + +JNIEXPORT jlong JNICALL Java_android_graphics_Paint_native_1clone(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + struct AndroidPaint *clone = g_memdup2(paint, sizeof(struct AndroidPaint)); + clone->gsk_stroke = gsk_stroke_copy(paint->gsk_stroke); + clone->font = pango_font_description_copy(paint->font); + return _INTPTR(clone); +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1recycle(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + gsk_stroke_free(paint->gsk_stroke); + pango_font_description_free(paint->font); + g_free(paint); +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color(JNIEnv *env, jclass clazz, jlong paint_ptr, jint color) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + paint->color.red = ((color >> 16) & 0xFF) / 255.f; + paint->color.green = ((color >> 8) & 0xFF) / 255.f; + paint->color.blue = ((color >> 0) & 0xFF) / 255.f; + paint->color.alpha = ((color >> 24) & 0xFF) / 255.f; +} + +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1color(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + return ((int)(paint->color.red * 0xFF) << 16) + ((int)(paint->color.green * 0xFF) << 8) + ((int)(paint->color.blue * 0xFF) << 0) + ((int)(paint->color.alpha * 0xFF) << 24); +} + +#define STYLE_FILL 0 +#define STYLE_STROKE 1 +#define STYLE_FILL_AND_STROKE 2 + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1style(JNIEnv *env, jclass clazz, jlong paint_ptr, jint style) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + paint->is_fill = style == STYLE_FILL || style == STYLE_FILL_AND_STROKE; + paint->is_stroke = style == STYLE_STROKE || style == STYLE_FILL_AND_STROKE; +} + +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1style(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + if (paint->is_fill && paint->is_stroke) + return STYLE_FILL_AND_STROKE; + else if (paint->is_fill) + return STYLE_FILL; + else + return STYLE_STROKE; +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1width(JNIEnv *env, jclass clazz, jlong paint_ptr, jfloat width) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + gsk_stroke_set_line_width(paint->gsk_stroke, width); +} + +JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1stroke_1width(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + return gsk_stroke_get_line_width(paint->gsk_stroke); +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1cap(JNIEnv *env, jclass clazz, jlong paint_ptr, jint cap) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + gsk_stroke_set_line_cap(paint->gsk_stroke, cap); +} + +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1stroke_1cap(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + return gsk_stroke_get_line_cap(paint->gsk_stroke); +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1join(JNIEnv *env, jclass clazz, jlong paint_ptr, jint join) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + gsk_stroke_set_line_join(paint->gsk_stroke, join); +} + +JNIEXPORT jint JNICALL Java_android_graphics_Paint_native_1get_1stroke_1join(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + return gsk_stroke_get_line_join(paint->gsk_stroke); +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1size(JNIEnv *env, jclass clazz, jlong paint_ptr, jfloat size) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + pango_font_description_set_size(paint->font, size * .8f * PANGO_SCALE); +} + +JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1text_1size(JNIEnv *env, jclass clazz, jlong paint_ptr) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + return pango_font_description_get_size(paint->font) / .8f / PANGO_SCALE; +} + +JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter(JNIEnv *env, jclass clazz, jlong paint_ptr, jint mode, jint color) +{ + struct AndroidPaint *paint = _PTR(paint_ptr); + graphene_matrix_init_from_float(&paint->color_matrix, (float[]){ + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0, 1, + }); + graphene_vec4_init(&paint->color_offset, ((color >> 16) & 0xFF) / 255.f, ((color >> 8) & 0xFF) / 255.f, ((color >> 0) & 0xFF) / 255.f, 0); + paint->use_color_filter = mode != -1; +} diff --git a/src/api-impl/android/graphics/Canvas.java b/src/api-impl/android/graphics/Canvas.java index 3b82dbab..e2f2d5df 100644 --- a/src/api-impl/android/graphics/Canvas.java +++ b/src/api-impl/android/graphics/Canvas.java @@ -57,6 +57,10 @@ public class Canvas { * @param paint The paint used to draw the rect */ public void drawRect(float left, float top, float right, float bottom, Paint paint) { + if (paint != null && paint.getXfermode() instanceof PorterDuffXfermode && ((PorterDuffXfermode)paint.getXfermode()).mode == PorterDuff.Mode.CLEAR) { + bitmap.eraseColor(0); + return; + } gsk_canvas.snapshot = bitmap.getSnapshot(); gsk_canvas.drawRect(left, top, right, bottom, paint); } diff --git a/src/api-impl/android/graphics/GskCanvas.java b/src/api-impl/android/graphics/GskCanvas.java index b4d7743b..0014c658 100644 --- a/src/api-impl/android/graphics/GskCanvas.java +++ b/src/api-impl/android/graphics/GskCanvas.java @@ -8,6 +8,8 @@ public class GskCanvas extends Canvas { public long snapshot; private int save_count = 0; + private static Paint default_paint = new Paint(); + public GskCanvas(long snapshot) { this.snapshot = snapshot; } @@ -43,21 +45,17 @@ public class GskCanvas extends Canvas { @Override public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) { - int color = 0; - if (paint != null && paint.colorFilter instanceof PorterDuffColorFilter) { - color = ((PorterDuffColorFilter) paint.colorFilter).getColor(); - } - native_drawBitmap(snapshot, bitmap.getTexture(), dst.left, dst.top, dst.width(), dst.height(), color); + native_drawBitmap(snapshot, bitmap.getTexture(), dst.left, dst.top, dst.width(), dst.height(), paint != null ? paint.paint : default_paint.paint); } @Override public void drawPath(Path path, Paint paint) { - native_drawPath(snapshot, path.getGskPath(), paint.getColor(), paint.getStyle().nativeInt); + native_drawPath(snapshot, path.getGskPath(), paint != null ? paint.paint : default_paint.paint); } @Override public void drawRect(float left, float top, float right, float bottom, Paint paint) { - native_drawRect(snapshot, left, top, right, bottom, paint.getColor()); + native_drawRect(snapshot, left, top, right, bottom, paint != null ? paint.paint : default_paint.paint); } @Override @@ -69,12 +67,12 @@ public class GskCanvas extends Canvas { @Override public void drawText(String text, float x, float y, Paint paint) { - native_drawText(snapshot, text, x, y, paint.getColor(), paint.getTextSize()); + native_drawText(snapshot, text, x, y, paint != null ? paint.paint : default_paint.paint); } @Override public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) { - native_drawLine(snapshot, startX, startY, stopX, stopY, paint.getColor(), paint.getStrokeWidth()); + native_drawLine(snapshot, startX, startY, stopX, stopY, paint != null ? paint.paint : default_paint.paint); } @Override @@ -91,7 +89,7 @@ public class GskCanvas extends Canvas { @Override public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint) { - native_drawRoundRect(snapshot, left, top, right, bottom, rx, ry, paint.getColor(), paint.getStrokeWidth(), paint.getStyle().nativeInt); + native_drawRoundRect(snapshot, left, top, right, bottom, rx, ry, paint != null ? paint.paint : default_paint.paint); } @Override @@ -99,15 +97,15 @@ public class GskCanvas extends Canvas { native_scale(snapshot, sx, sy); } - protected native void native_drawBitmap(long snapshot, long texture, int x, int y, int width, int height, int color); - protected native void native_drawRect(long snapshot, float left, float top, float right, float bottom, int color); - protected native void native_drawPath(long snapshot, long path, int color, int style); + protected native void native_drawBitmap(long snapshot, long texture, int x, int y, int width, int height, long paint); + protected native void native_drawRect(long snapshot, float left, float top, float right, float bottom, long paint); + protected native void native_drawPath(long snapshot, long path, long paint); protected native void native_translate(long snapshot, float dx, float dy); protected native void native_rotate(long snapshot, float degrees); protected native void native_save(long snapshot); protected native void native_restore(long snapshot); - protected native void native_drawLine(long snapshot, float startX, float startY, float stopX, float stopY, int color, float strokeWidth); - protected native void native_drawText(long snapshot, String text, float x, float y, int color, float textSize); - protected native void native_drawRoundRect(long snapshot, float left, float top, float right, float bottom, float rx, float ry, int color, float strokeWidth, int style); + protected native void native_drawLine(long snapshot, float startX, float startY, float stopX, float stopY, long paint); + protected native void native_drawText(long snapshot, String text, float x, float y, long paint); + protected native void native_drawRoundRect(long snapshot, float left, float top, float right, float bottom, float rx, float ry, long paint); protected native void native_scale(long snapshot, float sx, float sy); } diff --git a/src/api-impl/android/graphics/Paint.java b/src/api-impl/android/graphics/Paint.java index d56c5b99..1d335e78 100644 --- a/src/api-impl/android/graphics/Paint.java +++ b/src/api-impl/android/graphics/Paint.java @@ -17,14 +17,11 @@ public class Paint { public static final int AUTO_HINTING_TEXT_FLAG = (1 << 11); public static final int VERTICAL_TEXT_FLAG = (1 << 12); - private Typeface typeface = null; - ColorFilter colorFilter = null; - private int color = 0; - private float strokeWidth = 0; - private float textSize = 7; - private Style style = Style.FILL; + long paint; // native paint + private Xfermode xfermode; public Paint() { + paint = native_create(); } public Paint (int flags) { @@ -33,32 +30,29 @@ public class Paint { } public Paint(Paint paint) { - /* TODO: use sk_paint_clone */ - this(); - setColor(paint.getColor()); + this.paint = native_clone(paint.paint); } public void setColor(int color) { - this.color = color; + native_set_color(paint, color); } public int getColor() { - return color; + return native_get_color(paint); } public void setAntiAlias(boolean aa) { } public void setStrokeWidth(float width) { - this.strokeWidth = width; + native_set_stroke_width(paint, width); } public void setTextSize(float size) { - this.textSize = size; + native_set_text_size(paint, size); } public Typeface setTypeface(Typeface typeface) { - this.typeface = typeface; - return this.typeface; + return typeface; } public void getTextBounds(String text, int start, int end, Rect bounds) {} public void getTextBounds(char[] text, int index, int count, Rect bounds) {} @@ -74,7 +68,7 @@ public class Paint { } public void setStyle(Style style) { - this.style = style; + native_set_style(paint, style.ordinal()); } public float ascent() { @@ -83,7 +77,7 @@ public class Paint { public float measureText(char[] text, int index, int count) { return 10; } public float measureText(String text, int start, int end) { - return (end-start)*textSize; + return (end-start)*getTextSize()*.6f; } public float measureText(String text) { return measureText(text, 0, text.length()); @@ -93,7 +87,12 @@ public class Paint { } public ColorFilter setColorFilter(ColorFilter colorFilter) { - this.colorFilter = colorFilter; + if (colorFilter instanceof PorterDuffColorFilter) { + PorterDuffColorFilter porterDuff = (PorterDuffColorFilter)colorFilter; + native_set_color_filter(paint, porterDuff.getMode().ordinal(), porterDuff.getColor()); + } else { + native_set_color_filter(paint, -1, 0); + } return colorFilter; } @@ -104,12 +103,12 @@ public class Paint { * Geometry and text drawn with this style will be filled, ignoring all * stroke-related settings in the paint. */ - FILL(0), + FILL, /** * Geometry and text drawn with this style will be stroked, respecting * the stroke-related fields on the paint. */ - STROKE(1), + STROKE, /** * Geometry and text drawn with this style will be both filled and * stroked at the same time, respecting the stroke-related fields on @@ -117,12 +116,9 @@ public class Paint { * is oriented counter-clockwise. This restriction does not apply to * either FILL or STROKE. */ - FILL_AND_STROKE(2); + FILL_AND_STROKE; - Style(int nativeInt) { - this.nativeInt = nativeInt; - } - final int nativeInt; + public static final Style values[] = Style.values(); } public static class FontMetrics { @@ -180,12 +176,14 @@ public class Paint { public /*native*/ int getAlpha() { return 0; } public /*native*/ void setAlpha(int a) {} public float getStrokeWidth() { - return strokeWidth; + return native_get_stroke_width(paint); } public /*native*/ float getStrokeMiter() { return 0; } public /*native*/ void setStrokeMiter(float miter) {} - public /*native*/ float getTextSize() { return 0; } + public /*native*/ float getTextSize() { + return native_get_text_size(paint); + } public /*native*/ float getTextScaleX() { return 0; } public /*native*/ void setTextScaleX(float scaleX) {} @@ -199,6 +197,11 @@ public class Paint { public void setShadowLayer(float radius, float dx, float dy, int color) {} public Xfermode setXfermode(Xfermode xfermode) { + this.xfermode = xfermode; + return xfermode; + } + + public Xfermode getXfermode() { return xfermode; } @@ -208,36 +211,36 @@ public class Paint { /** * The stroke ends with the path, and does not project beyond it. */ - BUTT(0), + BUTT, /** * The stroke projects out as a semicircle, with the center at the * end of the path. */ - ROUND(1), + ROUND, /** * The stroke projects out as a square, with the center at the end * of the path. */ - SQUARE(2); + SQUARE; - private Cap(int nativeInt) {} + public static final Cap values[] = Cap.values(); } public enum Join { /** * The outer edges of a join meet at a sharp angle */ - MITER(0), + MITER, /** * The outer edges of a join meet in a circular arc. */ - ROUND(1), + ROUND, /** * The outer edges of a join meet with a straight line */ - BEVEL(2); + BEVEL; - private Join(int nativeInt) {} + public static final Join values[] = Join.values(); } public enum Align { @@ -246,9 +249,13 @@ public class Paint { RIGHT, } - public void setStrokeCap(Cap cap) {} + public void setStrokeCap(Cap cap) { + native_set_stroke_cap(paint, cap.ordinal()); + } - public void setStrokeJoin(Join join) {} + public void setStrokeJoin(Join join) { + native_set_stroke_join(paint, join.ordinal()); + } public Typeface getTypeface() { return new Typeface(); @@ -284,19 +291,43 @@ public class Paint { return new FontMetricsInt(); } - public void set(Paint paint) {} + public void set(Paint paint) { + native_recycle(this.paint); + this.paint = native_clone(paint.paint); + } public boolean isFilterBitmap() { return false; } - public Cap getStrokeCap() { return Cap.BUTT; } + public Cap getStrokeCap() { + return Cap.values[native_get_stroke_cap(paint)]; + } - public Join getStrokeJoin() { return Join.MITER; } + public Join getStrokeJoin() { + return Join.values[native_get_stroke_join(paint)]; + } public Locale getTextLocale() { return Locale.getDefault(); } public float getLetterSpacing() { return 1.0f; } public Style getStyle() { - return style; + return Style.values[native_get_style(paint)]; } + + private static native long native_create(); + private static native long native_clone(long paint); + private static native void native_recycle(long paint); + private static native void native_set_color(long paint, int color); + private static native int native_get_color(long paint); + private static native void native_set_style(long paint, int style); + private static native int native_get_style(long paint); + private static native void native_set_stroke_width(long paint, float width); + private static native float native_get_stroke_width(long paint); + private static native void native_set_stroke_cap(long paint, int cap); + private static native int native_get_stroke_cap(long paint); + private static native void native_set_stroke_join(long paint, int join); + private static native int native_get_stroke_join(long paint); + private static native void native_set_text_size(long paint, float size); + private static native float native_get_text_size(long paint); + private static native void native_set_color_filter(long paint, int mode, int color); }