Hi!

I saw BigText and liked the idea. But I had pity on the small number of chars supported. So I tried a bit of coding over the weekend...

As we really don't need BigText and I have a lot of other stuff to do I just send it to the list as it is right now.

Have fun

        Florian
#!/usr/bin/python
#
# Urwid BigText example program
#    Copyright (C) 2004-2006  Ian Ward
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

"""
Urwid graphics example program.
"""
import sys
sys.path[0:0] = ['/home/str/ffesti/CVS/urwid/trunk']

import urwid
import urwid.raw_display
import consolefont

try: True # old python?
except: False, True = 0, 1


class EditWithCallback(urwid.Edit):
	def __init__(self, *argl, **argd):
		self.on_change = argd['on_change']
		del argd['on_change']
		self.edit_text = ""
		urwid.Edit.__init__(self, *argl, **argd)
		
	def set_edit_text(self, text):
		if self.on_change and text != self.edit_text:
			self.on_change(self, text)
		urwid.Edit.set_edit_text(self, text)
		

class SwitchingPadding(urwid.Padding):
	def padding_values(self, size, focus):
		maxcol = size[0]
		width, ignore = self.w.pack(focus=focus)
		if maxcol > width:
			self.align_type = "left"
		else:
			self.align_type = "right"
		return urwid.Padding.padding_values(self, size, focus)


class BigTextDisplay:
	palette = [
		('body',         'black',      'light gray', 'standout'),
		('header',       'white',      'dark red',   'bold'),
		('button normal','light gray', 'dark blue', 'standout'),
		('button select','white',      'dark green'),
		('button disabled','dark gray','dark blue'),
		('edit',         'light gray', 'dark blue'),
		('bigtext',      'white',      'black'),
		('chars',        'light gray', 'black'),
		('exit',         'white',      'dark cyan'),
		]
		
	def create_radio_button(self, g, name, font, fn):
		w = urwid.RadioButton(g, name, False, on_state_change=fn)
		w.font = font
		w = urwid.AttrWrap(w, 'button normal', 'button select')
		return w

	def create_disabled_radio_button(self, name):
		w = urwid.Text("    " + name + " (UTF-8 mode required)")
		w = urwid.AttrWrap(w, 'button disabled')
		return w
	
	def create_edit(self, label, text, fn):
		w = EditWithCallback(label, text, on_change = fn)
		w = urwid.AttrWrap(w, 'edit')
		return w

	def set_font_event(self, w, state):
		if state:
			self.bigtext.set_font(w.font)
			self.chars_avail.set_text(w.font.characters())

	def edit_change_event(self, w, text):
		self.bigtext.set_text(text)

	def setup_view(self):
		# setup mode radio buttons
		self.font_buttons = []
		group = []
		utf8 = urwid.get_encoding_mode() == "utf8"
		for name, file, mode in (
			("16x8 Full size (16x8)",
			 'latarcyrheb-sun16.psfu.gz', 'full'),
			("16x8 Half blocks (8x8)",
			 'latarcyrheb-sun16.psfu.gz', 'half'),
			("16x8 Quarter blocks (8x4)",
			 'latarcyrheb-sun16.psfu.gz', 'quarter'),
			("8x8 Full size (8x8)", 'lat0-08.psfu.gz', 'full'),
			("8x8 Half blocks (4x8)", 'lat0-08.psfu.gz', 'half'),
			("8x8 Quarter blocks (4x4)", 'lat0-08.psfu.gz', 'quarter')):
			font = consolefont.Font(
				'/lib/kbd/consolefonts/' + file, mode)
			if font.utf8_required and not utf8:
				rb = self.create_disabled_radio_button(name)
			else:
				rb = self.create_radio_button(group, name, font,
					self.set_font_event)
				if name  == "16x8 Full size (16x8)":
					chosen_font_rb = rb
					exit_font = font
			self.font_buttons.append( rb )
		
		# Create BigText
		self.bigtext = urwid.BigText("", None)
		bt = SwitchingPadding(self.bigtext, 'left', None)
		bt = urwid.AttrWrap(bt, 'bigtext')
		bt = urwid.Filler(bt, 'bottom', None, 17)
		bt = urwid.BoxAdapter(bt, 17)
		
		# Create chars_avail
		cah = urwid.Text("Characters Available:")
		self.chars_avail = urwid.Text("", wrap='any')
		ca = urwid.AttrWrap(self.chars_avail, 'chars')
		
		chosen_font_rb.set_state(True) # causes set_font_event call
	
		# Create Edit widget
		edit = self.create_edit("", "Urwid "+urwid.__version__,
			self.edit_change_event)
		
		# ListBox
		chars = urwid.Pile([cah, ca])
		fonts = urwid.Pile([urwid.Text("Fonts:")] + self.font_buttons,
			focus_item=1)
		col = urwid.Columns([('fixed',16,chars), fonts], 3, 
			focus_column=1)
		bt = urwid.Pile([bt, edit], focus_item=1)
		l = [bt, urwid.Divider(), col]
		w = urwid.ListBox( l )
		
		# Frame
		w = urwid.AttrWrap(w, 'body')
		hdr = urwid.Text("Urwid BigText example program - F8 exits.")
		hdr = urwid.AttrWrap(hdr, 'header')
		w = urwid.Frame(header=hdr, body=w)

		# Exit message
		exit = urwid.BigText(('exit'," Quit? "), exit_font)
		exit = urwid.Overlay(exit, w, 'center', None, 'middle', None)
		return w, exit


	def main(self):
		self.ui = urwid.raw_display.Screen()
		self.ui.register_palette(self.palette)
		self.ui.set_input_timeouts(5)
		self.view, self.exit_view = self.setup_view()
		self.ui.run_wrapper(self.run)
	
	def run(self):
		self.ui.set_mouse_tracking()
		size = self.ui.get_cols_rows()
		show_exit = False
		do_exit = False
		while True:
			if show_exit:
				canvas = self.exit_view.render(size)
			else:
				canvas = self.view.render(size, focus=True)
				
			self.ui.draw_screen(size, canvas)
			if do_exit:
				break
				
			keys = self.ui.get_input()
				
			if show_exit:
				if 'y' in keys or 'Y' in keys:
					do_exit = True
				show_exit = False
				continue

			self.handle_input(size, keys)
			if 'window resize' in keys:
				size = self.ui.get_cols_rows()
			if 'f8' in keys:
				show_exit = True
	
	def handle_input(self, size, keys):
		for k in keys:
			if urwid.is_mouse_event(k):
				event, button, col, row = k
				self.view.mouse_event( size, event, 
					button, col, row, focus=True )
			elif k != 'window resize':
				self.view.keypress( size, k )

