Very simple. Create a span for the text, get the width and reduce font-size until the span has the same width of the div container:
while($("#container").find("span").width() > $("#container").width()) {
var currentFontSize = parseInt($("#container").find("span").css("font-size"));
$("#container").find("span").css("font-size",currentFontSize-1);
}