Skip to content

Commit

Permalink
Parsing for style commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
kostub committed Aug 10, 2016
1 parent c89cba6 commit 8941815
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
6 changes: 6 additions & 0 deletions iosMath/lib/MTMathAtomFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,12 @@ + (MTFraction *)fractionWithNumeratorStr:(NSString *)numStr denominatorStr:(NSSt
@"!" : [[MTMathSpace alloc] initWithSpace:-3],
@"quad" : [[MTMathSpace alloc] initWithSpace:18], // quad = 1em = 18mu
@"qquad" : [[MTMathSpace alloc] initWithSpace:36], // qquad = 2em

// Style
@"displaystyle" : [[MTMathStyle alloc] initWithStyle:kMTLineStyleDisplay],
@"textstyle" : [[MTMathStyle alloc] initWithStyle:kMTLineStyleText],
@"scriptstyle" : [[MTMathStyle alloc] initWithStyle:kMTLineStyleScript],
@"scriptscriptstyle" : [[MTMathStyle alloc] initWithStyle:kMTLineStyleScriptScript],
};

}
Expand Down
19 changes: 19 additions & 0 deletions iosMath/lib/MTMathListBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ + (NSDictionary*) spaceToCommands
return spaceToCommands;
}

+ (NSDictionary*) styleToCommands
{
static NSDictionary* styleToCommands = nil;
if (!styleToCommands) {
styleToCommands = @{
@(kMTLineStyleDisplay) : @"displaystyle",
@(kMTLineStyleText) : @"textstyle",
@(kMTLineStyleScript) : @"scriptstyle",
@(kMTLineStyleScriptScript) : @"scriptscriptstyle",
};
}
return styleToCommands;
}

+ (MTMathList *)buildFromString:(NSString *)str
{
MTMathListBuilder* builder = [[MTMathListBuilder alloc] initWithString:str];
Expand Down Expand Up @@ -503,6 +517,11 @@ + (NSString *)mathListToString:(MTMathList *)ml
} else {
[str appendFormat:@"\\mkern%.1fmu", space.space];
}
} else if (atom.type == kMTMathAtomStyle) {
MTMathStyle* style = (MTMathStyle*) atom;
NSDictionary* styleToCommands = [MTMathListBuilder styleToCommands];
NSString* command = styleToCommands[@(style.style)];
[str appendFormat:@"\\%@ ", command];
} else if (atom.nucleus.length == 0) {
[str appendString:@"{}"];
} else if ([atom.nucleus isEqualToString:@"\u2236"]) {
Expand Down
23 changes: 23 additions & 0 deletions iosMathTests/MTMathListBuilderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,29 @@ - (void) testMathSpace
XCTAssertEqualObjects(latex, @"\\! ", @"%@", desc);
}

- (void) testMathStyle
{
NSString *str = @"\\textstyle y \\scriptstyle x";
MTMathList* list = [MTMathListBuilder buildFromString:str];
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @4, @"%@", desc);
MTMathStyle* style = list.atoms[0];
XCTAssertEqual(style.type, kMTMathAtomStyle, @"%@", desc);
XCTAssertEqualObjects(style.nucleus, @"", @"%@", desc);
XCTAssertEqual(style.style, kMTLineStyleText);

MTMathStyle* style2 = list.atoms[2];
XCTAssertEqual(style2.type, kMTMathAtomStyle, @"%@", desc);
XCTAssertEqualObjects(style2.nucleus, @"", @"%@", desc);
XCTAssertEqual(style2.style, kMTLineStyleScript);

// convert it back to latex
NSString* latex = [MTMathListBuilder mathListToString:list];
XCTAssertEqualObjects(latex, @"\\textstyle y\\scriptstyle x", @"%@", desc);
}

static NSArray* getTestDataParseErrors() {
return @[
@[@"}a", @(MTParseErrorMismatchBraces)],
Expand Down
4 changes: 2 additions & 2 deletions iosMathTests/MTTypesetterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,8 @@ - (void) testLatexSymbols
MTMathList* list = [[MTMathList alloc] init];
MTMathAtom* atom = [MTMathAtomFactory atomForLatexSymbolName:symName];
XCTAssertNotNil(atom);
if (atom.type == kMTMathAtomSpace) {
// Skip spaces as they don't get rendered.
if (atom.type >= kMTMathAtomBoundary) {
// Skip these types as they aren't symbols.
continue;
}

Expand Down

0 comments on commit 8941815

Please sign in to comment.