Skip to content

Commit f2425b8

Browse files
committed
Add support for style of subscript.
1 parent 4e99e83 commit f2425b8

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

Aztec.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@
241241
FF7EAEC4234D253B007A26E0 /* FontProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF7EAEC3234D253B007A26E0 /* FontProvider.swift */; };
242242
FF94935E245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */; };
243243
FF949360245740250085ABB3 /* SuperscriptFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */; };
244+
FF949362245744090085ABB3 /* SubscriptStringAttributeConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF949361245744090085ABB3 /* SubscriptStringAttributeConverter.swift */; };
245+
FF949364245744560085ABB3 /* SubscriptFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF949363245744560085ABB3 /* SubscriptFormatter.swift */; };
244246
FFA61E891DF18F3D00B71BF6 /* ParagraphStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */; };
245247
FFA61EC21DF6C1C900B71BF6 /* NSAttributedString+Archive.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */; };
246248
FFB5D29720BEB21A0038DCFB /* CiteFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */; };
@@ -527,6 +529,8 @@
527529
FF7EAEC3234D253B007A26E0 /* FontProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontProvider.swift; sourceTree = "<group>"; };
528530
FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SuperscriptStringAttributeConverter.swift; sourceTree = "<group>"; };
529531
FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuperscriptFormatter.swift; sourceTree = "<group>"; };
532+
FF949361245744090085ABB3 /* SubscriptStringAttributeConverter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubscriptStringAttributeConverter.swift; sourceTree = "<group>"; };
533+
FF949363245744560085ABB3 /* SubscriptFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubscriptFormatter.swift; sourceTree = "<group>"; };
530534
FFA61E881DF18F3D00B71BF6 /* ParagraphStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParagraphStyle.swift; sourceTree = "<group>"; };
531535
FFA61EC11DF6C1C900B71BF6 /* NSAttributedString+Archive.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSAttributedString+Archive.swift"; sourceTree = "<group>"; };
532536
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CiteFormatter.swift; sourceTree = "<group>"; };
@@ -1009,6 +1013,7 @@
10091013
F12F58601EF20394008AE298 /* TextListFormatter.swift */,
10101014
F12F58611EF20394008AE298 /* UnderlineFormatter.swift */,
10111015
FF94935F245740250085ABB3 /* SuperscriptFormatter.swift */,
1016+
FF949363245744560085ABB3 /* SubscriptFormatter.swift */,
10121017
FF61909D202481F4004BCD0A /* CodeFormatter.swift */,
10131018
FFB5D29620BEB21A0038DCFB /* CiteFormatter.swift */,
10141019
);
@@ -1095,6 +1100,7 @@
10951100
F1656FDD2152A6A6009C7E3A /* CiteStringAttributeConverter.swift */,
10961101
F15BA60C215159A600424120 /* ItalicStringAttributeConverter.swift */,
10971102
FF94935D245738AC0085ABB3 /* SuperscriptStringAttributeConverter.swift */,
1103+
FF949361245744090085ABB3 /* SubscriptStringAttributeConverter.swift */,
10981104
F15BA60E21515C0F00424120 /* UnderlineStringAttributeConverter.swift */,
10991105
);
11001106
path = Implementations;
@@ -1637,6 +1643,7 @@
16371643
F11326B21EF1AA91007FEE9A /* HTMLPre.swift in Sources */,
16381644
F1098741214FF55F00983B6A /* BoldElementAttributeConverter.swift in Sources */,
16391645
F17BC8AB1F4E512800398E2B /* AttributedStringSerializer.swift in Sources */,
1646+
FF949364245744560085ABB3 /* SubscriptFormatter.swift in Sources */,
16401647
F18986E11EF2040A0060EDBA /* FontFormatter.swift in Sources */,
16411648
F1FF7D9D201A147B007B0B32 /* Figure.swift in Sources */,
16421649
FFA61E891DF18F3D00B71BF6 /* ParagraphStyle.swift in Sources */,
@@ -1645,6 +1652,7 @@
16451652
FF7C89B01E3BC52F000472A8 /* NSAttributedString+FontTraits.swift in Sources */,
16461653
B5E94D101FE01335000E7C20 /* FigureElementConverter.swift in Sources */,
16471654
40A2986D1FD61B0C00AEDF3B /* ElementConverter.swift in Sources */,
1655+
FF949362245744090085ABB3 /* SubscriptStringAttributeConverter.swift in Sources */,
16481656
F1FF7DA1201A1D3E007B0B32 /* FigcaptionElementConverter.swift in Sources */,
16491657
F9982CF621877663001E606B /* TextViewPasteboardDelegate.swift in Sources */,
16501658
F1289FB72155244A001E07C5 /* AttributeType.swift in Sources */,

