Here's my first attempt at transitioning to the IncrementalTextLayout
class. It's quite a bit more complicated than the Label.
I'm creating the IncrementalTextLayout like this:
self.log_layout = pyglet.text.layout.IncrementalTextLayout(
pyglet.text.document.UnformattedDocument(""),
dpi=72,
multiline=True,
width=100*self.col_spacing,
height=40*self.row_spacing)
with these values for the variables:
self.log_font = pyglet.font.load('Monaco',12,dpi=72)
self.col_spacing = pyglet.font.Text(self.font, 'a').width
self.row_spacing = self.log_font.ascent - self.log_font.descent + self.leading
and then I call this function to add the first line to the layout:
def add_event(self, msg):
prefix = time.strftime("%b %d %H:%M:%S ")
self.log_layout.begin_update()
for line in msg.split('\n'):
self.log_layout.document.insert_text(-1, prefix+line)
prefix = ""
self.log_layout.end_update()
with this for the value of msg:
msg = "Time has passed in the main loop."
and I immediately get this:
Traceback (most recent call last):
[snip Twisted section of the traceback]
File "./monitor", line 188, in update
monitor_window.log_window.add_event(info)
File "/sm/engine/modules/widgets.py", line 226, in add_event
self.log_layout.end_update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
754, in end_update
self._update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1728, in _update
self._update_flow_lines()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1842, in _update_flow_lines
invalid_end = self._flow_lines(self.lines, invalid_start, invalid_end)
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1273, in _flow_lines
y -= leading
exceptions.UnboundLocalError: local variable 'leading' referenced
before assignment
So I investigated, and since "leading" was set twice in the function
in odd conditional branches, but only attempted to be read once, I
tried this:
Index: pyglet/text/layout.py
===================================================================
--- pyglet/text/layout.py (revision 2024)
+++ pyglet/text/layout.py (working copy)
@@ -1253,8 +1253,6 @@
line = lines[start - 1]
line_spacing = \
self._points_to_pixels(line_spacing_iterator[line.start])
- leading = \
- self._points_to_pixels(leading_iterator[line.start])
y = line.y
if line_spacing is None:
@@ -1268,9 +1266,8 @@
y -= margin_top_iterator[line.start]
line_spacing = \
self._points_to_pixels(line_spacing_iterator[line.start])
- leading = self._points_to_pixels(leading_iterator[line.start])
else:
- y -= leading
+ y -= self._points_to_pixels(leading_iterator[line.start])
if line_spacing is None:
y -= line.ascent
Which changed the crash to this:
Traceback (most recent call last):
[snip]
File "./monitor", line 188, in update
monitor_window.log_window.add_event(info)
File "/sm/engine/modules/widgets.py", line 226, in add_event
self.log_layout.end_update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
754, in end_update
self._update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1725, in _update
self._update_flow_lines()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1839, in _update_flow_lines
invalid_end = self._flow_lines(self.lines, invalid_start, invalid_end)
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1272, in _flow_lines
if line_spacing is None:
exceptions.UnboundLocalError: local variable 'line_spacing' referenced
before assignment
Ironically, that's the _very_ next line from the last crash. So I
tried adding one more line at the start of the offending loop:
Index: pyglet/text/layout.py
===================================================================
--- pyglet/text/layout.py (revision 2024)
+++ pyglet/text/layout.py (working copy)
@@ -1253,8 +1253,6 @@
line = lines[start - 1]
line_spacing = \
self._points_to_pixels(line_spacing_iterator[line.start])
- leading = \
- self._points_to_pixels(leading_iterator[line.start])
y = line.y
if line_spacing is None:
@@ -1264,13 +1262,13 @@
line_index = start
for line in lines[start:]:
+ line_spacing = None
if line.paragraph_begin:
y -= margin_top_iterator[line.start]
line_spacing = \
self._points_to_pixels(line_spacing_iterator[line.start])
- leading = self._points_to_pixels(leading_iterator[line.start])
else:
- y -= leading
+ y -= self._points_to_pixels(leading_iterator[line.start])
if line_spacing is None:
y -= line.ascent
Which got me further, but still...
Traceback (most recent call last):
[snip]
File "./monitor", line 188, in update
monitor_window.log_window.add_event(info)
File "/sm/engine/modules/widgets.py", line 226, in add_event
self.log_layout.end_update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
754, in end_update
self._update()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1724, in _update
self._update_glyphs()
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
1753, in _update_glyphs
self.owner_runs, self.glyphs, invalid_start, invalid_end)
File "/Library/Python/2.5/site-packages/pyglet/text/layout.py", line
942, in _get_owner_runs
owner_runs.set_run(run_start, end, owner)
File "/Library/Python/2.5/site-packages/pyglet/text/runlist.py",
line 180, in set_run
last_run = self.runs[0]
exceptions.IndexError: list index out of range
...but I understand the runlist.py code even less than the layout.py
coe. Since I'm not 100% positive my previous changes didn't _cause_
this new problem, I thought I'd stop and write this error report. :-)
Help!
~ Nathan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---