groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Groff] UTP and "WYSIWYG" groff


From: Gaius Mulley
Subject: Re: [Groff] UTP and "WYSIWYG" groff
Date: 29 Apr 2003 11:24:14 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

(Ted Harding) <address@hidden> writes:

> Hi Folks,
> 
> First, many congratulations to Larry Kollar and The UTP Team
> for producing an almost complete version of "Unix Text Processing".
> A very fine effort, beautifully done.

[ ... and gives a useful shell script ... ]

as a followup to this excellent shell script, here is a python script
I use for viewing large groff source documents and slide
presentations.  Especially useful for checking how floating keeps,
tables and pictures map onto pages.

The script requires python with tkinter support and it also uses
`psselect'.  The script is called `gp' and its synposis is:

Usage: gp [-rTHUMBNAILRESOLUTION] [-xTHUMBS] [-h] postscriptfile

Basically it generates a thumbnail image for each page, displays all
thumbnails in a two dim array and allows the user to click on a
thumbnail which activates a full size page preview.

enjoy,

Gaius


#!/usr/bin/python

import os
import sys
import glob
import getopt, string
from Tkinter import *

res = '-r10'
device = 'ppmraw'
imageDir = '/tmp/gp/png'
pspage = imageDir + '/pspage'
gvpid = 0
currentPage = 0
defaultXThumbnails = 5

from tkMessageBox import askokcancel

class quitButton(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()
        widget = Button(self, text='quit', command=self.quit)
        widget.pack(expand=YES, fill=BOTH, side=LEFT)
    def quit(self):
        ans = askokcancel('verify exit', "really quit?")
        if ans:
            Frame.quit(self)
            if gvpid > 0:
                os.kill(gvpid, 9)

class CheckButton(Frame):
    def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
        Frame.__init__(self, parent)
        self.var = StringVar()
        for pick in picks:
            rad = Checkbutton(self, text=pick, variable=self.var)
            rad.pack(side=side, anchor=anchor, expand=YES)
    def state(self):
        return self.var.get()

def about():
    win = Toplevel()                                     # make a new window
    Label(win,  text='A python front end to a groff and ghostscript\npowered 
presentation package!').pack()   # add a few widgets
    Button(win, text='ok', command=win.destroy).pack()   # set destroy callback
    win.focus_set()          # take over input focus,
    win.grab_set()           # disable other windows while I'm open,
    win.wait_window()        # and wait here until win destroyed

def makeThumbnails(psfile, res, dev):
    return os.system('echo showpage | gs -q -dBATCH -dSAFER ' + res + ' 
-sDEVICE=' + dev + ' -sOutputFile=' + imageDir + '/%d.' + dev + ' ' + psfile + 
' -\n')

def onPage(page):
    global gvpid, currentPage

    currentPage = page
    if gvpid == 0:
        gvpid = os.fork()
        if gvpid == 0:
            os.execvp('gv', ['-spartan', '-antialias', pspage])
    else:
        os.kill(gvpid, 1)

def doPage(page):
    buildPs(page)
    if page>0:
        onPage(page)

def buildPs(page):
    global psfile, pspage

    if page == 0:
        os.system('cp ' + psfile + ' ' + pspage)
    else:
        os.system('psselect -p' + `page` + ' ' + psfile + ' > ' + pspage)

def doNextPage():
    global currentPage

    doPage(currentPage + 1)


def doPrevPage():
    global currentPage

    if currentPage>1:
        doPage(currentPage - 1)

def makeSynopsis(dir, dev):
    n = 0
    for name in glob.glob(dir + '/*.' + dev):
        n = n + 1

    root = Tk()
    frame1 = Frame(root, bd=5, relief=RAISED)
    frame1.pack()

    Button(frame1, text='>', command=doNextPage).pack(side=LEFT, fill=X, 
anchor=SE)
    Button(frame1, text='<', command=doPrevPage).pack(side=LEFT, fill=X, 
anchor=SE)
    Button(frame1, text='about', command=about).pack(side=LEFT, fill=X, 
anchor=SE)

    quitButton(frame1).pack(side=LEFT, fill=X, anchor=SE)

    frame2 = Frame(root, bd=5, relief=RAISED)
    frame2.pack(padx=5,pady=5)

    buildPs(0)
    if makeThumbnails(pspage, res, device) != 0:
        print 'failed to run gs with device ' + device
        return

    i = 0
    j = 0
    k = 1
    butns = {}
    imgs = {}
    for name in glob.glob(dir + '/*.' + dev):
        mykey = '%d%d' % (j, i)
        imgs[mykey] = PhotoImage(file=name)
        butns[mykey] = Button(frame2, image=imgs[mykey], command=(lambda k=k: 
doPage(k))).grid(row=j,column=i)
        i = (i+1) % thumbs
        k = k+1
        if i == 0:
            j = j+1
    root.mainloop()

def Usage():
    print 'Usage: gp [-rTHUMBNAILRESOLUTION] [-xTHUMBS] [-h] postscriptfile'
    
def scanArguments():
    global psfile, res, thumbs

    thumbs = defaultXThumbnails
    psfile = ''
    if len(sys.argv) == 1:
        Usage()
    else:
        optlist, list = getopt.getopt(sys.argv, 'hr:x:')
        psfile = list[1]

        for opt in optlist:
            if opt[0] == '-h':
                Usage()
            if opt[0] == '-r':
                res = opt[0] + opt[1]
            if opt[0] == '-x':
                thumbs = string.atoi(opt[1])

    if psfile == '':
        psfile = imageDir + '/ps'

def initDirs():
    os.system('rm -rf /tmp/gp');
    os.system('mkdir /tmp/gp');
    os.system('mkdir ' + imageDir);

def killDirs():
    os.system('rm -rf /tmp/gp');

if __name__ == '__main__':
    global psfile

    initDirs()
    scanArguments()
    if len(sys.argv) > 1:
        makeSynopsis(imageDir, device)
    killDirs()

reply via email to

[Prev in Thread] Current Thread [Next in Thread]