def fontFilePath(self):
     """Returns the path to the file of the current font."""
     font = AppKit.NSFont.fontWithName_size_(self._font, self._fontSize)
     if font is not None:
         url = CoreText.CTFontDescriptorCopyAttribute(font.fontDescriptor(), CoreText.kCTFontURLAttribute)
         if url:
             return url.path()
     logger.warning("Cannot find the path to the font '%s'.", self._font)
     return None
def fontPath(fontName):
    font = CoreText.CTFontDescriptorCreateWithNameAndSize(fontName, 10)
    if font:
        url = CoreText.CTFontDescriptorCopyAttribute(
            font, CoreText.kCTFontURLAttribute)
        if url:
            return url.path()
    else:
        # warn if font doest not exists
        pass

    return None
Esempio n. 3
0
def getFeatureTagsForFontName(fontName):
    featureTags = []
    font = AppKit.NSFont.fontWithName_size_(fontName, 12)
    if font is None:
        return featureTags
    fontDescriptor = font.fontDescriptor()
    url = CoreText.CTFontDescriptorCopyAttribute(fontDescriptor, CoreText.kCTFontURLAttribute)
    psFontName = CoreText.CTFontDescriptorCopyAttribute(fontDescriptor, CoreText.kCTFontNameAttribute)
    if url is None or psFontName is None:
        return featureTags
    path = url.path()
    ext = os.path.splitext(path)[1].lower()
    macType = getMacCreatorAndType(path)[1]
    if ext in (".ttc", ".otc"):
        ft = _getTTFontFromTTC(path, psFontName)
    elif ext in (".ttf", ".otf"):
        ft = TTFont(path, lazy=True)
    elif ext == ".dfont" or macType == "FFIL":
        ft = _getTTFontFromSuitcase(path, psFontName)
        if ft is None:
            return featureTags
    else:
        return featureTags
    featureTags = set()
    if "GPOS" in ft and ft["GPOS"].table.FeatureList is not None:
        for record in ft["GPOS"].table.FeatureList.FeatureRecord:
            featureTags.add(record.FeatureTag)
    if "GSUB" in ft and ft["GSUB"].table.FeatureList is not None:
        for record in ft["GSUB"].table.FeatureList.FeatureRecord:
            featureTags.add(record.FeatureTag)
    if "feat" in ft:
        for featureName in ft["feat"].table.FeatureNames.FeatureName:
            for featureSetting in featureName.Settings.Setting:
                featureTag = SFNTLayoutTypes.reversedFeatureMap.get((featureName.FeatureType, featureSetting.SettingValue))
                if featureTag:
                    featureTag = featureTag.replace("_off", "")
                    featureTags.add(featureTag)
    ft.close()
    return list(sorted(featureTags))
Esempio n. 4
0
def getFeatureTagsForFontName(fontName):
    descriptor = CoreText.NSFontDescriptor.fontDescriptorWithName_size_(
        fontName, 12)
    featureDescriptions = CoreText.CTFontDescriptorCopyAttribute(
        descriptor, CoreText.kCTFontFeaturesAttribute)
    if featureDescriptions is None:
        return []
    featureTags = list()
    for featureDescription in featureDescriptions:
        featureType = featureDescription[
            CoreText.NSFontFeatureTypeIdentifierKey]
        for selector in featureDescription["CTFeatureTypeSelectors"]:
            featureSelector = selector[
                CoreText.NSFontFeatureSelectorIdentifierKey]
            featureTag = reversedFeatureMap.get((featureType, featureSelector))
            if featureTag:
                featureTag = featureTag.replace("_off", "")
                if featureTag not in featureTags:
                    featureTags.append(featureTag)
    return featureTags
