Forked from KevinGutowski/SubstringFontReporting.js
Last active
June 2, 2019 12:49
-
-
Save matt-curtis/db51a4c14f21141b134ea4e3b64bfffa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sketch = require('sketch') | |
let settingsAttribute = getSettingsAttributeForKey_Value(kLowerCaseType, kLowerCaseSmallCapsSelector) | |
applySubstringFontModification() | |
//getFontAttributesForSelectedRange() | |
function applySubstringFontModification() { | |
let document = sketch.getSelectedDocument() | |
let textLayer = document.selectedLayers.layers[0] | |
let textView = textLayer.sketchObject.editingDelegate().textView() | |
let textStorage = textView.textStorage() | |
let fonts = getFontsFromTextLayer(textLayer) | |
fonts.forEach(fontForRange => { | |
let font = fontForRange.font | |
let range = fontForRange.range | |
let fontSize = font.pointSize() | |
let descriptor = font | |
.fontDescriptor() | |
.fontDescriptorByAddingAttributes(settingsAttribute) | |
let newFont = NSFont.fontWithDescriptor_size(descriptor,fontSize) | |
let attrsDict = NSDictionary.dictionaryWithObject_forKey(newFont,NSFontAttributeName) | |
textStorage.addAttributes_range(attrsDict,range) | |
textStorage.fixAttributesInRange(range) | |
}) | |
textView.didChangeText() | |
} | |
function getFontAttributesForSelectedRange() { | |
let document = sketch.getSelectedDocument() | |
let textLayer = document.selectedLayers.layers[0] | |
let fonts = getFontsFromTextLayer(textLayer) | |
fonts.forEach((fontForRange, index) => { | |
let font = fontForRange.font | |
let fontFeatureSettings = font.fontDescriptor().fontAttributes()[NSFontFeatureSettingsAttribute] | |
console.log("Font" + (1 + index)) | |
console.log(fontFeatureSettings) | |
}) | |
} | |
// ----- Helper Functions | |
function getSettingsAttributeForKey_Value(key, value) { | |
let settingsAttribute = { | |
[NSFontFeatureSettingsAttribute]: [{ | |
[NSFontFeatureTypeIdentifierKey]: key, | |
[NSFontFeatureSelectorIdentifierKey]: value | |
}] | |
} | |
return settingsAttribute | |
} | |
function getFontsFromTextLayer(textLayer) { | |
let msTextLayer = textLayer.sketchObject | |
let textView = msTextLayer.editingDelegate().textView() | |
let textStorage = textView.textStorage() | |
let selectedRange = textView.selectedRange() | |
let effectiveRange = MOPointer.alloc().init() | |
let fonts = [] | |
if (selectedRange.length == 0) { | |
let font = textStorage.attribute_atIndex_longestEffectiveRange_inRange( | |
NSFontAttributeName, | |
selectedRange.location, | |
effectiveRange, | |
selectedRange | |
) | |
fonts.push({"font": font, "range": effectiveRange.value()}) | |
} | |
while (selectedRange.length > 0) { | |
let font = textStorage.attribute_atIndex_longestEffectiveRange_inRange( | |
NSFontAttributeName, | |
selectedRange.location, | |
effectiveRange, | |
selectedRange | |
) | |
selectedRange = NSMakeRange( | |
NSMaxRange(effectiveRange.value()), | |
NSMaxRange(selectedRange) - NSMaxRange(effectiveRange.value()) | |
) | |
fonts.push({"font": font, "range": effectiveRange.value()}) | |
} | |
return fonts | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment