Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
298 changes: 296 additions & 2 deletions packages/react-native/Libraries/Text/__tests__/Text-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';

import type {HostInstance} from 'react-native';
import type {AccessibilityProps} from 'react-native';
import type {AccessibilityProps, TextLayoutEvent} from 'react-native';

import accessibilityPropsSuite, {
rolePropSuite,
Expand All @@ -22,7 +22,7 @@ import * as Fantom from '@react-native/fantom';
import nullthrows from 'nullthrows';
import * as React from 'react';
import {createRef} from 'react';
import {Text} from 'react-native';
import {Text, View} from 'react-native';

const TEST_TEXT = 'the text';

Expand Down Expand Up @@ -651,6 +651,300 @@ describe('<Text>', () => {
});
});

describe('onTextLayout', () => {
it('reports each line where it is drawn', () => {
const lines: Array<{
text: string,
x: number,
width: number,
capHeight: number,
xHeight: number,
height: number,
ascender: number,
descender: number,
}> = [];
const root = Fantom.createRoot();

Fantom.runTask(() => {
root.render(
<Text
onTextLayout={event => {
for (const line of event.nativeEvent.lines) {
lines.push({
text: line.text,
x: line.x,
width: line.width,
capHeight: line.capHeight,
xHeight: line.xHeight,
height: line.height,
ascender: line.ascender,
descender: line.descender,
});
}
}}
style={{width: 200, textAlign: 'center'}}>
{TEST_TEXT}
</Text>,
);
});
Fantom.flushAllNativeEvents();

expect(lines).toHaveLength(1);
expect(lines[0].text).toBe(TEST_TEXT);
expect(lines[0].width).toBeGreaterThan(0);
expect(lines[0].width).toBeLessThan(200);
expect(lines[0].x).toBeCloseTo((200 - lines[0].width) / 2);
expect(lines[0].xHeight).toBeGreaterThan(0);
expect(lines[0].capHeight).toBeGreaterThan(lines[0].xHeight);
expect(lines[0].ascender).toBeGreaterThan(lines[0].capHeight);
expect(lines[0].ascender + lines[0].descender).toBeCloseTo(
lines[0].height,
);
});

it('draws the lines it reports when only `numberOfLines` changes', () => {
const LONG_TEXT = 'the text that is long enough to wrap onto more lines';
const renderText = (
root: ReturnType<typeof Fantom.createRoot>,
numberOfLines: number,
): ReadonlyArray<string> => {
let reportedLines: ReadonlyArray<string> = [];
Fantom.runTask(() => {
root.render(
<Text
numberOfLines={numberOfLines}
onTextLayout={event => {
reportedLines = event.nativeEvent.lines.map(line => line.text);
}}
style={{width: 100, height: 60}}>
{LONG_TEXT}
</Text>,
);
});
Fantom.flushAllNativeEvents();
return reportedLines;
};

const changedRoot = Fantom.createRoot();
expect(renderText(changedRoot, 2)).toHaveLength(2);
const twoLines = changedRoot.takeScreenshot();
expect(renderText(changedRoot, 1)).toHaveLength(1);
const oneLineRoot = Fantom.createRoot();
renderText(oneLineRoot, 1);

expect(twoLines.isEqual(oneLineRoot.takeScreenshot())).toBe(false);
expect(
changedRoot.takeScreenshot().isEqual(oneLineRoot.takeScreenshot()),
).toBe(true);
});

it('draws the lines it reports for a selectable text', () => {
const renderText = (
root: ReturnType<typeof Fantom.createRoot>,
selectable: boolean,
): ReadonlyArray<string> => {
let reportedLines: ReadonlyArray<string> = [];
Fantom.runTask(() => {
root.render(
<Text
onTextLayout={event => {
reportedLines = event.nativeEvent.lines.map(line => line.text);
}}
selectable={selectable}
style={{width: 100}}>
{TEST_TEXT}
</Text>,
);
});
Fantom.flushAllNativeEvents();
return reportedLines;
};

const selectableRoot = Fantom.createRoot();
expect(renderText(selectableRoot, true)).toEqual([TEST_TEXT]);
const plainRoot = Fantom.createRoot();
renderText(plainRoot, false);
const emptyRoot = Fantom.createRoot();
Fantom.runTask(() => {
emptyRoot.render(<Text style={{width: 100}} />);
});

expect(
selectableRoot.takeScreenshot().isEqual(plainRoot.takeScreenshot()),
).toBe(true);
expect(
selectableRoot.takeScreenshot().isEqual(emptyRoot.takeScreenshot()),
).toBe(false);
});

it('ends a cut last line with an ellipsis (plain text only)', () => {
const LONG_TEXT =
'The quick brown fox jumps over the lazy dog again and again';
const linesByText: {[name: string]: ReadonlyArray<string>} = {};
let blankLineLines: TextLayoutEvent['nativeEvent']['lines'] = [];
const reportLines =
(name: string) =>
(event: TextLayoutEvent): void => {
linesByText[name] = event.nativeEvent.lines.map(line => line.text);
if (name === 'blankLine') {
blankLineLines = event.nativeEvent.lines;
}
};

const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(
<View style={{width: 100}}>
<Text numberOfLines={2} onTextLayout={reportLines('cut')}>
{LONG_TEXT}
</Text>
<Text numberOfLines={2} onTextLayout={reportLines('fitting')}>
The quick fox
</Text>
<Text numberOfLines={2} onTextLayout={reportLines('inlineView')}>
{'The quick brown '}
<View style={{width: 10, height: 10}} />
{' fox jumps over the lazy dog again and again'}
</Text>
<Text numberOfLines={1} onTextLayout={reportLines('emoji')}>
{'\u{1F600}\u{1F600}\u{1F600} and more text after them'}
</Text>
<Text numberOfLines={2} onTextLayout={reportLines('blankLine')}>
{'Title\n\nMore details'}
</Text>
<Text numberOfLines={2} onTextLayout={reportLines('fittingLines')}>
{'Title\nMore'}
</Text>
<Text
numberOfLines={2}
onTextLayout={reportLines('trailingSpaces')}>
{`Title\nMore${' '.repeat(100)}\n`}
</Text>
<Text numberOfLines={1} onTextLayout={reportLines('clippedView')}>
{'Title\n'}
<View style={{width: 10, height: 10}} />
</Text>
</View>,
);
});
Fantom.flushAllNativeEvents();

expect(linesByText.cut).toHaveLength(2);
expect(linesByText.cut[1].endsWith('\u2026')).toBe(true);
expect(linesByText.fitting).toEqual(['The quick fox']);
// With an inline view or shaped emoji on the last line, the cut stays without an ellipsis.
expect(linesByText.inlineView).toHaveLength(2);
expect(linesByText.inlineView[1].endsWith('\u2026')).toBe(false);
expect(linesByText.emoji).toHaveLength(1);
expect(linesByText.emoji[0].endsWith('\u2026')).toBe(false);
// A blank line cut by a hard break still marks the cut.
expect(linesByText.blankLine).toEqual(['Title', '\u2026']);
// The ellipsis on a blank line has the metrics of its font, like the line above.
expect(blankLineLines[1].capHeight).toBeCloseTo(
blankLineLines[0].capHeight,
);
expect(blankLineLines[1].xHeight).toBeCloseTo(blankLineLines[0].xHeight);
expect(blankLineLines[1].xHeight).toBeGreaterThan(0);
expect(linesByText.fittingLines).toEqual(['Title', 'More']);
// Spaces wrapped before a final line break cut no text.
expect(linesByText.trailingSpaces).toEqual(['Title', 'More']);
// An inline view on a line that was cut away does not stop the ellipsis.
expect(linesByText.clippedView).toEqual(['Title\u2026']);
});

