DevelopperTools: util_sfontmaker.py

Line 
1 # -*- coding: iso-8859-1 -*-
2 #Sfontmaker:
3 #This script processes an image file to make it a valid sFont file.
4 #The input file should contain these characters:
5 #
6 # ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ ¡ ¢ £ € ¥ Š § š © ª « ¬ ­ ® ¯ ° ± ² ³ Ž µ ¶ · ž ¹ º » Œ œ Ÿ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
7 #
8 #This script can output only to bmp or (better) tga files. You'll have to convert to png with other tools.
9
10 import sys
11 import pygame
12
13 if len(sys.argv) < 2:
14   print
15   print "Usage: %s fileInput [fileOutput]" % sys.argv[0]
16   print "fileOutput defaults to 'output.tga' and can be in bmp or tga format."
17   sys.exit()
18
19 print "-" * 78
20 print sys.argv[0]
21 print "Script to process an image file and make it a sFont"
22 print "-" * 78
23 print " Opening file: %s" % sys.argv[1]
24 filename = sys.argv[1]
25 surface = pygame.image.load(filename)
26 charPos = []
27
28 if len(sys.argv) >= 3:
29   fOut = sys.argv[2]
30 else:
31   fOut = "output.tga"
32 PINK = (255,0,255)
33 VOID = surface.get_at((0,1))
34
35 #Pulisco una riga di pixel:
36 surface.fill(VOID, (0, 0, surface.get_width(), 1))
37
38 #Loop per i pixel:
39 print " Analyzing..."
40 print
41 wPixels = enumerate(surface.get_at((i, 0)) for i in xrange(surface.get_width()))
42
43 charStart = 0
44 inChar = False
45 warning = False
46 for i, wPixel in wPixels:
47   hPixels = enumerate(surface.get_at((i, j)) for j in xrange(surface.get_height()))
48   for j, hPixel in hPixels:
49     if hPixel != VOID and hPixel != PINK:
50       if not inChar:
51         inChar = True
52         charStart = i - 1
53       break
54     elif hPixel == VOID and j == surface.get_height() - 1:
55       if inChar:
56         #Controllo distanza minima:
57         if len(charPos) > 0:
58           lastChar = charPos[len(charPos) - 1]
59           if charStart - lastChar[1] < 2:
60             print " WARNING: character distance too little: %12s %12s" % (str(lastChar), str((charStart, i + 1)))
61             warning = True
62         charPos.append((charStart, i + 1))
63         inChar = False
64         break
65
66 print
67 print " Total characters found: %i" % len(charPos)
68 if len(charPos) != 190:
69   print " WARNING: characters should be 190!"
70   warning = True
71 else:
72   print
73 print
74
75 #Coloro i pixel:
76 charStart = 0
77 for i, char in enumerate(charPos):
78   if char[0] - charStart >= 1:
79     surface.fill(PINK, (charStart, 0, char[0] - charStart, 1))
80     charStart = char[1]
81  
82   if i == len(charPos) - 1:
83     surface.fill(PINK, (char[1] + 1, 0, surface.get_width() - char[1] - 1, 1))
84
85 #Salvo la nuova immagine:
86 pygame.image.save(surface, fOut)
87 print " Written file %s" % fOut
88 if warning:
89   print " Please read the WARNINGS and check your file!"
90 print "-" * 78
91 print "COMPLETED"
92 print "-" * 78