NSAttributedString HTML parser does not add bold attributes for system font of size lower than 18px

Hello!

Preface: I'm trying to create a attributed string from an html text with bold marks (<B>) using the initialiser for NSMutableAttributedString with the html document type.

The issue: However, whenever the font is system font of size lower than 18, the constructed attributed string does not contain the bold attribute. I've tried a different font (Helvetica) and it worked normally.

Code:

let font = NSFont.systemFont(ofSize: 15)
let color = NSColor.textColor
let rgbColor = color.usingColorSpace(.deviceRGB) ?? NSColor(deviceRed: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)

let cssColor = String(format: "rgba(%.0f, %.0f, %.0f, %0.2f)", rgbColor.redComponent * 255.0, rgbColor.greenComponent * 255.0, rgbColor.blueComponent * 255.0, rgbColor.alphaComponent)

let htmlString = "<html><head><title></title><style type='text/css'>" +
"body { font-family: '\(font.fontName)';" +
"       font-size: \(font.pointSize)px;" +
"       color: \(cssColor); }" +
"</style></head><body>I am not bold. <B>I am bold</B></body><html>"

print(htmlString)
guard let data = htmlString.data(using: .utf8, allowLossyConversion: false) else { return }

let string  = try! NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

print(string.description)

The resulting output from this code snippet is:

I am not bold. I am bold{
    NSColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSFont = "\".AppleSystemUIFont 15.00 pt. P [] (0x7fdb01705370) fobj=0x7fdb01705370, spc=3.98\"";
    ...
    NSStrokeColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSStrokeWidth = 0;
}

However if I increase the font size to 18 or change the font it works as expected:

I am not bold. {
    NSColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSFont = "\".AppleSystemUIFont 18.00 pt. P [] (0x7facfac1d7f0) fobj=0x7facfac1d7f0, spc=4.56\"";
    ...
}I am bold{
    NSColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSFont = "\".SFNS-Regular_wdth_opsz120000_GRAD_wght2BC0000 18.00 pt. P [] (0x7facf9f049c0) fobj=0x7facf9f049c0, spc=4.15\"";
    ...
}

Helvetica 15 works:

I am not bold. {
    NSColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSFont = "\"Helvetica 15.00 pt. P [] (0x7fd883c28850) fobj=0x7fd883b1c310, spc=4.17\"";
    ...
}I am bold{
    NSColor = "sRGB IEC61966-2.1 colorspace 1 1 1 1";
    NSFont = "\"Helvetica-Bold 15.00 pt. P [] (0x7fd883c28850) fobj=0x7fd883b1f320, spc=4.17\"";
    ...
}

Is there a way to fix this issue by changing some parameters or is this a fool's errand and I should try to write my own parser that would apply these attributes?

NSAttributedString HTML parser does not add bold attributes for system font of size lower than 18px
 
 
Q