You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
implement more GskCanvas methods
This commit is contained in:
@@ -65,6 +65,22 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1save
|
||||
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore
|
||||
(JNIEnv *, jobject, jlong);
|
||||
|
||||
/*
|
||||
* Class: android_graphics_GskCanvas
|
||||
* Method: native_drawLine
|
||||
* Signature: (JFFFFJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLine
|
||||
(JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jlong);
|
||||
|
||||
/*
|
||||
* Class: android_graphics_GskCanvas
|
||||
* Method: native_drawText
|
||||
* Signature: (JLjava/lang/String;FFJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText
|
||||
(JNIEnv *, jobject, jlong, jstring, jfloat, jfloat, jlong);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <gtk/gtk.h>
|
||||
#include <graphene.h>
|
||||
#include <pango/pango.h>
|
||||
|
||||
#include "include/c/sk_paint.h"
|
||||
#include "include/c/sk_path.h"
|
||||
@@ -47,25 +48,14 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect(JNIEnv *
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
sk_path_iter_destroy(iterator);
|
||||
@@ -94,3 +84,37 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore(JNIEnv *e
|
||||
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
||||
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, jlong paint_ptr)
|
||||
{
|
||||
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;
|
||||
|
||||
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));
|
||||
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
|
||||
GdkRGBA gdk_color;
|
||||
sk_paint_get_color4f(paint, (sk_color4f_t *)&gdk_color);
|
||||
PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(window));
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -62,17 +62,19 @@ public class GskCanvas extends Canvas {
|
||||
|
||||
@Override
|
||||
public void rotate(float degrees, float px, float py) {
|
||||
System.out.println("GskCanvas.rotate(" + degrees + ", " + px + ", " + py + ")");
|
||||
native_translate(snapshot, px, py);
|
||||
native_rotate(snapshot, degrees);
|
||||
native_translate(snapshot, -px, -py);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawText(String text, float x, float y, Paint paint) {
|
||||
System.out.println("GskCanvas.drawText(" + text + ", " + x + ", " + y + ", " + paint + ")");
|
||||
native_drawText(snapshot, text, x, y, paint.skia_paint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
|
||||
System.out.println("GskCanvas.drawLine(" + startX + ", " + startY + ", " + stopX + ", " + stopY + ", " + paint + ")");
|
||||
native_drawLine(snapshot, startX, startY, stopX, stopY, paint.skia_paint);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,6 +84,11 @@ public class GskCanvas extends Canvas {
|
||||
drawBitmap(bitmap, src, dst, paint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
|
||||
drawBitmap(bitmap, src, new Rect((int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom), paint);
|
||||
}
|
||||
|
||||
protected native void native_drawBitmap(long snapshot, long pixbuf, 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, long paint);
|
||||
@@ -89,4 +96,6 @@ public class GskCanvas extends Canvas {
|
||||
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, long paint);
|
||||
protected native void native_drawText(long snapshot, String text, float x, float y, long paint);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user