[rendering] Do not write duplicated hashes...
[wikipediardware.git] / host-tools / rendering / textrun.py
blob6e4b442b66915c521474f5124306625dc71e8e69
1 #!/usr/bin/env python
4 class TextRun:
5 """
6 A run of text...
7 """
8 def __init__(self, glyph):
9 self.x = glyph['x']
10 self.y = glyph['y']
11 self.font = glyph['font']
12 self.glyphs = []
14 def add_glyph(self, glyph):
15 self.glyphs.append(glyph)
17 def cmp(left, right):
18 if left.y < right.y:
19 return -1
20 elif left.y > right.y:
21 return 1
23 if left.x < right.x:
24 return -1
25 elif left.x > right.x:
26 return 1
27 return 0
32 # Load glyphs... share this with render_text.py
33 def load(file):
34 glyphs = []
35 for line in file:
36 split = line.strip().split(',')
39 glyph = { 'x' : int(split[0]),
40 'y' : int(split[1]),
41 'font' : split[2],
42 'glyph' : split[3] }
43 glyphs.append(glyph)
45 return glyphs
48 def generate_text_runs(glyphs, WIDTH):
49 text_runs = []
50 current = None
51 last_glyph = None
53 last_x = 0
54 last_y = 0
55 skip_paragraph = False
57 # Two passes....
58 for glyph in glyphs:
59 if glyph['x'] == 0 and glyph['y'] == 0 and glyph['font'] == '0' and glyph['glyph'] == '0':
60 if skip_paragraph:
61 print "Skipping paragraph due text being drawn over itself."
62 if current.x > WIDTH:
63 print "Skipping due too large x position"
64 else:
65 current.first_x = current.x - last_x
66 if current.first_x < 0:
67 current.first_x = (WIDTH - last_x) + current.x
68 assert current.first_x >= 0
69 current.first_y = current.y - last_y
70 text_runs.append(current)
71 last_y = current.y
72 if len(current.glyphs) != 0:
73 last_x = current.glyphs[-1]['x']
74 assert last_x <= WIDTH
76 current = None
77 last_glyph = None
78 skip_paragraph = False
79 continue
80 elif skip_paragraph:
81 continue
82 elif last_glyph and last_glyph['x'] == glyph['x']:
83 skip_paragraph = True
84 continue
87 if not current:
88 current = TextRun(glyph)
90 if glyph['x'] < WIDTH:
91 current.add_glyph(glyph)
92 else:
93 print "Omitting glyph due being out of space", glyph
95 last_glyph = glyph
97 if current:
98 text_runs.append(current)
99 current = None
101 return text_runs