Move (copy, so far) pyglet.ext.scene2d.textsprite.TextSprite to pyglet.font.Label, because it's more needed than an ext package, and not really useful as a sprite anyway.  Added horiz and vert align.
Move (copy, so far) pyglet.ext.scene2d.textsprite.TextSprite to
pyglet.font.Label, because it's more needed than an ext package, and
not really useful as a sprite anyway.  Added horiz and vert align.


Index: font/__init__.py
===================================================================
--- font/__init__.py	(revision 757)
+++ font/__init__.py	(working copy)
@@ -198,6 +198,134 @@
         if from_index:
             glPopMatrix()

+class Label(object):
+    '''Simple displayable text.
+    '''
+
+    _layout_width = None  # Width to layout text to
+    _text_width = 0       # Calculated width of text
+    _text_height = 0      # Calculated height of text (bottom descender to top
+                          # ascender)
+
+    _dirty = False        # Flag if require layout
+
+    # Alignment constants
+    LEFT = 'left'
+    CENTER = 'center'
+    RIGHT = 'right'
+    BOTTOM = 'bottom'
+    BASELINE = 'baseline'
+    TOP = 'top'
+
+    halign = LEFT
+    valign = BASELINE
+
+    def __init__(self, font, text, x=0, y=0, z=0, color=(1,1,1,1)):
+        self._dirty = True
+        self.font = font
+        self._text = text
+        self.color = color
+        self.x = x
+        self.y = y
+        self.leading = 0
+
+    def _clean(self):
+        '''Resolve changed layout'''
+        text = self._text + ' '
+        glyphs = self.font.get_glyphs(text)
+        self._glyph_string = GlyphString(text, glyphs)
+
+        self.lines = []
+        i = 0
+        if self._layout_width is None:
+            self._text_width = 0
+            while '\n' in text[i:]:
+                end = text.index('\n', i)
+                self.lines.append((i, end))
+                self._text_width = max(self._text_width,
+                                       self._glyph_string.get_subwidth(i, end))
+                i = end + 1
+            end = len(text)
+            if end != i:
+                self.lines.append((i, end))
+                self._text_width = max(self._text_width,
+                                       self._glyph_string.get_subwidth(i, end))
+
+        else:
+            bp = self._glyph_string.get_break_index(i, self._layout_width)
+            while i < len(text) and bp > i:
+                if text[bp-1] == '\n':
+                    self.lines.append((i, bp - 1))
+                else:
+                    self.lines.append((i, bp))
+                i = bp
+                bp = self._glyph_string.get_break_index(i, self._layout_width)
+            if i < len(text) - 1:
+                self.lines.append((i, len(text)))
+
+        self.line_height = self.font.ascent - self.font.descent + self.leading
+        self._text_height = self.line_height * len(self.lines)
+
+        self._dirty = False
+
+    def draw(self):
+        if self._dirty:
+            self._clean()
+
+        x = self.x
+        if self.halign == self.RIGHT:
+            x -= self.width
+        elif self.halign == self.CENTER:
+            x -= self.width / 2
+
+        y = self.y
+        if self.valign == self.BOTTOM:
+            y += self.height
+        elif self.valign == self.CENTER:
+            y += self.height / 2
+        elif self.valign == self.TOP:
+            y -= self.font.ascent
+
+        glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT)
+        glEnable(GL_TEXTURE_2D)
+        glColor4f(*self.color)
+        glPushMatrix()
+        glTranslatef(x, y, 0)
+        for start, end in self.lines:
+            self._glyph_string.draw(start, end)
+            glTranslatef(0, -self.line_height, 0)
+        glPopMatrix()
+        glPopAttrib()
+
+    def get_width(self):
+        if self._dirty:
+            self._clean()
+        if self._layout_width:
+            return self._layout_width
+        return self._text_width
+
+    def set_width(self, width):
+        self._layout_width = width
+        self._dirty = True
+
+    width = property(get_width, set_width)
+
+    def get_height(self):
+        if self._dirty:
+            self._clean()
+        return self._text_height
+
+    height = property(get_height)
+
+    def set_text(self, text):
+        self._text = text
+        self._dirty = True
+
+    text = property(lambda self: self._text, set_text)
+
+
+
+
 # Load platform dependent module
 if sys.platform == 'darwin':
     from pyglet.font.carbon import CarbonFont

seeminglee/pyglet64,niklaskorz/pyglet,adamlwgriffiths/Pyglet,niklaskorz/pyglet,adamlwgriffiths/Pyglet,niklaskorz/pyglet,seeminglee/pyglet64,niklaskorz/pyglet,adamlwgriffiths/Pyglet,seeminglee/pyglet64,adamlwgriffiths/Pyglet