Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returns "formatted" type with formatting annotations applied to subsections. "text-field" now accepts formatted text, allowing symbols to use multiple fonts within the same label.
- Loading branch information
Showing
44 changed files
with
863 additions
and
147 deletions.
There are no files selected for viewing
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
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
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
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
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,145 @@ | ||
// @flow | ||
|
||
import { NumberType, ValueType, FormattedType } from '../types'; | ||
|
||
|
||
import type { Expression } from '../expression'; | ||
import type EvaluationContext from '../evaluation_context'; | ||
import type ParsingContext from '../parsing_context'; | ||
import type { Type } from '../types'; | ||
|
||
export class FormattedSection { | ||
text: string | ||
scale: number | null | ||
fontStack: string | null | ||
|
||
constructor(text: string, scale: number | null, fontStack: string | null) { | ||
this.text = text; | ||
this.scale = scale; | ||
this.fontStack = fontStack; | ||
} | ||
} | ||
|
||
export class Formatted { | ||
sections: Array<FormattedSection> | ||
|
||
constructor(sections: Array<FormattedSection>) { | ||
this.sections = sections; | ||
} | ||
|
||
toString(): string { | ||
return this.sections.map(section => section.text).join(''); | ||
} | ||
|
||
serialize() { | ||
const serialized = ["format"]; | ||
for (const section of this.sections) { | ||
serialized.push(section.text); | ||
const fontStack = section.fontStack ? | ||
["literal", section.fontStack.split(',')] : | ||
null; | ||
serialized.push({ "text-font": fontStack, "font-scale": section.scale }); | ||
} | ||
return serialized; | ||
} | ||
} | ||
|
||
type FormattedSectionExpression = { | ||
text: Expression, | ||
scale: Expression | null; | ||
font: Expression | null; | ||
} | ||
|
||
export class FormattedExpression implements Expression { | ||
type: Type; | ||
sections: Array<FormattedSectionExpression>; | ||
|
||
constructor(sections: Array<FormattedSectionExpression>) { | ||
this.type = FormattedType; | ||
this.sections = sections; | ||
} | ||
|
||
static parse(args: Array<mixed>, context: ParsingContext): ?Expression { | ||
if (args.length < 3) { | ||
return context.error(`Expected at least two arguments.`); | ||
} | ||
|
||
if ((args.length - 1) % 2 !== 0) { | ||
return context.error(`Expected an even number of arguments.`); | ||
} | ||
|
||
const sections: Array<FormattedSectionExpression> = []; | ||
for (let i = 1; i < args.length - 1; i += 2) { | ||
const text = context.parse(args[i], 1, ValueType); | ||
if (!text) return null; | ||
const kind = text.type.kind; | ||
if (kind !== 'string' && kind !== 'value' && kind !== 'null') | ||
return context.error(`Formatted text type must be 'string', 'value', or 'null'.`); | ||
|
||
const options = (args[i + 1]: any); | ||
if (typeof options !== "object" || Array.isArray(options)) | ||
return context.error(`Format options argument must be an object.`); | ||
|
||
let scale = null; | ||
if (options['font-scale']) { | ||
scale = context.parse(options['font-scale'], 1, NumberType); | ||
if (!scale) return null; | ||
} | ||
|
||
let font = null; | ||
if (options['text-font']) { | ||
font = context.parse(options['text-font'], 1, ValueType); // Require array of strings? | ||
if (!font) return null; | ||
} | ||
sections.push({text, scale, font}); | ||
} | ||
|
||
return new FormattedExpression(sections); | ||
} | ||
|
||
evaluate(ctx: EvaluationContext) { | ||
return new Formatted( | ||
this.sections.map(section => | ||
new FormattedSection( | ||
section.text.evaluate(ctx) || "", | ||
section.scale ? section.scale.evaluate(ctx) : null, | ||
section.font ? section.font.evaluate(ctx).join(',') : null | ||
) | ||
) | ||
); | ||
} | ||
|
||
eachChild(fn: (Expression) => void) { | ||
for (const section of this.sections) { | ||
fn(section.text); | ||
if (section.scale) { | ||
fn(section.scale); | ||
} | ||
if (section.font) { | ||
fn(section.font); | ||
} | ||
} | ||
} | ||
|
||
possibleOutputs() { | ||
// Technically the combinatoric set of all children | ||
// Usually, this.text will be undefined anyway | ||
return [undefined]; | ||
} | ||
|
||
serialize() { | ||
const serialized = ["format"]; | ||
for (const section of this.sections) { | ||
serialized.push(section.text.serialize()); | ||
const options = {}; | ||
if (section.scale) { | ||
options['font-scale'] = section.scale.serialize(); | ||
} | ||
if (section.font) { | ||
options['text-font'] = section.font.serialize(); | ||
} | ||
serialized.push(options); | ||
} | ||
return serialized; | ||
} | ||
} |
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
Oops, something went wrong.