def __del__(self): try: if self._matrix: res = gdiplus.GdipDeleteMatrix(self._matrix) if self._brush: res = gdiplus.GdipDeleteBrush(self._brush) if self._graphics: res = gdiplus.GdipDeleteGraphics(self._graphics) if self._bitmap: res = gdiplus.GdipDisposeImage(self._bitmap) if self._dc: res = user32.ReleaseDC(0, self._dc) except: pass
def __init__(self, name, size, bold=False, italic=False, dpi=None): super(Win32Font, self).__init__() self.logfont = self.get_logfont(name, size, bold, italic, dpi) self.hfont = gdi32.CreateFontIndirectA(byref(self.logfont)) # Create a dummy DC for coordinate mapping dc = user32.GetDC(0) metrics = TEXTMETRIC() gdi32.SelectObject(dc, self.hfont) gdi32.GetTextMetricsA(dc, byref(metrics)) self.ascent = metrics.tmAscent self.descent = -metrics.tmDescent self.max_glyph_width = metrics.tmMaxCharWidth user32.ReleaseDC(0, dc)
def get_logfont(name, size, bold, italic, dpi): # Create a dummy DC for coordinate mapping dc = user32.GetDC(0) if dpi is None: dpi = 96 logpixelsy = dpi logfont = LOGFONT() # Conversion of point size to device pixels logfont.lfHeight = int(-size * logpixelsy // 72) if bold: logfont.lfWeight = FW_BOLD else: logfont.lfWeight = FW_NORMAL logfont.lfItalic = italic logfont.lfFaceName = asbytes(name) logfont.lfQuality = ANTIALIASED_QUALITY user32.ReleaseDC(0, dc) return logfont
def get_icon(image): # Alpha-blended icon: see http://support.microsoft.com/kb/318876 format = 'BGRA' pitch = len(format) * image.width header = BITMAPV5HEADER() header.bV5Size = sizeof(header) header.bV5Width = image.width header.bV5Height = image.height header.bV5Planes = 1 header.bV5BitCount = 32 header.bV5Compression = BI_BITFIELDS header.bV5RedMask = 0x00ff0000 header.bV5GreenMask = 0x0000ff00 header.bV5BlueMask = 0x000000ff header.bV5AlphaMask = 0xff000000 hdc = _user32.GetDC(None) dataptr = c_void_p() bitmap = _gdi32.CreateDIBSection(hdc, byref(header), DIB_RGB_COLORS, byref(dataptr), None, 0) _user32.ReleaseDC(None, hdc) image = image.get_image_data() data = image.get_data(format, pitch) memmove(dataptr, data, len(data)) mask = _gdi32.CreateBitmap(image.width, image.height, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = True iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap icon = _user32.CreateIconIndirect(byref(iconinfo)) _gdi32.DeleteObject(mask) _gdi32.DeleteObject(bitmap) return icon
def _create_cursor_from_image(self, cursor): """Creates platform cursor from an ImageCursor instance.""" fmt = 'BGRA' image = cursor.texture pitch = len(fmt) * image.width header = BITMAPINFOHEADER() header.biSize = sizeof(header) header.biWidth = image.width header.biHeight = image.height header.biPlanes = 1 header.biBitCount = 32 hdc = _user32.GetDC(None) dataptr = c_void_p() bitmap = _gdi32.CreateDIBSection(hdc, byref(header), DIB_RGB_COLORS, byref(dataptr), None, 0) _user32.ReleaseDC(None, hdc) image = image.get_image_data() data = image.get_data(fmt, pitch) memmove(dataptr, data, len(data)) mask = _gdi32.CreateBitmap(image.width, image.height, 1, 1, None) iconinfo = ICONINFO() iconinfo.fIcon = False iconinfo.hbmMask = mask iconinfo.hbmColor = bitmap iconinfo.xHotspot = int(cursor.hot_x) iconinfo.yHotspot = int(image.height - cursor.hot_y) icon = _user32.CreateIconIndirect(byref(iconinfo)) _gdi32.DeleteObject(mask) _gdi32.DeleteObject(bitmap) return icon