From 96519104ac8695d38c86fa8fbc5de8d4cf92d6cc Mon Sep 17 00:00:00 2001 From: Brad Andalman Date: Wed, 25 Jan 2023 16:13:54 -0800 Subject: [PATCH 1/2] =?UTF-8?q?Underlining=20doesn=E2=80=99t=20require=20s?= =?UTF-8?q?pecifying=20a=20foreground=20color?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made `color` optional when creating an underline element. In addition, if the `underlineColor` attribute is present, it will be preferred over `foregroundColor` (if any). If neither is specified, the underline color will not be output explicitly. --- DocX/AttributeElements.swift | 7 ++++++- DocX/NSUnderlineStyle+Elements.swift | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/DocX/AttributeElements.swift b/DocX/AttributeElements.swift index 2d376b0..b8a961e 100644 --- a/DocX/AttributeElements.swift +++ b/DocX/AttributeElements.swift @@ -40,7 +40,12 @@ extension Dictionary where Key == NSAttributedString.Key{ attributesElement.addChildren(self.outlineProperties(strokeWidth: strokeWidth, font:font)) } - if let style=self[.underlineStyle] as? Int, let color=self[.foregroundColor] as? NSColor{ + if let style=self[.underlineStyle] as? Int { + let foregroundColor = self[.foregroundColor] as? NSColor + let underlineColor = self[.underlineColor] as? NSColor + // If the `underlineColor` attribute is present, prefer that to the + // `foregroundColor` (if any) + let color = underlineColor ?? foregroundColor let underline=NSUnderlineStyle(rawValue: style) attributesElement.addChild(underline.underlineElement(for: color)) } diff --git a/DocX/NSUnderlineStyle+Elements.swift b/DocX/NSUnderlineStyle+Elements.swift index dd518b3..708db9e 100644 --- a/DocX/NSUnderlineStyle+Elements.swift +++ b/DocX/NSUnderlineStyle+Elements.swift @@ -41,9 +41,17 @@ extension NSUnderlineStyle{ return val } - func underlineElement(for color:NSColor)->AEXMLElement{ - let colorString=color.hexColorString - return AEXMLElement(name: "w:u", value: nil, attributes: ["w:color":colorString, "w:val":self.elementValue]) + func underlineElement(for color:NSColor?)->AEXMLElement{ + // Always add the underline value, which determines the underline style + var attributes = ["w:val": self.elementValue] + + // If color is specified, include that too + if let color = color { + let colorString = color.hexColorString + attributes["w:color"] = colorString + } + + return AEXMLElement(name: "w:u", value: nil, attributes: attributes) } var strikeThroughElement:AEXMLElement{ From 0fde408b8634bca7134b0648f7fc46e1414697ee Mon Sep 17 00:00:00 2001 From: Brad Andalman Date: Thu, 26 Jan 2023 10:55:48 -0800 Subject: [PATCH 2/2] No longer explicitly look for `foregroundColor` when setting color on an underline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When no underline color is explicitly specified on the element, Word already falls back to the text color. So, there’s no reason for DocX to perform this check as well. --- DocX/AttributeElements.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/DocX/AttributeElements.swift b/DocX/AttributeElements.swift index b8a961e..f3e7a89 100644 --- a/DocX/AttributeElements.swift +++ b/DocX/AttributeElements.swift @@ -41,13 +41,11 @@ extension Dictionary where Key == NSAttributedString.Key{ } if let style=self[.underlineStyle] as? Int { - let foregroundColor = self[.foregroundColor] as? NSColor + // If the `underlineColor` attribute is present, use that as the color let underlineColor = self[.underlineColor] as? NSColor - // If the `underlineColor` attribute is present, prefer that to the - // `foregroundColor` (if any) - let color = underlineColor ?? foregroundColor + let underline=NSUnderlineStyle(rawValue: style) - attributesElement.addChild(underline.underlineElement(for: color)) + attributesElement.addChild(underline.underlineElement(for: underlineColor)) } if let backgroundColor=self[.backgroundColor] as? NSColor{