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
Paint: implement textAlign and text bounds
This commit is contained in:
@@ -161,6 +161,22 @@ JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1get_1text_1size
|
|||||||
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter
|
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter
|
||||||
(JNIEnv *, jclass, jlong, jint, jint);
|
(JNIEnv *, jclass, jlong, jint, jint);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_graphics_Paint
|
||||||
|
* Method: native_get_text_bounds
|
||||||
|
* Signature: (JLjava/lang/String;Landroid/graphics/Rect;)V
|
||||||
|
*/
|
||||||
|
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1get_1text_1bounds
|
||||||
|
(JNIEnv *, jclass, jlong, jstring, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_graphics_Paint
|
||||||
|
* Method: native_set_text_align
|
||||||
|
* Signature: (JI)V
|
||||||
|
*/
|
||||||
|
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1align
|
||||||
|
(JNIEnv *, jclass, jlong, jint);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "pango/pango-layout.h"
|
||||||
#include <gdk/gdk.h>
|
#include <gdk/gdk.h>
|
||||||
#include <gsk/gsk.h>
|
#include <gsk/gsk.h>
|
||||||
#include <pango/pango.h>
|
#include <pango/pango.h>
|
||||||
@@ -6,6 +7,7 @@ struct AndroidPaint {
|
|||||||
GdkRGBA color;
|
GdkRGBA color;
|
||||||
GskStroke *gsk_stroke;
|
GskStroke *gsk_stroke;
|
||||||
PangoFontDescription *font;
|
PangoFontDescription *font;
|
||||||
|
PangoAlignment alignment;
|
||||||
graphene_matrix_t color_matrix;
|
graphene_matrix_t color_matrix;
|
||||||
graphene_vec4_t color_offset;
|
graphene_vec4_t color_offset;
|
||||||
bool is_fill:1;
|
bool is_fill:1;
|
||||||
|
|||||||
@@ -102,6 +102,13 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText(JNIEnv *
|
|||||||
const char *str = (*env)->GetStringUTFChars(env, text, NULL);
|
const char *str = (*env)->GetStringUTFChars(env, text, NULL);
|
||||||
pango_layout_set_text(layout, str, -1);
|
pango_layout_set_text(layout, str, -1);
|
||||||
(*env)->ReleaseStringUTFChars(env, text, str);
|
(*env)->ReleaseStringUTFChars(env, text, str);
|
||||||
|
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;
|
||||||
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(x, y));
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(x, y));
|
||||||
gtk_snapshot_append_layout(snapshot, layout, &paint->color);
|
gtk_snapshot_append_layout(snapshot, layout, &paint->color);
|
||||||
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(-x, -y));
|
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(-x, -y));
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <gsk/gsk.h>
|
#include <gsk/gsk.h>
|
||||||
|
#include <gtk/gtk.h>
|
||||||
|
|
||||||
#include "AndroidPaint.h"
|
#include "AndroidPaint.h"
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
@@ -131,3 +132,28 @@ JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1color_1filter(JN
|
|||||||
graphene_vec4_init(&paint->color_offset, ((color >> 16) & 0xFF) / 255.f, ((color >> 8) & 0xFF) / 255.f, ((color >> 0) & 0xFF) / 255.f, 0);
|
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;
|
paint->use_color_filter = mode != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern GtkWidget *window;
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1get_1text_1bounds(JNIEnv *env, jclass clazz, jlong paint_ptr, jstring text_ptr, jobject bounds)
|
||||||
|
{
|
||||||
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
||||||
|
PangoLayout *layout = pango_layout_new(gtk_widget_get_pango_context(window));
|
||||||
|
pango_layout_set_font_description(layout, paint->font);
|
||||||
|
const char *str = (*env)->GetStringUTFChars(env, text_ptr, NULL);
|
||||||
|
pango_layout_set_text(layout, str, -1);
|
||||||
|
(*env)->ReleaseStringUTFChars(env, text_ptr, str);
|
||||||
|
PangoRectangle rect;
|
||||||
|
pango_layout_get_pixel_extents(layout, NULL, &rect);
|
||||||
|
_SET_INT_FIELD(bounds, "left", rect.x);
|
||||||
|
_SET_INT_FIELD(bounds, "top", rect.y);
|
||||||
|
_SET_INT_FIELD(bounds, "right", rect.x + rect.width);
|
||||||
|
_SET_INT_FIELD(bounds, "bottom", rect.y + rect.height);
|
||||||
|
g_object_unref(layout);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1align(JNIEnv *env, jclass clazz, jlong paint_ptr, jint align)
|
||||||
|
{
|
||||||
|
struct AndroidPaint *paint = _PTR(paint_ptr);
|
||||||
|
paint->alignment = align;
|
||||||
|
}
|
||||||
|
|||||||
@@ -60,11 +60,18 @@ public class Paint {
|
|||||||
public Typeface setTypeface(Typeface typeface) {
|
public Typeface setTypeface(Typeface typeface) {
|
||||||
return typeface;
|
return typeface;
|
||||||
}
|
}
|
||||||
public void getTextBounds(String text, int start, int end, Rect bounds) {}
|
public void getTextBounds(String text, int start, int end, Rect bounds) {
|
||||||
public void getTextBounds(char[] text, int index, int count, Rect bounds) {}
|
if (end > text.length())
|
||||||
|
end = text.length();
|
||||||
|
native_get_text_bounds(paint, text.substring(start, end), bounds);
|
||||||
|
}
|
||||||
|
public void getTextBounds(char[] text, int index, int count, Rect bounds) {
|
||||||
|
native_get_text_bounds(paint, new String(text, index, count), bounds);
|
||||||
|
}
|
||||||
public int getTextWidths(String text, int start, int end, float[] widths) {
|
public int getTextWidths(String text, int start, int end, float[] widths) {
|
||||||
// TODO fix it
|
Rect bounds = new Rect();
|
||||||
return 0;
|
native_get_text_bounds(paint, text.substring(start, end), bounds);
|
||||||
|
return bounds.width();
|
||||||
}
|
}
|
||||||
public void setFilterBitmap(boolean filter) {}
|
public void setFilterBitmap(boolean filter) {}
|
||||||
|
|
||||||
@@ -78,7 +85,7 @@ public class Paint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public float ascent() {
|
public float ascent() {
|
||||||
return 10;
|
return -getTextSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float measureText(char[] text, int index, int count) { return 10; }
|
public float measureText(char[] text, int index, int count) { return 10; }
|
||||||
@@ -253,8 +260,8 @@ public class Paint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public enum Align {
|
public enum Align {
|
||||||
CENTER,
|
|
||||||
LEFT,
|
LEFT,
|
||||||
|
CENTER,
|
||||||
RIGHT,
|
RIGHT,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +277,10 @@ public class Paint {
|
|||||||
return new Typeface();
|
return new Typeface();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTextAlign(Align align) {}
|
public void setTextAlign(Align align) {
|
||||||
|
this.align = align;
|
||||||
|
native_set_text_align(paint, align.ordinal());
|
||||||
|
}
|
||||||
|
|
||||||
public Shader getShader() {
|
public Shader getShader() {
|
||||||
return shader;
|
return shader;
|
||||||
@@ -343,4 +353,6 @@ public class Paint {
|
|||||||
private static native void native_set_text_size(long paint, float size);
|
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 float native_get_text_size(long paint);
|
||||||
private static native void native_set_color_filter(long paint, int mode, int color);
|
private static native void native_set_color_filter(long paint, int mode, int color);
|
||||||
|
private static native void native_get_text_bounds(long paint, String text, Rect bounds);
|
||||||
|
private static native void native_set_text_align(long paint, int align);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user