Skip to content

Commit

Permalink
Add test for rendering each latex symbol.
Browse files Browse the repository at this point in the history
Fix the types of some close symbols.
  • Loading branch information
kostub committed Aug 9, 2016
1 parent 2ee006f commit e5cda29
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
3 changes: 3 additions & 0 deletions iosMath/lib/MTMathAtomFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ FOUNDATION_EXPORT NSString *const MTSymbolDegree;
*/
+ (NSString*) latexSymbolNameForAtom:(MTMathAtom*) atom;

/** Returns a list of all supported lated symbols names. */
+ (NSArray<NSString*>*) supportedLatexSymbolNames;

/** Deprecated. Use (MTLargeOperator *)operatorWithName:(NSString *)name limits:(bool) limits
instead. This sets the limits to false. */
+ (MTMathAtom *) operatorWithName:(NSString*) name __deprecated;
Expand Down
19 changes: 12 additions & 7 deletions iosMath/lib/MTMathAtomFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ + (NSString*) latexSymbolNameForAtom:(MTMathAtom*) atom
return dict[atom.nucleus];
}

+ (NSArray<NSString *> *)supportedLatexSymbolNames
{
NSDictionary<NSString*, MTMathAtom*>* commands = [MTMathAtomFactory supportedLatexSymbols];
return commands.allKeys;
}

+ (MTAccent*) accentWithName:(NSString*) accentName
{
NSDictionary<NSString*, NSString*> *accents = [MTMathAtomFactory accents];
Expand Down Expand Up @@ -229,9 +235,9 @@ + (MTFraction *)fractionWithNumeratorStr:(NSString *)numStr denominatorStr:(NSSt
return [self fractionWithNumerator:num denominator:denom];
}

+ (NSDictionary*) supportedLatexSymbols
+ (NSDictionary<NSString*, MTMathAtom*>*) supportedLatexSymbols
{
static NSDictionary* commands = nil;
static NSDictionary<NSString*, MTMathAtom*>* commands = nil;
if (!commands) {
commands = @{
@"square" : [MTMathAtomFactory placeholder],
Expand Down Expand Up @@ -291,10 +297,10 @@ + (NSDictionary*) supportedLatexSymbols
@"lgroup" : [MTMathAtom atomWithType:kMTMathAtomOpen value:@"\u27EE"],

// Close
@"rceil" : [MTMathAtom atomWithType:kMTMathAtomOpen value:@"\u2309"],
@"rfloor" : [MTMathAtom atomWithType:kMTMathAtomOpen value:@"\u230B"],
@"rangle" : [MTMathAtom atomWithType:kMTMathAtomOpen value:@"\u27E9"],
@"rgroup" : [MTMathAtom atomWithType:kMTMathAtomOpen value:@"\u27EF"],
@"rceil" : [MTMathAtom atomWithType:kMTMathAtomClose value:@"\u2309"],
@"rfloor" : [MTMathAtom atomWithType:kMTMathAtomClose value:@"\u230B"],
@"rangle" : [MTMathAtom atomWithType:kMTMathAtomClose value:@"\u27E9"],
@"rgroup" : [MTMathAtom atomWithType:kMTMathAtomClose value:@"\u27EF"],

// Arrows
@"leftarrow" : [MTMathAtom atomWithType:kMTMathAtomRelation value:@"\u2190"],
Expand Down Expand Up @@ -456,7 +462,6 @@ + (NSDictionary*) supportedLatexSymbols
@"angstrom" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u00C5"],
@"|" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u2016"],
@"vert" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"|"],
@"prime" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u2032"],
@"ldots" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u2026"],
@"prime" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u2032"],
@"hbar" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\u210F"],
Expand Down
60 changes: 60 additions & 0 deletions iosMathTests/MTTypesetterTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1279,4 +1279,64 @@ - (void) testMathTable
}
}
}

- (void) testLatexSymbols
{
// Test all latex symbols
NSArray<NSString*>* allSymbols = [MTMathAtomFactory supportedLatexSymbolNames];
for (NSString* symName in allSymbols) {
MTMathList* list = [[MTMathList alloc] init];
MTMathAtom* atom = [MTMathAtomFactory atomForLatexSymbolName:symName];
XCTAssertNotNil(atom);
if (atom.type == kMTMathAtomSpace) {
// Skip spaces as they don't get rendered.
continue;
}

[list addAtom:atom];

MTMathListDisplay* display = [MTTypesetter createLineForMathList:list font:self.font style:kMTLineStyleDisplay];
XCTAssertNotNil(display, @"Symbol %@", symName);

XCTAssertEqual(display.type, kMTLinePositionRegular);
XCTAssertTrue(CGPointEqualToPoint(display.position, CGPointZero));
XCTAssertEqualNSRange(display.range, NSMakeRange(0, 1));
XCTAssertFalse(display.hasScript);
XCTAssertEqual(display.index, NSNotFound);
XCTAssertEqual(display.subDisplays.count, 1, @"Symbol %@", symName);

MTDisplay* sub0 = display.subDisplays[0];
if (atom.type == kMTMathAtomLargeOperator && atom.nucleus.length == 1) {
// These large operators are rendered differently;
XCTAssertTrue([sub0 isKindOfClass:[MTLargeGlyphDisplay class]]);
MTLargeGlyphDisplay* glyph = (MTLargeGlyphDisplay*) sub0;
XCTAssertEqualsCGPoint(glyph.position, CGPointZero, 0.01);
XCTAssertEqualNSRange(glyph.range, NSMakeRange(0, 1));
XCTAssertFalse(glyph.hasScript);
} else {
XCTAssertTrue([sub0 isKindOfClass:[MTCTLineDisplay class]], @"Symbol %@", symName);
MTCTLineDisplay* line = (MTCTLineDisplay*) sub0;
XCTAssertEqual(line.atoms.count, 1);
if (atom.type != kMTMathAtomVariable) {
XCTAssertEqualObjects(line.attributedString.string, atom.nucleus);
}
XCTAssertTrue(CGPointEqualToPoint(line.position, CGPointZero));
XCTAssertEqualNSRange(line.range, NSMakeRange(0, 1));
XCTAssertFalse(line.hasScript);
}

// dimensions
XCTAssertEqual(display.ascent, sub0.ascent);
XCTAssertEqual(display.descent, sub0.descent);
XCTAssertEqual(display.width, sub0.width);

// All chars will occupy some space.
if (![atom.nucleus isEqualToString:@" "]) {
// all chars except space have height
XCTAssertGreaterThan(display.ascent + display.descent, 0, @"Symbol %@", symName);
}
// all chars have a width.
XCTAssertGreaterThan(display.width, 0);
}
}
@end

0 comments on commit e5cda29

Please sign in to comment.