Finally got back to this. While I was trying to work out how to do
what Alex described I found an alternate method to do it.

Essentially I just get the _style_runs dictionary, and text string
from the document to save it. To restore the state I simply impose
these on a new document. The style operations are controlled by the
methods getStyle and restoreStyle in the demo code below.

Seems to work, without having to understand much outside of the
_style_runs dictionary format.

Is this to simplistic and is there some reason I'll regret doing it
this way?

Regards

john

Here is my new label and code to exercise it in case anyone is
interested. Press A to just impose the styles from "Hello World" on
the other string, press B to copy the text and style from "Hello
World"

class JLabel(DocumentLabel):
    '''Text label.
    '''
    def __init__(self, text='',
                 font_name=None, font_size=None, bold=False,
italic=False,
                 color=(255, 255, 255, 255),
                 x=0, y=0, width=None, height=None,
                 anchor_x='left', anchor_y='baseline',
                 halign='left',
                 multiline=False, dpi=None, batch=None, group=None):
        document = decode_attributed(text)
        super(JLabel, self).__init__(document, x, y, width, height,
                                    anchor_x, anchor_y,
                                    multiline, dpi, batch, group)

        style = dict(halign=halign)

        if font_name:
            style['font_name'] = font_name
        if font_size:
            style['font_size'] = font_size
        if bold:
            style['bold'] = bold
        if italic:
            style['italic'] = italic
        if color:
            style['color'] = color

        self.document.set_style(0, len(self.document.text), style)

    def getStyle(self):
      return self.document._style_runs

    def restoreStyle(self, styleDict):
      for k in styleDict.keys():
        for i in styleDict[k]:
          self.document.set_style(i[0], i[1],  {k : i[2]})

    def backup(self):
      """ Gets the text and style data for a backup

      Returns
      text, styleDict
      """
      return self.document.text, self.getStyle()

    def restore(self, text, style):
      self.document.text = text
      self.restoreStyle(style)

window = pyglet.window.Window()

@window.event
def on_key_press(symbol, modifiers):
  if symbol == key.A:
    print "Change the label"
    label2.restoreStyle(label.getStyle())
  elif symbol == key.B:
    print "Restore the label"
    t, s = label.backup()
    label2.restore(t,s)

label = JLabel('{bold True}Hello,{bold False} world',
                font_name='Times New Roman',
                font_size=36,
                x=window.width//2, y=window.height//2,
                anchor_x='center', anchor_y='center')

label2 = JLabel('Hi one & all',
                font_name='Times New Roman',
                font_size=12,
                x=window.width//4, y=window.height//4,
                anchor_x='center', anchor_y='center')

@window.event
def on_draw():
    window.clear()
    label.draw()
    label2.draw()

pyglet.app.run()


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

Reply via email to