def create_imagefont(res_path, font_filename, point_size, text, color):
    done = False
    curr_size = 64
    texture_packer = None
    image_dict = None
    packResult = None

    # Retry until optimal font atlas size is found.
    while not done:
        try:
            result = pack_fonts(font_filename, point_size, text, color, curr_size)
            texture_packer = result[0]
            packResult = result[1]
            image_dict = result[2]
            done = True
        except PackerError:
            curr_size = next_power_of_two(curr_size)
            print "Failed, trying next power of two", curr_size

    borderSize = 1
    font_image_name = os.path.join(
        get_fonts_path(res_path), "%s_%s.%s" % (os.path.basename(font_filename).split(".")[0], str(point_size), "tga")
    )
    atlas_data = AtlasData(
        name=font_image_name,
        width=packResult[0],
        height=packResult[1],
        color_mode="RGBA",
        file_type="tga",
        border=borderSize,
    )
    for tex in texture_packer.texArr:
        atlas_data.add_texture(tex)

    parser = get_parser("xml")
    parser.parse(atlas_data)
    parser.save("%s.%s" % (font_image_name.split(".")[0], parser.get_file_ext()))

    atlas_image = Image.new("RGBA", (packResult[0], packResult[1]), color)

    index = 0
    for name in image_dict.keys():
        tex = texture_packer.get_texture(name)
        atlas_image.paste(image_dict[name], (tex.x, tex.y))
        index += 1

    atlas_image.save(os.path.join(get_fonts_path(res_path), font_image_name), "tga")
def create_atlas(texMode, dirPath, atlasPath, dirName, args):
    done = False
    curr_size = int(args['maxrects_bin_size'])
    texture_packer = None
    imagesList = None
    packResult = None

    # Retry until optimal font atlas size is found.
    while not done:
        try:
            result = pack_atlas(args, dirPath, curr_size)
            texture_packer = result[0]
            packResult = result[1]
            imagesList = result[2]
            done = True
        except PackerError:
            curr_size = next_power_of_two(curr_size)
            print "Failed, trying next power of two", curr_size

    borderSize = 1
    atlas_name = '%s.%s' % (dirName, args['atlas_type'])
    atlas_data = AtlasData(name=dirName, width=packResult[0], height=packResult[1], color_mode=texMode, file_type=args['atlas_type'], border=borderSize)
    for tex in texture_packer.texArr:
        atlas_data.add_texture(tex)

    parser = get_parser(args['output_data_type'])
    parser.parse(atlas_data)
    parser.save('%s.%s' % (os.path.join(atlasPath, os.path.basename(dirPath)), parser.get_file_ext()))

    atlas_image = Image.new(texMode, (packResult[0], packResult[1]), get_color(args['bg_color']))

    index = 0
    for image in imagesList:
        tex = texture_packer.get_texture(image[0])
        atlas_image.paste(image[1], (tex.x, tex.y))
        index += 1

    atlas_image.save(os.path.join(atlasPath, os.path.basename(dirPath)) + "." + args['atlas_type'], args['atlas_type'])
    if (args['verbose']):
        atlas_image.show()