//
// Latex Project Public Licence (lppl1.3) applies
// Originally written by Kwanleung Tse
// First Release : 1st July, 2014
// Last Update : 27th September, 2014
//
//
package worker4math.hktex2;

import java.util.ArrayList;
import java.util.HashMap;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Drawblock {

    float moleculeheight = 0.0f;
    float moleculelength = 0.0f;
    
	public  void drawblock(Canvas canvas, 
			ArrayList<String> block,
			ArrayList<Float> xpos,
			ArrayList<Float> ypos,
			ArrayList<String> attribute,
			HashMap<String, String> symbolunicode, HashMap<String, Integer> symboltype, HashMap<String, String> matchsymbol, 
			ArrayList<Typeface> tf ) {
      	
		int i,j1,ii,i2=0;
		int numatom = 0;
	    int fontcolor=0;
	    
    	String key = "";
		  
	    char temp = ' ';
	    char charExclaim = '!';

	    Colorfile color = new Colorfile();
	    color.colorfile();
      	  
	    Paint mPaint = new Paint();
	    mPaint.setColor(Color.BLACK);    
	    Paint lPaint= new Paint();
	    Paint sPaint = new Paint();
	    sPaint.setTypeface(tf.get(4));

  	  
	    numatom = block.size();
  	
	    for (i=0;i<=numatom-1;i++){
	    	if (attribute.get(i).charAt(0)=='g'){
	    		canvas.drawBitmap(DocumentBitmap.bitmap.get(block.get(i)), xpos.get(i), ypos.get(i), null);
	    	} else if (attribute.get(i).charAt(0)=='G'){
	    		canvas.drawBitmap(DocumentBitmap.bitmap.get(block.get(i)), xpos.get(i), ypos.get(i), null);
	    	} else if (attribute.get(i).charAt(0)=='1'){
	    		mPaint.setUnderlineText(false);mPaint.setStrikeThruText(false);mPaint.setTextSkewX(0);mPaint.setAlpha(255);mPaint.setShadowLayer(0,0,0,0);
	    		mPaint.setTextSize(Integer.parseInt(attribute.get(i).substring(1,4)));
        	
	    		Long il=Long.parseLong(attribute.get(i).substring(9,17),16);
	    		ii=il.intValue();
	    		mPaint.setColor(ii);
	    		mPaint.setTypeface(tf.get(Integer.parseInt(attribute.get(i).substring(4,7))));
	    		canvas.drawText(block.get(i), xpos.get(i), ypos.get(i), mPaint);
	    	} else {
	    		String atom = block.get(i);	           
  	
	    		Float positionx = xpos.get(i);	     	            	  
	    		Float positiony = ypos.get(i);	    
	    		String textattribute=attribute.get(i);
  	  
	    		if (textattribute.charAt(0)=='0'){
	    			if (textattribute.charAt(18) == '1'){mPaint.setUnderlineText(true);sPaint.setUnderlineText(true);} else {mPaint.setUnderlineText(false);sPaint.setUnderlineText(false);};
	    			if (textattribute.charAt(19) == '1'){mPaint.setStrikeThruText(true);sPaint.setStrikeThruText(true);} else{mPaint.setStrikeThruText(false);sPaint.setStrikeThruText(false);} ;
	    			if (textattribute.substring(20,25)!="+0.00"){mPaint.setTextSkewX(Float.parseFloat(textattribute.substring(20,25)));sPaint.setTextSkewX(Float.parseFloat(textattribute.substring(20,25)));}
	    			else {mPaint.setTextSkewX(0);sPaint.setTextSkewX(0);} ;
	    			if (textattribute.substring(25,29)!="0xFF"){ mPaint.setAlpha(Integer.parseInt(textattribute.substring(27,29),16));sPaint.setAlpha(Integer.parseInt(textattribute.substring(27,29),16));}
	    			else {mPaint.setAlpha(255);sPaint.setAlpha(255);};
	    			if (textattribute.charAt(29) != '0'){
	    				Long il=Long.parseLong(color.colorinteger.get(textattribute.substring(32)).substring(2),16);
	    				fontcolor=il.intValue();
	    				mPaint.setShadowLayer(Float.parseFloat(textattribute.substring(29,30)),Float.parseFloat(textattribute.substring(30,31)),
	    						Float.parseFloat(textattribute.substring(31,32)),fontcolor);
	    				sPaint.setShadowLayer(Float.parseFloat(textattribute.substring(29,30)),Float.parseFloat(textattribute.substring(30,31)),
	    						Float.parseFloat(textattribute.substring(31,32)),fontcolor);
	    			} else {
	    				mPaint.setShadowLayer(0,0,0,0);sPaint.setShadowLayer(0,0,0,0);  		  
	    			};
	    		} else {
	    			mPaint.setUnderlineText(false);sPaint.setUnderlineText(false);mPaint.setStrikeThruText(false);sPaint.setStrikeThruText(false);
	    			mPaint.setShadowLayer(0,0,0,0);sPaint.setShadowLayer(0,0,0,0);mPaint.setAlpha(255);sPaint.setAlpha(255);
	    			mPaint.setTextSkewX(0);sPaint.setTextSkewX(0);
	    		}
	    		mPaint.setTextSize(Integer.parseInt(attribute.get(i).substring(1,4)));
	    		Long il=Long.parseLong(attribute.get(i).substring(9,17),16);
	    		fontcolor=il.intValue();
        
	    		j1=0;
	    		for (int j=0; j1<=atom.length()-1 && j<1000; j++){
      	  
	    			temp = atom.charAt(j1);
	    			if (temp != charExclaim) {
             
	    				int fontinteger=Integer.parseInt(attribute.get(i).substring(4,7));
          	
	    				mPaint.setColor(fontcolor);
	    				mPaint.setTypeface(tf.get(fontinteger));
	    				canvas.drawText(Character.toString(temp), positionx, positiony, mPaint);
       	  
	    				float tempsize = mPaint.measureText(Character.toString(temp));
	    				positionx = positionx + tempsize;
	    				j1++;
	    			}  else if (temp == charExclaim) {
	    				key ="";
	    				for (i2=j1+1;!(atom.charAt(i2)==' ');i2++){
	    					key = key + atom.charAt(i2);
	    				}	                 			
	    				if (symboltype.containsKey(key) && (symboltype.get(key)<=13 ||symboltype.get(key)>=16) ){
	    					sPaint.setTextSize(Integer.parseInt(attribute.get(i).substring(1,4)));
	    					sPaint.setColor(fontcolor);
	    					canvas.drawText(symbolunicode.get(key), positionx, positiony, sPaint); 
	    					j1=j1+key.length()+2;     			  
	    				}else if (symboltype.containsKey(key) && (symboltype.get(key)==14 ||  symboltype.get(key)==15)){
	    					mPaint.setTextSize(Integer.parseInt(attribute.get(i).substring(1,4))); 
	    					mPaint.setTypeface(tf.get(5));
	    					mPaint.setColor(fontcolor);
	    					canvas.drawText(symbolunicode.get(key), positionx, positiony, mPaint);  
	    					j1=j1+key.length()+2;
	    				}else if (key.equals("hlin")){
	    					lPaint.setColor(fontcolor);
	    					int strokewidth=1;
	    					lPaint.setStrokeWidth(strokewidth);
	    					float tempy=Integer.parseInt(attribute.get(i).substring(1,4));
	    					canvas.drawLine(positionx, positiony, positionx+tempy, positiony, lPaint);
	    					j1+=6;	  
	    				}else if (key.equals("vlin")){
	    					lPaint.setColor(fontcolor);
	    					int strokewidth=1;
	    					lPaint.setStrokeWidth(strokewidth);
	    					float tempy=Integer.parseInt(attribute.get(i).substring(1,4));
	    					canvas.drawLine(positionx, positiony, positionx, positiony-tempy, lPaint);
            			  j1+=6;	  
	    				}else if (key.equals("dno")){
//  do nothing
	    					j1+=5;
	    				}  else if (key.equals("hsp")){	                 	    	 
	    					j1+=5;            		   
	    				} else if (key.equals("vsp")){	                 	    	 
	    					j1+=5;	              		   
	    				}else if (key.equals("gho")){	        	    	
	    					j1+=5;	    	              		   
	    				}else  {       	    	
	    					mPaint.setColor(fontcolor);
	    					mPaint.setTypeface(tf.get(0));
	    					canvas.drawText(symbolunicode.get(key), positionx, positiony, mPaint);	  		           

	    					j1=j1+key.length()+2;                      }
       	     
	    			}   
	    		} }}
           } 
    } 

