fmt.py 18.1 KB
Newer Older
1
# Text formatting abstractions
2
# Note -- this module is obsolete, it's too slow anyway
3 4 5 6 7 8 9 10 11 12 13 14


import string
import Para


# A formatter back-end object has one method that is called by the formatter:
# addpara(p), where p is a paragraph object.  For example:


# Formatter back-end to do nothing at all with the paragraphs
class NullBackEnd:
15 16 17 18 19 20 21 22 23 24 25 26
    #
    def __init__(self):
        pass
    #
    def addpara(self, p):
        pass
    #
    def bgn_anchor(self, id):
        pass
    #
    def end_anchor(self, id):
        pass
27 28 29 30


# Formatter back-end to collect the paragraphs in a list
class SavingBackEnd(NullBackEnd):
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
    #
    def __init__(self):
        self.paralist = []
    #
    def addpara(self, p):
        self.paralist.append(p)
    #
    def hitcheck(self, h, v):
        hits = []
        for p in self.paralist:
            if p.top <= v <= p.bottom:
                for id in p.hitcheck(h, v):
                    if id not in hits:
                        hits.append(id)
        return hits
    #
    def extract(self):
        text = ''
        for p in self.paralist:
            text = text + (p.extract())
        return text
    #
    def extractpart(self, long1, long2):
        if long1 > long2: long1, long2 = long2, long1
        para1, pos1 = long1
        para2, pos2 = long2
        text = ''
        while para1 < para2:
            ptext = self.paralist[para1].extract()
            text = text + ptext[pos1:]
            pos1 = 0
            para1 = para1 + 1
        ptext = self.paralist[para2].extract()
        return text + ptext[pos1:pos2]
    #
    def whereis(self, d, h, v):
        total = 0
        for i in range(len(self.paralist)):
            p = self.paralist[i]
            result = p.whereis(d, h, v)
            if result is not None:
                return i, result
        return None
    #
    def roundtowords(self, long1, long2):
        i, offset = long1
        text = self.paralist[i].extract()
        while offset > 0 and text[offset-1] != ' ': offset = offset-1
        long1 = i, offset
        #
        i, offset = long2
        text = self.paralist[i].extract()
        n = len(text)
        while offset < n-1 and text[offset] != ' ': offset = offset+1
        long2 = i, offset
        #
        return long1, long2
    #
    def roundtoparagraphs(self, long1, long2):
        long1 = long1[0], 0
        long2 = long2[0], len(self.paralist[long2[0]].extract())
        return long1, long2
93 94 95 96


# Formatter back-end to send the text directly to the drawing object
class WritingBackEnd(NullBackEnd):
97 98 99 100 101 102 103 104
    #
    def __init__(self, d, width):
        self.d = d
        self.width = width
        self.lineno = 0
    #
    def addpara(self, p):
        self.lineno = p.render(self.d, 0, self.lineno, self.width)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127


# A formatter receives a stream of formatting instructions and assembles
# these into a stream of paragraphs on to a back-end.  The assembly is
# parametrized by a text measurement object, which must match the output
# operations of the back-end.  The back-end is responsible for splitting
# paragraphs up in lines of a given maximum width.  (This is done because
# in a windowing environment, when the window size changes, there is no
# need to redo the assembly into paragraphs, but the splitting into lines
# must be done taking the new window size into account.)