it('draws the ellipsis after a cut emoji in a font that has it', () => {
const renderText = (
root: ReturnType<typeof Fantom.createRoot>,
text: string,
numberOfLines: number,
): ReadonlyArray<string> => {
let reportedLines: ReadonlyArray<string> = [];
Fantom.runTask(() => {
root.render(
<View style={{width: 200, alignItems: 'flex-start'}}>
<Text
numberOfLines={numberOfLines}
onTextLayout={event => {
reportedLines = event.nativeEvent.lines.map(
line => line.text,
);
}}>
{text}
</Text>
</View>,
);
});
Fantom.flushAllNativeEvents();
return reportedLines;
};

const cutRoot = Fantom.createRoot();
const [cutLine] = renderText(
cutRoot,
'Nice \u{1F600}\nmore words after the line break',
1,
);
const typedRoot = Fantom.createRoot();
renderText(typedRoot, 'Nice \u{1F600}\u2026', 1);

expect(cutLine).toBe('Nice \u{1F600}\u2026');
expect(cutRoot.takeScreenshot().isEqual(typedRoot.takeScreenshot())).toBe(
true,
);
});

it('reports the same lines for a cut text that shrinks to fit', () => {
const LONG_TEXT =
'The quick brown fox jumps over the lazy dog again and again';
const cases = Array.from({length: 61}, (_, i) => ({
numberOfLines: 1 + (i % 2),
width: 80 + i,
stretchedLines: [] as Array<string>,
shrunkLines: [] as Array<string>,
}));
const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(
<>
{cases.map(testCase => (
<React.Fragment key={testCase.width}>
<View style={{width: testCase.width}}>
<Text
numberOfLines={testCase.numberOfLines}
onTextLayout={event => {
testCase.stretchedLines = event.nativeEvent.lines.map(
line => line.text,
);
}}>
{LONG_TEXT}
</Text>
</View>
<View style={{width: testCase.width, alignItems: 'flex-start'}}>
<Text
numberOfLines={testCase.numberOfLines}
onTextLayout={event => {
testCase.shrunkLines = event.nativeEvent.lines.map(
line => line.text,
);
}}>
{LONG_TEXT}
</Text>
</View>
</React.Fragment>
))}
</>,
);
});
Fantom.flushAllNativeEvents();

