scripts/last-dotplot
changeset 903 3af6b02e3f9c
parent 901 2003b209d750
child 904 c2f50cd4e29d
     1.1 --- a/scripts/last-dotplot	Thu Nov 02 22:18:05 2017 +0900
     1.2 +++ b/scripts/last-dotplot	Wed Nov 08 11:07:22 2017 +0900
     1.3 @@ -11,6 +11,7 @@
     1.4  
     1.5  import gzip
     1.6  from fnmatch import fnmatchcase
     1.7 +import subprocess
     1.8  import itertools, optparse, os, re, sys
     1.9  
    1.10  # Try to make PIL/PILLOW work:
    1.11 @@ -484,10 +485,29 @@
    1.12      for i, j in zip(seqNames, rangePixBegs):
    1.13          yield i, bp_per_pix * j - seqLimits[i][0]
    1.14  
    1.15 +def getFont(opts):
    1.16 +    if opts.fontfile:
    1.17 +        return ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.18 +    fileNames = []
    1.19 +    try:
    1.20 +        x = ["fc-match", "-f%{file}", "arial"]
    1.21 +        p = subprocess.Popen(x, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    1.22 +        out, err = p.communicate()
    1.23 +        fileNames.append(out)
    1.24 +    except OSError as e:
    1.25 +        warn("fc-match error: " + str(e))
    1.26 +    fileNames.append("/Library/Fonts/Arial.ttf")  # for Mac
    1.27 +    for i in fileNames:
    1.28 +        try:
    1.29 +            font = ImageFont.truetype(i, opts.fontsize)
    1.30 +            warn("font: " + i)
    1.31 +            return font
    1.32 +        except IOError as e:
    1.33 +            warn("font load error: " + str(e))
    1.34 +    return ImageFont.load_default()
    1.35 +
    1.36  def lastDotplot(opts, args):
    1.37 -    if opts.fontfile:  font = ImageFont.truetype(opts.fontfile, opts.fontsize)
    1.38 -    else:              font = ImageFont.load_default()
    1.39 -
    1.40 +    font = getFont(opts)
    1.41      image_mode = 'RGB'
    1.42      forward_color = ImageColor.getcolor(opts.forwardcolor, image_mode)
    1.43      reverse_color = ImageColor.getcolor(opts.reversecolor, image_mode)
    1.44 @@ -632,7 +652,7 @@
    1.45      og = optparse.OptionGroup(op, "Text options")
    1.46      og.add_option("-f", "--fontfile", metavar="FILE",
    1.47                    help="TrueType or OpenType font file")
    1.48 -    og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=11,
    1.49 +    og.add_option("-s", "--fontsize", metavar="SIZE", type="int", default=14,
    1.50                    help="TrueType or OpenType font size (default: %default)")
    1.51      og.add_option("--labels1", type="int", default=0, metavar="N", help=
    1.52                    "genome1 labels: 0=name, 1=name:length, "
    1.53 @@ -687,6 +707,6 @@
    1.54  
    1.55      try: lastDotplot(opts, args)
    1.56      except KeyboardInterrupt: pass  # avoid silly error message
    1.57 -    except Exception, e:
    1.58 +    except Exception as e:
    1.59          prog = os.path.basename(sys.argv[0])
    1.60          sys.exit(prog + ": error: " + str(e))