def __init__(self, name, size, bold=False, italic=False, stretch=False, dpi=None): if not name: name = self._default_name # assert type(bold) is bool, "Only a boolean value is supported for bold in the current font renderer." # assert type(italic) is bool, "Only a boolean value is supported for bold in the current font renderer." if stretch: warnings.warn( "The current font render does not support stretching.") super().__init__(name, size, bold, italic, stretch, dpi) self._name = name family = ctypes.c_void_p() name = ctypes.c_wchar_p(name) # Look in private collection first: if self._private_fonts: gdiplus.GdipCreateFontFamilyFromName(name, self._private_fonts, ctypes.byref(family)) # Then in system collection: if not family: gdiplus.GdipCreateFontFamilyFromName(name, None, ctypes.byref(family)) # Nothing found, use default font. if not family: self._name = self._default_name gdiplus.GdipCreateFontFamilyFromName(ctypes.c_wchar_p(self._name), None, ctypes.byref(family)) if dpi is None: unit = UnitPoint self.dpi = 96 else: unit = UnitPixel size = (size * dpi) // 72 self.dpi = dpi style = 0 if bold: style |= FontStyleBold if italic: style |= FontStyleItalic self._gdipfont = ctypes.c_void_p() gdiplus.GdipCreateFont(family, ctypes.c_float(size), style, unit, ctypes.byref(self._gdipfont)) gdiplus.GdipDeleteFontFamily(family)
def __init__(self, name, size, bold=False, italic=False, dpi=None): if not name: name = self._default_name super(GDIPlusFont, self).__init__(name, size, bold, italic, dpi) family = ctypes.c_void_p() name = ctypes.c_wchar_p(name) # Look in private collection first: if self._private_fonts: gdiplus.GdipCreateFontFamilyFromName(name, self._private_fonts, ctypes.byref(family)) # Then in system collection: if not family: gdiplus.GdipCreateFontFamilyFromName(name, None, ctypes.byref(family)) # Nothing found, use default font. if not family: name = self._default_name gdiplus.GdipCreateFontFamilyFromName(ctypes.c_wchar_p(name), None, ctypes.byref(family)) if dpi is None: unit = UnitPoint self.dpi = 96 else: unit = UnitPixel size = (size * dpi) // 72 self.dpi = dpi style = 0 if bold: style |= FontStyleBold if italic: style |= FontStyleItalic self._gdipfont = ctypes.c_void_p() gdiplus.GdipCreateFont(family, ctypes.c_float(size), style, unit, ctypes.byref(self._gdipfont)) gdiplus.GdipDeleteFontFamily(family)