validate glyph image



  • Is it possible to get the fileName for an image in a glyph?

    I would like to find glyphs that include images, and then validate that the image exists.

    g.image will only identify glyphs with valid image files. And print(g.image) shows RImage offsets for every glyph even if it doesn't include an image.


  • admin

    glyph.image.data will be None when the image does not exists

    see https://github.com/robotools/fontParts/blob/master/Lib/fontParts/fontshell/image.py#L42-L52


  • admin

    or you can use the lower level defcon objects...

    but I would not recommend to use file paths as image data could be dynamic (when a UFO is not saved there the fileName will not exists yet)



  • The problem is that glyph.image.data will return None if the glyph doesn't include a fileName, and it will also return None if the glyph includes a non-valid fileName.

    or you can use the lower level defcon objects...

    yeah, I'm not good enough with python to go down that road :)

    I'm thinking I'm going to read the .glif file and check against what's included in the images folder (with the assumption that the ufo has already been saved). It's a workaround, but it should be good-enough for what we're trying to do.



  • A followup for anyone who is trying to do the same thing:

    def glyph_missing_images(font):
        from xml.dom import minidom
        image_files = []
        if os.path.exists(os.path.join(font.path, "images")):
            image_files = [i for i in os.listdir(os.path.join(font.path, "images"))]
        for g in os.listdir(os.path.join(font.path, "glyphs")):
            if g.endswith(".glif"):
                glif_xml = minidom.parse(os.path.join(font.path, "glyphs", g))
    
                glyphName = glif_xml.getElementsByTagName("glyph")[0].getAttribute("name")
    
                i = glif_xml.getElementsByTagName("image")
                if i != []:
                    fileName = i[0].getAttribute("fileName")
                    if fileName not in image_files:
                        print(f"Missing image file in glyph: {glyphName}. Cannot find image file: {fileName}")
    

  • admin

    oh this is hard :)

    You are missing image data which is stored in the memory only.

    f = CurrentFont().asDefcon()
    g = CurrentGlyph().asDefcon()
    
    # a font object has a centralised images object collecting all image data.
    print(g.image.fileName in f.images)
    # if the fileName has no data, the image doesnt exist in memory and on disk
    

Log in to reply