Skip to content

Commit

Permalink
Fixed underlining when no explicit foreground color is set (#37)
Browse files Browse the repository at this point in the history
* Underlining doesn’t require specifying a foreground color

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.

* No longer explicitly look for `foregroundColor` when setting color on an underline

When no underline color is explicitly specified on the <w:u> element, Word already falls back to the text color. So, there’s no reason for DocX to perform this check as well.
  • Loading branch information
andalman authored Jan 27, 2023
1 parent 9999b2e commit 0b722d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 5 additions & 2 deletions DocX/AttributeElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +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 {
// If the `underlineColor` attribute is present, use that as the color
let underlineColor = self[.underlineColor] as? NSColor

let underline=NSUnderlineStyle(rawValue: style)
attributesElement.addChild(underline.underlineElement(for: color))
attributesElement.addChild(underline.underlineElement(for: underlineColor))
}

if let backgroundColor=self[.backgroundColor] as? NSColor{
Expand Down
14 changes: 11 additions & 3 deletions DocX/NSUnderlineStyle+Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0b722d3

Please sign in to comment.