Commit af0d70a7 authored by Tobias Pape's avatar Tobias Pape

Enhance the dotviewer.

- include Droid fonts (under Apache license)
- allow for unicode by sending UTF-8 over the wire and force unicode decode on the other end
- Make all text antialiased. It was already enabled unconditionally for the status bar and is now
  for the normal text, too.
parent 9fd2f8b2
......@@ -270,3 +270,24 @@ files from the above website, and the same terms of use apply.
LineBreak-*.txt
UnicodeData-*.txt
UnihanNumeric-*.txt
License for 'dotviewer/font/'
=============================
Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Detailled license information is contained in the NOTICE file in the
directory.
......@@ -10,8 +10,8 @@ from pygame.locals import *
this_dir = os.path.dirname(os.path.abspath(__file__))
FONT = os.path.join(this_dir, 'cyrvetic.ttf')
FIXEDFONT = os.path.join(this_dir, 'VeraMoBd.ttf')
FONT = os.path.join(this_dir, 'font', 'DroidSans.ttf')
FIXEDFONT = os.path.join(this_dir, 'font', 'DroidSansMono.ttf')
COLOR = {
'black': (0,0,0),
'white': (255,255,255),
......@@ -51,6 +51,12 @@ def getcolor(name, default):
else:
return default
def forceunicode(name):
return name if isinstance(name, unicode) else name.decode("utf-8")
def forcestr(name):
return name if isinstance(name, str) else name.encode("utf-8")
class GraphLayout:
fixedfont = False
......@@ -106,12 +112,12 @@ def wait_for_async_cmd():
class Node:
def __init__(self, name, x, y, w, h, label, style, shape, color, fillcolor):
self.name = name
self.name = forceunicode(name)
self.x = float(x)
self.y = float(y)
self.w = float(w)
self.h = float(h)
self.label = label
self.label = forceunicode(label)
self.style = style
self.shape = shape
self.color = color
......@@ -125,8 +131,8 @@ class Edge:
label = None
def __init__(self, nodes, tail, head, cnt, *rest):
self.tail = nodes[tail]
self.head = nodes[head]
self.tail = nodes[forceunicode(tail)]
self.head = nodes[forceunicode(head)]
cnt = int(cnt)
self.points = [(float(rest[i]), float(rest[i+1]))
for i in range(0, cnt*2, 2)]
......@@ -655,11 +661,7 @@ class TextSnippet:
part = parts[i]
word = part[0]
try:
try:
img = font.render(word, False, *part[1:])
except pygame.error, e:
# Try *with* anti-aliasing to work around a bug in SDL
img = font.render(word, True, *part[1:])
img = font.render(word, True, *part[1:])
except pygame.error:
del parts[i] # Text has zero width
else:
......
This diff is collapsed.
Copyright (C) 2008 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
##########
This directory contains the fonts for the platform. They are licensed
under the Apache 2 license.
......@@ -33,8 +33,9 @@ def display_page(page, wait=True, save_tmp_file=None):
def reload(graph_id):
page = getpage(graph_id)
if save_tmp_file:
from drawgraph import forcestr
f = open(save_tmp_file, 'w')
f.write(page.source)
f.write(forcestr(page.source))
f.close()
messages.extend(page_messages(page, graph_id))
send_graph_messages(io, messages)
......@@ -75,7 +76,8 @@ def display_page(page, wait=True, save_tmp_file=None):
def page_messages(page, graph_id):
import graphparse
return graphparse.parse_dot(graph_id, page.source, page.links,
from drawgraph import forcestr
return graphparse.parse_dot(graph_id, forcestr(page.source), page.links,
getattr(page, 'fixedfont', False))
def send_graph_messages(io, messages):
......
......@@ -4,7 +4,7 @@ import pygame
from pygame.locals import *
from drawgraph import GraphRenderer, FIXEDFONT
from drawgraph import Node, Edge
from drawgraph import EventQueue, wait_for_events
from drawgraph import EventQueue, wait_for_events, forceunicode, forcestr
METAKEYS = dict([
......@@ -285,7 +285,7 @@ class GraphDisplay(Display):
if e.key == K_ESCAPE:
return None
elif e.key == K_RETURN:
return text.encode('latin-1') # XXX do better
return forcestr(text) # return utf-8 encoded
elif e.key == K_BACKSPACE:
text = text[:-1]
elif e.unicode and ord(e.unicode) >= ord(' '):
......@@ -423,7 +423,7 @@ class GraphDisplay(Display):
self.layout.request_reload()
def setstatusbar(self, text, fgcolor=None, bgcolor=None):
info = (text, fgcolor or self.STATUSBAR_FGCOLOR, bgcolor or self.STATUSBAR_BGCOLOR)
info = (forceunicode(text), fgcolor or self.STATUSBAR_FGCOLOR, bgcolor or self.STATUSBAR_BGCOLOR)
if info != self.statusbarinfo:
self.statusbarinfo = info
self.must_redraw = True
......@@ -711,7 +711,7 @@ def renderline(text, font, fgcolor, width, maxheight=sys.maxint,
lines = []
while words:
line = words.pop(0)
img = font.render(line or ' ', 1, fgcolor)
img = font.render(line or ' ', True, fgcolor)
while words:
longerline = line + ' ' + words[0]
longerimg = font.render(longerline, 1, fgcolor)
......@@ -723,7 +723,7 @@ def renderline(text, font, fgcolor, width, maxheight=sys.maxint,
img = longerimg
w, h = img.get_size()
if h > maxheight:
img = font.render('...', 1, overflowcolor)
img = font.render('...', True, overflowcolor)
w, h = img.get_size()
while lines and h > maxheight:
maxheight += lines.pop().get_size()[1]
......
......@@ -44,6 +44,7 @@ class GraphPage(object):
class DotFileGraphPage(GraphPage):
def compute(self, dotfile):
from drawgraph import forceunicode
f = open(dotfile, 'r')
self.source = f.read()
self.source = forceunicode(f.read())
f.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment