-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android: Send <Text> metrics in onTextLayout events
Summary: @public As we're doing in D9440914 (OSS 64a5253), send text metrics in an onTextLayout callback. These can be used by surrounding views for doing complicated layout like: - displaying a cursor at the end of text - vertical centering using capheight-baseline This right now isn't very performant but is only done when `onTextLayout` is set. I plan to optimize it with a capheight and xheight cache in a follow up diff. Reviewed By: achen1 Differential Revision: D9585613 fbshipit-source-id: aa20535b8371d5aecf15822d66a0d973c9a7eeda
- Loading branch information
1 parent
36199d3
commit 737f937
Showing
3 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
ReactAndroid/src/main/java/com/facebook/react/views/text/FontMetricsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.views.text; | ||
|
||
import android.content.Context; | ||
import android.graphics.Rect; | ||
import android.text.Layout; | ||
import android.text.TextPaint; | ||
import android.util.DisplayMetrics; | ||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.WritableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
|
||
public class FontMetricsUtil { | ||
public static WritableArray getFontMetrics(CharSequence text, Layout layout, TextPaint paint, Context context) { | ||
DisplayMetrics dm = context.getResources().getDisplayMetrics(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
WritableArray lines = Arguments.createArray(); | ||
for (int i = 0; i < layout.getLineCount(); i++) { | ||
Rect bounds = new Rect(); | ||
layout.getLineBounds(i, bounds); | ||
|
||
WritableMap line = Arguments.createMap(); | ||
TextPaint paintCopy = new TextPaint(paint); | ||
paintCopy.setTextSize(paintCopy.getTextSize() * 100); | ||
Rect capHeightBounds = new Rect(); | ||
paintCopy.getTextBounds("T", 0, 1, capHeightBounds); | ||
Rect xHeightBounds = new Rect(); | ||
paintCopy.getTextBounds("x", 0, 1, xHeightBounds); | ||
line.putDouble("x", bounds.left / dm.density); | ||
line.putDouble("y", bounds.top / dm.density); | ||
line.putDouble("width", layout.getLineWidth(i) / dm.density); | ||
line.putDouble("height", bounds.height() / dm.density); | ||
line.putDouble("descender", layout.getLineDescent(i) / dm.density); | ||
line.putDouble("ascender", -layout.getLineAscent(i) / dm.density); | ||
line.putDouble("baseline", layout.getLineBaseline(i) / dm.density); | ||
line.putDouble( | ||
"capHeight", capHeightBounds.height() / 100 * paint.getTextSize() / dm.density); | ||
line.putDouble("xHeight", xHeightBounds.height() / 100 * paint.getTextSize() / dm.density); | ||
line.putString( | ||
"text", text.subSequence(layout.getLineStart(i), layout.getLineEnd(i)).toString()); | ||
lines.pushMap(line); | ||
} | ||
return lines; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shouldn't it use
DisplayMetricsHolder
?