Skip to content

Commit e9b6ec6

Browse files
ingun37kostub
authored andcommitted
add colorbox command (#121)
* add colorbox feature * change pod version 1.5.3 -> 1.7.0
1 parent ad2c515 commit e9b6ec6

File tree

8 files changed

+120
-5
lines changed

8 files changed

+120
-5
lines changed

Podfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ EXTERNAL SOURCES:
99
:path: "./"
1010

1111
SPEC CHECKSUMS:
12-
iosMath: dfb93f207b01838413242bb5252b3eaf91558a12
12+
iosMath: f7a6cbadf9d836d2149c2a84c435b1effc244cba
1313

1414
PODFILE CHECKSUM: bade56080a0531a08830155fc215a0a5b44dd183
1515

16-
COCOAPODS: 1.5.3
16+
COCOAPODS: 1.7.0

iosMath/lib/MTMathList.h

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef NS_ENUM(NSUInteger, MTMathAtomType)
7373
/// Denotes style changes during rendering.
7474
kMTMathAtomStyle,
7575
kMTMathAtomColor,
76+
kMTMathAtomColorbox,
7677

7778
// Atoms after this point are not part of TeX and do not have the usual structure.
7879

@@ -340,6 +341,23 @@ typedef NS_ENUM(unsigned int, MTLineStyle) {
340341

341342
@end
342343

344+
/** An atom representing an colorbox element.
345+
@note None of the usual fields of the `MTMathAtom` apply even though this
346+
class inherits from `MTMathAtom`. i.e. it is meaningless to have a value
347+
in the nucleus, subscript or superscript fields. */
348+
@interface MTMathColorbox : MTMathAtom
349+
350+
/// Creates an empty color with a nil environment
351+
- (instancetype) init NS_DESIGNATED_INITIALIZER;
352+
353+
/** The style represented by this object. */
354+
@property (nonatomic, nullable) NSString* colorString;
355+
356+
/// The inner math list
357+
@property (nonatomic, nullable) MTMathList* innerList;
358+
359+
@end
360+
343361
/** An atom representing an table element. This atom is not like other
344362
atoms and is not present in TeX. We use it to represent the `\halign` command
345363
in TeX with some simplifications. This is used for matrices, equation

iosMath/lib/MTMathList.m

+51
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static BOOL isNotBinaryOperator(MTMathAtom* prevNode)
6868
return @"Style";
6969
case kMTMathAtomColor:
7070
return @"Color";
71+
case kMTMathAtomColorbox:
72+
return @"Colorbox";
7173
case kMTMathAtomTable:
7274
return @"Table";
7375
}
@@ -122,6 +124,9 @@ + (instancetype)atomWithType:(MTMathAtomType)type value:(NSString *)value
122124
case kMTMathAtomColor:
123125
return [[MTMathColor alloc] init];
124126

127+
case kMTMathAtomColorbox:
128+
return [[MTMathColorbox alloc] init];
129+
125130
default:
126131
return [[MTMathAtom alloc] initWithType:type value:value];
127132
}
@@ -702,6 +707,52 @@ - (instancetype)finalized
702707

703708
@end
704709

710+
#pragma mark - MTMathColorbox
711+
712+
@implementation MTMathColorbox
713+
714+
715+
- (instancetype)init
716+
{
717+
self = [super initWithType:kMTMathAtomColorbox value:@""];
718+
return self;
719+
}
720+
721+
- (instancetype)initWithType:(MTMathAtomType)type value:(NSString *)value
722+
{
723+
if (type == kMTMathAtomColorbox) {
724+
return [self init];
725+
}
726+
@throw [NSException exceptionWithName:@"InvalidMethod"
727+
reason:@"[MTMathColorbox initWithType:value:] cannot be called. Use [MTMathColorbox init] instead."
728+
userInfo:nil];
729+
}
730+
731+
- (NSString *)stringValue
732+
{
733+
NSMutableString* str = [NSMutableString stringWithString:@"\\colorbox"];
734+
[str appendFormat:@"{%@}{%@}", self.colorString, self.innerList.stringValue];
735+
return str;
736+
}
737+
738+
- (id)copyWithZone:(NSZone *)zone
739+
{
740+
MTMathColorbox* op = [super copyWithZone:zone];
741+
op.innerList = [self.innerList copyWithZone:zone];
742+
op->_colorString = self.colorString;
743+
return op;
744+
}
745+
746+
- (instancetype)finalized
747+
{
748+
MTMathColorbox *newInner = [super finalized];
749+
newInner.innerList = newInner.innerList.finalized;
750+
return newInner;
751+
}
752+
753+
@end
754+
755+
705756
#pragma mark - MTMathTable
706757

707758
@interface MTMathTable ()

iosMath/lib/MTMathListBuilder.m

+6
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ - (MTMathAtom*) atomForCommand:(NSString*) command
500500
mathColor.colorString = [self readColor];
501501
mathColor.innerList = [self buildInternal:true];
502502
return mathColor;
503+
} else if ([command isEqualToString:@"colorbox"]) {
504+
// A color command has 2 arguments
505+
MTMathColorbox* mathColorbox = [[MTMathColorbox alloc] init];
506+
mathColorbox.colorString = [self readColor];
507+
mathColorbox.innerList = [self buildInternal:true];
508+
return mathColorbox;
503509
} else {
504510
NSString* errorMessage = [NSString stringWithFormat:@"Invalid command \\%@", command];
505511
[self setError:MTParseErrorInvalidCommand message:errorMessage];

iosMath/render/MTMathListDisplay.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ NS_ASSUME_NONNULL_BEGIN
5252
// The local color, if the color was mutated local with the color
5353
// command
5454
@property (nonatomic, nullable) MTColor *localTextColor;
55-
55+
/// The background color for this display.
56+
@property (nonatomic, nullable) MTColor *localBackgroundColor;
5657
@end
5758

5859
/// A rendering of a single CTLine as an MTDisplay

iosMath/render/MTMathListDisplay.m

+17
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ @implementation MTDisplay
4545

4646
- (void)draw:(CGContextRef)context
4747
{
48+
if (self.localBackgroundColor != nil) {
49+
CGContextSaveGState(context);
50+
CGContextSetBlendMode(context, kCGBlendModeNormal);
51+
CGContextSetFillColorWithColor(context, self.localBackgroundColor.CGColor);
52+
CGContextFillRect(context, [self displayBounds]);
53+
CGContextRestoreGState(context);
54+
}
55+
4856
}
4957

5058
- (CGRect) displayBounds
@@ -154,6 +162,7 @@ - (void)dealloc
154162

155163
- (void)draw:(CGContextRef)context
156164
{
165+
[super draw:context];
157166
CGContextSaveGState(context);
158167

159168
CGContextSetTextPosition(context, self.position.x, self.position.y);
@@ -212,6 +221,7 @@ - (void)setTextColor:(MTColor *)textColor
212221

213222
- (void)draw:(CGContextRef)context
214223
{
224+
[super draw:context];
215225
CGContextSaveGState(context);
216226

217227
// Make the current position the origin as all the positions of the sub atoms are relative to the origin.
@@ -324,6 +334,7 @@ - (void)setTextColor:(MTColor *)textColor
324334

325335
- (void)draw:(CGContextRef)context
326336
{
337+
[super draw:context];
327338
[_numerator draw:context];
328339
[_denominator draw:context];
329340

@@ -416,6 +427,7 @@ - (void)setTextColor:(MTColor *)textColor
416427

417428
- (void)draw:(CGContextRef)context
418429
{
430+
[super draw:context];
419431
// draw the radicand & degree at its position
420432
[self.radicand draw:context];
421433
[self.degree draw:context];
@@ -474,6 +486,7 @@ - (instancetype)initWithGlpyh:(CGGlyph) glyph range:(NSRange) range font:(MTFont
474486

475487
- (void)draw:(CGContextRef)context
476488
{
489+
[super draw:context];
477490
CGContextSaveGState(context);
478491

479492
[self.textColor setFill];
@@ -530,6 +543,7 @@ - (instancetype)initWithGlyphs:(NSArray<NSNumber *> *)glyphs offsets:(NSArray<NS
530543

531544
- (void)draw:(CGContextRef)context
532545
{
546+
[super draw:context];
533547
CGContextSaveGState(context);
534548

535549
[self.textColor setFill];
@@ -671,6 +685,7 @@ - (void)setTextColor:(MTColor *)textColor
671685

672686
- (void)draw:(CGContextRef)context
673687
{
688+
[super draw:context];
674689
// Draw the elements.
675690
[self.upperLimit draw:context];
676691
[self.lowerLimit draw:context];
@@ -703,6 +718,7 @@ - (void)setTextColor:(MTColor *)textColor
703718

704719
- (void)draw:(CGContextRef)context
705720
{
721+
[super draw:context];
706722
[self.inner draw:context];
707723

708724
CGContextSaveGState(context);
@@ -770,6 +786,7 @@ - (void) updateAccenteePosition
770786

771787
- (void)draw:(CGContextRef)context
772788
{
789+
[super draw:context];
773790
[self.accentee draw:context];
774791

775792
CGContextSaveGState(context);

iosMath/render/internal/MTTypesetter.m

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef NS_ENUM(int, MTInterElementSpaceType) {
4848
NSUInteger getInterElementSpaceArrayIndexForType(MTMathAtomType type, BOOL row) {
4949
switch (type) {
5050
case kMTMathAtomColor:
51+
case kMTMathAtomColorbox:
5152
case kMTMathAtomOrdinary:
5253
case kMTMathAtomPlaceholder: // A placeholder is treated as ordinary
5354
return 0;
@@ -629,6 +630,21 @@ - (void) createDisplayAtoms:(NSArray*) preprocessed
629630
break;
630631
}
631632

633+
case kMTMathAtomColorbox: {
634+
// stash the existing layout
635+
if (_currentLine.length > 0) {
636+
[self addDisplayLine];
637+
}
638+
MTMathColorbox* colorboxAtom = (MTMathColorbox*) atom;
639+
MTDisplay* display = [MTTypesetter createLineForMathList:colorboxAtom.innerList font:_font style:_style];
640+
641+
display.localBackgroundColor = [MTColor colorFromHexString:colorboxAtom.colorString];
642+
display.position = _currentPosition;
643+
_currentPosition.x += display.width;
644+
[_displayAtoms addObject:display];
645+
break;
646+
}
647+
632648
case kMTMathAtomRadical: {
633649
// stash the existing layout
634650
if (_currentLine.length > 0) {

iosMathExample/example/ViewController.m

+8-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ - (void)viewDidLoad
8282
// set the size of the content view
8383
// Disable horizontal scrolling.
8484
[self setEqualWidths:contentView andView:self.scrollView];
85-
[self setHeight:4280 forView:contentView];
85+
[self setHeight:4350 forView:contentView];
8686

8787

8888
// Demo formulae
@@ -303,7 +303,7 @@ - (void)viewDidLoad
303303
self.labels[39] = [self createMathLabel:@"\\hat{\\frac12} \\; \\hat{\\sqrt 3}" withHeight:50];
304304

305305
// large roots
306-
self.labels[40] = [self createMathLabel:@"\\sqrt{1+\\sqrt{1+\\sqrt{1+\\sqrt{1+\\sqrt{1+\\cdots}}}}}" withHeight:80];
306+
self.labels[40] = [self createMathLabel:@"\\colorbox{#f0f0e0}{\\sqrt{1+\\colorbox{#d0c0d0}{\\sqrt{1+\\colorbox{#a080c0}{\\sqrt{1+\\colorbox{#7050a0}{\\sqrt{1+\\colorbox{403060}{\\colorbox{#102000}{\\sqrt{1+\\cdots}}}}}}}}}}}" withHeight:80];
307307

308308
self.labels[41] = [self createMathLabel:@"\\begin{bmatrix}"
309309
"a & b\\\\ c & d \\\\ e & f \\\\ g & h \\\\ i & j"
@@ -314,6 +314,12 @@ - (void)viewDidLoad
314314
self.labels[44] = [self createMathLabel:@"\\mathrm{using\\ mathrm}" withHeight:30];
315315
self.labels[45] = [self createMathLabel:@"\\text{using text}" withHeight:30];
316316
self.labels[46] = [self createMathLabel:@"\\text{Mary has }\\$500 + \\$200." withHeight:30];
317+
318+
self.labels[47] = [self createMathLabel:@"\\colorbox{#888888}{\\begin{pmatrix}"
319+
"\\colorbox{#ff0000}{a} & \\colorbox{#00ff00}{b} \\\\"
320+
"\\colorbox{#00aaff}{c} & \\colorbox{#f0f0f0}{d}"
321+
"\\end{pmatrix}}"
322+
withHeight:70];
317323

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

0 commit comments

Comments
 (0)