def main():
	BigTextDisplay().main()
	
if '__main__'==__name__:
	main()
#!/usr/bin/python
#
# Console fonts for Urwid BigText
#    Copyright (C) 2007  Florian Festi
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

import gzip
import sys
sys.path[0:0] = ['/home/str/ffesti/CVS/urwid/trunk']

from urwid.util import apply_target_encoding
from urwid.canvas import Canvas

# see http://www.win.tue.nl/~aeb/linux/kbd/font-formats.html

class Font:

    # mirrored - currently unused
    halfblocks2 = (u" " # empty
                  u"\u2596" # up left
                  u"\u259D" # up right
                  u"\u2580" # up
                  u"\u2596" # down left
                  u"\u258C" # left
                  u"\u259E" # down left, up right
                  u"\u259B" # up, down left
                  u"\u2597" # down right
                  u"\u259A" # up left, down right                  
                  u"\u2590" # right                  
                  u"\u259C" # up, down right
                  u"\u2584" # down
                  u"\u2599" # down, up left
                  u"\u259F" # down, up right
                  u"\u2588" # full
                  )

    halfblocks = (u" " # empty
                  u"\u259D" # up right
                  u"\u2598" # up left
                  u"\u2580" # up
                  u"\u2597" # down right
                  u"\u2590" # right    
                  u"\u259A" # up left, down right
                  u"\u259C" # up, down right
                  u"\u2596" # down left
                  u"\u259E" # down left, up right
                  u"\u258C" # left
                  u"\u259B" # up, down left
                  u"\u2584" # down
                  u"\u259F" # down, up right                  
                  u"\u2599" # down, up left
                  u"\u2588" # full
                  )

    # ascii replacements for quarter blocks/ currently in a bad shape...
    halfblocks_ascii = (
                  u" " # empty
                  u"'" # up right
                  u"'" # up left
                  u"\"" # up
                  u"," # down right
                  u")" # right    
                  u"\\" # up left, down right
                  u"9" # up, down right
                  u"," # down left
                  u"/" # down left, up right
                  u"l" # left
                  u"P" # up, down left
                  u"m" # down
                  u"d" # down, up right
                  u"b" # down, up left
                  u"M" # full
                  )

    modenames = ("256 characters, no directory",
                 "512 characters, no directory",
                 "256 characters, Unicode directory",
                 "512 characters, Unicode directory",)

    def __init__(self, filename, mode='half', ascii=False):
        """Load linux console font for use as text graphic
        Does currently only support PSF 1 (.psfu.gz). Font files
        without unicode map are still untested
        mode : 'full', 'half', 'quarter'
        """

        if filename.endswith('.gz'):
            self.content = gzip.open(filename).read()
        else:
            self.content = open(filename).read()

        if ord(self.content[0]) != 0x36 or \
               ord(self.content[1]) != 0x04:
            print ord(self.content[0]), ord(self.content[1])
            raise ValueError, "Not a PFS 1 file"

        if ascii:
            self.halfblocks = self.halfblocks_ascii

        self.utf8_required = not ascii

        self.mode = ord(self.content[2])
        if (self.mode & 1):
            self.length = 512
        else:
            self.length = 256

        self.height = ord(self.content[3])

        self._generateChars(mode)

        if (self.mode & 0x2):
            self._generateUnicodeMap()
        else:
            self._generateCharMap()

        if mode in ('half', 'quarter'):
            self.height = (self.height+1) / 2
        
    def __str__(self):
        return "<%s %s, 8x%d>" % (self.__class__.__name__,
                                 self.modenames[self.mode],
                                 self.height)

    def characters(self):
        chars = self.chars.keys()
        chars.sort()
        return "".join(chars)

    def char_width(self, c):
        if self.chars.has_key(c):
            return len(self.chars[c][0])
        return 0                                    

    def char_data(self, c):
        return self.chars[c]

    def render(self, c):
        l = self.chars[c]
        width = len(l[0])
        tl = []
        csl = []
        for d in l:
            t, cs = apply_target_encoding(d)
            tl.append(t)
            csl.append(cs)        
        return Canvas(tl, None, csl, maxcol=width, check_width=False )

    def _generateChars(self, mode):
        if mode == "half":
            mapfunc = self._getCharmap_half
        elif mode == "full":
            mapfunc = self._getCharmap_full
        elif mode == "quarter":
            mapfunc = self._getCharmap_quarter
        else:
            raise ValueError, "Unknown font mode"
            
        self.char_map = []
        for idx in range(self.length):
            self.char_map.append(mapfunc(idx))

    def _getCharmap_full(self, nr):
        start = 4 + nr*self.height
        result = []
        for idx in range(start, start+self.height):
            line = [] 
            c = ord(self.content[idx])
            for bit in range(8):
                mask = 1 << (7-bit)
                if c & mask:
                    line.append(self.halfblocks[15])
                else:
                    line.append(self.halfblocks[0])
            result.append(u''.join(line))
        return result

    def _getCharmap_half(self, nr):
        start = 4 + nr*self.height
        result = []
        for idx in range(start, start+self.height, 2):
            line = [] 
            c1 = ord(self.content[idx])
            c2 = ord(self.content[idx+1])

            for bit in range(8):
                b1 = (c1 >> (7-bit)) & 0x1
                b2 = (c2 >> (7-bit)) & 0x1
                b1 |= b1 << 1
                b2 |= b2 << 1
                char  = b1 | (b2 << 2)
                line.append(self.halfblocks[char])

            result.append(u''.join(line))
        return result
        

    def _getCharmap_quarter(self, nr):
        start = 4 + nr*self.height
        result = []
        for idx in range(start, start+self.height, 2):
            line = [] 
            c1 = ord(self.content[idx])
            c2 = ord(self.content[idx+1])

            for bit in range(4):
                b1 = (c1 >> ((3-bit)*2)) & 0x3
                b2 = (c2 >> ((3-bit)*2)) & 0x3
                char  = b1 + (b2 << 2)
                line.append(self.halfblocks[char])

            result.append(u''.join(line))
        return result
            
    def _generateUnicodeMap(self):
        self.chars = {}
        idx = 4+self.length*self.height
        cont = self.content
        char_idx = 0
        while idx < len(self.content):
            uni = ord(cont[idx]) + (ord(cont[idx+1]) << 8)
            idx += 2
            if uni == 0xFFFF:
                char_idx += 1
            elif uni == 0xFFFE:
                raise ValueError, "Don't support sequences yet"
            else:
                self.chars[unichr(uni)] = self.char_map[char_idx]

    def _generateCharMap(self):
        # untested
        self.chars = {}
        for idx in xrange(self.length):
            self.chars[chr(idx)] = self.char_map[idx]

def main():
    import codecs

    utf8 = codecs.getencoder("utf8")

    font = Font('/lib/kbd/consolefonts/lat1-12.psfu.gz', 'quarter', True)
    #font = Font('/lib/kbd/consolefonts/lat0-08.psfu.gz', 'half', True)
    #font = Font('/lib/kbd/consolefonts/latarcyrheb-sun16.psfu.gz', 'half')
    print utf8(font.characters())[0]
    #print font.characters()
    
if __name__ =='__main__':
    main()
_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid

Reply via email to