154 |
public float getScale(float height) { |
public float getScale(float height) { |
155 |
float start = 1f; |
float start = 1f; |
156 |
while (getFontMetrics(start).getHeight() < height) start += 1f; |
while (getFontMetrics(start).getHeight() < height) start += 1f; |
157 |
if ((int)getFontMetrics(start-1f).getHeight() == (int)height) return start-1f; |
if (dbg) p("Intended height: " + height); |
158 |
else if ((int)getFontMetrics(start).getHeight() == (int)height) return start; |
if (dbg) p("Seeking area from " + (start-1f) + " (height: " + |
159 |
|
getFontMetrics(start-1f).getHeight() + ") to " + start + " (height: " + |
160 |
|
getFontMetrics(start).getHeight() + ")"); |
161 |
|
|
162 |
|
if ((int)getFontMetrics(start-1f).getHeight() == (int)height) { |
163 |
|
if (dbg) p("Best: " + (start-1f) + " (height: " + |
164 |
|
getFontMetrics(start-1f).getHeight() + ")"); |
165 |
|
return start-1f; |
166 |
|
} else if ((int)getFontMetrics(start).getHeight() == (int)height) { |
167 |
|
if (dbg) p("Best: " + (start) + " (height: " + |
168 |
|
getFontMetrics(start).getHeight() + ")"); |
169 |
|
return start; |
170 |
|
} |
171 |
|
|
172 |
if (dbg) p("Enters a recursive search for the best font scale for given height."); |
if (dbg) p("Enters a recursive search for the best font scale for given height."); |
|
if (dbg) p("Intended height: " + height + " Start scale: " + start + |
|
|
" (height: " + getFontMetrics(start).getHeight() + ")"); |
|
173 |
return seekBestScale(start-1f, start, 0, height); |
return seekBestScale(start-1f, start, 0, height); |
174 |
|
|
175 |
/* |
/* |
194 |
* @param height the intended height of drawn text |
* @param height the intended height of drawn text |
195 |
*/ |
*/ |
196 |
private float seekBestScale(float start, float end, float best, float height) { |
private float seekBestScale(float start, float end, float best, float height) { |
197 |
if (getFontMetrics(start).getHeight() > height) return 1; // for failsafety |
if (getFontMetrics(start).getHeight() > height) return 1; // for fail-safety |
198 |
if (dbg) p("Start: " + start + " End: " + end + " Best: " + best + " Height: " + height); |
if (dbg) p("Start: " + start + " End: " + end + " Best: " + best + " Height: " + height); |
199 |
|
|
200 |
float current = getFontMetrics(start + (end-start)/2).getHeight(); |
float current = start + (end-start)/2; |
201 |
if ((int)current == (int)getFontMetrics(best).getHeight() |
float current_height = getFontMetrics(current).getHeight(); |
202 |
|| (int)current == (int)height) { |
float best_height = getFontMetrics(best).getHeight(); |
203 |
if (dbg) p("Best: " + best + " (height: " + current + ")"); |
|
204 |
|
if ((int)current_height == (int)height && (int)current_height != (int)best_height) { |
205 |
|
if (dbg) p("Best: " + current_height + " (height: " + current_height + ")"); |
206 |
|
return current; |
207 |
|
} else if ((int)current_height == (int)best_height) { |
208 |
|
if (dbg) p("Best: " + best_height + " (height: " + best_height + ")"); |
209 |
return best; |
return best; |
210 |
} else best = start + (end-start)/2; |
} else best = current; |
211 |
|
|
212 |
if (current > height) return seekBestScale(start, start + (end-start)/2, best, height); |
if (current_height > height) return seekBestScale(start, current, best, height); |
213 |
else return seekBestScale(start + (end-start)/2, end, best, height); |
else return seekBestScale(current, end, best, height); |
214 |
} |
} |
215 |
} |
} |
216 |
|
|