Re: pyglet text drawing

310 views
Skip to first unread message

Txema Vicente

unread,
Jun 20, 2012, 8:17:58 AM6/20/12
to pyglet...@googlegroups.com

> Is this even possible? It feels like the text api is too rich and
> advanced and thus hidden away what I need, and the pieces I can use
> are spread over too many classes.
>

Take a look at this classes:

document = pyglet.text.document.FormattedDocument()
textbox = pyglet.text.layout.IncrementalTextLayout(document, w, h,
batch=batch)

style={}
self.document.set_style(0, len(document), style)

And if it is editable:
pyglet.text.caret.Caret(textbox)



Roger Flores

unread,
Jun 26, 2012, 9:16:28 PM6/26/12
to pyglet...@googlegroups.com
Here is my followup for anyone else that needs to draw text like a console.  The code I have is pasted at the bottom.

My goal was to pop up a 80x25 console window, and then draw black text, colored text, underlined text, and inverted text to it.


The code is mostly there except 1) I can't switch the window background from black to white and 2) I can't change the text color either.


On Wed, Jun 20, 2012 at 5:17 AM, Txema Vicente <tx...@nabla.net> wrote:
Take a look at this classes:

document = pyglet.text.document.FormattedDocument()
textbox = pyglet.text.layout.IncrementalTextLayout(document, w, h, batch=batch)


About 95% of the files in pyglet/text/* are about attributes and text layout.  _GlyphBox.place() seems to be the code to do the color, back color and underline via extending arrays of vertices/quads.  I don't know OpenGL well enough but it seems like moving this into the text API would allow this to be used by all.  I suspect that a line of text could involve 80 rectangles (triangles?) plus 80 little line segments underneath to underline with this method though.

If this is the only way pyglet supports colored text then I'd love the right vertices code to paste in and get it done.  I was hoping though to just pass in a color, when not none, like pyglet.font.GlyphString(text, glyphs, x, y, black, white), or even better, have the colors in a context so they don't have to keep being passed around when drawing lots of text.


-Roger

console.py:
import sys

import pyglet


class Console():

        def __init__(self):
                self.font = pyglet.font.load('Arial', 14)  # substitute a monospaced font in
                # get the font metrics (average width and height).  Use '0' as average.
                text= '0'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs)
                self.char_width = glyph_string.get_subwidth(0, 1)
                self.char_height = self.font.ascent - self.font.descent
                #self.char_width = 9
                #self.char_height = 14
               
                self.window = pyglet.window.Window(80 * self.char_width, 25 * self.char_height, caption='console 80x25')
                pyglet.gl.glEnable( pyglet.gl.GL_TEXTURE_2D)
               
               
        def handle_events(self):
                if self.window.has_exit:
                        sys.exit()
                       
                self.window.dispatch_events()
               
               
        def draw(self):
                self.window.clear()  # how to clear to white instead of black?
                x = 0
                y = self.window.height
               
                # black text
                text= 'hello world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y - self.font.ascent)
                glyph_string.draw()
                y = y - self.char_height
               
                # red text
                text= 'hello red world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y - self.font.ascent)
                glyph_string.draw()  # how to set red?
                y = y - self.char_height
               
                # underlined text
                text= 'hello underlined world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y - self.font.ascent)
                glyph_string.draw()  # how to underline?
                y = y - self.char_height
               
                # white text and black background
                text= 'hello inverted world'
                glyphs = self.font.get_glyphs(text)
                glyph_string = pyglet.font.GlyphString(text, glyphs, x, y - self.font.ascent)
                glyph_string.draw()
                y = y - self.char_height
               
                self.window.flip()
               
               
con = Console()

# It's preferable to use pyglet.app.run() to call your draw routine as
# needed (decorate the draw function).  But if that's not possible, handle
# events frequently, but without consuming all the CPU.
while True:
    con.handle_events()
    con.draw()


Roger Flores

unread,
Jun 28, 2012, 4:15:12 PM6/28/12
to pyglet...@googlegroups.com
Is anyone able to look/run the code and change it so the text is drawn with a color other than white?  Or are there docs that I missed?  Is this the right list for this question or should is there a more appropriate one I should be using?


Appreciatively,
-Roger

Winston Wolff

unread,
Jun 29, 2012, 12:48:22 AM6/29/12
to pyglet...@googlegroups.com
I don't know an answer but I know there is a test that changes text color. You could look through those.

-ww
> --
> You received this message because you are subscribed to the Google Groups "pyglet-users" group.
> To post to this group, send email to pyglet...@googlegroups.com.
> To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.

Roger Flores

unread,
Jun 29, 2012, 3:54:53 AM6/29/12
to pyglet...@googlegroups.com
On Thu, Jun 28, 2012 at 9:48 PM, Winston Wolff <winsto...@gmail.com> wrote:
I don't know an answer but I know there is a test that changes text color. You could look through those.


That was a helpful clue.  The code in there is:
        fnt = font.load(self.font_name, self.font_size)
        self.label = font.Text(fnt, self.text, 10, 10, color=(0, 0, 1, 1))

This uses the font.Text api, which was not recommended because GlyphString is faster.  On the other hand it works, meaning it draws in a color other than white.  I put it into console.py and now I can see red text, so the code is 50% done now.  Now just underline and inverted text are missing.



Reply all
Reply to author
Forward
0 new messages