Skip to content

Commit e3f4a7b

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix precision of TextInlineViews in Android
Summary: TextInlineViews in Android was incorrectly converting values to from float to int, this produced to loose precision and to render incomplete texts in some components. This diff changed the types from int to float, avoiding loose precision. The impact of this bug is not that high because in the conversion to int we were using Math.ceil(), which was already rounding the result to the next pixel. changeLog: [Android][Fixed] Fix precision of TextInlineViews in Fabric Android Reviewed By: JoshuaGross, shergin Differential Revision: D21541159 fbshipit-source-id: 4741ab96964c35af1c1b7d3e821e505ecef2efce
1 parent 774ebd9 commit e3f4a7b

File tree

9 files changed

+17
-16
lines changed

9 files changed

+17
-16
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ private long measure(
464464
float maxWidth,
465465
float minHeight,
466466
float maxHeight,
467-
@Nullable int[] attachmentsPositions) {
467+
@Nullable float[] attachmentsPositions) {
468468
ReactContext context =
469469
rootTag < 0 ? mReactApplicationContext : mReactContextForRootTag.get(rootTag);
470470
return mMountingManager.measure(

ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ public long measure(
519519
@NonNull YogaMeasureMode widthMode,
520520
float height,
521521
@NonNull YogaMeasureMode heightMode,
522-
@Nullable int[] attachmentsPositions) {
522+
@Nullable float[] attachmentsPositions) {
523523

524524
return mViewManagerRegistry
525525
.get(componentName)

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public long measure(
318318
YogaMeasureMode widthMode,
319319
float height,
320320
YogaMeasureMode heightMode,
321-
@Nullable int[] attachmentsPositions) {
321+
@Nullable float[] attachmentsPositions) {
322322
return 0;
323323
}
324324

ReactAndroid/src/main/java/com/facebook/react/views/slider/ReactSliderManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public long measure(
259259
YogaMeasureMode widthMode,
260260
float height,
261261
YogaMeasureMode heightMode,
262-
@Nullable int[] attachmentsPositions) {
262+
@Nullable float[] attachmentsPositions) {
263263
SeekBar reactSlider = new ReactSlider(context, null, STYLE);
264264
final int spec =
265265
View.MeasureSpec.makeMeasureSpec(

ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public long measure(
210210
YogaMeasureMode widthMode,
211211
float height,
212212
YogaMeasureMode heightMode,
213-
@Nullable int[] attachmentsPositions) {
213+
@Nullable float[] attachmentsPositions) {
214214
ReactSwitch view = new ReactSwitch(context);
215215
view.setShowText(false);
216216
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextViewManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public long measure(
120120
YogaMeasureMode widthMode,
121121
float height,
122122
YogaMeasureMode heightMode,
123-
@Nullable int[] attachmentsPositions) {
123+
@Nullable float[] attachmentsPositions) {
124124

125125
return TextLayoutManager.measureText(
126126
context,

ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static long measureText(
216216
float height,
217217
YogaMeasureMode heightYogaMeasureMode,
218218
ReactTextViewManagerCallback reactTextViewManagerCallback,
219-
@Nullable int[] attachmentsPositions) {
219+
@Nullable float[] attachmentsPositions) {
220220

221221
// TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic)
222222
TextPaint textPaint = sTextPaintInstance;
@@ -234,6 +234,7 @@ public static long measureText(
234234
if (text == null) {
235235
throw new IllegalStateException("Spannable element has not been prepared in onBeforeLayout");
236236
}
237+
237238
BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint);
238239
float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN;
239240

@@ -412,9 +413,9 @@ public static long measureText(
412413

413414
// The attachment array returns the positions of each of the attachments as
414415
attachmentsPositions[attachmentPosition] =
415-
(int) Math.ceil(PixelUtil.toSPFromPixel(placeholderTopPosition));
416+
PixelUtil.toSPFromPixel(placeholderTopPosition);
416417
attachmentsPositions[attachmentPosition + 1] =
417-
(int) Math.ceil(PixelUtil.toSPFromPixel(placeholderLeftPosition));
418+
PixelUtil.toSPFromPixel(placeholderLeftPosition);
418419
attachmentIndex++;
419420
}
420421
}

ReactCommon/fabric/attributedstring/conversions.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,9 @@ inline folly::dynamic toDynamic(const AttributedString &attributedString) {
611611
if (fragment.isAttachment()) {
612612
dynamicFragment["isAttachment"] = true;
613613
dynamicFragment["width"] =
614-
(int)fragment.parentShadowView.layoutMetrics.frame.size.width;
614+
fragment.parentShadowView.layoutMetrics.frame.size.width;
615615
dynamicFragment["height"] =
616-
(int)fragment.parentShadowView.layoutMetrics.frame.size.height;
616+
fragment.parentShadowView.layoutMetrics.frame.size.height;
617617
}
618618
dynamicFragment["textAttributes"] = toDynamic(fragment.textAttributes);
619619
fragments.push_back(dynamicFragment);

ReactCommon/fabric/textlayoutmanager/platform/android/TextLayoutManager.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ TextMeasurement TextLayoutManager::doMeasure(
5050
}
5151
}
5252
auto env = Environment::current();
53-
auto attachmentPositions = env->NewIntArray(attachmentsCount * 2);
53+
auto attachmentPositions = env->NewFloatArray(attachmentsCount * 2);
5454

5555
static auto measure =
5656
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
@@ -64,7 +64,7 @@ TextMeasurement TextLayoutManager::doMeasure(
6464
jfloat,
6565
jfloat,
6666
jfloat,
67-
jintArray)>("measure");
67+
jfloatArray)>("measure");
6868

6969
auto minimumSize = layoutConstraints.minimumSize;
7070
auto maximumSize = layoutConstraints.maximumSize;
@@ -93,7 +93,7 @@ TextMeasurement TextLayoutManager::doMeasure(
9393
maximumSize.height,
9494
attachmentPositions));
9595

96-
jint *attachmentData = env->GetIntArrayElements(attachmentPositions, 0);
96+
jfloat *attachmentData = env->GetFloatArrayElements(attachmentPositions, 0);
9797

9898
auto attachments = TextMeasurement::Attachments{};
9999
if (attachmentsCount > 0) {
@@ -104,8 +104,8 @@ TextMeasurement TextLayoutManager::doMeasure(
104104
if (fragment["isAttachment"] == true) {
105105
float top = attachmentData[attachmentIndex * 2];
106106
float left = attachmentData[attachmentIndex * 2 + 1];
107-
float width = fragment["width"].getInt();
108-
float height = fragment["height"].getInt();
107+
float width = (float)fragment["width"].getDouble();
108+
float height = (float)fragment["height"].getDouble();
109109

110110
auto rect = facebook::react::Rect{{left, top},
111111
facebook::react::Size{width, height}};

0 commit comments

Comments
 (0)