Skip to content

Commit

Permalink
Remove todos, fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLoer committed Jul 24, 2018
1 parent 13eff18 commit 1c43c07
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 72 deletions.
7 changes: 7 additions & 0 deletions docs/components/expression-metadata.js
Expand Up @@ -134,6 +134,13 @@ const types = {
collator: [{
type: 'collator',
parameters: [ '{ "case-sensitive": boolean, "diacritic-sensitive": boolean, "locale": string }' ]
}],
formatted: [{
type: 'formatted',
parameters: [
'string',
'{ "font-scale": number, "text-font": array<string> }'
]
}]
};

Expand Down
3 changes: 2 additions & 1 deletion src/style-spec/expression/definitions/formatted.js
Expand Up @@ -95,7 +95,8 @@ export class FormattedExpression implements Expression {
}

possibleOutputs() {
// TODO: think about right answer here
// Technically the combinatoric set of all children
// Usually, this.text will be undefined anyway
return [undefined];
}

Expand Down
1 change: 0 additions & 1 deletion src/style-spec/expression/parsing_context.js
Expand Up @@ -114,7 +114,6 @@ class ParsingContext {
parsed = new Coercion(expected, [parsed]);
}
} else if (expected.kind === 'formatted' && (actual.kind === 'value' || actual.kind === 'string')) {
// TODO: this isn't working yet
if (!options.omitTypeAnnotations) {
parsed = new Coercion(expected, [parsed]);
}
Expand Down
3 changes: 2 additions & 1 deletion src/symbol/quads.js
Expand Up @@ -131,7 +131,8 @@ export function getGlyphQuads(anchor: Anchor,

for (let k = 0; k < positionedGlyphs.length; k++) {
const positionedGlyph = positionedGlyphs[k];
const glyph = positions[positionedGlyph.fontStack][positionedGlyph.glyph];
const glyphPositions = positions[positionedGlyph.fontStack];
const glyph = glyphPositions && glyphPositions[positionedGlyph.glyph];
if (!glyph) continue;

const rect = glyph.rect;
Expand Down
25 changes: 15 additions & 10 deletions src/symbol/shaping.js
Expand Up @@ -26,7 +26,7 @@ export type PositionedGlyph = {
y: number,
vertical: boolean,
scale: number,
fontStack: string // TODO: either don't store per glyph or reduce to a number?
fontStack: string
};

// A collection of positioned glyphs and some metadata
Expand All @@ -53,14 +53,14 @@ class TaggedString {
this.sections = [];
}

static fromFeature(text: string | Formatted, rootFontStack: string) {
static fromFeature(text: string | Formatted, defaultFontStack: string) {
const result = new TaggedString();
if (text instanceof Formatted) {
for (let i = 0; i < text.sections.length; i++) {
const section = text.sections[i];
result.sections.push({
scale: section.scale || 1,
fontStack: section.fontStack || rootFontStack
fontStack: section.fontStack || defaultFontStack
});
result.text += section.text;
for (let j = 0; j < section.text.length; j++) {
Expand All @@ -69,7 +69,7 @@ class TaggedString {
}
} else {
result.text = text;
result.sections.push({ scale: 1, fontStack: rootFontStack });
result.sections.push({ scale: 1, fontStack: defaultFontStack });
for (let i = 0; i < text.length; i++) {
result.sectionIndex.push(0);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ function breakLines(input: TaggedString, lineBreakPoints: Array<number>): Array<

function shapeText(text: string | Formatted,
glyphs: {[string]: {[number]: ?StyleGlyph}},
rootFontStack: string,
defaultFontStack: string,
maxWidth: number,
lineHeight: number,
textAnchor: SymbolAnchor,
Expand All @@ -153,7 +153,7 @@ function shapeText(text: string | Formatted,
translate: [number, number],
verticalHeight: number,
writingMode: 1 | 2): Shaping | false {
const logicalInput = TaggedString.fromFeature(text, rootFontStack);
const logicalInput = TaggedString.fromFeature(text, defaultFontStack);

if (writingMode === WritingMode.vertical) {
logicalInput.verticalizePunctuation();
Expand Down Expand Up @@ -212,6 +212,7 @@ function shapeText(text: string | Formatted,
if (!positionedGlyphs.length)
return false;

shaping.text = shaping.text.toString();
return shaping;
}

Expand Down Expand Up @@ -252,7 +253,8 @@ function determineAverageLineWidth(logicalInput: TaggedString,

for (let index = 0; index < logicalInput.length(); index++) {
const section = logicalInput.getSection(index);
const glyph = glyphMap[section.fontStack][logicalInput.getCharCode(index)];
const positions = glyphMap[section.fontStack];
const glyph = positions && positions[logicalInput.getCharCode(index)];
if (!glyph)
continue;
totalWidth += glyph.metrics.advance * section.scale + spacing;
Expand Down Expand Up @@ -361,7 +363,8 @@ function determineLineBreaks(logicalInput: TaggedString,
for (let i = 0; i < logicalInput.length(); i++) {
const section = logicalInput.getSection(i);
const codePoint = logicalInput.getCharCode(i);
const glyph = glyphMap[section.fontStack][codePoint];
const positions = glyphMap[section.fontStack];
const glyph = positions && positions[codePoint];

if (glyph && !whitespace[codePoint])
currentX += glyph.metrics.advance * section.scale + spacing;
Expand Down Expand Up @@ -465,7 +468,8 @@ function shapeLines(shaping: Shaping,
// at 24 points, we can calculate how much it will move when
// we scale up or down.
const baselineOffset = (lineMaxScale - section.scale) * 24;
const glyph = glyphMap[section.fontStack][codePoint];
const positions = glyphMap[section.fontStack];
const glyph = positions && positions[codePoint];

if (!glyph) continue;

Expand Down Expand Up @@ -512,7 +516,8 @@ function justifyLine(positionedGlyphs: Array<PositionedGlyph>,
return;

const lastPositionedGlyph = positionedGlyphs[end];
const glyph = glyphMap[lastPositionedGlyph.fontStack][lastPositionedGlyph.glyph];
const positions = glyphMap[lastPositionedGlyph.fontStack];
const glyph = positions && positions[lastPositionedGlyph.glyph];
if (glyph) {
const lastAdvance = glyph.metrics.advance * lastPositionedGlyph.scale;
const lineIndent = (positionedGlyphs[end].x + lastAdvance) * justify;
Expand Down
22 changes: 16 additions & 6 deletions test/expected/text-shaping-default.json
Expand Up @@ -4,31 +4,41 @@
"glyph": 97,
"x": -32.5,
"y": -17,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 98,
"x": -19.5,
"y": -17,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 99,
"x": -5.5,
"y": -17,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 100,
"x": 5.5,
"y": -17,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 101,
"x": 19.5,
"y": -17,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
}
],
"text": "abcde",
Expand All @@ -37,4 +47,4 @@
"left": -32.5,
"right": 32.5,
"writingMode": 1
}
}
40 changes: 30 additions & 10 deletions test/expected/text-shaping-linebreak.json
Expand Up @@ -4,61 +4,81 @@
"glyph": 97,
"x": -32.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 98,
"x": -19.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 99,
"x": -5.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 100,
"x": 5.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 101,
"x": 19.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 97,
"x": -32.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 98,
"x": -19.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 99,
"x": -5.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 100,
"x": 5.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 101,
"x": 19.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
}
],
"text": "abcde abcde",
Expand Down
40 changes: 30 additions & 10 deletions test/expected/text-shaping-newline.json
Expand Up @@ -4,61 +4,81 @@
"glyph": 97,
"x": -32.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 98,
"x": -19.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 99,
"x": -5.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 100,
"x": 5.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 101,
"x": 19.5,
"y": -29,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 97,
"x": -32.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 98,
"x": -19.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 99,
"x": -5.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 100,
"x": 5.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
},
{
"glyph": 101,
"x": 19.5,
"y": -5,
"vertical": false
"vertical": false,
"scale": 1,
"fontStack": "Test"
}
],
"text": "abcde\nabcde",
Expand Down

0 comments on commit 1c43c07

Please sign in to comment.