Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, assign) CGFloat fontSize;
@property (nonatomic, strong, nullable) RCTUIColor *mathTextColor;

@property (nonatomic, readonly) CGFloat boxHeight;

#if TARGET_OS_OSX
/// Pre-renders the formula into self.image and sets self.bounds.
/// Must be called after latex/fontSize/mathTextColor are set, before the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

@implementation ENRMMathInlineAttachment

- (CGFloat)boxHeight
{
#if !TARGET_OS_OSX
[self prepareIfNeeded];
#endif
return _cachedSize.height;
}

#if !TARGET_OS_OSX

- (void)prepareIfNeeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,37 +284,68 @@ void applyBaselineOffset(NSMutableAttributedString *output, NSRange range)
return;
}

__block CGFloat maximumLineHeight = 0;
// Math paragraphs leave maximumLineHeight at 0, so fall back to minimumLineHeight.
__block CGFloat targetLineHeight = 0;
[output enumerateAttribute:NSParagraphStyleAttributeName
inRange:range
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(NSParagraphStyle *paragraphStyle, __unused NSRange subrange, __unused BOOL *stop) {
if (!paragraphStyle) {
return;
}
maximumLineHeight = MAX(paragraphStyle.maximumLineHeight, maximumLineHeight);
CGFloat clamp = MAX(paragraphStyle.maximumLineHeight, paragraphStyle.minimumLineHeight);
targetLineHeight = MAX(clamp, targetLineHeight);
}];

if (maximumLineHeight <= 0) {
if (targetLineHeight <= 0) {
return;
}

__block CGFloat maximumFontLineHeight = 0;
[output enumerateAttribute:NSFontAttributeName
inRange:range
options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired
usingBlock:^(UIFont *font, __unused NSRange subrange, __unused BOOL *stop) {
if (!font) {
return;
}
maximumFontLineHeight = MAX(UIFontLineHeight(font), maximumFontLineHeight);
}];
// Center on real text; on a math-only line center the math box instead of its font.
__block CGFloat textLineHeight = 0;
#if ENRICHED_MARKDOWN_MATH
__block BOOL hasMath = NO;
#endif
[output enumerateAttributesInRange:range
options:0
usingBlock:^(NSDictionary<NSAttributedStringKey, id> *attrs, __unused NSRange subrange,
__unused BOOL *stop) {
#if ENRICHED_MARKDOWN_MATH
if ([attrs[NSAttachmentAttributeName] isKindOfClass:[ENRMMathInlineAttachment class]]) {
hasMath = YES;
return;
}
#endif
UIFont *font = attrs[NSFontAttributeName];
if (font) {
textLineHeight = MAX(UIFontLineHeight(font), textLineHeight);
}
}];

CGFloat contentLineHeight = textLineHeight;

#if ENRICHED_MARKDOWN_MATH
// Math only grows the content height, so measure it (parsing LaTeX) only when text
// alone wouldn't already fill the line.
if (hasMath && targetLineHeight > textLineHeight) {
__block CGFloat mathBoxHeight = 0;
[output enumerateAttribute:NSAttachmentAttributeName
inRange:range
options:0
usingBlock:^(id value, __unused NSRange subrange, __unused BOOL *stop) {
if ([value isKindOfClass:[ENRMMathInlineAttachment class]]) {
mathBoxHeight = MAX(((ENRMMathInlineAttachment *)value).boxHeight, mathBoxHeight);
}
}];
contentLineHeight = MAX(textLineHeight, mathBoxHeight);
}
#endif

if (maximumFontLineHeight <= 0 || maximumLineHeight < maximumFontLineHeight) {
if (contentLineHeight <= 0 || targetLineHeight <= contentLineHeight) {
return;
}

CGFloat baseLineOffset = (maximumLineHeight - maximumFontLineHeight) / 2.0;
CGFloat baseLineOffset = (targetLineHeight - contentLineHeight) / 2.0;

[output enumerateAttribute:NSBaselineOffsetAttributeName
inRange:range
Expand Down
Loading