Skip to content

Commit 80cdd96

Browse files
committed
Add support for the \text command.
Adds support for #12.
1 parent a66c6dd commit 80cdd96

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

iosMath/lib/MTMathAtomFactory.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ + (NSDictionary*) delimValueToName
868868
@"mathbb": @(kMTFontStyleBlackboard),
869869
@"mathbfit": @(kMTFontStyleBoldItalic),
870870
@"bm": @(kMTFontStyleBoldItalic),
871+
@"text": @(kMTFontStyleRoman),
871872
};
872873
}
873874
return fontStyles;

iosMath/lib/MTMathListBuilder.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ @implementation MTMathListBuilder {
4444
MTInner* _currentInnerAtom;
4545
MTEnvProperties* _currentEnv;
4646
MTFontStyle _currentFontStyle;
47+
BOOL _spacesAllowed;
4748
}
4849

4950
- (instancetype)initWithString:(NSString *)str
@@ -181,13 +182,18 @@ - (MTMathList*)buildInternal:(BOOL) oneCharOnly stopChar:(unichar) stop
181182
}
182183
MTFontStyle fontStyle = [MTMathAtomFactory fontStyleWithName:command];
183184
if (fontStyle != NSNotFound) {
185+
BOOL oldSpacesAllowed = _spacesAllowed;
186+
// Text has special consideration where it allows spaces without escaping.
187+
_spacesAllowed = [command isEqualToString:@"text"];
184188
MTFontStyle oldFontStyle = _currentFontStyle;
185189
_currentFontStyle = fontStyle;
186190
MTMathList* sublist = [self buildInternal:true];
187-
prevAtom = [sublist.atoms lastObject];
188-
[list append:sublist];
189191
// Restore the font style.
190192
_currentFontStyle = oldFontStyle;
193+
_spacesAllowed = oldSpacesAllowed;
194+
195+
prevAtom = [sublist.atoms lastObject];
196+
[list append:sublist];
191197
if (oneCharOnly) {
192198
return list;
193199
}
@@ -212,6 +218,9 @@ - (MTMathList*)buildInternal:(BOOL) oneCharOnly stopChar:(unichar) stop
212218
MTMathAtom* table = [self buildTable:nil firstList:list row:NO];
213219
return [MTMathList mathListWithAtoms:table, nil];
214220
}
221+
} else if (_spacesAllowed && ch == ' ') {
222+
// If spaces are allowed then spaces do not need escaping with a \ before being used.
223+
atom = [MTMathAtomFactory atomForLatexSymbolName:@" "];
215224
} else {
216225
atom = [MTMathAtomFactory atomForCharacter:ch];
217226
if (!atom) {

iosMath/render/internal/MTTypesetter.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ UTF32Char getDefaultStyle(unichar ch) {
217217
}
218218

219219
static const UTF32Char kMTUnicodeMathCapitalScriptStart = 0x1D49C;
220-
static const UTF32Char kMTUnicodeMathLowerScriptStart = 0x1D4B6;
220+
// TODO(kostub): Unused in Latin Modern Math - if another font is used determine if
221+
// this should be applicable.
222+
// static const UTF32Char kMTUnicodeMathLowerScriptStart = 0x1D4B6;
221223

222224
// mathcal/mathscr (caligraphic or script)
223225
UTF32Char getCaligraphic(unichar ch) {
@@ -252,8 +254,9 @@ UTF32Char getCaligraphic(unichar ch) {
252254
if (IS_UPPER_EN(ch)) {
253255
unicode = kMTUnicodeMathCapitalScriptStart + (ch - 'A');
254256
} else if (IS_LOWER_EN(ch)) {
255-
// TODO:(kostub) Latin Modern Math does not have lower case caligraphic characters.
256-
unicode = kMTUnicodeMathLowerScriptStart + (ch - 'a');
257+
// Latin Modern Math does not have lower case caligraphic characters, so we use
258+
// the default style instead of showing a ?
259+
unicode = getDefaultStyle(ch);
257260
} else {
258261
// Caligraphic characters don't exist for greek or numbers, we give them the
259262
// default treatment.

iosMathExample/example/ViewController.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,9 @@ - (void)viewDidLoad
308308
"\\end{bmatrix}"
309309
withHeight:120];
310310
self.labels[42] = [self createMathLabel:@"x{\\scriptstyle y}z" withHeight:30];
311-
self.labels[43] = [self createMathLabel:@"x \\mathrm x \\mathbf x \\mathcal X \\mathfrak x \\mathsf x \\bm x \\mathtt x \\mathit \\Lambda" withHeight:30];
312-
self.labels[44] = [self createMathLabel:@"\\mathrm{different\\ text}" withHeight:30];
311+
self.labels[43] = [self createMathLabel:@"x \\mathrm x \\mathbf x \\mathcal X \\mathfrak x \\mathsf x \\bm x \\mathtt x \\mathit \\Lambda \\cal g" withHeight:30];
312+
self.labels[44] = [self createMathLabel:@"\\mathrm{using\\ mathrm}" withHeight:30];
313+
self.labels[45] = [self createMathLabel:@"\\text{using text}" withHeight:30];
313314

314315
for (NSUInteger i = 1; i < self.labels.count; i++) {
315316
[self addLabelWithIndex:i inArray:self.labels toView:contentView];

iosMathTests/MTMathListBuilderTest.m

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,25 +1240,25 @@ - (void) testFontOneChar
12401240

12411241
- (void) testFontMultipleChars
12421242
{
1243-
NSString *str = @"\\frak{AB}";
1243+
NSString *str = @"\\frak{xy}";
12441244
MTMathList* list = [MTMathListBuilder buildFromString:str];
12451245
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];
12461246

12471247
XCTAssertNotNil(list, @"%@", desc);
12481248
XCTAssertEqualObjects(@(list.atoms.count), @2, @"%@", desc);
12491249
MTMathAtom* atom = list.atoms[0];
12501250
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
1251-
XCTAssertEqualObjects(atom.nucleus, @"A", @"%@", desc);
1251+
XCTAssertEqualObjects(atom.nucleus, @"x", @"%@", desc);
12521252
XCTAssertEqual(atom.fontStyle, kMTFontStyleFraktur);
12531253

12541254
atom = list.atoms[1];
12551255
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
1256-
XCTAssertEqualObjects(atom.nucleus, @"B", @"%@", desc);
1256+
XCTAssertEqualObjects(atom.nucleus, @"y", @"%@", desc);
12571257
XCTAssertEqual(atom.fontStyle, kMTFontStyleFraktur);
12581258

12591259
// convert it back to latex
12601260
NSString* latex = [MTMathListBuilder mathListToString:list];
1261-
XCTAssertEqualObjects(latex, @"\\mathfrak{AB}", @"%@", desc);
1261+
XCTAssertEqualObjects(latex, @"\\mathfrak{xy}", @"%@", desc);
12621262
}
12631263

12641264
- (void) testFontOneCharInside
@@ -1289,4 +1289,32 @@ - (void) testFontOneCharInside
12891289
NSString* latex = [MTMathListBuilder mathListToString:list];
12901290
XCTAssertEqualObjects(latex, @"\\sqrt{\\mathrm{x}}y", @"%@", desc);
12911291
}
1292+
1293+
- (void) testText
1294+
{
1295+
NSString *str = @"\\text{x y}";
1296+
MTMathList* list = [MTMathListBuilder buildFromString:str];
1297+
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];
1298+
1299+
XCTAssertNotNil(list, @"%@", desc);
1300+
XCTAssertEqualObjects(@(list.atoms.count), @3, @"%@", desc);
1301+
MTMathAtom* atom = list.atoms[0];
1302+
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
1303+
XCTAssertEqualObjects(atom.nucleus, @"x", @"%@", desc);
1304+
XCTAssertEqual(atom.fontStyle, kMTFontStyleRoman);
1305+
1306+
atom = list.atoms[1];
1307+
XCTAssertEqual(atom.type, kMTMathAtomOrdinary, @"%@", desc);
1308+
XCTAssertEqualObjects(atom.nucleus, @" ", @"%@", desc);
1309+
1310+
atom = list.atoms[2];
1311+
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
1312+
XCTAssertEqualObjects(atom.nucleus, @"y", @"%@", desc);
1313+
XCTAssertEqual(atom.fontStyle, kMTFontStyleRoman);
1314+
1315+
1316+
// convert it back to latex
1317+
NSString* latex = [MTMathListBuilder mathListToString:list];
1318+
XCTAssertEqualObjects(latex, @"\\mathrm{x\\ y}", @"%@", desc);
1319+
}
12921320
@end

0 commit comments

Comments
 (0)