Skip to content

FontDatabase

Base classes

Name Children Inherits
QFontDatabase
PySide6.QtGui
QFontDatabase(self, /) -> None

⋔ Inheritance diagram

graph TD
  3880177491["gui.FontDatabase"]
  2436318848["QtGui.QFontDatabase"]
  37984305["Shiboken.Object"]
  2751680898["builtins.object"]
  2436318848 --> 3880177491
  37984305 --> 2436318848
  2751680898 --> 37984305

🛈 DocStrings

Bases: QFontDatabase

Information about the fonts available in the underlying window system.

Source code in prettyqt/gui/fontdatabase.py
class FontDatabase(QtGui.QFontDatabase):
    """Information about the fonts available in the underlying window system."""

    font_paths: ClassVar[dict[str, int]] = {}

    @classmethod
    def add_fonts_from_folder(cls, path: datatypes.PathType):
        path = pathlib.Path(path)
        for p in path.iterdir():
            if p.suffix.lower() in [".ttf", ".otf"]:
                logger.debug("adding font %r to database.", p)
                cls.addApplicationFont(str(p))

    @classmethod
    def add_font(cls, path: datatypes.PathType, ttf_hash: str | None = None) -> int:
        path = pathlib.Path(path)
        font_id = cls.addApplicationFont(str(path))
        if not cls.applicationFontFamilies(font_id):
            msg = (
                f"Font {path!r} appears to be empty. "
                "If you are on Windows 10, please read "
                "https://support.microsoft.com/"
                "en-us/kb/3053676"
            )
            raise RuntimeError(msg)
        if ttf_hash is not None:
            content = path.read_bytes()
            if hashlib.md5(content).hexdigest() != ttf_hash:
                msg = f"Font is corrupt at: {path!r}"
                raise OSError(msg)
        cls.font_paths[str(path)] = font_id
        return font_id

    @classmethod
    def remove_font(cls, font: datatypes.PathType | int):
        font_id = font if isinstance(font, int) else cls.font_paths[str(font)]
        cls.removeApplicationFont(font_id)

    @classmethod
    def get_system_font(
        cls, font_type: SystemFontStr | QtGui.QFontDatabase.SystemFont
    ) -> gui.Font:
        return gui.Font(cls.systemFont(SYSTEM_FONT.get_enum_value(font_type)))

:fa-brands-github: Show source on GitHub