-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathENRMMathInlineRenderer.m
More file actions
68 lines (55 loc) · 1.83 KB
/
ENRMMathInlineRenderer.m
File metadata and controls
68 lines (55 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import "ENRMMathInlineRenderer.h"
#import "ENRMMathInlineAttachment.h"
#import "MarkdownASTNode.h"
#import "RenderContext.h"
#import "RendererFactory.h"
#import "StyleConfig.h"
#import "ENRMFeatureFlags.h"
#if ENRICHED_MARKDOWN_MATH
@implementation ENRMMathInlineRenderer {
__weak RendererFactory *_rendererFactory;
StyleConfig *_config;
}
- (instancetype)initWithRendererFactory:(id)rendererFactory config:(id)config
{
if (self = [super init]) {
_rendererFactory = rendererFactory;
_config = (StyleConfig *)config;
}
return self;
}
- (void)renderNode:(MarkdownASTNode *)node into:(NSMutableAttributedString *)output context:(RenderContext *)context
{
NSString *latex = [self extractLatexFromNode:node];
if (latex.length == 0) {
return;
}
NSDictionary *textAttrs = [context getTextAttributes];
UIFont *currentFont = textAttrs[NSFontAttributeName];
ENRMMathInlineAttachment *attachment = [[ENRMMathInlineAttachment alloc] init];
attachment.latex = latex;
attachment.fontSize = currentFont ? currentFont.pointSize : [_config paragraphFontSize];
attachment.mathTextColor = [_config inlineMathColor];
#if TARGET_OS_OSX
// On macOS, NSLayoutManager uses self.image/self.bounds rather than calling
// imageForBounds:textContainer:characterIndex:, so we pre-render eagerly.
[attachment renderForMacOS];
#endif
NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
[output appendAttributedString:attachmentString];
}
- (NSString *)extractLatexFromNode:(MarkdownASTNode *)node
{
if (node.content.length > 0) {
return node.content;
}
NSMutableString *buffer = [NSMutableString string];
for (MarkdownASTNode *child in node.children) {
if (child.content.length > 0) {
[buffer appendString:child.content];
}
}
return buffer;
}
@end
#endif