# Formatter base class.  Initialize it with a text measurement object,
# which is used for text measurements, and a back-end object,
# which receives the completed paragraphs.  The formatting methods are:
# setfont(font)
# setleftindent(nspaces)
# setjust(type) where type is 'l', 'c', 'r', or 'lr'
# flush()
# vspace(nlines)
# needvspace(nlines)
# addword(word, nspaces)
class BaseFormatter:
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
    #
    def __init__(self, d, b):
        # Drawing object used for text measurements
        self.d = d
        #
        # BackEnd object receiving completed paragraphs
        self.b = b
        #
        # Parameters of the formatting model
        self.leftindent = 0
        self.just = 'l'
        self.font = None
        self.blanklines = 0
        #
        # Parameters derived from the current font
        self.space = d.textwidth(' ')
        self.line = d.lineheight()
        self.ascent = d.baseline()
        self.descent = self.line - self.ascent
        #
        # Parameter derived from the default font
        self.n_space = self.space
        #
        # Current paragraph being built
        self.para = None
        self.nospace = 1
        #
        # Font to set on the next word
        self.nextfont = None
    #
    def newpara(self):
        return Para.Para()
    #
    def setfont(self, font):
        if font is None: return
        self.font = self.nextfont = font
        d = self.d
        d.setfont(font)
        self.space = d.textwidth(' ')
        self.line = d.lineheight()
        self.ascent = d.baseline()
        self.descent = self.line - self.ascent
    #
    def setleftindent(self, nspaces):
        self.leftindent = int(self.n_space * nspaces)
        if self.para:
            hang = self.leftindent - self.para.indent_left
            if hang > 0 and self.para.getlength() <= hang:
                self.para.makehangingtag(hang)
                self.nospace = 1
            else:
                self.flush()
    #
    def setrightindent(self, nspaces):
        self.rightindent = int(self.n_space * nspaces)
        if self.para:
            self.para.indent_right = self.rightindent
            self.flush()
    #
    def setjust(self, just):
        self.just = just
        if self.para:
            self.para.just = self.just
    #
    def flush(self):
        if self.para:
            self.b.addpara(self.para)
            self.para = None
            if self.font is not None:
                self.d.setfont(self.font)
        self.nospace = 1
    #
    def vspace(self, nlines):
        self.flush()
        if nlines > 0:
            self.para = self.newpara()
            tuple = None, '', 0, 0, 0, int(nlines*self.line), 0
            self.para.words.append(tuple)
            self.flush()
            self.blanklines = self.blanklines + nlines
    #
    def needvspace(self, nlines):
        self.flush() # Just to be sure
        if nlines > self.blanklines:
            self.vspace(nlines - self.blanklines)
    #
    def addword(self, text, space):
        if self.nospace and not text:
            return
        self.nospace = 0
        self.blanklines = 0
        if not self.para:
            self.para = self.newpara()
            self.para.indent_left = self.leftindent
            self.para.just = self.just
            self.nextfont = self.font
        space = int(space * self.space)
        self.para.words.append((self.nextfont, text,
                self.d.textwidth(text), space, space,
                self.ascent, self.descent))
        self.nextfont = None
    #
    def bgn_anchor(self, id):
        if not self.para:
            self.nospace = 0
            self.addword('', 0)
        self.para.bgn_anchor(id)
    #
    def end_anchor(self, id):
        if not self.para:
            self.nospace = 0
            self.addword('', 0)
        self.para.end_anchor(id)
241 242 243 244


# Measuring object for measuring text as viewed on a tty
class NullMeasurer:
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    #
    def __init__(self):
        pass
    #
    def setfont(self, font):
        pass
    #
    def textwidth(self, text):
        return len(text)
    #
    def lineheight(self):
        return 1
    #
    def baseline(self):
        return 0
260 261 262 263


# Drawing object for writing plain ASCII text to a file
class FileWriter:
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    #
    def __init__(self, fp):
        self.fp = fp
        self.lineno, self.colno = 0, 0
    #
    def setfont(self, font):
        pass
    #
    def text(self, (h, v), str):
        if not str: return
        if '\n' in str:
            raise ValueError, 'can\'t write \\n'
        while self.lineno < v:
            self.fp.write('\n')
            self.colno, self.lineno = 0, self.lineno + 1
        while self.lineno > v:
            # XXX This should never happen...
            self.fp.write('\033[A') # ANSI up arrow
            self.lineno = self.lineno - 1
        if self.colno < h:
            self.fp.write(' ' * (h - self.colno))
        elif self.colno > h:
            self.fp.write('\b' * (self.colno - h))
        self.colno = h
        self.fp.write(str)
        self.colno = h + len(str)
290 291 292 293


# Formatting class to do nothing at all with the data
class NullFormatter(BaseFormatter):
294 295 296 297 298
    #
    def __init__(self):
        d = NullMeasurer()
        b = NullBackEnd()
        BaseFormatter.__init__(self, d, b)
299 300 301 302


# Formatting class to write directly to a file
class WritingFormatter(BaseFormatter):
303 304 305 306 307 308 309 310 311 312 313
    #
    def __init__(self, fp, width):
        dm = NullMeasurer()
        dw = FileWriter(fp)
        b = WritingBackEnd(dw, width)
        BaseFormatter.__init__(self, dm, b)
        self.blanklines = 1
    #
    # Suppress multiple blank lines
    def needvspace(self, nlines):
        BaseFormatter.needvspace(self, min(1, nlines))
314 315 316 317 318 319 320 321 322


