Skip to content

Commit

Permalink
Added usedFontNames(inRTFData:) to parse RTF data for used fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
peterb180369 committed Nov 12, 2019
1 parent bded13a commit 455268b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions BXSwiftUtils/Strings/NSAttributedString+Codable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,44 @@ extension NSAttributedString
}


//----------------------------------------------------------------------------------------------------------------------

extension NSAttributedString
{
/// Returns an array of all font names that are used in the RTFD data.
///
/// Simply creating an NSAttributedString and then enumerating its font attribute doesn't work, because
/// at this point the OS has already replaced missing fonts with a default font, and all information which
/// fonts are missing has been lost. For this reason this custom function parses the RTFD data and extracts
/// the relevant information.

public class func usedFontNames(inRTFData data:Data) -> [String]
{
// {\fonttbl\f0\fnil\fcharset0 LithosPro-Regular;}

var strings:[String] = []

guard let string = String(bytes:data, encoding:.ascii) else { return strings }
guard let regex = try? NSRegularExpression(pattern:"fonttbl.* +(.*);",options:[]) else { return strings }
let matches = regex.matches(in:string, options:[], range:NSMakeRange(0,string.count))

for match in matches
{
if match.numberOfRanges >= 2
{
let range = match.range(at:1)

if range.location != NSNotFound, let r = Range(range,in:string)
{
let str = String(string[r])
strings += str
}
}
}

return strings
}
}


//----------------------------------------------------------------------------------------------------------------------

0 comments on commit 455268b

Please sign in to comment.