Author Topic: A script to delete small contours  (Read 1304 times)

insigne

  • Sr. Member
  • ****
  • Posts: 11
A script to delete small contours
« on: 2013-03-19, 15:01:26 »
Does anyone have a script that can identify small "junk" contours that are sometimes created during boolean operations?

Ideally, something that identifies that area of the contour, then deletes it if it is smaller than a certain area.

insigne

  • Sr. Member
  • ****
  • Posts: 11
Re: A script to delete small contours
« Reply #1 on: 2013-03-20, 18:53:45 »
Here's what I came up with. Not elegant, but it works well enough to save some time.

#FLM: Digital mode
#xy of min value
dx = 120
dy = 120
#border = d.border
def GetContourBBox(g, i0, len):
  r = Rect(g.nodes[i0].point, g.nodes[i0].point)
  for i in range(len - 1):
    p = g.nodes[i0 + i + 1].point
    r = r + p
  return r
 
def TestSquare(g, i0, len):
  if len <= 2:
    return 0
  r = GetContourBBox(g, i0, len)
  #if the BB of the contour is greater than threshold
  if (r.width * r.height) > (dx * dy):
    #print "greater than threshold"
    #print (r.width * r.height)
    return 0
  else:
    #print (r.width * r.height)
    return 1
   
def process(f, g, index):
  if len(g) == 0:
    return 1
  #print g
  glyph = Glyph(g)
  contours_count = glyph.GetContoursNumber()
  #print "contourcount"
  #print contours_count
  for cont in range(contours_count):
    i0 = glyph.GetContourBegin(cont)
    l = glyph.GetContourLength(cont)
    #print "length of contour"
    #print l
    if TestSquare(glyph, i0, l):
       print "deleting from"
       print g
       g.DeleteContour(cont)
font = fl.font
glyphs = font.glyphs
 
for index in range(len(glyphs)):
  if fl.Selected(index) and index != fl.iglyph :
    process(font, glyphs[index], index)
    fl.UpdateGlyph(index)
   
process(font, glyphs[fl.iglyph], fl.iglyph)
fl.UpdateGlyph()