diff --git a/DocX/AttributeElements.swift b/DocX/AttributeElements.swift index e214842..2e46f37 100644 --- a/DocX/AttributeElements.swift +++ b/DocX/AttributeElements.swift @@ -60,6 +60,12 @@ extension Dictionary where Key == NSAttributedString.Key{ attributesElement.addChild(element) } + // Superscript / Subscript + if let baselineOffset = self[.baselineOffset] as? CGFloat, + let vertAlignElement = vertAlignElement(baselineOffset: baselineOffset) { + attributesElement.addChild(vertAlignElement) + } + // Character styles if let characterStyleId = self[.characterStyleId] as? String { attributesElement.addChild(characterStyleElement(styleId: characterStyleId)) @@ -127,6 +133,25 @@ extension Dictionary where Key == NSAttributedString.Key{ return [colorElement,outlineElement] } + func vertAlignElement(baselineOffset: CGFloat) -> AEXMLElement? { + // If `baselineOffset` is below zero, we'll assume subscript. + // If it's above zero, we'll assume superscript. + var vertAlignValue: String? + if baselineOffset > 0 { + vertAlignValue = "superscript" + } else if baselineOffset < 0 { + vertAlignValue = "subscript" + } + + if let vertAlignValue = vertAlignValue { + return AEXMLElement(name: "w:vertAlign", + value: nil, + attributes: ["w:val": vertAlignValue]) + } else { + return nil + } + } + func characterStyleElement(styleId: String) -> AEXMLElement { return AEXMLElement(name: "w:rStyle", value: nil,