|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui; |
|
18 |
|
19 import java.util.Enumeration; |
|
20 import java.util.Hashtable; |
|
21 |
|
22 import org.eclipse.ercp.swt.mobile.MobileShell; |
|
23 import org.eclipse.swt.SWT; |
|
24 import org.eclipse.swt.graphics.FontData; |
|
25 import org.eclipse.swt.internal.qt.graphics.FontUtils; |
|
26 import org.eclipse.swt.widgets.*; |
|
27 |
|
28 /** |
|
29 * Implementation of LCDUI <code>Font</code> class. |
|
30 */ |
|
31 public final class Font { |
|
32 |
|
33 /** |
|
34 * System font face. |
|
35 * |
|
36 * @value for FACE_SYSTEM is 0. |
|
37 */ |
|
38 public static final int FACE_SYSTEM = 0; |
|
39 |
|
40 /** |
|
41 * Monospace font face. |
|
42 * |
|
43 * @value for FACE_MONOSPACE is 32. |
|
44 */ |
|
45 public static final int FACE_MONOSPACE = 32; |
|
46 |
|
47 /** |
|
48 * Proportional font face. |
|
49 * |
|
50 * @value for FACE_PROPORTIONAL is 64. |
|
51 */ |
|
52 public static final int FACE_PROPORTIONAL = 64; |
|
53 |
|
54 /** |
|
55 * Plain font style. |
|
56 * |
|
57 * @value for STYLE_PLAIN is 0. |
|
58 */ |
|
59 public static final int STYLE_PLAIN = 0; |
|
60 |
|
61 /** |
|
62 * Bold font style. |
|
63 * |
|
64 * @value for STYLE_BOLD is 1. |
|
65 */ |
|
66 public static final int STYLE_BOLD = 1; |
|
67 |
|
68 /** |
|
69 * Italic font style. |
|
70 * |
|
71 * @value for STYLE_ITALIC is 2. |
|
72 */ |
|
73 public static final int STYLE_ITALIC = 2; |
|
74 |
|
75 /** |
|
76 * Underlined font style. |
|
77 * |
|
78 * @value for STYLE_UNDERLINED is 4. |
|
79 */ |
|
80 public static final int STYLE_UNDERLINED = 4; |
|
81 |
|
82 /** |
|
83 * Small font size. |
|
84 * |
|
85 * @value for SIZE_SMALL is 8. |
|
86 */ |
|
87 public static final int SIZE_SMALL = 8; |
|
88 |
|
89 /** |
|
90 * Medium font size. |
|
91 * |
|
92 * @value for SIZE_MEDIUM is 0. |
|
93 */ |
|
94 public static final int SIZE_MEDIUM = 0; |
|
95 |
|
96 /** |
|
97 * Large font size. |
|
98 * |
|
99 * @value for SIZE_LARGE is 16. |
|
100 */ |
|
101 public static final int SIZE_LARGE = 16; |
|
102 |
|
103 /** |
|
104 * Font specifier for default input text drawing. |
|
105 * |
|
106 * @value for FONT_INPUT_TEXT is 1. |
|
107 */ |
|
108 public static final int FONT_INPUT_TEXT = 1; |
|
109 |
|
110 /** |
|
111 * Font specifier for default Item and Screen drawing. |
|
112 * |
|
113 * @value for FONT_STATIC_TEXT is 0. |
|
114 */ |
|
115 public static final int FONT_STATIC_TEXT = 0; |
|
116 |
|
117 private static final int STYLE_ALL = STYLE_PLAIN | STYLE_BOLD |
|
118 | STYLE_ITALIC | STYLE_UNDERLINED; |
|
119 |
|
120 private static final String FONTDATA_CONST = "|-1|-1|-1|-1|-1|-1|1|"; |
|
121 |
|
122 /** |
|
123 * Lookup table which stores LCDUI fonts based on FontData keys. |
|
124 */ |
|
125 private static Hashtable fontTable = new Hashtable(); |
|
126 private static FontUtils fontUtils; |
|
127 private static int[] tempFontRect = new int[4]; |
|
128 private static FontData eswtTempFontData; |
|
129 private static FontData eswtSystemFontData; |
|
130 private int eswtTempFontAscent; |
|
131 private int eswtTempFontHeight; |
|
132 private int eswtTempFontWidth; |
|
133 |
|
134 // Instance members. |
|
135 |
|
136 private org.eclipse.swt.graphics.Font eswtFont; |
|
137 private int face; |
|
138 private int style; |
|
139 private int size; |
|
140 |
|
141 /** |
|
142 * Disposes all fonts created. |
|
143 */ |
|
144 static void disposeFonts() { |
|
145 for (Enumeration e = fontTable.elements(); e.hasMoreElements();) { |
|
146 Font font = (Font) e.nextElement(); |
|
147 font.eswtFont.dispose(); |
|
148 } |
|
149 fontTable.clear(); |
|
150 } |
|
151 |
|
152 /** |
|
153 * LCDUI Font --> eSWT Font |
|
154 */ |
|
155 static org.eclipse.swt.graphics.Font getESWTFont(Font font) { |
|
156 if (font != null) { |
|
157 return font.eswtFont; |
|
158 } |
|
159 return null; |
|
160 } |
|
161 |
|
162 /** |
|
163 * eSWT Font --> LCDUI Font |
|
164 */ |
|
165 static Font getFont(final org.eclipse.swt.graphics.Font font) { |
|
166 if (font != null) { |
|
167 FontData fd = getFontData(font); |
|
168 return getFont(fd); |
|
169 } |
|
170 return null; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Returns default font. |
|
175 * |
|
176 * @return default system font. |
|
177 */ |
|
178 public static Font getDefaultFont() { |
|
179 return getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM); |
|
180 } |
|
181 |
|
182 /** |
|
183 * Request specified Font. |
|
184 * |
|
185 * @param specifier font specifier |
|
186 * @return the font |
|
187 */ |
|
188 public static Font getFont(int specifier) { |
|
189 if (specifier == FONT_INPUT_TEXT || specifier == FONT_STATIC_TEXT) { |
|
190 return getFont(getFontData(specifier)); |
|
191 } |
|
192 else { |
|
193 throw new IllegalArgumentException( |
|
194 MsgRepository.FONT_EXCEPTION_INVALID_SPECIFIER); |
|
195 } |
|
196 } |
|
197 |
|
198 /** |
|
199 * Request Font based on parameters. |
|
200 * |
|
201 * @param face font face |
|
202 * @param style font style |
|
203 * @param size font size |
|
204 * @return the font |
|
205 */ |
|
206 public static Font getFont(int face, int style, int size) { |
|
207 if (face != FACE_SYSTEM && face != FACE_MONOSPACE |
|
208 && face != FACE_PROPORTIONAL) { |
|
209 throw new IllegalArgumentException( |
|
210 MsgRepository.FONT_EXCEPTION_INVALID_FACE); |
|
211 } |
|
212 if (style < STYLE_PLAIN || style > STYLE_ALL) { |
|
213 throw new IllegalArgumentException( |
|
214 MsgRepository.FONT_EXCEPTION_INVALID_STYLE); |
|
215 } |
|
216 if (size != SIZE_SMALL && size != SIZE_MEDIUM && size != SIZE_LARGE) { |
|
217 throw new IllegalArgumentException( |
|
218 MsgRepository.FONT_EXCEPTION_INVALID_SIZE); |
|
219 } |
|
220 FontData fd = getFontData(face, style, size); |
|
221 Font font = getFont(fd); |
|
222 // We must handle underlining separately because it is not part of |
|
223 // eSWT's style. |
|
224 if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) { |
|
225 font.style |= Font.STYLE_UNDERLINED; |
|
226 } |
|
227 return font; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Request freesized Font. |
|
232 * |
|
233 * @param face font face |
|
234 * @param style font style |
|
235 * @param height font height |
|
236 * @return the font |
|
237 */ |
|
238 static Font getFreeSizedFont(int face, int style, int height) { |
|
239 if (face != FACE_SYSTEM && face != FACE_MONOSPACE |
|
240 && face != FACE_PROPORTIONAL) { |
|
241 throw new IllegalArgumentException( |
|
242 MsgRepository.FONT_EXCEPTION_INVALID_FACE); |
|
243 } |
|
244 if (style < STYLE_PLAIN || style > STYLE_ALL) { |
|
245 throw new IllegalArgumentException( |
|
246 MsgRepository.FONT_EXCEPTION_INVALID_STYLE); |
|
247 } |
|
248 if (height < 0) { |
|
249 throw new IllegalArgumentException( |
|
250 MsgRepository.FONT_EXCEPTION_INVALID_SIZE); |
|
251 } |
|
252 FontData fd = getFontData(face, style, height); |
|
253 Font font = getFont(fd); |
|
254 // We must handle underlining separately because it is not part of |
|
255 // eSWT's style. |
|
256 if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) { |
|
257 font.style |= Font.STYLE_UNDERLINED; |
|
258 } |
|
259 return font; |
|
260 } |
|
261 |
|
262 /** |
|
263 * Lookup or create a font based on given FontData. |
|
264 * |
|
265 * @param fontdata font's data |
|
266 * @return a font |
|
267 */ |
|
268 private static Font getFont(FontData fontdata) { |
|
269 Font ret = null; |
|
270 if (fontdata != null) { |
|
271 if (fontTable.containsKey(fontdata)) { |
|
272 ret = (Font) fontTable.get(fontdata); |
|
273 } |
|
274 else { |
|
275 ret = new Font(fontdata); |
|
276 } |
|
277 } |
|
278 return ret; |
|
279 } |
|
280 |
|
281 /** |
|
282 * Constructor (private).<br> |
|
283 * |
|
284 * @param reqFD the requested eSWT FontData |
|
285 */ |
|
286 private Font(final FontData reqFD) { |
|
287 ESWTUIThreadRunner.syncExec(new Runnable() { |
|
288 public void run() { |
|
289 eswtFont = new org.eclipse.swt.graphics.Font(ESWTUIThreadRunner |
|
290 .getInstance().getDisplay(), reqFD); |
|
291 } |
|
292 }); |
|
293 // get FontData info from resulted Font, because resulting FontData |
|
294 // might be different than the requested one |
|
295 FontData resFD = getFontData(eswtFont); |
|
296 this.face = mapFontNameToFace(resFD.getName()); |
|
297 this.style = mapESWTStyleToStyle(resFD.getStyle()); |
|
298 this.size = mapHeightToSize(resFD.getHeight()); |
|
299 fontTable.put(resFD, this); |
|
300 } |
|
301 |
|
302 /** |
|
303 * Return font's style. |
|
304 * |
|
305 * @return style |
|
306 */ |
|
307 public int getStyle() { |
|
308 return style; |
|
309 } |
|
310 |
|
311 /** |
|
312 * Return font's size. |
|
313 * |
|
314 * @return size |
|
315 */ |
|
316 public int getSize() { |
|
317 return size; |
|
318 } |
|
319 |
|
320 /** |
|
321 * Return font's face. |
|
322 * |
|
323 * @return face |
|
324 */ |
|
325 public int getFace() { |
|
326 return face; |
|
327 } |
|
328 |
|
329 /** |
|
330 * Checks if the font is plain styled. |
|
331 * |
|
332 * @return true if the font is plain, false otherwise |
|
333 */ |
|
334 public boolean isPlain() { |
|
335 return (style == Font.STYLE_PLAIN); |
|
336 } |
|
337 |
|
338 /** |
|
339 * Checks if the font is bold styled. |
|
340 * |
|
341 * @return true if the font is bold, false otherwise |
|
342 */ |
|
343 public boolean isBold() { |
|
344 return ((style & Font.STYLE_BOLD) == Font.STYLE_BOLD); |
|
345 } |
|
346 |
|
347 /** |
|
348 * Checks if the font is italic styled. |
|
349 * |
|
350 * @return true if the font is italic, false otherwise |
|
351 */ |
|
352 public boolean isItalic() { |
|
353 return ((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC); |
|
354 } |
|
355 |
|
356 /** |
|
357 * Checks if the font is underlined styled. |
|
358 * |
|
359 * @return true if the font is underlined, false otherwise. |
|
360 */ |
|
361 public boolean isUnderlined() { |
|
362 return ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED); |
|
363 } |
|
364 |
|
365 /** |
|
366 * Return font's height. |
|
367 * |
|
368 * @return height |
|
369 */ |
|
370 public int getHeight() { |
|
371 if (eswtTempFontHeight == 0) { |
|
372 ESWTUIThreadRunner.safeSyncExec(new Runnable() { |
|
373 public void run() { |
|
374 FontUtils fu = eswtGetFontUtils(eswtFont); |
|
375 eswtTempFontHeight = fu.getAscent() + fu.getDescent(); |
|
376 } |
|
377 }); |
|
378 } |
|
379 return eswtTempFontHeight; |
|
380 } |
|
381 |
|
382 /** |
|
383 * The position of the baseline from the top of the font in pixels. |
|
384 * |
|
385 * @return the baseline position |
|
386 */ |
|
387 public int getBaselinePosition() { |
|
388 if (eswtTempFontAscent == 0) { |
|
389 ESWTUIThreadRunner.safeSyncExec(new Runnable() { |
|
390 public void run() { |
|
391 eswtTempFontAscent = eswtGetFontUtils(eswtFont).getAscent(); |
|
392 } |
|
393 }); |
|
394 } |
|
395 return eswtTempFontAscent; |
|
396 } |
|
397 |
|
398 /** |
|
399 * Returns the width of the given character in pixels. |
|
400 * |
|
401 * @param c the character |
|
402 * @return the width |
|
403 */ |
|
404 public int charWidth(char c) { |
|
405 return stringWidth(String.valueOf(c)); |
|
406 } |
|
407 |
|
408 /** |
|
409 * Returns the width of the given character array's region in pixels. |
|
410 * |
|
411 * @param c characters |
|
412 * @param offset starting position |
|
413 * @param length the length |
|
414 * @return the width |
|
415 */ |
|
416 public int charsWidth(char[] c, int offset, int length) { |
|
417 return stringWidth(new String(c, offset, length)); |
|
418 } |
|
419 |
|
420 /** |
|
421 * Returns string width in pixels. |
|
422 * |
|
423 * @param string the string |
|
424 * @return the width |
|
425 */ |
|
426 public int stringWidth(String string) { |
|
427 if (string == null) { |
|
428 throw new NullPointerException( |
|
429 MsgRepository.FONT_EXCEPTION_NULL_STRING); |
|
430 } |
|
431 final String finalString = string; |
|
432 eswtTempFontWidth = 0; |
|
433 ESWTUIThreadRunner.safeSyncExec(new Runnable() { |
|
434 public void run() { |
|
435 eswtGetFontUtils(eswtFont).getBoundingRect(tempFontRect, finalString); |
|
436 eswtTempFontWidth = tempFontRect[2]; |
|
437 } |
|
438 }); |
|
439 return eswtTempFontWidth; |
|
440 } |
|
441 |
|
442 /** |
|
443 * Get the static FontUtils instance setup with given font. |
|
444 */ |
|
445 private static FontUtils eswtGetFontUtils(org.eclipse.swt.graphics.Font font) { |
|
446 if (fontUtils == null) { |
|
447 fontUtils = new FontUtils(font.handle); |
|
448 } |
|
449 else { |
|
450 fontUtils.setFont(font.handle); |
|
451 } |
|
452 return fontUtils; |
|
453 } |
|
454 |
|
455 /** |
|
456 * Returns sub-string width in pixels. |
|
457 * |
|
458 * @param string the string |
|
459 * @param offset starting position |
|
460 * @param length the length |
|
461 * @return the width of the specified region |
|
462 */ |
|
463 public int substringWidth(String string, int offset, int length) { |
|
464 return stringWidth(string.substring(offset, offset + length)); |
|
465 } |
|
466 |
|
467 /** |
|
468 * Get FontData of a eSWT control. |
|
469 * |
|
470 * @param control eSWT control |
|
471 * @return FontData |
|
472 */ |
|
473 private static FontData getFontData(final int specifier) { |
|
474 ESWTUIThreadRunner.syncExec(new Runnable() { |
|
475 public void run() { |
|
476 Control control = null; |
|
477 MobileShell shell = new MobileShell(ESWTUIThreadRunner |
|
478 .getInstance().getDisplay(), SWT.NONE); |
|
479 if (specifier == FONT_INPUT_TEXT) { |
|
480 control = new Text(shell, SWT.NONE); |
|
481 } |
|
482 else { |
|
483 control = new Label(shell, SWT.NONE); |
|
484 } |
|
485 eswtTempFontData = control.getFont().getFontData()[0]; |
|
486 control.dispose(); |
|
487 shell.dispose(); |
|
488 } |
|
489 }); |
|
490 return eswtTempFontData; |
|
491 } |
|
492 |
|
493 /** |
|
494 * Get FontData of a eSWT font. |
|
495 * |
|
496 * @param font eSWT font |
|
497 * @return FontData |
|
498 */ |
|
499 private static FontData getFontData( |
|
500 final org.eclipse.swt.graphics.Font font) { |
|
501 ESWTUIThreadRunner.syncExec(new Runnable() { |
|
502 public void run() { |
|
503 eswtTempFontData = font.getFontData()[0]; |
|
504 } |
|
505 }); |
|
506 return eswtTempFontData; |
|
507 } |
|
508 |
|
509 /** |
|
510 * Construct FontData with the given specifiers. |
|
511 * |
|
512 * @return FontData |
|
513 */ |
|
514 private static FontData getFontData(final int face, final int style, |
|
515 final int size) { |
|
516 ESWTUIThreadRunner.syncExec(new Runnable() { |
|
517 public void run() { |
|
518 int underlined = 0; |
|
519 if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) { |
|
520 underlined = 1; |
|
521 } |
|
522 /* FontData constructor format: |
|
523 * version|name|height|style|underline| |
|
524 * overline|strike|stretch|pitch|qt-style|weight|strategy| |
|
525 */ |
|
526 StringBuffer sb = new StringBuffer(); |
|
527 sb.append("1|"); // version |
|
528 sb.append(mapFaceToFontName(face)); // family name |
|
529 sb.append("|"); |
|
530 sb.append(mapSizeToHeight(size)); // height |
|
531 sb.append("|"); |
|
532 sb.append(mapStyleToESWTStyle(style)); // style |
|
533 sb.append("|QT|1|");//Qt version 2 string for extra formats |
|
534 sb.append((new Integer(underlined).toString())); // underline |
|
535 sb.append(FONTDATA_CONST); |
|
536 eswtTempFontData = new FontData(sb.toString()); |
|
537 } |
|
538 }); |
|
539 return eswtTempFontData; |
|
540 } |
|
541 |
|
542 /** |
|
543 * Returns System default font's FontData instance. |
|
544 * |
|
545 * @return system FontData |
|
546 */ |
|
547 private static FontData getSystemFontData() { |
|
548 if (eswtSystemFontData == null) { |
|
549 ESWTUIThreadRunner.syncExec(new Runnable() { |
|
550 public void run() { |
|
551 eswtSystemFontData = ESWTUIThreadRunner.getInstance() |
|
552 .getDisplay().getSystemFont().getFontData()[0]; |
|
553 } |
|
554 }); |
|
555 } |
|
556 return eswtSystemFontData; |
|
557 } |
|
558 |
|
559 /** |
|
560 * Get eSWT font name from LCDUI face parameter. |
|
561 * |
|
562 * @param face font's face type |
|
563 * @return font name |
|
564 */ |
|
565 private static String mapFaceToFontName(int face) { |
|
566 // TODO: how to map face values to names |
|
567 switch (face) { |
|
568 case Font.FACE_MONOSPACE: |
|
569 case Font.FACE_PROPORTIONAL: |
|
570 case Font.FACE_SYSTEM: |
|
571 default: |
|
572 return getSystemFontData().getName(); |
|
573 } |
|
574 } |
|
575 |
|
576 /** |
|
577 * Get face of a given eSWT font name. |
|
578 * |
|
579 * @param eswtFontName font's name |
|
580 * @return font face |
|
581 */ |
|
582 private static int mapFontNameToFace(String eswtFontName) { |
|
583 // TODO: how to map names to face values |
|
584 if (eswtFontName.equals(getSystemFontData().getName())) { |
|
585 return Font.FACE_SYSTEM; |
|
586 } |
|
587 else { |
|
588 // Font.FACE_MONOSPACE: |
|
589 // Font.FACE_PROPORTIONAL: |
|
590 return 0; |
|
591 } |
|
592 } |
|
593 |
|
594 /** |
|
595 * Get eSWT font height from LCDUI size parameter.<br> |
|
596 * 0 maps to system's default font size, others map directly. |
|
597 * |
|
598 * @param size font's size |
|
599 * @return font height |
|
600 */ |
|
601 private static int mapSizeToHeight(int size) { |
|
602 //retreive the system default height |
|
603 int defHeight = getSystemFontData().getHeight(); |
|
604 |
|
605 if (size == Font.SIZE_SMALL) { |
|
606 //calculate the small size height as a ratio of system default medium size and round off the value to an integer |
|
607 return (int)Math.floor( (defHeight * ( 3f/4 )) + 0.5f ); |
|
608 } |
|
609 else if (size == Font.SIZE_LARGE) { |
|
610 //calculate the large height as a ratio of system default medium size and round off the value to an integer |
|
611 return (int)Math.floor( (defHeight * ( 4.5f/4 )) + 0.5f ); |
|
612 } |
|
613 else { |
|
614 //return the system default height for medium size which is generally 12 but 7 in symbian for qt |
|
615 return defHeight; |
|
616 } |
|
617 } |
|
618 |
|
619 /** |
|
620 * Get LCDUI size from eSWT font height |
|
621 * |
|
622 * @param height font's height |
|
623 * @return font size |
|
624 */ |
|
625 private static int mapHeightToSize(int height) { |
|
626 int defHeight = getSystemFontData().getHeight(); |
|
627 if (height < defHeight) { |
|
628 return Font.SIZE_SMALL; |
|
629 } |
|
630 else if (height > defHeight) { |
|
631 return Font.SIZE_LARGE; |
|
632 } |
|
633 else { |
|
634 return Font.SIZE_MEDIUM; |
|
635 } |
|
636 } |
|
637 |
|
638 /** |
|
639 * Return eSWT style. |
|
640 * |
|
641 * @param style Font's LCDUI style. |
|
642 * @return Font's eSWT style. |
|
643 */ |
|
644 private static int mapStyleToESWTStyle(int style) { |
|
645 int retStyle = SWT.NORMAL; |
|
646 if ((style & Font.STYLE_BOLD) == Font.STYLE_BOLD) { |
|
647 retStyle |= SWT.BOLD; |
|
648 } |
|
649 if ((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC) { |
|
650 retStyle |= SWT.ITALIC; |
|
651 } |
|
652 return retStyle; |
|
653 } |
|
654 |
|
655 /** |
|
656 * Return LCDUI style. |
|
657 * |
|
658 * @param eswtStyle Font's eSWT style. |
|
659 * @return Font's LCDUI style. |
|
660 */ |
|
661 private static int mapESWTStyleToStyle(int eswtStyle) { |
|
662 int retStyle = Font.STYLE_PLAIN; |
|
663 if ((eswtStyle & SWT.BOLD) != 0) { |
|
664 retStyle |= Font.STYLE_BOLD; |
|
665 } |
|
666 if ((eswtStyle & SWT.ITALIC) != 0) { |
|
667 retStyle |= Font.STYLE_ITALIC; |
|
668 } |
|
669 return retStyle; |
|
670 } |
|
671 |
|
672 } |