vis_files/button.js
/// Splits a string into substrings to accout for subscripts
Splits a string into substrings to accout for subscripts
Splits a string into substrings to accout for subscripts
function splitintosub(st,font)                             
{
	var lab=[];
	var si = get_fontsize(font);
	var fsi = Math.floor(0.7*si);
	var font2 = resize_font(font,fsi);
	var italicfont = "italic "+si+"px Times";
	var italicfont2 = "italic "+fsi+"px Times";
	if(font.includes("bold") == true){ italicfont = "bold "+italicfont; italicfont2 = "bold "+italicfont2;}
	var j = 0, w = 0, italic = 0;
	do{
		if(st.substr(j,1) != "_" && st.substr(j,1) != "^" && st.substr(j,1) != "&"){
			jst = j;
			while(j < st.length && st.substr(j,1) != "_" && st.substr(j,1) != "^" && st.substr(j,1) != "&") j++;
			frag = st.substr(jst,j-jst);
			if(italic == 0) fo = font; else fo = italicfont;
			if(frag == "Σ") fo = resize_font(font,Math.floor(1.2*si));
			dw = textwidth_simp(frag,fo)+1;
			lab.push({frag:frag, type:"norm", size:"big", w:w, dw:dw, font:fo, dy:0, italic:italic});
			w += dw;
		}
		else{
			if(st.substr(j,1) == "&"){
				italic = 1-italic;
				j++;
			}
			else{
				var type;
				if(st.substr(j,1) == "_"){ type = "sub"; dy = 0.2*si;}
				else{ type = "sup"; dy = -0.4*si;}
				j++; 
				if(st.substr(j,1) == "{"){
					j++;
					jst = j;
					while(j < st.length && st.substr(j,1) != "}") j++;
					if(j ==  st.length) alertp("Bracket problem");
					frag = st.substr(jst,j-jst);
					j++;
				}
				else{
					jst = j;
					while(j < st.length && st.substr(j,1) != " " &&  st.substr(j,1) != "=" && st.substr(j,1) != "]" && st.substr(j,1) != ")" && st.substr(j,1) != "}" && st.substr(j,1) != "," && st.substr(j,1) != "." && st.substr(j,1) != "_" && st.substr(j,1) != "^" && st.substr(j,1) != "&") j++;
					frag = st.substr(jst,j-jst);
				}
				if(frag != ""){
					dw = textwidth_simp(frag,font2);
					if(italic == 0) fo = font2; else fo = italicfont2;
					lab.push({frag:frag, type:type, w:w, dw:dw, font:fo, dy:dy, italic:italic});
					w += dw;
				}
			}
		}
	}while(j < st.length);
	var i;
	for(i = 0; i < lab.length-1; i++){
		if((lab[i].type == "sub" && lab[i+1].type == "sup") || (lab[i].type == "sup" && lab[i+1].type == "sub")){
			var dw = lab[i].dw, dw2 = lab[i+1].dw;
			if(dw2 > dw){
				for(j = i+1; j < lab.length; j++) lab[j].w -= dw;
			}
			else{
				lab[i+1].w -= dw;
				for(j = i+2; j < lab.length; j++) lab[j].w -= dw2;
			}
		}
	}
	for(i = 1; i < lab.length; i++){
		if(lab[i].type == "sub"){
			if(lab[i-1].type == "norm" && lab[i-1].italic == 1){		
				dw = 2;
				for(j = i; j < lab.length; j++) lab[j].w -= dw;
			}
		}
	}
	return lab;
}
function splitintosub ( st , font ) { var lab = [ ] ; var si = get_fontsize ( font ) ; var fsi = Math . floor ( 0.7 * si ) ; var font2 = resize_font ( font , fsi ) ; var italicfont = "italic " + si + "px Times" ; var italicfont2 = "italic " + fsi + "px Times" ; if ( font . includes ( "bold" ) == true ) { italicfont = "bold " + italicfont ; italicfont2 = "bold " + italicfont2 ; } var j = 0 , w = 0 , italic = 0 ; do { if ( st . substr ( j , 1 ) != "_" && st . substr ( j , 1 ) != "^" && st . substr ( j , 1 ) != "&" ) { jst = j ; while ( j < st . length && st . substr ( j , 1 ) != "_" && st . substr ( j , 1 ) != "^" && st . substr ( j , 1 ) != "&" ) j ++ ; frag = st . substr ( jst , j - jst ) ; if ( italic == 0 ) fo = font ; else fo = italicfont ; if ( frag == "Σ") o  esize_font( f ont, M ath. f loor( 1 .2* s i) ) ; dw = textwidth_simp ( frag , fo ) + 1 ; lab . push ( { frag : frag , type : "norm" , size : "big" , w : w , dw : dw , font : fo , dy : 0 , italic : italic } ) ; w += dw ; } else { if ( st . substr ( j , 1 ) == "&" ) { italic = 1 - italic ; j ++ ; } else { var type ; if ( st . substr ( j , 1 ) == "_" ) { type = "sub" ; dy = 0.2 * si ; } else { type = "sup" ; dy = - 0.4 * si ; } j ++ ; if ( st . substr ( j , 1 ) == "{" ) { j ++ ; jst = j ; while ( j < st . length && st . substr ( j , 1 ) != "}" ) j ++ ; if ( j == st . length ) alertp ( "Bracket problem" ) ; frag = st . substr ( jst , j - jst ) ; j ++ ; } else { jst = j ; while ( j < st . length && st . substr ( j , 1 ) != " " && st . substr ( j , 1 ) != "=" && st . substr ( j , 1 ) != "]" && st . substr ( j , 1 ) != ")" && st . substr ( j , 1 ) != "}" && st . substr ( j , 1 ) != "," && st . substr ( j , 1 ) != "." && st . substr ( j , 1 ) != "_" && st . substr ( j , 1 ) != "^" && st . substr ( j , 1 ) != "&" ) j ++ ; frag = st . substr ( jst , j - jst ) ; } if ( frag != "" ) { dw = textwidth_simp ( frag , font2 ) ; if ( italic == 0 ) fo = font2 ; else fo = italicfont2 ; lab . push ( { frag : frag , type : type , w : w , dw : dw , font : fo , dy : dy , italic : italic } ) ; w += dw ; } } } } while ( j < st . length ) ; var i ; for ( i = 0 ; i < lab . length - 1 ; i ++ ) { if ( ( lab [ i ] . type == "sub" && lab [ i + 1 ] . type == "sup" ) || ( lab [ i ] . type == "sup" && lab [ i + 1 ] . type == "sub" ) ) { var dw = lab [ i ] . dw , dw2 = lab [ i + 1 ] . dw ; if ( dw2 > dw ) { for ( j = i + 1 ; j < lab . length ; j ++ ) lab [ j ] . w -= dw ; } else { lab [ i + 1 ] . w -= dw ; for ( j = i + 2 ; j < lab . length ; j ++ ) lab [ j ] . w -= dw2 ; } } } for ( i = 1 ; i < lab . length ; i ++ ) { if ( lab [ i ] . type == "sub" ) { if ( lab [ i - 1 ] . type == "norm" && lab [ i - 1 ] . italic == 1 ) { dw = 2 ; for ( j = i ; j < lab . length ; j ++ ) lab [ j ] . w -= dw ; } } } return lab ; }
Splits a string into substrings to accout for subscripts
Splits a string into substrings to accout for subscripts