# A "FunnyFormatter" writes ASCII text with a twist: *bold words*,
# _italic text_ and _underlined words_, and `quoted text'.
# It assumes that the fonts are 'r', 'i', 'b', 'u', 'q': (roman,
# italic, bold, underline, quote).
# Moreover, if the font is in upper case, the text is converted to
# UPPER CASE.
class FunnyFormatter(WritingFormatter):
323 324 325 326
    #
    def flush(self):
        if self.para: finalize(self.para)
        WritingFormatter.flush(self)
327 328 329 330 331 332 333 334 335 336


# Surrounds *bold words* and _italic text_ in a paragraph with
# appropriate markers, fixing the size (assuming these characters'
# width is 1).
openchar = \
    {'b':'*', 'i':'_', 'u':'_', 'q':'`', 'B':'*', 'I':'_', 'U':'_', 'Q':'`'}
closechar = \
    {'b':'*', 'i':'_', 'u':'_', 'q':'\'', 'B':'*', 'I':'_', 'U':'_', 'Q':'\''}
def finalize(para):
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    oldfont = curfont = 'r'
    para.words.append(('r', '', 0, 0, 0, 0)) # temporary, deleted at end
    for i in range(len(para.words)):
        fo, te, wi = para.words[i][:3]
        if fo is not None: curfont = fo
        if curfont != oldfont:
            if closechar.has_key(oldfont):
                c = closechar[oldfont]
                j = i-1
                while j > 0 and para.words[j][1] == '': j = j-1
                fo1, te1, wi1 = para.words[j][:3]
                te1 = te1 + c
                wi1 = wi1 + len(c)
                para.words[j] = (fo1, te1, wi1) + \
                        para.words[j][3:]
            if openchar.has_key(curfont) and te:
                c = openchar[curfont]
                te = c + te
                wi = len(c) + wi
                para.words[i] = (fo, te, wi) + \
                        para.words[i][3:]
            if te: oldfont = curfont
            else: oldfont = 'r'
        if curfont in string.uppercase:
            te = string.upper(te)
            para.words[i] = (fo, te, wi) + para.words[i][3:]
    del para.words[-1]
364 365 366 367 368 369 370


# Formatter back-end to draw the text in a window.
# This has an option to draw while the paragraphs are being added,
# to minimize the delay before the user sees anything.
# This manages the entire "document" of the window.
class StdwinBackEnd(SavingBackEnd):
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    #
    def __init__(self, window, drawnow):
        self.window = window
        self.drawnow = drawnow
        self.width = window.getwinsize()[0]
        self.selection = None
        self.height = 0
        window.setorigin(0, 0)
        window.setdocsize(0, 0)
        self.d = window.begindrawing()
        SavingBackEnd.__init__(self)
    #
    def finish(self):
        self.d.close()
        self.d = None
        self.window.setdocsize(0, self.height)
    #
    def addpara(self, p):
        self.paralist.append(p)
        if self.drawnow:
            self.height = \
                    p.render(self.d, 0, self.height, self.width)
        else:
            p.layout(self.width)
            p.left = 0
            p.top = self.height
            p.right = self.width
            p.bottom = self.height + p.height
            self.height = p.bottom
    #
    def resize(self):
        self.window.change((0, 0), (self.width, self.height))
        self.width = self.window.getwinsize()[0]
        self.height = 0
        for p in self.paralist:
            p.layout(self.width)
            p.left = 0
            p.top = self.height
            p.right = self.width
            p.bottom = self.height + p.height
            self.height = p.bottom
        self.window.change((0, 0), (self.width, self.height))
        self.window.setdocsize(0, self.height)
    #
    def redraw(self, area):
        d = self.window.begindrawing()
        (left, top), (right, bottom) = area
        d.erase(area)
        d.cliprect(area)
        for p in self.paralist:
            if top < p.bottom and p.top < bottom:
                v = p.render(d, p.left, p.top, p.right)
        if self.selection:
            self.invert(d, self.selection)
        d.close()
    #
    def setselection(self, new):
        if new:
            long1, long2 = new
            pos1 = long1[:3]
            pos2 = long2[:3]
            new = pos1, pos2
        if new != self.selection:
            d = self.window.begindrawing()
            if self.selection:
                self.invert(d, self.selection)
            if new:
                self.invert(d, new)
            d.close()
            self.selection = new
    #
    def getselection(self):
        return self.selection
    #
    def extractselection(self):
        if self.selection:
            a, b = self.selection
            return self.extractpart(a, b)
        else:
            return None
    #
    def invert(self, d, region):
        long1, long2 = region
        if long1 > long2: long1, long2 = long2, long1
        para1, pos1 = long1
        para2, pos2 = long2
        while para1 < para2:
            self.paralist[para1].invert(d, pos1, None)
            pos1 = None
            para1 = para1 + 1
        self.paralist[para2].invert(d, pos1, pos2)
    #
    def search(self, prog):
        import re, string
        if type(prog) is type(''):
            prog = re.compile(string.lower(prog))
        if self.selection:
            iold = self.selection[0][0]
        else:
            iold = -1
        hit = None
        for i in range(len(self.paralist)):
            if i == iold or i < iold and hit:
                continue
            p = self.paralist[i]
            text = string.lower(p.extract())
            match = prog.search(text)
            if match:
                a, b = match.group(0)
                long1 = i, a
                long2 = i, b
                hit = long1, long2
                if i > iold:
                    break
        if hit:
            self.setselection(hit)
            i = hit[0][0]
            p = self.paralist[i]
            self.window.show((p.left, p.top), (p.right, p.bottom))
            return 1
        else:
            return 0
    #
    def showanchor(self, id):
        for i in range(len(self.paralist)):
            p = self.paralist[i]
            if p.hasanchor(id):
                long1 = i, 0
                long2 = i, len(p.extract())
                hit = long1, long2
                self.setselection(hit)
                self.window.show(
                        (p.left, p.top), (p.right, p.bottom))
                break
