pico_utils
Utilities not used anywhere else (but can be used from your own scripts)
1"""Utilities not used anywhere else (but can be used from your own scripts)""" 2 3from utils import * 4from pico_defs import decode_p8str 5 6k_ctrl_chars = Dynamic( 7 end=chr(0), 8 rep=chr(1), 9 bg=chr(2), 10 horz=chr(3), 11 vert=chr(4), 12 move=chr(5), 13 cmd=chr(6), 14 audio=chr(7), 15 back=chr(8), 16 tab=chr(9), 17 line=chr(10), 18 decor=chr(11), 19 fg=chr(12), 20 ret=chr(13), 21 cfont=chr(14), 22 dfont=chr(15), 23) 24 25k_ctrl_cmds = Dynamic( 26 skips = "123456789", 27 clear = 'c', 28 delay = 'd', 29 home = 'g', 30 jump = 'j', 31 tab_stop = 's', 32 wrap = 'r', 33 ch_width = 'x', 34 ch_height = 'y', 35 wide = 'w', 36 tall = 't', 37 stripe = '=', 38 pinball = 'p', 39 invert = 'i', 40 border = 'b', 41 solid = '#', 42 disable = '-', 43 poke = '@', 44 endpoke = '!', 45 char = '.', 46 hexchar = ':', 47) 48 49k_ctrl_flag_cmds = (k_ctrl_cmds.wide, k_ctrl_cmds.tall, k_ctrl_cmds.stripe, k_ctrl_cmds.pinball, k_ctrl_cmds.invert, k_ctrl_cmds.border, k_ctrl_cmds.solid) 50 51class PicoFont(Tuple): 52 width = 4; height = 6; wide_width = 8; tab_width = 16 53 width_adjusts = (); is_custom = True 54 55k_pico_font = PicoFont(is_custom=False) 56 57def get_p8scii_param(str, pos): 58 ch = str_get(str, pos, '') 59 if '0' <= ch <= '9': 60 return ord(ch) - ord('0') 61 elif ch >= 'a': # all the way up 62 return ord(ch) - ord('a') + 10 63 else: 64 return 0 65 66# WARNING: a lot of this is not tested yet 67def parse_p8scii(str): 68 """Parse a pico8 string (`p8str`) that may contain p8scii sequences, yielding them in parsed form""" 69 70 start = 0 71 while start < len(str): 72 end = start 73 while end < len(str) and str[end] >= chr(16): 74 end += 1 75 76 if end > start: 77 yield start, str[start:end] 78 if end >= len(str): 79 break 80 81 start = end 82 cch = str[start] 83 pos = start + 1 84 length = 0 85 86 if cch == k_ctrl_chars.end: 87 yield start, Dynamic(type=cch, rest=str[pos:]) 88 return 89 90 elif cch == k_ctrl_chars.rep: 91 val = get_p8scii_param(str, pos) 92 ch = str_get(str, pos + 1, '') 93 length = 2 94 yield start, Dynamic(type=cch, count=val, char=ch) 95 96 elif cch in (k_ctrl_chars.bg, k_ctrl_chars.fg): 97 val = get_p8scii_param(str, pos) 98 length = 1 99 yield start, Dynamic(type=cch, color=val) 100 101 elif cch == k_ctrl_chars.horz: 102 val = get_p8scii_param(str, pos) - 16 103 length = 1 104 yield start, Dynamic(type=cch, horz=val, vert=0) 105 106 elif cch == k_ctrl_chars.vert: 107 val = get_p8scii_param(str, pos) - 16 108 length = 1 109 yield start, Dynamic(type=cch, horz=0, vert=val) 110 111 elif cch == k_ctrl_chars.move: 112 hval = get_p8scii_param(str, pos) - 16 113 vval = get_p8scii_param(str, pos + 1) - 16 114 length = 2 115 yield start, Dynamic(type=cch, horz=hval, vert=vval) 116 117 elif cch == k_ctrl_chars.decor: 118 val = get_p8scii_param(str, pos) 119 ch = str_get(str, pos + 1, '') 120 length = 2 121 hval, vval = (val & 0x3) - 2, (val >> 2) - 8 122 yield start, Dynamic(type=cch, horz=hval, vert=vval, char=ch) 123 124 elif cch == k_ctrl_chars.audio: 125 endpos = pos 126 while endpos < len(str) and str[endpos] not in " \n": # TODO: inexact... 127 endpos += 1 128 yield start, Dynamic(type=cch, sound=str[pos:endpos]) 129 length = endpos + 1 - pos 130 131 elif cch == k_ctrl_chars.cmd: 132 cmd = str_get(str, pos) 133 if cmd in (k_ctrl_cmds.delay, k_ctrl_cmds.tab_stop, k_ctrl_chars.ch_width, k_ctrl_chars.ch_height): 134 val = get_p8scii_param(str, pos + 1) 135 length = 2 136 yield start, Dynamic(type=cmd, count=val) 137 elif cmd == k_ctrl_cmds.clear: 138 val = get_p8scii_param(str, pos + 1) 139 length = 2 140 yield start, Dynamic(type=cmd, color=val) 141 elif cmd == k_ctrl_cmds.jump: 142 hval = get_p8scii_param(str, pos + 1) * 4 143 vval = get_p8scii_param(str, pos + 2) * 4 144 length = 3 145 yield start, Dynamic(type=cmd, horz=hval, vert=vval) 146 elif cmd == k_ctrl_cmds.wrap: 147 val = get_p8scii_param(str, pos + 1) * 4 148 length = 2 149 yield start, Dynamic(type=cmd, count=val) 150 elif cmd in k_ctrl_flag_cmds: 151 length = 1 152 yield start, Dynamic(type=cmd, enable=True) 153 elif cmd == k_ctrl_cmds.disable: 154 cmd2 = str_get(str, pos + 1) 155 cmd = cmd2 if cmd in k_ctrl_flag_cmds else cmd 156 length = 2 157 yield start, Dynamic(type=cmd, enable=False) 158 elif cmd == k_ctrl_cmds.poke: 159 addr = maybe_int(str[pos + 1 : pos + 5], base=16) 160 count = maybe_int(str[pos + 5 : pos + 9], base=16) 161 data_len = default(count, 0) 162 data = str[pos + 9 : pos + 9 + data_len] 163 length = 9 + data_len 164 yield start, Dynamic(type=cmd, addr=addr, count=count, data=data) 165 elif cmd == k_ctrl_cmds.endpoke: 166 addr = maybe_int(str[pos + 1 : pos + 5], base=16) 167 data = str[pos + 5 :] 168 yield start, Dynamic(type=cmd, addr=addr, data=data) 169 return 170 elif cmd == k_ctrl_cmds.char: 171 data = str[pos + 1 : pos + 9] 172 length = 9 173 yield start, Dynamic(type=cmd, data=data) 174 elif cmd == k_ctrl_cmds.hexchar: 175 data = str[pos + 1 : pos + 17] 176 length = 17 177 yield start, Dynamic(type=cmd, data=data) 178 else: 179 length = 1 180 yield start, Dynamic(type=cmd) 181 182 else: 183 yield start, Dynamic(type=cch) 184 185 start = pos + length 186 187class P8sciiFlags(Bitmask): 188 wide = tall = pinball = custom_font = wrapped = ... # corresponds to pico8 state 189 # invert = border = solid = stripe = ... (this pico8 state doesn't matter) 190 word_wrap = ... # custom flags 191 none = 0 192 193class MeasurerHookBase: 194 def on_line_end(m, measurer, pos): 195 pass 196 197# WARNING: a lot of this is not tested yet 198class P8sciiMeasurer: 199 def __init__(m, *, pos=Point.zero, flags=P8sciiFlags.none, wrap_width=None, font=k_pico_font, hook=None): 200 m.start_x, m.start_y = pos.x, pos.y 201 m.x, m.y = pos.x, pos.y 202 m.max_x, m.max_y = pos.x, pos.y 203 m.custom_font = font 204 m.flags = flags 205 m.wrap_w = 128 if flags.wrapped else sys.maxsize 206 m.word_wrap_w = default(wrap_width, 128) if flags.word_wrap else sys.maxsize 207 m.word_wrap = flags.word_wrap 208 m.wraps = [] 209 m.saved_wrap = None 210 m.hook = hook 211 m.update_font(m.cutsom_font if m.flags.custom_font else k_pico_font) 212 m.update_wide() 213 m.curr_ch_h = m.ch_h 214 215 def update_font(m, font): 216 m.font = font 217 m.ch_w, m.ch_h = font.width, font.height 218 m.big_ch_w = font.wide_width 219 m.last_ch_w = m.ch_w 220 m.tab_w = font.tab_width 221 222 def update_wide(m): 223 m.wide_x = m.flags.wide or m.flags.pinball 224 m.wide_y = m.flags.tall or m.flags.pinball 225 226 def advance_line(m, pos): 227 if m.hook: 228 m.hook.on_line_end(m, pos) 229 230 m.x = m.start_x 231 m.y += m.curr_ch_h 232 m.curr_ch_h = m.ch_h 233 m.max_y = max(m.y, m.max_y) 234 235 def process_rawch(m, pos, ch_w, ch_h): 236 if m.wide_x: 237 ch_w *= 2 238 if m.wide_y: 239 ch_h *= 2 240 241 m.last_ch_w = ch_w 242 m.x += ch_w 243 244 if m.word_wrap and m.x > m.word_wrap_w: 245 # prepare manual wrapping (won't work well with curr_ch_h currently...) 246 if m.saved_wrap: 247 wrap_pos, wrap_x, replace = m.saved_wrap 248 m.wraps.append((wrap_pos, replace)) 249 m.saved_wrap = None 250 else: 251 wrap_x = m.x - ch_w 252 m.wraps.append((pos, False)) 253 254 post_wrap_w = m.x - wrap_x 255 m.x = wrap_x # just for the line hook 256 m.advance_line(pos) 257 m.x += post_wrap_w 258 259 elif m.x > m.wrap_w: 260 m.advance_line(pos) 261 m.x += ch_w 262 263 m.max_x = max(m.x, m.max_x) 264 m.curr_ch_h = max(m.curr_ch_h, ch_h) 265 266 def process_rawline(m, start, line): 267 for i, ch in enumerate(line): 268 pos = start + i 269 270 ch_w = m.big_ch_w if ord(ch) >= 128 else m.ch_w 271 if m.font.width_adjusts: 272 ch_w += list_get(m.font.width_adjusts, ord(ch), 0) 273 274 if m.word_wrap and ch == ' ': 275 m.saved_wrap = pos, m.x + ch_w, True 276 277 m.process_rawch(pos, ch_w, m.ch_h) 278 279 def move_to(m, x, y): 280 m.x, m.y = x, y 281 m.max_x = max(m.x, m.max_x) 282 m.max_y = max(m.y, m.max_y) 283 284 def process(m, text): 285 for start, cmd in parse_p8scii(text): 286 if isinstance(cmd, str): 287 m.process_rawline(start, cmd) 288 elif cmd.type == k_ctrl_chars.rep: 289 m.process_rawline(cmd.char * cmd.count) 290 elif cmd.type == (k_ctrl_chars.horz, k_ctrl_chars.vert, k_ctrl_chars.move): 291 m.move_to(m.x + cmd.horz, m.y + cmd.vert) 292 elif cmd.type == k_ctrl_chars.back: 293 m.x -= m.last_ch_w 294 elif cmd.type == k_ctrl_chars.tab: 295 tab_delta = m.tab_w - m.x % m.tab_w 296 m.move_to(m.x + tab_delta, m.y) 297 elif cmd.type == k_ctrl_chars.line: 298 m.advance_line(start) 299 elif cmd.type == k_ctrl_chars.ret: 300 m.x = m.start_x 301 elif cmd.type == k_ctrl_chars.cfont: 302 m.update_font(m.custom_font) 303 elif cmd.type == k_ctrl_chars.dfont: 304 m.update_font(k_pico_font) 305 elif cmd.type == k_ctrl_cmds.home: 306 m.x, m.y = m.start_x, m.start_y 307 elif cmd.type == k_ctrl_cmds.jump: 308 m.move_to(cmd.horz, cmd.vert) 309 elif cmd.type == k_ctrl_cmds.tab_stop: 310 m.tab_w = cmd.count 311 elif cmd.type == k_ctrl_cmds.wrap: 312 m.wrap_w = cmd.count 313 elif cmd.type == k_ctrl_cmds.ch_width: 314 m.ch_w = cmd.count 315 m.big_ch_w = cmd.count + m.font.wide_width - m.font.width 316 elif cmd.type == k_ctrl_cmds.ch_height: 317 m.ch_h = cmd.count 318 elif cmd.type in (k_ctrl_cmds.char, k_ctrl_cmds.hexchar): 319 m.process_rawch(start, 8, 8) 320 elif cmd.type == k_ctrl_cmds.wide: 321 m.flags.wide = cmd.enable 322 m.update_wide() 323 elif cmd.type == k_ctrl_cmds.tall: 324 m.flags.tall = cmd.enable 325 m.update_wide() 326 elif cmd.type == k_ctrl_cmds.pinball: 327 m.flags.pinball = cmd.enable 328 m.update_wide() 329 330 if m.word_wrap: 331 delta = 0 332 for pos, replace in m.wraps: 333 pos += delta 334 if replace: 335 text = str_replace_at(text, pos, 1, "\n") 336 else: 337 text = str_insert(text, pos, "\n") 338 delta += 1 339 340 m.wraps.clear() 341 if m.x > m.start_x: 342 m.saved_wrap = 0, m.x, False 343 return text 344 # else, nothing to return 345 346 def finish(m): 347 m.advance_line(0) 348 return Point(m.max_x, m.max_y) 349 350def measure_p8scii(text, **opts): 351 """Measures the width of a p8scii string (see P8sciiMeasurer's ctor for which params to give)""" 352 measurer = P8sciiMeasurer(**opts) 353 text = measurer.process(text) 354 if e(text): 355 return measurer.finish(), text 356 else: 357 return measurer.finish() 358 359def bytes_to_string_contents(input): 360 """convert a p8str or bytes objects to a pico8 string literal, without the surrounding "-s""" 361 from pico_output import format_string_literal # already implemented here... 362 if not isinstance(input, str): 363 input = decode_p8str(input) 364 return format_string_literal(input, long=False, quote='"')[1:-1]
52class PicoFont(Tuple): 53 width = 4; height = 6; wide_width = 8; tab_width = 16 54 width_adjusts = (); is_custom = True
If a class has Tuple as a baseclass, it's transformed into a named tuple. All non-private attributes of the class other than functions and descriptors become tuple fields. Atributes with a value of ... are required, otherwise - optional with the given default value. The tuple behaves similarly to collections.namedtuple Inherited classes behave like regular classes unless they have Tuple as a direct baseclass. Advanced: ExcludeAttr/IncludeAttr, _values keyword parameter, const base fields keyword parameters Atributes with a name ending with _ can be referred to without the _.
Return a callable object that fetches the given item(s) from its operand. After f = itemgetter(2), the call f(r) returns r[2]. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
Return a callable object that fetches the given item(s) from its operand. After f = itemgetter(2), the call f(r) returns r[2]. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
Return a callable object that fetches the given item(s) from its operand. After f = itemgetter(2), the call f(r) returns r[2]. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
Return a callable object that fetches the given item(s) from its operand. After f = itemgetter(2), the call f(r) returns r[2]. After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
68def parse_p8scii(str): 69 """Parse a pico8 string (`p8str`) that may contain p8scii sequences, yielding them in parsed form""" 70 71 start = 0 72 while start < len(str): 73 end = start 74 while end < len(str) and str[end] >= chr(16): 75 end += 1 76 77 if end > start: 78 yield start, str[start:end] 79 if end >= len(str): 80 break 81 82 start = end 83 cch = str[start] 84 pos = start + 1 85 length = 0 86 87 if cch == k_ctrl_chars.end: 88 yield start, Dynamic(type=cch, rest=str[pos:]) 89 return 90 91 elif cch == k_ctrl_chars.rep: 92 val = get_p8scii_param(str, pos) 93 ch = str_get(str, pos + 1, '') 94 length = 2 95 yield start, Dynamic(type=cch, count=val, char=ch) 96 97 elif cch in (k_ctrl_chars.bg, k_ctrl_chars.fg): 98 val = get_p8scii_param(str, pos) 99 length = 1 100 yield start, Dynamic(type=cch, color=val) 101 102 elif cch == k_ctrl_chars.horz: 103 val = get_p8scii_param(str, pos) - 16 104 length = 1 105 yield start, Dynamic(type=cch, horz=val, vert=0) 106 107 elif cch == k_ctrl_chars.vert: 108 val = get_p8scii_param(str, pos) - 16 109 length = 1 110 yield start, Dynamic(type=cch, horz=0, vert=val) 111 112 elif cch == k_ctrl_chars.move: 113 hval = get_p8scii_param(str, pos) - 16 114 vval = get_p8scii_param(str, pos + 1) - 16 115 length = 2 116 yield start, Dynamic(type=cch, horz=hval, vert=vval) 117 118 elif cch == k_ctrl_chars.decor: 119 val = get_p8scii_param(str, pos) 120 ch = str_get(str, pos + 1, '') 121 length = 2 122 hval, vval = (val & 0x3) - 2, (val >> 2) - 8 123 yield start, Dynamic(type=cch, horz=hval, vert=vval, char=ch) 124 125 elif cch == k_ctrl_chars.audio: 126 endpos = pos 127 while endpos < len(str) and str[endpos] not in " \n": # TODO: inexact... 128 endpos += 1 129 yield start, Dynamic(type=cch, sound=str[pos:endpos]) 130 length = endpos + 1 - pos 131 132 elif cch == k_ctrl_chars.cmd: 133 cmd = str_get(str, pos) 134 if cmd in (k_ctrl_cmds.delay, k_ctrl_cmds.tab_stop, k_ctrl_chars.ch_width, k_ctrl_chars.ch_height): 135 val = get_p8scii_param(str, pos + 1) 136 length = 2 137 yield start, Dynamic(type=cmd, count=val) 138 elif cmd == k_ctrl_cmds.clear: 139 val = get_p8scii_param(str, pos + 1) 140 length = 2 141 yield start, Dynamic(type=cmd, color=val) 142 elif cmd == k_ctrl_cmds.jump: 143 hval = get_p8scii_param(str, pos + 1) * 4 144 vval = get_p8scii_param(str, pos + 2) * 4 145 length = 3 146 yield start, Dynamic(type=cmd, horz=hval, vert=vval) 147 elif cmd == k_ctrl_cmds.wrap: 148 val = get_p8scii_param(str, pos + 1) * 4 149 length = 2 150 yield start, Dynamic(type=cmd, count=val) 151 elif cmd in k_ctrl_flag_cmds: 152 length = 1 153 yield start, Dynamic(type=cmd, enable=True) 154 elif cmd == k_ctrl_cmds.disable: 155 cmd2 = str_get(str, pos + 1) 156 cmd = cmd2 if cmd in k_ctrl_flag_cmds else cmd 157 length = 2 158 yield start, Dynamic(type=cmd, enable=False) 159 elif cmd == k_ctrl_cmds.poke: 160 addr = maybe_int(str[pos + 1 : pos + 5], base=16) 161 count = maybe_int(str[pos + 5 : pos + 9], base=16) 162 data_len = default(count, 0) 163 data = str[pos + 9 : pos + 9 + data_len] 164 length = 9 + data_len 165 yield start, Dynamic(type=cmd, addr=addr, count=count, data=data) 166 elif cmd == k_ctrl_cmds.endpoke: 167 addr = maybe_int(str[pos + 1 : pos + 5], base=16) 168 data = str[pos + 5 :] 169 yield start, Dynamic(type=cmd, addr=addr, data=data) 170 return 171 elif cmd == k_ctrl_cmds.char: 172 data = str[pos + 1 : pos + 9] 173 length = 9 174 yield start, Dynamic(type=cmd, data=data) 175 elif cmd == k_ctrl_cmds.hexchar: 176 data = str[pos + 1 : pos + 17] 177 length = 17 178 yield start, Dynamic(type=cmd, data=data) 179 else: 180 length = 1 181 yield start, Dynamic(type=cmd) 182 183 else: 184 yield start, Dynamic(type=cch) 185 186 start = pos + length
Parse a pico8 string (p8str) that may contain p8scii sequences, yielding them in parsed form
188class P8sciiFlags(Bitmask): 189 wide = tall = pinball = custom_font = wrapped = ... # corresponds to pico8 state 190 # invert = border = solid = stripe = ... (this pico8 state doesn't matter) 191 word_wrap = ... # custom flags 192 none = 0
If a class has Bitmask as a baseclass, it's transformed into a bitmask. All non-private attributes of the class other than functions and descriptors become bitmask fields. Atributes with a value of ... receive bits automatically. A bitmask is like a struct, but with its fields mapped into a single bitmask integer. Bitmasks can also be manipulated via: & | ^ Inherited classes behave like regular classes unless they have Bitmask as a direct baseclass. Advanced: ExcludeAttr/IncludeAttr, _values keyword parameter Atributes with a name ending with _ can be referred to without the _.
272 def __init__(m, value = None, **kwargs): 273 if value is None: 274 m.value = 0 275 elif isinstance(value, int) and (value & full_mask) == value: 276 m.value = value 277 elif isinstance(value, str) and value in full_name_mask_map: 278 m.value = full_name_mask_map[value] 279 elif isinstance(value, m.__class__): 280 m.value = value.value 281 elif isinstance(value, (tuple, list)): 282 m.value = reduce(lambda x,y:x|y, (full_name_mask_map[part] for part in value), 0) 283 else: 284 raise ValueError(f"{value} not value of {cls_name}") 285 286 if kwargs: 287 for key, value in kwargs.items(): 288 setattr(m, key, value)
199class P8sciiMeasurer: 200 def __init__(m, *, pos=Point.zero, flags=P8sciiFlags.none, wrap_width=None, font=k_pico_font, hook=None): 201 m.start_x, m.start_y = pos.x, pos.y 202 m.x, m.y = pos.x, pos.y 203 m.max_x, m.max_y = pos.x, pos.y 204 m.custom_font = font 205 m.flags = flags 206 m.wrap_w = 128 if flags.wrapped else sys.maxsize 207 m.word_wrap_w = default(wrap_width, 128) if flags.word_wrap else sys.maxsize 208 m.word_wrap = flags.word_wrap 209 m.wraps = [] 210 m.saved_wrap = None 211 m.hook = hook 212 m.update_font(m.cutsom_font if m.flags.custom_font else k_pico_font) 213 m.update_wide() 214 m.curr_ch_h = m.ch_h 215 216 def update_font(m, font): 217 m.font = font 218 m.ch_w, m.ch_h = font.width, font.height 219 m.big_ch_w = font.wide_width 220 m.last_ch_w = m.ch_w 221 m.tab_w = font.tab_width 222 223 def update_wide(m): 224 m.wide_x = m.flags.wide or m.flags.pinball 225 m.wide_y = m.flags.tall or m.flags.pinball 226 227 def advance_line(m, pos): 228 if m.hook: 229 m.hook.on_line_end(m, pos) 230 231 m.x = m.start_x 232 m.y += m.curr_ch_h 233 m.curr_ch_h = m.ch_h 234 m.max_y = max(m.y, m.max_y) 235 236 def process_rawch(m, pos, ch_w, ch_h): 237 if m.wide_x: 238 ch_w *= 2 239 if m.wide_y: 240 ch_h *= 2 241 242 m.last_ch_w = ch_w 243 m.x += ch_w 244 245 if m.word_wrap and m.x > m.word_wrap_w: 246 # prepare manual wrapping (won't work well with curr_ch_h currently...) 247 if m.saved_wrap: 248 wrap_pos, wrap_x, replace = m.saved_wrap 249 m.wraps.append((wrap_pos, replace)) 250 m.saved_wrap = None 251 else: 252 wrap_x = m.x - ch_w 253 m.wraps.append((pos, False)) 254 255 post_wrap_w = m.x - wrap_x 256 m.x = wrap_x # just for the line hook 257 m.advance_line(pos) 258 m.x += post_wrap_w 259 260 elif m.x > m.wrap_w: 261 m.advance_line(pos) 262 m.x += ch_w 263 264 m.max_x = max(m.x, m.max_x) 265 m.curr_ch_h = max(m.curr_ch_h, ch_h) 266 267 def process_rawline(m, start, line): 268 for i, ch in enumerate(line): 269 pos = start + i 270 271 ch_w = m.big_ch_w if ord(ch) >= 128 else m.ch_w 272 if m.font.width_adjusts: 273 ch_w += list_get(m.font.width_adjusts, ord(ch), 0) 274 275 if m.word_wrap and ch == ' ': 276 m.saved_wrap = pos, m.x + ch_w, True 277 278 m.process_rawch(pos, ch_w, m.ch_h) 279 280 def move_to(m, x, y): 281 m.x, m.y = x, y 282 m.max_x = max(m.x, m.max_x) 283 m.max_y = max(m.y, m.max_y) 284 285 def process(m, text): 286 for start, cmd in parse_p8scii(text): 287 if isinstance(cmd, str): 288 m.process_rawline(start, cmd) 289 elif cmd.type == k_ctrl_chars.rep: 290 m.process_rawline(cmd.char * cmd.count) 291 elif cmd.type == (k_ctrl_chars.horz, k_ctrl_chars.vert, k_ctrl_chars.move): 292 m.move_to(m.x + cmd.horz, m.y + cmd.vert) 293 elif cmd.type == k_ctrl_chars.back: 294 m.x -= m.last_ch_w 295 elif cmd.type == k_ctrl_chars.tab: 296 tab_delta = m.tab_w - m.x % m.tab_w 297 m.move_to(m.x + tab_delta, m.y) 298 elif cmd.type == k_ctrl_chars.line: 299 m.advance_line(start) 300 elif cmd.type == k_ctrl_chars.ret: 301 m.x = m.start_x 302 elif cmd.type == k_ctrl_chars.cfont: 303 m.update_font(m.custom_font) 304 elif cmd.type == k_ctrl_chars.dfont: 305 m.update_font(k_pico_font) 306 elif cmd.type == k_ctrl_cmds.home: 307 m.x, m.y = m.start_x, m.start_y 308 elif cmd.type == k_ctrl_cmds.jump: 309 m.move_to(cmd.horz, cmd.vert) 310 elif cmd.type == k_ctrl_cmds.tab_stop: 311 m.tab_w = cmd.count 312 elif cmd.type == k_ctrl_cmds.wrap: 313 m.wrap_w = cmd.count 314 elif cmd.type == k_ctrl_cmds.ch_width: 315 m.ch_w = cmd.count 316 m.big_ch_w = cmd.count + m.font.wide_width - m.font.width 317 elif cmd.type == k_ctrl_cmds.ch_height: 318 m.ch_h = cmd.count 319 elif cmd.type in (k_ctrl_cmds.char, k_ctrl_cmds.hexchar): 320 m.process_rawch(start, 8, 8) 321 elif cmd.type == k_ctrl_cmds.wide: 322 m.flags.wide = cmd.enable 323 m.update_wide() 324 elif cmd.type == k_ctrl_cmds.tall: 325 m.flags.tall = cmd.enable 326 m.update_wide() 327 elif cmd.type == k_ctrl_cmds.pinball: 328 m.flags.pinball = cmd.enable 329 m.update_wide() 330 331 if m.word_wrap: 332 delta = 0 333 for pos, replace in m.wraps: 334 pos += delta 335 if replace: 336 text = str_replace_at(text, pos, 1, "\n") 337 else: 338 text = str_insert(text, pos, "\n") 339 delta += 1 340 341 m.wraps.clear() 342 if m.x > m.start_x: 343 m.saved_wrap = 0, m.x, False 344 return text 345 # else, nothing to return 346 347 def finish(m): 348 m.advance_line(0) 349 return Point(m.max_x, m.max_y)
200 def __init__(m, *, pos=Point.zero, flags=P8sciiFlags.none, wrap_width=None, font=k_pico_font, hook=None): 201 m.start_x, m.start_y = pos.x, pos.y 202 m.x, m.y = pos.x, pos.y 203 m.max_x, m.max_y = pos.x, pos.y 204 m.custom_font = font 205 m.flags = flags 206 m.wrap_w = 128 if flags.wrapped else sys.maxsize 207 m.word_wrap_w = default(wrap_width, 128) if flags.word_wrap else sys.maxsize 208 m.word_wrap = flags.word_wrap 209 m.wraps = [] 210 m.saved_wrap = None 211 m.hook = hook 212 m.update_font(m.cutsom_font if m.flags.custom_font else k_pico_font) 213 m.update_wide() 214 m.curr_ch_h = m.ch_h
236 def process_rawch(m, pos, ch_w, ch_h): 237 if m.wide_x: 238 ch_w *= 2 239 if m.wide_y: 240 ch_h *= 2 241 242 m.last_ch_w = ch_w 243 m.x += ch_w 244 245 if m.word_wrap and m.x > m.word_wrap_w: 246 # prepare manual wrapping (won't work well with curr_ch_h currently...) 247 if m.saved_wrap: 248 wrap_pos, wrap_x, replace = m.saved_wrap 249 m.wraps.append((wrap_pos, replace)) 250 m.saved_wrap = None 251 else: 252 wrap_x = m.x - ch_w 253 m.wraps.append((pos, False)) 254 255 post_wrap_w = m.x - wrap_x 256 m.x = wrap_x # just for the line hook 257 m.advance_line(pos) 258 m.x += post_wrap_w 259 260 elif m.x > m.wrap_w: 261 m.advance_line(pos) 262 m.x += ch_w 263 264 m.max_x = max(m.x, m.max_x) 265 m.curr_ch_h = max(m.curr_ch_h, ch_h)
267 def process_rawline(m, start, line): 268 for i, ch in enumerate(line): 269 pos = start + i 270 271 ch_w = m.big_ch_w if ord(ch) >= 128 else m.ch_w 272 if m.font.width_adjusts: 273 ch_w += list_get(m.font.width_adjusts, ord(ch), 0) 274 275 if m.word_wrap and ch == ' ': 276 m.saved_wrap = pos, m.x + ch_w, True 277 278 m.process_rawch(pos, ch_w, m.ch_h)
285 def process(m, text): 286 for start, cmd in parse_p8scii(text): 287 if isinstance(cmd, str): 288 m.process_rawline(start, cmd) 289 elif cmd.type == k_ctrl_chars.rep: 290 m.process_rawline(cmd.char * cmd.count) 291 elif cmd.type == (k_ctrl_chars.horz, k_ctrl_chars.vert, k_ctrl_chars.move): 292 m.move_to(m.x + cmd.horz, m.y + cmd.vert) 293 elif cmd.type == k_ctrl_chars.back: 294 m.x -= m.last_ch_w 295 elif cmd.type == k_ctrl_chars.tab: 296 tab_delta = m.tab_w - m.x % m.tab_w 297 m.move_to(m.x + tab_delta, m.y) 298 elif cmd.type == k_ctrl_chars.line: 299 m.advance_line(start) 300 elif cmd.type == k_ctrl_chars.ret: 301 m.x = m.start_x 302 elif cmd.type == k_ctrl_chars.cfont: 303 m.update_font(m.custom_font) 304 elif cmd.type == k_ctrl_chars.dfont: 305 m.update_font(k_pico_font) 306 elif cmd.type == k_ctrl_cmds.home: 307 m.x, m.y = m.start_x, m.start_y 308 elif cmd.type == k_ctrl_cmds.jump: 309 m.move_to(cmd.horz, cmd.vert) 310 elif cmd.type == k_ctrl_cmds.tab_stop: 311 m.tab_w = cmd.count 312 elif cmd.type == k_ctrl_cmds.wrap: 313 m.wrap_w = cmd.count 314 elif cmd.type == k_ctrl_cmds.ch_width: 315 m.ch_w = cmd.count 316 m.big_ch_w = cmd.count + m.font.wide_width - m.font.width 317 elif cmd.type == k_ctrl_cmds.ch_height: 318 m.ch_h = cmd.count 319 elif cmd.type in (k_ctrl_cmds.char, k_ctrl_cmds.hexchar): 320 m.process_rawch(start, 8, 8) 321 elif cmd.type == k_ctrl_cmds.wide: 322 m.flags.wide = cmd.enable 323 m.update_wide() 324 elif cmd.type == k_ctrl_cmds.tall: 325 m.flags.tall = cmd.enable 326 m.update_wide() 327 elif cmd.type == k_ctrl_cmds.pinball: 328 m.flags.pinball = cmd.enable 329 m.update_wide() 330 331 if m.word_wrap: 332 delta = 0 333 for pos, replace in m.wraps: 334 pos += delta 335 if replace: 336 text = str_replace_at(text, pos, 1, "\n") 337 else: 338 text = str_insert(text, pos, "\n") 339 delta += 1 340 341 m.wraps.clear() 342 if m.x > m.start_x: 343 m.saved_wrap = 0, m.x, False 344 return text 345 # else, nothing to return
351def measure_p8scii(text, **opts): 352 """Measures the width of a p8scii string (see P8sciiMeasurer's ctor for which params to give)""" 353 measurer = P8sciiMeasurer(**opts) 354 text = measurer.process(text) 355 if e(text): 356 return measurer.finish(), text 357 else: 358 return measurer.finish()
Measures the width of a p8scii string (see P8sciiMeasurer's ctor for which params to give)
360def bytes_to_string_contents(input): 361 """convert a p8str or bytes objects to a pico8 string literal, without the surrounding "-s""" 362 from pico_output import format_string_literal # already implemented here... 363 if not isinstance(input, str): 364 input = decode_p8str(input) 365 return format_string_literal(input, long=False, quote='"')[1:-1]
convert a p8str or bytes objects to a pico8 string literal, without the surrounding "-s