Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer breaking lines at a zero width space. #8255

Merged
merged 3 commits into from
May 23, 2019
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
35 changes: 22 additions & 13 deletions src/symbol/shaping.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,18 @@ function calculateBadness(lineWidth: number,
return raggedness + Math.abs(penalty) * penalty;
}

function calculatePenalty(codePoint: number, nextCodePoint: number) {
function calculatePenalty(codePoint: number, nextCodePoint: number, penalizableIdeographicBreak: boolean) {
let penalty = 0;
// Force break on newline
if (codePoint === 0x0a) {
penalty -= 10000;
}
// Penalize breaks between characters that allow ideographic breaking because
// they are less preferable than breaks at spaces (or zero width spaces).
if (penalizableIdeographicBreak) {
penalty += 150;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the rendering test failures, it looks like this change regresses the balanced line breaking in #3743. The penalty is probably too high.

}

// Penalize open parenthesis at end of line
if (codePoint === 0x28 || codePoint === 0xff08) {
penalty += 50;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text-max-width/ideographic-punctuation-breaking test failure shows that this penalty is too low compared to the new penalty introduced above.

Expand Down Expand Up @@ -353,6 +359,8 @@ function determineLineBreaks(logicalInput: TaggedString,
const potentialLineBreaks = [];
const targetWidth = determineAverageLineWidth(logicalInput, spacing, maxWidth, glyphMap);

const hasServerSuggestedBreakpoints = logicalInput.text.indexOf("\u200b") >= 0;

let currentX = 0;

for (let i = 0; i < logicalInput.length(); i++) {
Expand All @@ -366,18 +374,19 @@ function determineLineBreaks(logicalInput: TaggedString,

// Ideographic characters, spaces, and word-breaking punctuation that often appear without
// surrounding spaces.
if ((i < logicalInput.length() - 1) &&
(breakable[codePoint] ||
charAllowsIdeographicBreaking(codePoint))) {

potentialLineBreaks.push(
evaluateBreak(
i + 1,
currentX,
targetWidth,
potentialLineBreaks,
calculatePenalty(codePoint, logicalInput.getCharCode(i + 1)),
false));
if ((i < logicalInput.length() - 1)) {
const ideographicBreak = charAllowsIdeographicBreaking(codePoint);
if (breakable[codePoint] || ideographicBreak) {

potentialLineBreaks.push(
evaluateBreak(
i + 1,
currentX,
targetWidth,
potentialLineBreaks,
calculatePenalty(codePoint, logicalInput.getCharCode(i + 1), ideographicBreak && hasServerSuggestedBreakpoints),
false));
}
}
}

Expand Down
123 changes: 123 additions & 0 deletions test/expected/text-shaping-zero-width-space.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"positionedGlyphs": [
{
"glyph": 19977,
"x": -63,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -42,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -21,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 0,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 21,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 42,
"y": -41,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -63,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -42,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -21,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 0,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 21,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 42,
"y": -17,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": -21,
"y": 7,
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 19977,
"x": 0,
"y": 7,
"vertical": false,
"scale": 1,
"fontStack": "Test"
}
],
"text": "三三​三三​三三​三三三三三三​三三",
"top": -36,
"bottom": 36,
"left": -63,
"right": 63,
"writingMode": 1,
"lineCount": 3
}
12 changes: 11 additions & 1 deletion test/fixtures/fontstack-glyphs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1918,5 +1918,15 @@
"top": -5,
"advance": 15
}
},
"19977": {
"id": 19977,
"metrics": {
"width": 18,
"height": 18,
"left": 2,
"top": -8,
"advance": 21
}
}
}
}
9 changes: 9 additions & 0 deletions test/unit/symbol/shaping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ test('shaping', (t) => {
if (UPDATE) fs.writeFileSync(path.join(__dirname, '/../../expected/text-shaping-newlines-in-middle.json'), JSON.stringify(shaped, null, 2));
t.deepEqual(shaped, expectedNewLinesInMiddle);

// Prefer zero width spaces when breaking lines. Zero width spaces are used by Mapbox data sources as a hint that
// a position is ideal for breaking.
const expectedZeroWidthSpaceBreak = JSON.parse(fs.readFileSync(path.join(__dirname, '/../../expected/text-shaping-zero-width-space.json')));

shaped = shaping.shapeText(Formatted.fromString('三三\u200b三三\u200b三三\u200b三三三三三三\u200b三三'), glyphs, fontStack, 5 * oneEm, oneEm, 'center', 'center', 0, [0, 0], WritingMode.horizontal);
if (UPDATE) fs.writeFileSync(path.join(__dirname, '/../../expected/text-shaping-zero-width-space.json'), JSON.stringify(shaped, null, 2));
t.deepEqual(shaped, expectedZeroWidthSpaceBreak);


// Null shaping.
shaped = shaping.shapeText(Formatted.fromString(''), glyphs, fontStack, 15 * oneEm, oneEm, 'center', 'center', 0 * oneEm, [0, 0], WritingMode.horizontal);
t.equal(false, shaped);
Expand Down