FONTS = {}

FONTS.small = {
	widthAdjust = -4,
	heightAdjust = -14,
	Xheight = 15,
}

FONTS.medium = {
	widthAdjust = -4,
	heightAdjust = -17,
	Xheight = 18,
}

FONTS.big = {
	widthAdjust = -6,
	heightAdjust = -30,
	Xheight = 34,
}


FONTS.quad = {
	widthAdjust = -4,
	heightAdjust = -14,
	Xheight = 15,
}


fonts = {}
fonts.sizes = {"small", "medium", "big", "quad"}

function fonts.alignY(fontname, alignment, scale)
	local alignment = alignment or 1
	local scale = scale or 1
	return -FONTS[fontname].Xheight/2*alignment*scale
end

function fonts.getHeight(fontname, scale)
	local scale = scale or 1
	return FONTS[fontname].Xheight*scale
end


function fonts.init()
	local path = daisy.getResourcesFolderPath("/daisyMoon/lib/fonts/")
	local contents = daisy.getFolderContents(path, "*.dat")	
	for index, row in pairs(contents) do
		local fontName = string.sub(row, 1, string.len(row) - string.len(".dat"))		
		local adjust = FONTS[fontName] or {}
		FONTS[fontName] = adjust
		local font_dat = "fonts/"..row
		local names = video.getSpriteStateNames(font_dat)
		for index, name in pairs(names) do
			local char = {sprite = video.createSpriteState(name, font_dat)}
			--print(fontName, index, name, char.sprite or "noooo")
			local w,h = video.getSpriteStateOriginalSize(char.sprite)		
			char.w, char.h = w or 0,h or 0
			char.w = char.w + (adjust.widthAdjust or 0)
			char.h = char.h + (adjust.heightAdjust or 0)
			
			if tostring(tonumber(name)) == name then
				FONTS[fontName][tonumber(name)] = char
			else
				FONTS[fontName][name] = char
			end		
			--print("font", fontName, index, font_dat)
		end
		
		if not adjust[32] and adjust[105] then
			adjust[32] = {w = adjust[105].w, h = adjust[105].h}
		end
	end
end