def _create_bitmap(self, width, height): self._data = (ctypes.c_byte * (4 * width * height))() self._bitmap = ctypes.c_void_p() self._format = PixelFormat32bppARGB gdiplus.GdipCreateBitmapFromScan0(width, height, width * 4, self._format, self._data, ctypes.byref(self._bitmap)) self._graphics = ctypes.c_void_p() gdiplus.GdipGetImageGraphicsContext(self._bitmap, ctypes.byref(self._graphics)) gdiplus.GdipSetPageUnit(self._graphics, UnitPixel) self._dc = user32.GetDC(0) gdi32.SelectObject(self._dc, self.font.hfont) gdiplus.GdipSetTextRenderingHint(self._graphics, TextRenderingHintAntiAliasGridFit) self._brush = ctypes.c_void_p() gdiplus.GdipCreateSolidFill(0xffffffff, ctypes.byref(self._brush)) self._matrix = ctypes.c_void_p() gdiplus.GdipCreateMatrix(ctypes.byref(self._matrix)) self._flags = (DriverStringOptionsCmapLookup | DriverStringOptionsRealizedAdvance) self._rect = Rect(0, 0, width, height) self._bitmap_height = height
def __init__(self, font): super(Win32GlyphRenderer, self).__init__(font) self.font = font # Pessimistically round up width and height to 4 byte alignment width = font.max_glyph_width height = font.ascent - font.descent width = (width | 0x3) + 1 height = (height | 0x3) + 1 self._create_bitmap(width, height) gdi32.SelectObject(self._dc, self.font.hfont)
def _get_image(self, text, width, height, lsb): # There's no such thing as a greyscale bitmap format in GDI. We can # create an 8-bit palette bitmap with 256 shades of grey, but # unfortunately antialiasing will not work on such a bitmap. So, we # use a 32-bit bitmap and use the red channel as OpenGL's alpha. gdi32.SelectObject(self._dc, self._bitmap) gdi32.SelectObject(self._dc, self.font.hfont) gdi32.SetBkColor(self._dc, 0x0) gdi32.SetTextColor(self._dc, 0x00ffffff) gdi32.SetBkMode(self._dc, OPAQUE) # Draw to DC user32.FillRect(self._dc, byref(self._bitmap_rect), self._black) gdi32.ExtTextOutA(self._dc, -lsb, 0, 0, c_void_p(), text, len(text), c_void_p()) gdi32.GdiFlush() # Create glyph object and copy bitmap data to texture image = pyglet.image.ImageData(width, height, 'AXXX', self._bitmap_data, self._bitmap_rect.right * 4) return image
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