for (const {numberOfLines, stretchedLines, shrunkLines} of cases) {
expect(stretchedLines).toHaveLength(numberOfLines);
expect(stretchedLines[numberOfLines - 1].endsWith('\u2026')).toBe(true);
expect(shrunkLines).toEqual(stretchedLines);
}
});
});

describe('ref', () => {
it('is a element node', () => {
const elementRef = createRef<HostInstance>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ TextMeasurement TextLayoutManager::measure(
.attachments = attachments};
}

LinesMeasurements TextLayoutManager::measureLines(
const AttributedStringBox& /*attributedStringBox*/,
const ParagraphAttributes& /*paragraphAttributes*/,
const Size& /*size*/) const {
return {};
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class TextLayoutManager {
const TextLayoutContext &layoutContext,
const LayoutConstraints &layoutConstraints) const;

/*
* Measures the lines of `attributedStringBox` laid out at `size`. This
* implementation does not lay text out and returns no lines; a subclass that
* does, such as Fantom's `FantomTextLayoutManager`, overrides it.
*/
virtual LinesMeasurements measureLines(
const AttributedStringBox &attributedStringBox,
const ParagraphAttributes &paragraphAttributes,
const Size &size) const;

protected:
std::shared_ptr<const ContextContainer> contextContainer_;
TextMeasureCache textMeasureCache_;
Expand Down
8 changes: 6 additions & 2 deletions private/react-native-fantom/__docs__/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ without custom path mappings. Keep a fixture alongside the test that uses it.
Default color emoji selection follows Unicode 16.0 `Emoji_Presentation`.
Explicit text and emoji variation selectors override that default. Selectors and
keycap sequences stay with their bases during fallback and wrapping; HarfBuzz
shapes color emoji sequences for both measurement and painting. General script
shaping, text decorations and ellipsis are not implemented.
shapes color emoji sequences for both measurement and painting. With the default
`ellipsizeMode` (`tail`), text cut by `numberOfLines` ends with a tail ellipsis,
except when its last visible line has an inline view or shaped emoji, or when
only spaces were cut. A trailing line break adds no empty last line. General
script shaping, text decorations and head or middle ellipsis are not
implemented.

### Public native build

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Object {

exports[`Fantom screen snapshots captures the feed and its scrolled viewport 1`] = `
Object {
"hash": "a71fd606e25d99e9afc33d276138ae2db1d42352ec0995d23bdaefab811eba17",
"hash": "16b6dd92373af0ffaa0b670193ed834214e9c14f21d5b3cf693138e977141112",
"type": "Screenshot",
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <react/renderer/components/image/ImageProps.h>
#include <react/renderer/components/image/ImageState.h>
#include <react/renderer/components/scrollview/ScrollViewState.h>
#include <react/renderer/components/text/ParagraphProps.h>
#include <react/renderer/components/text/ParagraphState.h>
#include <react/renderer/core/ConcreteState.h>
#include <algorithm>
Expand Down Expand Up @@ -314,7 +315,7 @@ void paintView(
}

const std::string_view name(view.componentName ? view.componentName : "");
if (name == "Paragraph" && view.state) {
if ((name == "Paragraph" || name == "SelectableParagraph") && view.state) {
const auto& state =
static_cast<const ConcreteState<ParagraphState>&>(*view.state)
.getData();
Expand All @@ -327,7 +328,9 @@ void paintView(
layoutText(
&canvas,
state.attributedString,
state.paragraphAttributes,
// As on iOS, from the props: the state keeps the attributes it was
// created with when only they change.
static_cast<const ParagraphProps&>(*view.props).paragraphAttributes,
frame.size.width),
frame.size.width);
canvas.restore();
Expand Down
Loading
Loading