Spaces:
Runtime error
Runtime error
File size: 8,397 Bytes
d82cf6a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
"""Base class for structured (hierarchical) document formats.
"""
import re
import pyglet
from pyglet.gl import *
class _InlineElementGroup(pyglet.graphics.Group):
def __init__(self, texture, program, order=0, parent=None):
super().__init__(order, parent)
self.texture = texture
self.program = program
def set_state(self):
self.program.use()
glActiveTexture(GL_TEXTURE0)
glBindTexture(self.texture.target, self.texture.id)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
def unset_state(self):
glDisable(GL_BLEND)
self.program.stop()
def __eq__(self, other):
return (self.__class__ is other.__class__ and
self._order == other.order and
self.program == other.program and
self.parent == other.parent and
self.texture.target == other.texture.target and
self.texture.id == other.texture.id)
def __hash__(self):
return hash((self._order, self.program, self.parent,
self.texture.target, self.texture.id))
class ImageElement(pyglet.text.document.InlineElement):
def __init__(self, image, width=None, height=None):
self.image = image.get_texture()
self.width = width is None and image.width or width
self.height = height is None and image.height or height
self.vertex_lists = {}
anchor_y = self.height // image.height * image.anchor_y
ascent = max(0, self.height - anchor_y)
descent = min(0, -anchor_y)
super().__init__(ascent, descent, self.width)
def place(self, layout, x, y, z):
program = pyglet.text.layout.get_default_image_layout_shader()
group = _InlineElementGroup(self.image.get_texture(), program, 0, layout.group)
x1 = x
y1 = y + self.descent
x2 = x + self.width
y2 = y + self.height + self.descent
vertex_list = program.vertex_list_indexed(4, pyglet.gl.GL_TRIANGLES, [0, 1, 2, 0, 2, 3],
layout.batch, group,
position=('f', (x1, y1, z, x2, y1, z, x2, y2, z, x1, y2, z)),
tex_coords=('f', self.image.tex_coords))
self.vertex_lists[layout] = vertex_list
def remove(self, layout):
self.vertex_lists[layout].delete()
del self.vertex_lists[layout]
def _int_to_roman(number):
# From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81611
if not 0 < number < 4000:
raise ValueError("Argument must be between 1 and 3999")
integers = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
numerals = ('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
result = ""
for i in range(len(integers)):
count = int(number // integers[i])
result += numerals[i] * count
number -= integers[i] * count
return result
class ListBuilder:
def begin(self, decoder, style):
"""Begin a list.
:Parameters:
`decoder` : `StructuredTextDecoder`
Decoder.
`style` : dict
Style dictionary that applies over the entire list.
"""
left_margin = decoder.current_style.get('margin_left') or 0
tab_stops = decoder.current_style.get('tab_stops')
if tab_stops:
tab_stops = list(tab_stops)
else:
tab_stops = []
tab_stops.append(left_margin + 50)
style['margin_left'] = left_margin + 50
style['indent'] = -30
style['tab_stops'] = tab_stops
def item(self, decoder, style, value=None):
"""Begin a list item.
:Parameters:
`decoder` : `StructuredTextDecoder`
Decoder.
`style` : dict
Style dictionary that applies over the list item.
`value` : str
Optional value of the list item. The meaning is list-type
dependent.
"""
mark = self.get_mark(value)
if mark:
decoder.add_text(mark)
decoder.add_text('\t')
def get_mark(self, value=None):
"""Get the mark text for the next list item.
:Parameters:
`value` : str
Optional value of the list item. The meaning is list-type
dependent.
:rtype: str
"""
return ''
class UnorderedListBuilder(ListBuilder):
def __init__(self, mark):
"""Create an unordered list with constant mark text.
:Parameters:
`mark` : str
Mark to prepend to each list item.
"""
self.mark = mark
def get_mark(self, value):
return self.mark
class OrderedListBuilder(ListBuilder):
format_re = re.compile('(.*?)([1aAiI])(.*)')
def __init__(self, start, fmt):
"""Create an ordered list with sequentially numbered mark text.
The format is composed of an optional prefix text, a numbering
scheme character followed by suffix text. Valid numbering schemes
are:
``1``
Decimal Arabic
``a``
Lowercase alphanumeric
``A``
Uppercase alphanumeric
``i``
Lowercase Roman
``I``
Uppercase Roman
Prefix text may typically be ``(`` or ``[`` and suffix text is
typically ``.``, ``)`` or empty, but either can be any string.
:Parameters:
`start` : int
First list item number.
`fmt` : str
Format style, for example ``"1."``.
"""
self.next_value = start
self.prefix, self.numbering, self.suffix = self.format_re.match(fmt).groups()
assert self.numbering in '1aAiI'
def get_mark(self, value):
if value is None:
value = self.next_value
self.next_value = value + 1
if self.numbering in 'aA':
try:
mark = 'abcdefghijklmnopqrstuvwxyz'[value - 1]
except ValueError:
mark = '?'
if self.numbering == 'A':
mark = mark.upper()
return f'{self.prefix}{mark}{self.suffix}'
elif self.numbering in 'iI':
try:
mark = _int_to_roman(value)
except ValueError:
mark = '?'
if self.numbering == 'i':
mark = mark.lower()
return f'{self.prefix}{mark}{self.suffix}'
else:
return f'{self.prefix}{value}{self.suffix}'
class StructuredTextDecoder(pyglet.text.DocumentDecoder):
def decode(self, text, location=None):
self.len_text = 0
self.current_style = {}
self.next_style = {}
self.stack = []
self.list_stack = []
self.document = pyglet.text.document.FormattedDocument()
if location is None:
location = pyglet.resource.FileLocation('')
self.decode_structured(text, location)
return self.document
def decode_structured(self, text, location):
raise NotImplementedError('abstract')
def push_style(self, key, styles):
old_styles = {}
for name in styles.keys():
old_styles[name] = self.current_style.get(name)
self.stack.append((key, old_styles))
self.current_style.update(styles)
self.next_style.update(styles)
def pop_style(self, key):
# Don't do anything if key is not in stack
for match, _ in self.stack:
if key == match:
break
else:
return
# Remove all innermost elements until key is closed.
while True:
match, old_styles = self.stack.pop()
self.next_style.update(old_styles)
self.current_style.update(old_styles)
if match == key:
break
def add_text(self, text):
self.document.insert_text(self.len_text, text, self.next_style)
self.next_style.clear()
self.len_text += len(text)
def add_element(self, element):
self.document.insert_element(self.len_text, element, self.next_style)
self.next_style.clear()
self.len_text += 1
|