scripts/last-dotplot
changeset 1068 a29f9cec16e1
parent 1062 6d8d56a74b14
child 1069 da8b081ec496
     1.1 --- a/scripts/last-dotplot	Mon Jun 08 14:28:46 2020 +0900
     1.2 +++ b/scripts/last-dotplot	Thu Jul 30 21:14:13 2020 +0900
     1.3 @@ -420,21 +420,17 @@
     1.4          return seqName + ":" + prettyNum(beg) + "-" + prettyNum(end)
     1.5      return seqName
     1.6  
     1.7 -def rangeLabels(seqRanges, labelOpt, font, fontsize, image_mode, textRot):
     1.8 -    if fontsize:
     1.9 -        image_size = 1, 1
    1.10 -        im = Image.new(image_mode, image_size)
    1.11 -        draw = ImageDraw.Draw(im)
    1.12 +def rangeLabels(seqRanges, labelOpt, font, textDraw, textRot):
    1.13      x = y = 0
    1.14      for r in seqRanges:
    1.15          text = labelText(r, labelOpt)
    1.16 -        if fontsize:
    1.17 -            x, y = draw.textsize(text, font=font)
    1.18 +        if textDraw is not None:
    1.19 +            x, y = textDraw.textsize(text, font=font)
    1.20              if textRot:
    1.21                  x, y = y, x
    1.22          yield text, x, y, r[3]
    1.23  
    1.24 -def dataFromRanges(sortedRanges, font, fontSize, imageMode, labelOpt, textRot):
    1.25 +def dataFromRanges(sortedRanges, font, textDraw, labelOpt, textRot):
    1.26      for seqName, rangeBeg, rangeEnd, strandNum in sortedRanges:
    1.27          out = [seqName, str(rangeBeg), str(rangeEnd)]
    1.28          if strandNum > 0:
    1.29 @@ -442,8 +438,7 @@
    1.30          logging.info("\t".join(out))
    1.31      logging.info("")
    1.32      rangeSizes = [e - b for n, b, e, s in sortedRanges]
    1.33 -    labs = list(rangeLabels(sortedRanges, labelOpt, font, fontSize,
    1.34 -                            imageMode, textRot))
    1.35 +    labs = list(rangeLabels(sortedRanges, labelOpt, font, textDraw, textRot))
    1.36      margin = max(i[2] for i in labs)
    1.37      # xxx the margin may be too big, because some labels may get omitted
    1.38      return rangeSizes, labs, margin
    1.39 @@ -867,14 +862,16 @@
    1.40                                cutRanges1, cutRangesB1, cutRanges2, cutRangesB2)
    1.41      sortedRanges1, sortedRanges2 = sortOut
    1.42  
    1.43 +    textDraw = None
    1.44 +    if opts.fontsize:
    1.45 +        textDraw = ImageDraw.Draw(Image.new(image_mode, (1, 1)))
    1.46 +
    1.47      textRot1 = "vertical".startswith(opts.rot1)
    1.48 -    i1 = dataFromRanges(sortedRanges1, font,
    1.49 -                        opts.fontsize, image_mode, opts.labels1, textRot1)
    1.50 +    i1 = dataFromRanges(sortedRanges1, font, textDraw, opts.labels1, textRot1)
    1.51      rangeSizes1, labelData1, tMargin = i1
    1.52  
    1.53      textRot2 = "horizontal".startswith(opts.rot2)
    1.54 -    i2 = dataFromRanges(sortedRanges2, font,
    1.55 -                        opts.fontsize, image_mode, opts.labels2, textRot2)
    1.56 +    i2 = dataFromRanges(sortedRanges2, font, textDraw, opts.labels2, textRot2)
    1.57      rangeSizes2, labelData2, lMargin = i2
    1.58  
    1.59      maxPixels1 = opts.width  - lMargin