1
|
package org.distorted.keyboard;
|
2
|
|
3
|
import android.graphics.Canvas;
|
4
|
import android.graphics.Paint;
|
5
|
import android.graphics.Paint.Align;
|
6
|
|
7
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
8
|
|
9
|
public class StringBlock
|
10
|
{
|
11
|
//private static final String TAG_BLOCK="SokobanStringBlock";
|
12
|
|
13
|
private String str;
|
14
|
private int height,length;
|
15
|
private int realheight;
|
16
|
private int size, len;
|
17
|
private Paint mPaint;
|
18
|
private String buffer;
|
19
|
|
20
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
21
|
|
22
|
public StringBlock(String s, int h, int l)
|
23
|
{
|
24
|
str =s;
|
25
|
length =l;
|
26
|
height =h;
|
27
|
len = str.length();
|
28
|
|
29
|
mPaint = new Paint();
|
30
|
buffer = new String();
|
31
|
|
32
|
mPaint.setTextAlign(Align.LEFT);
|
33
|
mPaint.setAntiAlias(true);
|
34
|
mPaint.setFakeBoldText(true);
|
35
|
|
36
|
size = computeOptimalSize();
|
37
|
}
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public void draw(Canvas c, int x,int y, int color)
|
42
|
{
|
43
|
String tmp;
|
44
|
int begin=0, end=0;
|
45
|
int delta = (height-realheight)/2;
|
46
|
|
47
|
mPaint.setColor(color);
|
48
|
mPaint.setTextSize(size);
|
49
|
|
50
|
while( end>=0 && end<len )
|
51
|
{
|
52
|
end = getLine(str,size,begin,length);
|
53
|
|
54
|
if( end>=0 )
|
55
|
{
|
56
|
if( str.charAt(begin) == '\n' ) begin++;
|
57
|
|
58
|
if( end>begin )
|
59
|
{
|
60
|
tmp = str.substring(begin,end);
|
61
|
|
62
|
if( end<len && str.charAt(end+1) != '\n' )
|
63
|
displayJustified( tmp, size, x, y+delta, length, c);
|
64
|
else
|
65
|
c.drawText( tmp, x, y+delta+size, mPaint);
|
66
|
}
|
67
|
|
68
|
y += (begin==end ? size/2 : size);
|
69
|
begin = end;
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
private void displayJustified(String str, int fontHeight, int x, int y, int length, Canvas c)
|
77
|
{
|
78
|
int len = str.length();
|
79
|
int numspaces = retNumSpaces(str);
|
80
|
|
81
|
if( str.charAt(len-1) == ' ' ) numspaces--;
|
82
|
|
83
|
float left=x,w = (numspaces>0 ? (float)(length-mPaint.measureText(str))/numspaces : 0);
|
84
|
String tmp;
|
85
|
int begin,end=0;
|
86
|
|
87
|
while( end<len )
|
88
|
{
|
89
|
begin=end;
|
90
|
end = str.indexOf(' ', begin)+1;
|
91
|
if( end<=0 ) end = len;
|
92
|
|
93
|
tmp = str.substring(begin,end);
|
94
|
c.drawText( tmp, left, y+fontHeight, mPaint);
|
95
|
left+= (mPaint.measureText(tmp)+w);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
private int retNumSpaces(String str)
|
102
|
{
|
103
|
int begin=0, ret=0;
|
104
|
|
105
|
while( true )
|
106
|
{
|
107
|
begin = str.indexOf(' ', begin) +1;
|
108
|
|
109
|
if( begin>0 ) ret++;
|
110
|
else break;
|
111
|
}
|
112
|
|
113
|
return ret;
|
114
|
}
|
115
|
|
116
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117
|
|
118
|
private int getLine(String text, int fontHeight, int begin, int width)
|
119
|
{
|
120
|
int nearestSpace = text.indexOf(' ' , begin+1);
|
121
|
int nearestNewline = text.indexOf('\n', begin+1);
|
122
|
int len=0;
|
123
|
|
124
|
mPaint.setTextSize(fontHeight);
|
125
|
|
126
|
if( nearestNewline>=0 && nearestNewline<nearestSpace ) return nearestNewline;
|
127
|
if( nearestSpace<0 ) return text.length();
|
128
|
|
129
|
buffer = text.substring(begin,nearestSpace);
|
130
|
|
131
|
len = (int)mPaint.measureText(buffer);
|
132
|
|
133
|
if( len>=width ) return nearestSpace+1;
|
134
|
|
135
|
int lastSpace = nearestSpace;
|
136
|
|
137
|
while( len<width )
|
138
|
{
|
139
|
lastSpace = nearestSpace;
|
140
|
|
141
|
nearestNewline = text.indexOf('\n', lastSpace+1);
|
142
|
nearestSpace = text.indexOf(' ' , lastSpace+1);
|
143
|
|
144
|
if( nearestNewline>=0 && nearestNewline<nearestSpace )
|
145
|
{
|
146
|
buffer = text.substring(begin,nearestNewline);
|
147
|
len = (int)mPaint.measureText(buffer);
|
148
|
return len<width ? nearestNewline : lastSpace+1;
|
149
|
}
|
150
|
if( nearestSpace<0 )
|
151
|
{
|
152
|
buffer = text.substring(begin);
|
153
|
len= (int)mPaint.measureText(buffer);
|
154
|
return len<width ? text.length() : lastSpace+1;
|
155
|
}
|
156
|
|
157
|
buffer = text.substring(begin,nearestSpace);
|
158
|
len = (int)mPaint.measureText(buffer);
|
159
|
}
|
160
|
return lastSpace+1;
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
private int computeOptimalSize()
|
166
|
{
|
167
|
int h=0, trysize=10;
|
168
|
|
169
|
while( h<height )
|
170
|
{
|
171
|
realheight = h;
|
172
|
h = height(++trysize);
|
173
|
}
|
174
|
|
175
|
return trysize-1;
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
private int height(int trySize)
|
181
|
{
|
182
|
int y=0 , begin=0, end=0;
|
183
|
|
184
|
while( end>=0 && end<len )
|
185
|
{
|
186
|
end = getLine(str,trySize,begin,length);
|
187
|
|
188
|
if( end>=0 )
|
189
|
{
|
190
|
if( str.charAt(begin) == '\n' ) begin++;
|
191
|
y += (begin==end ? trySize/2 : trySize);
|
192
|
begin = end;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
return y;
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
// end of RRStringBlock
|
201
|
}
|