Beispiel #1
0
    def addGoogleFont(self, fontName):
        """Add a font directly from the Google Font repository, saving it to the user prefs folder"""

        # Construct and send Google Font url from name
        repoURL = f"https://fonts.googleapis.com/css2?family={ fontName.replace(' ', '+') }&display=swap"
        repoResp = requests.get(repoURL)
        if not repoResp.ok:
            # If font name is not found, raise error
            raise MissingFontError(
                "Font `{}` could not be retrieved from the Google Font library."
                .format(fontName))
        # Get and send file url from returned CSS data
        fileURL = re.findall("(?<=src: url\().*(?=\) format)",
                             repoResp.content.decode())[0]
        fileFormat = re.findall("(?<=format\(\').*(?=\'\)\;)",
                                repoResp.content.decode())[0]
        fileResp = requests.get(fileURL)
        if not fileResp.ok:
            # If font file is not available, raise error
            raise MissingFontError(
                "OST file for Google font `{}` could not be accessed".format(
                    fontName))
        # Save retrieved font as an OST file
        fileName = Path(prefs.paths['fonts']) / f"{fontName}.{fileFormat}"
        logging.info("Font \"{}\" was successfully installed at: {}".format(
            fontName, prefs.paths['fonts']))
        with open(fileName, "wb") as fileObj:
            fileObj.write(fileResp.content)
        # Add font and return
        return self.addFontFile(fileName)
Beispiel #2
0
 def getDefaultSansFont(self):
     """Load and return the FontInfo for the first found default font"""
     for name in ['Verdana', 'DejaVu Sans', 'Bitstream Vera Sans', 'Tahoma']:
         these = self.getFontsMatching(name, fallback=False)
         if not these:
             continue
         if type(these) in (list, set):
             this = these[0]
         # if str or Path then get a FontInfo object
         if type(this) in [str, Path]:
             this = self.addFontFiles(this)
         return this
     raise MissingFontError("Failed to find any of the default fonts. "
                            "Existing fonts: {}"
                            .format(list(self._fontInfos)))