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
android.text.Layout: actually measure text sizes
This commit is contained in:
@@ -76,10 +76,10 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLine
|
||||
/*
|
||||
* Class: android_graphics_GskCanvas
|
||||
* Method: native_drawText
|
||||
* Signature: (JLjava/lang/String;FFJ)V
|
||||
* Signature: (JLjava/lang/String;FFJJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText
|
||||
(JNIEnv *, jobject, jlong, jstring, jfloat, jfloat, jlong);
|
||||
(JNIEnv *, jobject, jlong, jstring, jfloat, jfloat, jlong, jlong);
|
||||
|
||||
/*
|
||||
* Class: android_graphics_GskCanvas
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <graphene.h>
|
||||
#include <pango/pango.h>
|
||||
|
||||
#include "include/c/sk_font.h"
|
||||
#include "include/c/sk_paint.h"
|
||||
#include "include/c/sk_path.h"
|
||||
|
||||
@@ -104,13 +105,17 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawLine(JNIEnv *
|
||||
|
||||
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)
|
||||
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)
|
||||
{
|
||||
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
||||
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
|
||||
sk_font_t *font = (sk_font_t *)_PTR(font_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));
|
||||
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);
|
||||
const char *str = (*env)->GetStringUTFChars(env, text, NULL);
|
||||
pango_layout_set_text(layout, str, -1);
|
||||
(*env)->ReleaseStringUTFChars(env, text, str);
|
||||
@@ -118,6 +123,7 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawText(JNIEnv *
|
||||
gtk_snapshot_append_layout(snapshot, layout, &gdk_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)
|
||||
|
||||
@@ -69,7 +69,7 @@ public class GskCanvas extends Canvas {
|
||||
|
||||
@Override
|
||||
public void drawText(String text, float x, float y, Paint paint) {
|
||||
native_drawText(snapshot, text, x, y, paint.skia_paint);
|
||||
native_drawText(snapshot, text, x, y, paint.skia_paint, paint.skia_font);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -102,6 +102,6 @@ public class GskCanvas extends Canvas {
|
||||
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);
|
||||
protected native void native_drawText(long snapshot, String text, float x, float y, long paint, long font);
|
||||
protected native void native_drawRoundRect(long snapshot, float left, float top, float right, float bottom, float rx, float ry, int color, float strokeWidth);
|
||||
}
|
||||
|
||||
@@ -92,13 +92,17 @@ public class Paint {
|
||||
}
|
||||
|
||||
public float measureText(char[] text, int index, int count) { return 10; }
|
||||
public float measureText(String text, int start, int end) { return 10; }
|
||||
public float measureText(String text) {
|
||||
public float measureText(String text, int start, int end) {
|
||||
if (skia_font == 0)
|
||||
skia_font = native_create_font();
|
||||
return native_measure_text(skia_font, text, 0, text.length(), skia_paint);
|
||||
return native_measure_text(skia_font, text, start, end, skia_paint);
|
||||
}
|
||||
public float measureText(String text) {
|
||||
return measureText(text, 0, text.length());
|
||||
}
|
||||
public float measureText(CharSequence text, int start, int end) {
|
||||
return measureText(text.toString(), start, end);
|
||||
}
|
||||
public float measureText(CharSequence text, int start, int end) { return 10; }
|
||||
|
||||
public ColorFilter setColorFilter(ColorFilter colorFilter) {
|
||||
this.colorFilter = colorFilter;
|
||||
|
||||
@@ -32,7 +32,9 @@ public class Layout {
|
||||
|
||||
public int getWidth() {return 10;}
|
||||
|
||||
public int getHeight() {return 10;}
|
||||
public int getHeight() {
|
||||
return (int)(paint.measureText("_") * 3);
|
||||
}
|
||||
|
||||
public void draw(Canvas canvas) {
|
||||
canvas.drawText(text.toString(), 0, 0, paint);
|
||||
@@ -41,7 +43,7 @@ public class Layout {
|
||||
public int getParagraphDirection(int line) {return 0;}
|
||||
|
||||
public static float getDesiredWidth(CharSequence source, int start, int end, TextPaint paint) {
|
||||
return 400;
|
||||
return paint.measureText(source, start, end);
|
||||
}
|
||||
|
||||
public int getLineEnd(int line) {return 100;}
|
||||
|
||||
Reference in New Issue
Block a user