Esempio n. 5
0
def getNamedInstancesForFont(font):
    """
    Return a dict { postscriptName: location } of all named instances in a given font.
    """
    instances = OrderedDict()
    if font is None:
        return instances
    fontDescriptor = font.fontDescriptor()
    url = CoreText.CTFontDescriptorCopyAttribute(fontDescriptor,
                                                 CoreText.kCTFontURLAttribute)
    if url is None:
        return instances

    variationAxesDescriptions = CoreText.CTFontCopyVariationAxes(font)
    if variationAxesDescriptions is None:
        # non-variable fonts have no named instances
        return instances
    tagNameMap = {}
    for variationAxesDescription in variationAxesDescriptions:
        tag = convertIntToVariationTag(variationAxesDescription[
            CoreText.kCTFontVariationAxisIdentifierKey])
        name = variationAxesDescription[CoreText.kCTFontVariationAxisNameKey]
        tagNameMap[tag] = name

    ft = TTFont(url.path(), lazy=True, fontNumber=0)
    if "fvar" in ft:
        cgFont, _ = CoreText.CTFontCopyGraphicsFont(font, None)
        fvar = ft["fvar"]

        for instance in fvar.instances:
            fontVariations = dict()
            for axis, value in instance.coordinates.items():
                fontVariations[tagNameMap[axis]] = value

            varFont = CoreText.CGFontCreateCopyWithVariations(
                cgFont, fontVariations)
            postScriptName = CoreText.CGFontCopyPostScriptName(varFont)
            instances[postScriptName] = instance.coordinates

    ft.close()
    return instances
Esempio n. 6
0
def getFeatureTagsForFontName(fontName):
    descriptor = CoreText.NSFontDescriptor.fontDescriptorWithName_size_(fontName, 12)
    featureDescriptions = CoreText.CTFontDescriptorCopyAttribute(descriptor, CoreText.kCTFontFeaturesAttribute)
    if featureDescriptions is None:
        return []
    return getFeatureTagsForDescriptions(featureDescriptions)
    def testFunctions(self):

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateWithAttributes)
        v = CoreText.CTFontDescriptorCreateWithAttributes({
            CoreText.kCTFontNameAttribute:
            "Optima Bold",
            CoreText.kCTFontSizeAttribute:
            "23.4",
        })
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateWithNameAndSize)
        v = CoreText.CTFontDescriptorCreateWithNameAndSize("Optima Bold", 15.0)
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)
        descriptor = v

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateCopyWithAttributes)
        v = CoreText.CTFontDescriptorCreateCopyWithAttributes(
            v, {"foo": "bar"})
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateCopyWithVariation)
        v = CoreText.CTFontDescriptorCreateCopyWithVariation(v, 0, 5.0)
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateCopyWithFeature)
        v = CoreText.CTFontDescriptorCreateCopyWithFeature(v, 0, 6)
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateMatchingFontDescriptors)
        v = CoreText.CTFontDescriptorCreateMatchingFontDescriptors(
            descriptor, None)
        self.assertIsInstance(v, CoreText.CFArrayRef)

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCreateMatchingFontDescriptor)
        v = CoreText.CTFontDescriptorCreateMatchingFontDescriptor(
            descriptor, None)
        self.assertIsInstance(v, CoreText.CTFontDescriptorRef)

        self.assertResultIsCFRetained(CoreText.CTFontDescriptorCopyAttributes)
        v = CoreText.CTFontDescriptorCopyAttributes(descriptor)
        self.assertIsInstance(v, CoreText.CFDictionaryRef)

        self.assertResultIsCFRetained(CoreText.CTFontDescriptorCopyAttribute)
        v = CoreText.CTFontDescriptorCopyAttribute(
            descriptor, CoreText.kCTFontNameAttribute)
        self.assertEqual(v, "Optima Bold")

        self.assertResultIsCFRetained(
            CoreText.CTFontDescriptorCopyLocalizedAttribute)
        self.assertArgIsOut(CoreText.CTFontDescriptorCopyLocalizedAttribute, 2)
        v, locattr = CoreText.CTFontDescriptorCopyLocalizedAttribute(
            descriptor, CoreText.kCTFontDisplayNameAttribute, None)
        self.assertIsInstance(v, str)
        self.assertIsInstance(locattr, (str, type(None)))

        v = CoreText.CTFontDescriptorGetTypeID()
        self.assertIsInstance(v, int)