function larghezzaStringaInPixel(testoDaMisurare, fontSize, fontFam) 
{ 
	var divFantasmaContenitoreStringa = document.createElement("div"); 
	with (divFantasmaContenitoreStringa) 
		{ 
			id = "divFantasmaContenitoreStringa"; 
			style.visibility = "hidden"; 
			style.position = "absolute";
			style.fontFamily = fontFam;
			style.fontSize = fontSize; 
			style.backgroundColor='green';
		} 
	document.body.appendChild(divFantasmaContenitoreStringa); 
	carattere_spazio_da_sostituire = / /g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, "_"); 

	carattere_spazio_da_sostituire = /&nbsp;/g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, "_"); 	

	testoDaMisurare = testoDaMisurare.replace(/\s+/g, ""); 
	carattere_spazio_da_sostituire = /\r/g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, ""); 

	carattere_spazio_da_sostituire = /\n/g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, ""); 
		
	divFantasmaContenitoreStringa.innerHTML = testoDaMisurare; 	
	rightcoord = divFantasmaContenitoreStringa.getBoundingClientRect().right;
	leftcoord = divFantasmaContenitoreStringa.getBoundingClientRect().left;
	tmpWidth = (rightcoord - leftcoord);
	document.body.removeChild(divFantasmaContenitoreStringa); 	
	return tmpWidth; 
} 

function altezzaStringaInPixel(testoDaMisurare, fontSize, fontFam) 
{ 
	var divFantasmaContenitoreStringa = document.createElement("div"); 
	with (divFantasmaContenitoreStringa) 
		{ 
			id = "divFantasmaContenitoreStringa"; 
			style.visibility = "hidden"; 
			style.top = '0px';
			style.left = '0px';
			style.backgroundcolor = '#000000';
			style.position = "absolute"; 
 			style.fontFamily = fontFam;
			style.fontSize = fontSize; 
		} 
	document.body.appendChild(divFantasmaContenitoreStringa); 
	carattere_spazio_da_sostituire = / /g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, "_"); 
	carattere_spazio_da_sostituire = /<br>/g; 
	testoDaMisurare = testoDaMisurare.replace(carattere_spazio_da_sostituire, ""); 

	//testoDaMisurare = testoDaMisurare.replaceAll("\r\n|\r|\n", '');

	divFantasmaContenitoreStringa.innerHTML = testoDaMisurare; 	
	bottomcoord = divFantasmaContenitoreStringa.getBoundingClientRect().bottom;
	topcoord = divFantasmaContenitoreStringa.getBoundingClientRect().top;
	tmpHeight = (bottomcoord-topcoord);
	document.body.removeChild(divFantasmaContenitoreStringa); 
	return tmpHeight; 
} 

function allinea_massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite, lench)
{
	var res = '';
	var i = lench;
	var LT = larghezzaStringaInPixel(testo.substr(0, i), fontSize, fontFam);
	
	while (LT<lungLimite)
			{
				i++;
				LT = larghezzaStringaInPixel(testo.substr(0, i), fontSize, fontFam);
			}

	while (LT>lungLimite)
			{
				i--;
				LT = larghezzaStringaInPixel(testo.substr(0, i), fontSize, fontFam);
			}
	res = testo.substr(0, i);
	return res;
}


function massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite)
{
	var res = '';
	var LT = larghezzaStringaInPixel(testo, fontSize, fontFam);

	if (LT<=lungLimite)
		{
			res = testo;
		}
	else
		{
			var StrLn = testo.length;
			var StrLimL = Math.floor(StrLn*lungLimite/LT);
			res = allinea_massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite, StrLimL);
		}
	return res;
}


function massima_porzione_di_testo_intero_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite)
{
	var res = massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite);
	if (res!='')
		{
			if ((res.substr(res.lenght-1, 1)!=' ') && (res.substr(res.lenght, 1)!=' '))
				{
					var ind = res.lastIndexOf(' ');
					if (ind!=-1)
						{
							res = res.substr(0, ind+1);
						}
				}
		}
	return res;
}

function metti_puntini_sospensivi(testo, fontSize, fontFam, lungLimite)
{
	var ch_puntini_sospensivi = '&hellip;';
	var res = '';
	if (testo!='')
		{
			res = testo+ch_puntini_sospensivi;
			var L = larghezzaStringaInPixel(res, fontSize, fontFam);
			if (L<=lungLimite)
				{
					return res;
				}
			else
				{
					res = metti_puntini_sospensivi(testo.substr(0, testo.length-1), fontSize, fontFam, lungLimite);
				}		
		}
	return res;
}

function testo_formattato_contenuto_in_orizzontale(testo, fontSize, fontFam, lungLimite)
{
	var res = massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(testo, fontSize, fontFam, lungLimite);
	if (res!='')
		{
			res = metti_puntini_sospensivi(testo, fontSize, fontFam, lungLimite);
		}
	return res;
}

function testo_formattato_contenuto_in_rect(testo, fontSize, fontFam, lungLimite, altLimite)
{
	var res = '';
	var T = ''
	var PT = '';
	var TR = testo;
	var altRes = altLimite;
	var altStr = altezzaStringaInPixel(TR, fontSize, fontFam);
	alert(TR);
	alert('altezza limite: '+altRes);
	alert('altezza stringa'+altStr);
	while (altRes>=altStr)//(altRes>=altStr)
		{
			PT = massima_porzione_di_testo_in_orizzontale_contenibile_in_lunghezza(TR, fontSize, fontFam, lungLimite);
			T = T + PT;
			TR = TR.substr(PT.length, TR.length-PT.length);
			altRes=altRes-altStr;
			altStr = altezzaStringaInPixel(TR, fontSize, fontFam);
			if (altRes>=altStr)
				{
					T=T+'<br>';
				}
		}
	res = T;
	return res;
}

function testo_formattato_contenuto_intero_in_rect(testo, fontSize, fontFam, lungLimite, altLimite)
{
	var res = '';
	var T = ''
	var PT = '';
	var TR = testo;
	var altRes = altLimite;
	var altStr = altezzaStringaInPixel(TR, fontSize, fontFam);
	
	while (altRes>=altStr)
		{
			PT = massima_porzione_di_testo_intero_in_orizzontale_contenibile_in_lunghezza(TR, fontSize, fontFam, lungLimite);
			T = T + PT;
			TR = TR.substr(PT.length, TR.length-PT.length);
			altRes=altRes-altStr;
			altStr = altezzaStringaInPixel(TR, fontSize, fontFam);
			if (altRes>=altStr)
				{
					T=T+'<br>';
				}
		}
	res = T;
	return res;
}

