summaryrefslogtreecommitdiff
path: root/systems/android/hktex/HKtex2/src/worker4math/hktex2/ParagraphFormatting2.java
blob: e5fbbc5e6af60b7901556db257ac1faea5016b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// 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 android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;


public class ParagraphFormatting2 {

	public void paragraphformatting2(Canvas canvas, ParagraphBlock paragraphblock,  Float pagelength, String paragraph, Integer tid, Float indent, String thisattribute, Typeface tf){
 
		ArrayList<String> line = new ArrayList<String>();
		ArrayList<Float> width = new ArrayList<Float>();
		ArrayList<Float> height = new ArrayList<Float>();
		ArrayList<Float> depth = new ArrayList<Float>();
		ArrayList<Float> xpos = new ArrayList<Float>();
		ArrayList<Float> ypos = new ArrayList<Float>(); 
		ArrayList<String> attribute = new ArrayList<String>();
		ArrayList<Float> tlinelength = new ArrayList<Float>();
		Float linelength = 0.0f;
		Boolean newfirstline = false;
				
		int i;
        int j=0;

        int linenu=0;
        int fontsize = Integer.parseInt(thisattribute.substring(1,4));
        
        float linelength2 = 0;
        Boolean newfirstline2=false;;
        float spacelength = 0;

        float linelength1=0;
    	Paint textpaint = new Paint();
	    textpaint.setColor(Color.BLACK);	  	       
	  	textpaint.setTextSize(fontsize); 
	  	textpaint.setTypeface(tf);

	  	synchronized(this){paragraphblock.init(tid);}
	  	spacelength=textpaint.measureText(" ");

	  	paragraph=paragraph.trim();
	  	String words[] = paragraph.split(" ");
	  	String word="";
	  	newfirstline2=false;
	  	int sizej = words.length;
	  	j=0;
	  	line.add("");linenu=0;width.add(0.0f);height.add((float)fontsize);depth.add(0.0f);xpos.add(indent);ypos.add(0.0f);attribute.add(thisattribute);
	  	linelength2=indent;
	  	for (i=0;j<=sizej-1;i++){
	  		word=words[j].trim();
	  		word=words[j];
	  		if (word.equals("")){j++;} else{
	  			String line2=line.get(linenu)+word;

	  			linelength1=linelength2;
	  			linelength2=indent+textpaint.measureText(line2);
	  			if (linelength2  > pagelength){
	  				if (linenu==0){newfirstline2=true;}; 
	  				linelength2=linelength1;
	  				if (word.contains("-")){
	  					int sw=word.indexOf("-");
	  					String tword1=word.substring(0,sw+1);
	  					String tword2=word.substring(sw+1);
		  
	  					line2=line.get(linenu)+tword1;			
	  					linelength1=linelength2;
	  					linelength2=indent+textpaint.measureText(line2);
	  					if (linelength2  > pagelength){	  
	  						linelength2=linelength1;
	  						line.add("");linenu++;width.add(0.0f);height.add((float)fontsize);depth.add(0.0f);xpos.add(0.0f);ypos.add(0.0f);attribute.add(thisattribute);
	  						indent=0.0f;
	  						line.set(linenu, word+" ");
	  						linelength2=indent+textpaint.measureText(line.get(linenu)); 
	  						width.set(linenu, linelength2);
	  						ypos.set(linenu, ypos.get(linenu-1)+fontsize);			  
	  					}
	  					else {
	  						line.set(linenu,line.get(linenu)+tword1);	 	    	 
	  						width.set(linenu, linelength2-indent);    	  
	  						line.add("");linenu++;width.add(0.0f);height.add((float)fontsize);depth.add(0.0f);xpos.add(0.0f);ypos.add(0.0f);attribute.add(thisattribute);
	  						indent=0.0f;
	  						line.set(linenu, tword2+" ");
	  						linelength2=textpaint.measureText(line.get(linenu));
	  						width.set(linenu, linelength2);
	  						ypos.set(linenu, ypos.get(linenu-1)+fontsize+Header.linegap);
	  					};
	  					j++;
	  				} else {
	  					linelength2=linelength1;
	  					line.add("");linenu++;width.add(0.0f);height.add((float)fontsize);depth.add(0.0f);xpos.add(0.0f);ypos.add(0.0f);attribute.add(thisattribute);
	  					indent=0.0f;
	  					line.set(linenu, word+" ");
	  					linelength2=indent+textpaint.measureText(line.get(linenu));
	  					width.set(linenu, linelength2);
	  					ypos.set(linenu, ypos.get(linenu-1)+fontsize);	
	  					j++;
	  				}
	  			}else{
	  				line.set(linenu,line.get(linenu)+word+" ");
	  				linelength2=indent+textpaint.measureText(line.get(linenu));
	  				if (linenu==0 ){width.set(linenu, linelength2-indent);} else {width.set(linenu, linelength2);};
	  				j++;}
	  		}}
	  	linelength2=linelength2+spacelength;width.set(linenu, width.get(linenu)+spacelength);
		
	  	for (i=0;i<=linenu;i++){
			tlinelength.add(textpaint.measureText(line.get(i)));
		}
		linelength=linelength2;
		newfirstline=newfirstline2;
		synchronized(this){paragraphblock.add(tid, line, width, height, depth, xpos,ypos, attribute, tlinelength, linelength, newfirstline);};		
	}
}