-
Notifications
You must be signed in to change notification settings - Fork 6
/
text2img.py
143 lines (115 loc) · 5.19 KB
/
text2img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
import cairo
import pango
import pangocairo
import sys
import codecs
import optparse
def main():
parser = optparse.OptionParser(usage="Usage: %prog [options] font1...")
parser.add_option('-b', '--bold', dest='bold', action='store_true',
help='Generate bold output, if supported by font.')
parser.add_option('-i', '--italic',dest='italic',action='store_true',
help='Generate italic output, if supported by font.')
parser.add_option('-u', '--underline',dest='underline',action='store_true',
help='Generate underline output, if supported by font.')
parser.add_option('-l', '--list',dest='list_fonts',action='store_true',
help='List available fonts and exit.')
parser.add_option('-f', '--file',dest='input_file',action='store',
default='text.txt',help='Text file to use for image text.')
(opts,args) = parser.parse_args()
if len(args) < 1 and not opts.list_fonts:
parser.print_help()
return 0
generate_images(opts,args)
def generate_images(opts,args):
# Set up variables for drawing space
width = 2550 # Will resize to match text; default 8.5"x11" @ 300dpi
height = 3300
MARGIN_X = 300
MARGIN_Y = 300
LINE_SPACE = 35 #TODO: Command-line opts
LANG = "ka"
TESS_LANG = "kat"
# Set up decent spacing for box files
attrs = pango.AttrList() #TODO: Command line opts or config
attrs.insert(pango.AttrLanguage(LANG,0,-1))
attrs.insert(pango.AttrLetterSpacing(10000,0,-1))
attrs.insert(pango.AttrSize(48000,0,-1))
attrs.insert(pango.AttrFallback(False,0,-1))
attrs.insert(pango.AttrStyle(pango.STYLE_NORMAL,0,-1))
attrs.insert(pango.AttrWeight(pango.WEIGHT_NORMAL,0,-1))
attrs.insert(pango.AttrUnderline(pango.UNDERLINE_NONE,0,-1))
# Instantiate Cairo surface and context
surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = pangocairo.CairoContext(cairo.Context(surf))
# Instantiate PangoCairo context
context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
#get font families:
font_map = pangocairo.cairo_font_map_get_default()
# to see family names:
if opts.list_fonts:
print [f.get_name() for f in font_map.list_families()]
return
# Set up pango layout
layout = context.create_layout()
layout.set_attributes(attrs)
layout.set_width((width+MARGIN_X)*pango.SCALE)
layout.set_spacing(LINE_SPACE*pango.SCALE)
#print layout.get_width()
# Read text from file TODO: Command line argument
# TODO: Multiple files print to multiple image documents
text = ''
with codecs.open(opts.input_file) as text_file:
for line in text_file:
text += line + '\n'
font_names = args
# (variant, reset,"desc")
#Font variants is an array of tuples consisting of: (AttrStyle, InverseAttrStyle, Name)
font_variants = [
(pango.AttrStyle(pango.STYLE_NORMAL,0,-1),pango.AttrStyle(pango.STYLE_NORMAL,0,-1),"")]
#Add other stylings based on command-line options
if opts.italic:
font_variants.append((pango.AttrStyle(pango.STYLE_ITALIC,0,-1),pango.AttrStyle(pango.STYLE_NORMAL,0,-1),"italic"))
if opts.bold:
font_variants.append((pango.AttrWeight(pango.WEIGHT_HEAVY,0,-1),pango.AttrWeight(pango.WEIGHT_NORMAL,0,-1),"bold"))
if opts.underline:
font_variants.append((pango.AttrUnderline(pango.UNDERLINE_SINGLE,0,-1),pango.AttrUnderline(pango.UNDERLINE_NONE,0,-1),"underline"))
#Generate pages for each font name and variation.
for fn in font_names:
for fvar in font_variants:
# Change to a new variant
attrs.change(fvar[0])
layout.set_attributes(attrs)
# Change to a new font
fontname = fn
font = pango.FontDescription(fontname + " 25")
layout.set_font_description(font)
layout.set_text(text)
(ink, logical) = layout.get_pixel_extents()
# If layout exceeds size of surface, change surface size
if logical[2] > (width-MARGIN_X) or logical[3] > (height-MARGIN_Y):
width = logical[2]+MARGIN_X
height = logical[3]+MARGIN_Y
surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = pangocairo.CairoContext(cairo.Context(surf))
context.update_layout(layout)
#draw a background rectangle:
context.rectangle(0,0,width,height)
context.set_source_rgb(1, 1, 1)
context.fill()
# Translate context so that desired text upperleft corner is at 0,0
context.translate(50,25)
context.set_source_rgb(0, 0, 0)
context.update_layout(layout)
context.show_layout(layout)
# Write to image
# TODO: Specify outfile on command line
#print fontname
#print fvar[2]
with open(TESS_LANG+"."+fontname+fvar[2]+".exp0.png", "wb") as image_file:
surf.write_to_png(image_file)
attrs.change(fvar[1])
layout.set_attributes(attrs)
if __name__ == "__main__":
main()