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 Paint as combination of GDK, GSK and Pango attributes
This commit is contained in:
14
src/api-impl-jni/graphics/AndroidPaint.h
Normal file
14
src/api-impl-jni/graphics/AndroidPaint.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <gdk/gdk.h>
|
||||
#include <gsk/gsk.h>
|
||||
#include <pango/pango.h>
|
||||
|
||||
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;
|
||||
};
|
||||
@@ -3,58 +3,41 @@
|
||||
#include <pango/pango.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
|
||||
133
src/api-impl-jni/graphics/android_graphics_Paint.c
Normal file
133
src/api-impl-jni/graphics/android_graphics_Paint.c
Normal file
@@ -0,0 +1,133 @@
|
||||
#include <glib.h>
|
||||
#include <gsk/gsk.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user