Skip to content

Commit 8a05924

Browse files
author
farfromrefuge
committed
fix(android): rewrote text size handling. It now seems to be consistent with TextView drawing (with very slight difference)
1 parent 629c372 commit 8a05924

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

packages/ui-canvas/platforms/android/java/com/akylas/canvas/CanvasView.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ protected void dispatchDraw(Canvas canvas) {
2929
}
3030
}
3131

32-
private static float FONT_SIZE_FACTOR = -1;
33-
public static float getFontSizeFactor(Context context) {
34-
if (FONT_SIZE_FACTOR == -1) {
35-
FONT_SIZE_FACTOR = android.util.TypedValue.applyDimension(
36-
android.util.TypedValue.COMPLEX_UNIT_SP, 1, context.getResources().getDisplayMetrics());
37-
}
38-
return FONT_SIZE_FACTOR;
32+
public static float getFontSizeFactor(Context context, float textSize) {
33+
return android.util.TypedValue.applyDimension(
34+
android.util.TypedValue.COMPLEX_UNIT_SP, textSize, context.getResources().getDisplayMetrics());
3935
}
4036
}

packages/ui-canvas/platforms/android/java/com/akylas/canvas/StaticLayout.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.text.TextPaint;
1111
import android.text.TextUtils;
1212
import java.lang.CharSequence;
13+
import android.util.Log;
1314

1415
public class StaticLayout {
1516

@@ -32,6 +33,9 @@ public static android.text.StaticLayout.Builder createStaticLayoutBuilder(CharSe
3233
public static android.text.StaticLayout createStaticLayout(CharSequence source, TextPaint paint, int width,
3334
Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad, TextUtils.TruncateAt ellipsize, int ellipsizedWidth, int height) {
3435
android.text.StaticLayout staticLayout = null;
36+
// we set our custom flag to discover it in FontSizeSpan
37+
Log.d("JS", "createStaticLayout " + paint + " " + paint.getFlags());
38+
paint.setFlags(paint.getFlags() | 2048);
3539
if (Build.VERSION.SDK_INT >= 23) {
3640
staticLayout = createStaticLayoutBuilder(source, paint, width, align, spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth).build();
3741
} else {
@@ -64,7 +68,7 @@ public static android.text.StaticLayout createEllipsizeStaticLayout(android.text
6468
float lineHeight = staticLayout.getLineBottom(i) - staticLayout.getLineTop(i);
6569
float totalHeight = lineHeight * lineCount;
6670
if (maxLines > 1) {
67-
while (totalHeight > maxHeight && maxLines > 0) {
71+
while (totalHeight > maxHeight && maxLines > 1) {
6872
maxLines--;
6973
i++;
7074
lineHeight = staticLayout.getLineBottom(i) - staticLayout.getLineTop(i);

src/ui-canvas/canvas.android.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class ProxyClass<T> {
9999
}
100100
}
101101

102+
let FONT_SIZE_FACTOR;
103+
let SCREEN_DENSITY;
102104
class Canvas extends ProxyClass<android.graphics.Canvas> {
103105
mBitmap: android.graphics.Bitmap;
104106
mShouldReleaseBitmap = false;
@@ -153,6 +155,18 @@ class Canvas extends ProxyClass<android.graphics.Canvas> {
153155
} else if (methodName === 'drawView') {
154156
drawViewOnCanvas(native, args[0], args[1]);
155157
return true;
158+
} else if (methodName === 'drawText') {
159+
// TODO move that to native
160+
const paint = args[args.length - 1];
161+
const textSize = paint.getTextSize();
162+
if (!FONT_SIZE_FACTOR) {
163+
SCREEN_DENSITY = Screen.mainScreen.scale;
164+
FONT_SIZE_FACTOR = com.akylas.canvas.CanvasView.getFontSizeFactor(Utils.android.getApplicationContext(), 1);
165+
}
166+
paint.setTextSize((textSize * FONT_SIZE_FACTOR) / SCREEN_DENSITY);
167+
native[methodName](...args);
168+
paint.setTextSize(textSize);
169+
return true;
156170
}
157171
}
158172
getImage() {
@@ -166,7 +180,6 @@ class Canvas extends ProxyClass<android.graphics.Canvas> {
166180
}
167181
}
168182

169-
let FONT_SIZE_FACTOR;
170183
export class Paint extends ProxyClass<android.graphics.Paint> {
171184
mNative: android.graphics.Paint;
172185
mFontInternal: Font;
@@ -197,12 +210,6 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
197210
return;
198211
}
199212
args[0] = createColorParam(args[0]);
200-
} else if (methodName === 'setTextSize') {
201-
// we apply a small factor so that font size is the same as in TextView
202-
if (!FONT_SIZE_FACTOR) {
203-
FONT_SIZE_FACTOR = com.akylas.canvas.CanvasView.getFontSizeFactor(Utils.android.getApplicationContext()) / Screen.mainScreen.scale;
204-
}
205-
args[0] *= FONT_SIZE_FACTOR;
206213
} else if (methodName === 'setTypeface') {
207214
if (args[0] instanceof Font) {
208215
this.mFontInternal = args[0];
@@ -219,7 +226,7 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
219226
args[3] = createColorParam(args[3]);
220227
}
221228
}
222-
setTextSize(size){}
229+
setTextSize(size) {}
223230
setFont(font: Font) {
224231
this.mFontInternal = font;
225232
if (this.handlesFont) {

src/ui-canvas/typings/android.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare namespace com {
1818
export class CanvasView extends globalAndroid.view.View {
1919
sizeChangedListener?: SizeChangedListener;
2020
drawListener?: DrawListener;
21-
static getFontSizeFactor(context: globalAndroid.content.Context): number;
21+
static getFontSizeFactor(context: globalAndroid.content.Context, size: number): number;
2222
}
2323
export class SizeChangedListener {
2424
constructor(impl?: { onSizeChanged(w: number, h: number, oldw: number, oldh: number) });

src/ui-canvaslabel/canvaslabel.common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
Observable,
1414
ObservableArray,
1515
PercentLength,
16+
Screen,
1617
Utils,
1718
View,
1819
profile
@@ -56,7 +57,8 @@ export type VerticalTextAlignment = 'initial' | 'top' | 'middle' | 'bottom' | 'c
5657
// const debugPaint = new Paint();
5758
// debugPaint.style = Style.STROKE;
5859
// debugPaint.color = 'red';
59-
60+
let FONT_SIZE_FACTOR;
61+
let SCREEN_DENSITY;
6062
export abstract class SpanBase extends Shape {
6163
static get isSpan() {
6264
return true;
@@ -228,6 +230,10 @@ export abstract class SpanBase extends Shape {
228230
paint.setFontFamily(fontFamily);
229231
paint.setFontWeight(fontweight);
230232
paint.setFontStyle(fontstyle);
233+
if (__ANDROID__ && !FONT_SIZE_FACTOR) {
234+
SCREEN_DENSITY = Screen.mainScreen.scale;
235+
FONT_SIZE_FACTOR = com.akylas.canvas.CanvasView.getFontSizeFactor(Utils.android.getApplicationContext(), 1);
236+
}
231237
paint.textSize = fontSize;
232238
cachedPaint = paintCache[cacheKey] = paint;
233239
paintFontCache[fontKey] = paint;

0 commit comments

Comments
 (0)