Aztec/Classes/Converters/ElementsToAttributedString/Implementations/GenericElementConverter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class GenericElementConverter: ElementConverter {
3535
lazy var codeFormatter = CodeFormatter()
3636
lazy var liFormatter = LiFormatter()
3737
lazy var superscriptFormatter = SuperscriptFormatter()
38+
lazy var subscriptFormatter = SubscriptFormatter()
3839

3940
public lazy var elementFormattersMap: [Element: AttributeFormatter] = {
4041
return [
@@ -58,6 +59,7 @@ class GenericElementConverter: ElementConverter {
5859
.code: self.codeFormatter,
5960
.li: self.liFormatter,
6061
.sup: self.superscriptFormatter,
62+
.sub: self.subscriptFormatter,
6163
]
6264
}()
6365

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Foundation
2+
import UIKit
3+
4+
5+
/// Converts the subscript style information from string attributes and aggregates it into an
6+
/// existing array of element nodes.
7+
///
8+
open class SubscriptStringAttributeConverter: StringAttributeConverter {
9+
10+
private let toggler = HTMLStyleToggler(defaultElement: .sub, cssAttributeMatcher: NeverCSSAttributeMatcher())
11+
12+
public func convert(
13+
attributes: [NSAttributedString.Key: Any],
14+
andAggregateWith elementNodes: [ElementNode]) -> [ElementNode] {
15+
16+
var elementNodes = elementNodes
17+
18+
// We add the representation right away, if it exists... as it could contain attributes beyond just this
19+
// style. The enable and disable methods below can modify this as necessary.
20+
//
21+
if let representation = attributes[NSAttributedString.Key.subHtmlRepresentation] as? HTMLRepresentation,
22+
case let .element(representationElement) = representation.kind {
23+
24+
elementNodes.append(representationElement.toElementNode())
25+
}
26+
27+
if shouldEnable(for: attributes) {
28+
return toggler.enable(in: elementNodes)
29+
} else {
30+
return toggler.disable(in: elementNodes)
31+
}
32+
}
33+
34+
// MARK: - Style Detection
35+
36+
func shouldEnable(for attributes: [NSAttributedString.Key : Any]) -> Bool {
37+
return hasTraits(for: attributes)
38+
}
39+
40+
func hasTraits(for attributes: [NSAttributedString.Key : Any]) -> Bool {
41+
guard let baselineOffset = attributes[.baselineOffset] as? NSNumber else {
42+
return false
43+
}
44+
45+
return baselineOffset.intValue < 0;
46+
}
47+
}
48+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import UIKit
2+
3+
class SubscriptFormatter: StandardAttributeFormatter {
4+
5+
init() {
6+
super.init(attributeKey: .baselineOffset,
7+
attributeValue: NSNumber(-4),
8+
htmlRepresentationKey: .subHtmlRepresentation)
9+
}
10+
}

Aztec/Classes/NSAttributedString/Conversions/AttributedStringParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AttributedStringParser {
2828
ConditionalItalicStringAttributeConverter(),
2929
UnderlineStringAttributeConverter(),
3030
SuperscriptStringAttributeConverter(),
31+
SubscriptStringAttributeConverter(),
3132
]
3233

3334
// MARK: - Attachment Converters

0 commit comments

Comments
 (0)