コード例 #1
0
    def createCharTexture(self, char, mode=None):
        """Create character's texture/bitmap as a Numeric array w/ width and height

        This uses PyGame and Numeric to attempt to create
        an anti-aliased luminance texture-map of the given
        character and return it as (data, width, height).
        """
        try:
            letter_render = self.font.render(char, 1, (255, 255, 255))
        except:
            traceback.print_exc()
            return None, font.CharacterMetrics(char, 0, 0)
        else:
            # XXX Figure out why this rotate appears to be required :(
            letter_render = transform.rotate(letter_render, -90.0)
            colour = surfarray.array3d(letter_render)
            alpha = surfarray.array_alpha(letter_render)
            colour[:, :, 1] = alpha
            colour = colour[:, :, :2]
            colour = contiguous(colour).astype('B')
            # This produces what looks like garbage, but displays correctly
            colour.shape = (
                colour.shape[1],
                colour.shape[0],
            ) + colour.shape[2:]
            return colour, font.CharacterMetrics(
                char,
                colour.shape[0],
                colour.shape[1],
            )
コード例 #2
0
 def createChar(self, char, mode=None):
     """Create the single-character display list"""
     glyph = self.font.getGlyph(char)
     if glyph:
         metrics = font.CharacterMetrics(
             char,
             glyph.width / self.getScale(),
             glyph.height / self.getScale(),
         )
         list = glGenLists(1)
         if list == 0:
             raise RuntimeError(
                 """Unable to generate display list for %s""" % (self, ))
         glNewList(list, GL_COMPILE)
         try:
             try:
                 if glyph.outlines and glyph.contours:
                     self.renderGlyph(glyph, mode=mode)
                 else:
                     glyph.renderAdvance(self.getScale())
             except Exception:
                 glDeleteLists(list, 1)
                 raise
         finally:
             glEndList()
         return list, metrics
     else:
         return None, None
コード例 #3
0
ファイル: wglfont.py プロジェクト: BearGuy/image-processing
    def _createSingleChar(self, wgldc, char, base=None):
        """Create the single-character (polygonal) display list

        Note:
            This is actually used by both the bitmap and
            polygonal geometry versions, though the metrics
            returned are rather less than useful for bitmap :(
        """
        if base is None:
            base = glGenLists(1)
        metrics = GLYPHMETRICSFLOAT()
        try:
            wglUseFontOutlinesW(
                wgldc,  # the win32-specific OpenGL Context handle
                ord(char),  # character to create
                1,  # create a single character
                base,  # display list to fill
                self.deviation,
                self.extrusion,
                WGL_FONT_POLYGONS,
                metrics,  # metrics float structure to be filled
            )
        except:
            print """couldn't get outline for character""", repr(char)
        realMetrics = font.CharacterMetrics(
            char,
            metrics.gmfCellIncX,
            metrics.gmfptGlyphOrigin.y + metrics.gmfBlackBoxY,
        )
        return base, realMetrics
コード例 #4
0
    def createCharTexture(self, char, mode=None):
        """Create character's texture/bitmap as a Numeric array w/ width and height

        This uses PyGame and Numeric to attempt to create
        an anti-aliased luminance texture-map of the given
        character and return it as (data, width, height).
        """
        if __debug__:
            log.info("""Creating texture for %r""", char)
        dc = wx.MemoryDC()
        bm = wx.EmptyBitmap(1, 1)
        dc.SelectObject(bm)
        dc.SetFont(self.font)
        width, height = dc.GetTextExtent(char)
        bitmap = wx.EmptyBitmap(width, height)
        dc.SelectObject(bitmap)
        #~ dc.SetBackgroundMode( wx.TRANSPARENT )
        dc.SetBackgroundMode(wx.SOLID)
        dc.SetTextForeground('#ffffff')
        dc.SetTextBackground('#000000')
        dc.DrawText(char, 0, 0)
        dc.SelectObject(wx.NullBitmap)
        image = wx.ImageFromBitmap(bitmap)
        #~ import pdb
        #~ pdb.set_trace()
        #~ directory = '~/bitmaps'
        #~ directory = os.path.expanduser( directory )
        #~ image.SaveFile( '%s/char%s.png'%(directory,ord(char)), wx.BITMAP_TYPE_PNG)
        data = image.GetData()
        data = frombuffer(data, 'b')
        data = reshape(data, (height, width, 3))
        data = data[::-1, :, :2]
        assert shape(data) == (
            height, width,
            2), """Data array has changed shape is %s should be %s""" % (
                shape(data), (height, width, 3))
        data = data.tobytes()
        #~ print 'data', repr(data)
        return data, font.CharacterMetrics(
            char,
            width,
            height,
        )
コード例 #5
0
ファイル: glutfont.py プロジェクト: superizer/Python-
 def createChar(self, char, mode=None):
     """Create the single-character display list
     """
     metrics = font.CharacterMetrics(
         char, GLUT.glutBitmapWidth(self.specifier, ord(char)),
         self.charHeight)
     list = glGenLists(1)
     glNewList(list, GL_COMPILE)
     try:
         try:
             if metrics.char != ' ':
                 GLUT.glutBitmapCharacter(self.specifier, ord(char))
             else:
                 glBitmap(0, 0, 0, 0, metrics.width, 0, None)
         except Exception:
             glDeleteLists(list, 1)
             list = None
     finally:
         glEndList()
     return list, metrics