505 506 507 508 509


# GL extensions

class GLFontCache:
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
    #
    def __init__(self):
        self.reset()
        self.setfont('')
    #
    def reset(self):
        self.fontkey = None
        self.fonthandle = None
        self.fontinfo = None
        self.fontcache = {}
    #
    def close(self):
        self.reset()
    #
    def setfont(self, fontkey):
        if fontkey == '':
            fontkey = 'Times-Roman 12'
        elif ' ' not in fontkey:
            fontkey = fontkey + ' 12'
        if fontkey == self.fontkey:
            return
        if self.fontcache.has_key(fontkey):
            handle = self.fontcache[fontkey]
        else:
            import string
            i = string.index(fontkey, ' ')
            name, sizestr = fontkey[:i], fontkey[i:]
            size = eval(sizestr)
            key1 = name + ' 1'
            key = name + ' ' + `size`
            # NB key may differ from fontkey!
            if self.fontcache.has_key(key):
                handle = self.fontcache[key]
            else:
                if self.fontcache.has_key(key1):
                    handle = self.fontcache[key1]
                else:
                    import fm
                    handle = fm.findfont(name)
                    self.fontcache[key1] = handle
                handle = handle.scalefont(size)
                self.fontcache[fontkey] = \
                        self.fontcache[key] = handle
        self.fontkey = fontkey
        if self.fonthandle != handle:
            self.fonthandle = handle
            self.fontinfo = handle.getfontinfo()
            handle.setfont()
558 559 560


class GLMeasurer(GLFontCache):
561 562 563 564 565 566 567 568 569
    #
    def textwidth(self, text):
        return self.fonthandle.getstrwidth(text)
    #
    def baseline(self):
        return self.fontinfo[6] - self.fontinfo[3]
    #
    def lineheight(self):
        return self.fontinfo[6]
570 571 572


class GLWriter(GLFontCache):
573 574 575 576 577 578 579 580 581 582 583 584 585 586
    #
    # NOTES:
    # (1) Use gl.ortho2 to use X pixel coordinates!
    #
    def text(self, (h, v), text):
        import gl, fm
        gl.cmov2i(h, v + self.fontinfo[6] - self.fontinfo[3])
        fm.prstr(text)
    #
    def setfont(self, fontkey):
        oldhandle = self.fonthandle
        GLFontCache.setfont(fontkey)
        if self.fonthandle != oldhandle:
            handle.setfont()
587 588 589


class GLMeasurerWriter(GLMeasurer, GLWriter):
590
    pass
591 592 593


class GLBackEnd(SavingBackEnd):
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    #
    def __init__(self, wid):
        import gl
        gl.winset(wid)
        self.wid = wid
        self.width = gl.getsize()[1]
        self.height = 0
        self.d = GLMeasurerWriter()
        SavingBackEnd.__init__(self)
    #
    def finish(self):
        pass
    #
    def addpara(self, p):
        self.paralist.append(p)
        self.height = p.render(self.d, 0, self.height, self.width)
    #
    def redraw(self):
        import gl
        gl.winset(self.wid)
        width = gl.getsize()[1]
        if width != self.width:
            setdocsize = 1
            self.width = width
            for p in self.paralist:
                p.top = p.bottom = None
        d = self.d
        v = 0
        for p in self.paralist:
            v = p.render(d, 0, v, width)