javauis/lcdui_qt/src/javax/microedition/lcdui/Font.java
changeset 23 98ccebc37403
parent 21 2a9601315dfc
child 35 85266cc22c7f
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description: 
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 package javax.microedition.lcdui;
    17 package javax.microedition.lcdui;
    18 
    18 
    19 import java.util.Enumeration;
    19 import java.util.Enumeration;
    26 import org.eclipse.swt.widgets.*;
    26 import org.eclipse.swt.widgets.*;
    27 
    27 
    28 /**
    28 /**
    29  * Implementation of LCDUI <code>Font</code> class.
    29  * Implementation of LCDUI <code>Font</code> class.
    30  */
    30  */
    31 public final class Font {
    31 public final class Font
       
    32 {
    32 
    33 
    33     /**
    34     /**
    34      * System font face.
    35      * System font face.
    35      *
    36      *
    36      * @value for FACE_SYSTEM is 0.
    37      * @value for FACE_SYSTEM is 0.
   113      * @value for FONT_STATIC_TEXT is 0.
   114      * @value for FONT_STATIC_TEXT is 0.
   114      */
   115      */
   115     public static final int FONT_STATIC_TEXT = 0;
   116     public static final int FONT_STATIC_TEXT = 0;
   116 
   117 
   117     private static final int STYLE_ALL = STYLE_PLAIN | STYLE_BOLD
   118     private static final int STYLE_ALL = STYLE_PLAIN | STYLE_BOLD
   118             | STYLE_ITALIC | STYLE_UNDERLINED;
   119                                          | STYLE_ITALIC | STYLE_UNDERLINED;
   119 
   120 
   120     private static final String FONTDATA_CONST = "|-1|-1|-1|-1|-1|-1|1|";
   121     private static final String FONTDATA_CONST = "|-1|-1|-1|-1|-1|-1|1|";
   121 
   122 
   122     /**
   123     /**
   123      * Lookup table which stores LCDUI fonts based on FontData keys.
   124      * Lookup table which stores LCDUI fonts based on FontData keys.
   139     private int size;
   140     private int size;
   140 
   141 
   141     /**
   142     /**
   142      * Disposes all fonts created.
   143      * Disposes all fonts created.
   143      */
   144      */
   144     static void disposeFonts() {
   145     static void disposeFonts()
   145         for (Enumeration e = fontTable.elements(); e.hasMoreElements();) {
   146     {
       
   147         for(Enumeration e = fontTable.elements(); e.hasMoreElements();)
       
   148         {
   146             Font font = (Font) e.nextElement();
   149             Font font = (Font) e.nextElement();
   147             font.eswtFont.dispose();
   150             font.eswtFont.dispose();
   148         }
   151         }
   149         fontTable.clear();
   152         fontTable.clear();
   150     }
   153     }
   151 
   154 
   152     /**
   155     /**
   153      * LCDUI Font --> eSWT Font
   156      * LCDUI Font --> eSWT Font
   154      */
   157      */
   155     static org.eclipse.swt.graphics.Font getESWTFont(Font font) {
   158     static org.eclipse.swt.graphics.Font getESWTFont(Font font)
   156         if (font != null) {
   159     {
       
   160         if(font != null)
       
   161         {
   157             return font.eswtFont;
   162             return font.eswtFont;
   158         }
   163         }
   159         return null;
   164         return null;
   160     }
   165     }
   161 
   166 
   162     /**
   167     /**
   163      * eSWT Font --> LCDUI Font
   168      * eSWT Font --> LCDUI Font
   164      */
   169      */
   165     static Font getFont(final org.eclipse.swt.graphics.Font font) {
   170     static Font getFont(final org.eclipse.swt.graphics.Font font)
   166         if (font != null) {
   171     {
       
   172         if(font != null)
       
   173         {
   167             FontData fd = getFontData(font);
   174             FontData fd = getFontData(font);
   168             return getFont(fd);
   175             return getFont(fd);
   169         }
   176         }
   170         return null;
   177         return null;
   171     }
   178     }
   173     /**
   180     /**
   174      * Returns default font.
   181      * Returns default font.
   175      *
   182      *
   176      * @return default system font.
   183      * @return default system font.
   177      */
   184      */
   178     public static Font getDefaultFont() {
   185     public static Font getDefaultFont()
       
   186     {
   179         return getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
   187         return getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
   180     }
   188     }
   181 
   189 
   182     /**
   190     /**
   183      * Request specified Font.
   191      * Request specified Font.
   184      *
   192      *
   185      * @param specifier font specifier
   193      * @param specifier font specifier
   186      * @return the font
   194      * @return the font
   187      */
   195      */
   188     public static Font getFont(int specifier) {
   196     public static Font getFont(int specifier)
   189         if (specifier == FONT_INPUT_TEXT || specifier == FONT_STATIC_TEXT) {
   197     {
       
   198         if(specifier == FONT_INPUT_TEXT || specifier == FONT_STATIC_TEXT)
       
   199         {
   190             return getFont(getFontData(specifier));
   200             return getFont(getFontData(specifier));
   191         }
   201         }
   192         else {
   202         else
       
   203         {
   193             throw new IllegalArgumentException(
   204             throw new IllegalArgumentException(
   194                     MsgRepository.FONT_EXCEPTION_INVALID_SPECIFIER);
   205                 MsgRepository.FONT_EXCEPTION_INVALID_SPECIFIER);
   195         }
   206         }
   196     }
   207     }
   197 
   208 
   198     /**
   209     /**
   199      * Request Font based on parameters.
   210      * Request Font based on parameters.
   201      * @param face font face
   212      * @param face font face
   202      * @param style font style
   213      * @param style font style
   203      * @param size font size
   214      * @param size font size
   204      * @return the font
   215      * @return the font
   205      */
   216      */
   206     public static Font getFont(int face, int style, int size) {
   217     public static Font getFont(int face, int style, int size)
   207         if (face != FACE_SYSTEM && face != FACE_MONOSPACE
   218     {
   208                 && face != FACE_PROPORTIONAL) {
   219         if(face != FACE_SYSTEM && face != FACE_MONOSPACE
       
   220                 && face != FACE_PROPORTIONAL)
       
   221         {
   209             throw new IllegalArgumentException(
   222             throw new IllegalArgumentException(
   210                     MsgRepository.FONT_EXCEPTION_INVALID_FACE);
   223                 MsgRepository.FONT_EXCEPTION_INVALID_FACE);
   211         }
   224         }
   212         if (style < STYLE_PLAIN || style > STYLE_ALL) {
   225         if(style < STYLE_PLAIN || style > STYLE_ALL)
       
   226         {
   213             throw new IllegalArgumentException(
   227             throw new IllegalArgumentException(
   214                     MsgRepository.FONT_EXCEPTION_INVALID_STYLE);
   228                 MsgRepository.FONT_EXCEPTION_INVALID_STYLE);
   215         }
   229         }
   216         if (size != SIZE_SMALL && size != SIZE_MEDIUM && size != SIZE_LARGE) {
   230         if(size != SIZE_SMALL && size != SIZE_MEDIUM && size != SIZE_LARGE)
       
   231         {
   217             throw new IllegalArgumentException(
   232             throw new IllegalArgumentException(
   218                     MsgRepository.FONT_EXCEPTION_INVALID_SIZE);
   233                 MsgRepository.FONT_EXCEPTION_INVALID_SIZE);
   219         }
   234         }
   220         FontData fd = getFontData(face, style, size);
   235         FontData fd = getFontData(face, style, size);
   221         Font font = getFont(fd);
   236         Font font = getFont(fd);
   222         // We must handle underlining separately because it is not part of
   237         // We must handle underlining separately because it is not part of
   223         // eSWT's style.
   238         // eSWT's style.
   224         if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) {
   239         if((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED)
       
   240         {
   225             font.style |= Font.STYLE_UNDERLINED;
   241             font.style |= Font.STYLE_UNDERLINED;
   226         }
   242         }
   227         return font;
   243         return font;
   228     }
   244     }
   229 
   245 
   233      * @param face font face
   249      * @param face font face
   234      * @param style font style
   250      * @param style font style
   235      * @param height font height
   251      * @param height font height
   236      * @return the font
   252      * @return the font
   237      */
   253      */
   238     static Font getFreeSizedFont(int face, int style, int height) {
   254     static Font getFreeSizedFont(int face, int style, int height)
   239         if (face != FACE_SYSTEM && face != FACE_MONOSPACE
   255     {
   240                 && face != FACE_PROPORTIONAL) {
   256         if(face != FACE_SYSTEM && face != FACE_MONOSPACE
       
   257                 && face != FACE_PROPORTIONAL)
       
   258         {
   241             throw new IllegalArgumentException(
   259             throw new IllegalArgumentException(
   242                     MsgRepository.FONT_EXCEPTION_INVALID_FACE);
   260                 MsgRepository.FONT_EXCEPTION_INVALID_FACE);
   243         }
   261         }
   244         if (style < STYLE_PLAIN || style > STYLE_ALL) {
   262         if(style < STYLE_PLAIN || style > STYLE_ALL)
       
   263         {
   245             throw new IllegalArgumentException(
   264             throw new IllegalArgumentException(
   246                     MsgRepository.FONT_EXCEPTION_INVALID_STYLE);
   265                 MsgRepository.FONT_EXCEPTION_INVALID_STYLE);
   247         }
   266         }
   248         if (height < 0) {
   267         if(height < 0)
       
   268         {
   249             throw new IllegalArgumentException(
   269             throw new IllegalArgumentException(
   250                     MsgRepository.FONT_EXCEPTION_INVALID_SIZE);
   270                 MsgRepository.FONT_EXCEPTION_INVALID_SIZE);
   251         }
   271         }
   252         FontData fd = getFontData(face, style, height);
   272         FontData fd = getFontData(face, style, height);
   253         Font font = getFont(fd);
   273         Font font = getFont(fd);
   254         // We must handle underlining separately because it is not part of
   274         // We must handle underlining separately because it is not part of
   255         // eSWT's style.
   275         // eSWT's style.
   256         if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) {
   276         if((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED)
       
   277         {
   257             font.style |= Font.STYLE_UNDERLINED;
   278             font.style |= Font.STYLE_UNDERLINED;
   258         }
   279         }
   259         return font;
   280         return font;
   260     }
   281     }
   261 
   282 
   263      * Lookup or create a font based on given FontData.
   284      * Lookup or create a font based on given FontData.
   264      *
   285      *
   265      * @param fontdata font's data
   286      * @param fontdata font's data
   266      * @return a font
   287      * @return a font
   267      */
   288      */
   268     private static Font getFont(FontData fontdata) {
   289     private static Font getFont(FontData fontdata)
       
   290     {
   269         Font ret = null;
   291         Font ret = null;
   270         if (fontdata != null) {
   292         if(fontdata != null)
   271             if (fontTable.containsKey(fontdata)) {
   293         {
       
   294             if(fontTable.containsKey(fontdata))
       
   295             {
   272                 ret = (Font) fontTable.get(fontdata);
   296                 ret = (Font) fontTable.get(fontdata);
   273             }
   297             }
   274             else {
   298             else
       
   299             {
   275                 ret = new Font(fontdata);
   300                 ret = new Font(fontdata);
   276             }
   301             }
   277         }
   302         }
   278         return ret;
   303         return ret;
   279     }
   304     }
   281     /**
   306     /**
   282      * Constructor (private).<br>
   307      * Constructor (private).<br>
   283      *
   308      *
   284      * @param reqFD the requested eSWT FontData
   309      * @param reqFD the requested eSWT FontData
   285      */
   310      */
   286     private Font(final FontData reqFD) {
   311     private Font(final FontData reqFD)
   287         ESWTUIThreadRunner.syncExec(new Runnable() {
   312     {
   288             public void run() {
   313         ESWTUIThreadRunner.syncExec(new Runnable()
       
   314         {
       
   315             public void run()
       
   316             {
   289                 eswtFont = new org.eclipse.swt.graphics.Font(ESWTUIThreadRunner
   317                 eswtFont = new org.eclipse.swt.graphics.Font(ESWTUIThreadRunner
   290                         .getInstance().getDisplay(), reqFD);
   318                         .getInstance().getDisplay(), reqFD);
   291             }
   319             }
   292         });
   320         });
   293         // get FontData info from resulted Font, because resulting FontData
   321         // get FontData info from resulted Font, because resulting FontData
   302     /**
   330     /**
   303      * Return font's style.
   331      * Return font's style.
   304      *
   332      *
   305      * @return style
   333      * @return style
   306      */
   334      */
   307     public int getStyle() {
   335     public int getStyle()
       
   336     {
   308         return style;
   337         return style;
   309     }
   338     }
   310 
   339 
   311     /**
   340     /**
   312      * Return font's size.
   341      * Return font's size.
   313      *
   342      *
   314      * @return size
   343      * @return size
   315      */
   344      */
   316     public int getSize() {
   345     public int getSize()
       
   346     {
   317         return size;
   347         return size;
   318     }
   348     }
   319 
   349 
   320     /**
   350     /**
   321      * Return font's face.
   351      * Return font's face.
   322      *
   352      *
   323      * @return face
   353      * @return face
   324      */
   354      */
   325     public int getFace() {
   355     public int getFace()
       
   356     {
   326         return face;
   357         return face;
   327     }
   358     }
   328 
   359 
   329     /**
   360     /**
   330      * Checks if the font is plain styled.
   361      * Checks if the font is plain styled.
   331      *
   362      *
   332      * @return true if the font is plain, false otherwise
   363      * @return true if the font is plain, false otherwise
   333      */
   364      */
   334     public boolean isPlain() {
   365     public boolean isPlain()
       
   366     {
   335         return (style == Font.STYLE_PLAIN);
   367         return (style == Font.STYLE_PLAIN);
   336     }
   368     }
   337 
   369 
   338     /**
   370     /**
   339      * Checks if the font is bold styled.
   371      * Checks if the font is bold styled.
   340      *
   372      *
   341      * @return true if the font is bold, false otherwise
   373      * @return true if the font is bold, false otherwise
   342      */
   374      */
   343     public boolean isBold() {
   375     public boolean isBold()
       
   376     {
   344         return ((style & Font.STYLE_BOLD) == Font.STYLE_BOLD);
   377         return ((style & Font.STYLE_BOLD) == Font.STYLE_BOLD);
   345     }
   378     }
   346 
   379 
   347     /**
   380     /**
   348      * Checks if the font is italic styled.
   381      * Checks if the font is italic styled.
   349      *
   382      *
   350      * @return true if the font is italic, false otherwise
   383      * @return true if the font is italic, false otherwise
   351      */
   384      */
   352     public boolean isItalic() {
   385     public boolean isItalic()
       
   386     {
   353         return ((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC);
   387         return ((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC);
   354     }
   388     }
   355 
   389 
   356     /**
   390     /**
   357      * Checks if the font is underlined styled.
   391      * Checks if the font is underlined styled.
   358      *
   392      *
   359      * @return true if the font is underlined, false otherwise.
   393      * @return true if the font is underlined, false otherwise.
   360      */
   394      */
   361     public boolean isUnderlined() {
   395     public boolean isUnderlined()
       
   396     {
   362         return ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED);
   397         return ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED);
   363     }
   398     }
   364 
   399 
   365     /**
   400     /**
   366      * Return font's height.
   401      * Return font's height.
   367      *
   402      *
   368      * @return height
   403      * @return height
   369      */
   404      */
   370     public int getHeight() {
   405     public int getHeight()
   371         if (eswtTempFontHeight == 0) {
   406     {
   372             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   407         if(eswtTempFontHeight == 0)
   373                 public void run() {
   408         {
       
   409             ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   410             {
       
   411                 public void run()
       
   412                 {
   374                     FontUtils fu = eswtGetFontUtils(eswtFont);
   413                     FontUtils fu = eswtGetFontUtils(eswtFont);
   375                     eswtTempFontHeight = fu.getAscent() + fu.getDescent();
   414                     eswtTempFontHeight = fu.getAscent() + fu.getDescent();
   376                 }
   415                 }
   377             });
   416             });
   378         }
   417         }
   382     /**
   421     /**
   383      * The position of the baseline from the top of the font in pixels.
   422      * The position of the baseline from the top of the font in pixels.
   384      *
   423      *
   385      * @return the baseline position
   424      * @return the baseline position
   386      */
   425      */
   387     public int getBaselinePosition() {
   426     public int getBaselinePosition()
   388         if (eswtTempFontAscent == 0) {
   427     {
   389             ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   428         if(eswtTempFontAscent == 0)
   390                 public void run() {
   429         {
       
   430             ESWTUIThreadRunner.safeSyncExec(new Runnable()
       
   431             {
       
   432                 public void run()
       
   433                 {
   391                     eswtTempFontAscent = eswtGetFontUtils(eswtFont).getAscent();
   434                     eswtTempFontAscent = eswtGetFontUtils(eswtFont).getAscent();
   392                 }
   435                 }
   393             });
   436             });
   394         }
   437         }
   395         return eswtTempFontAscent;
   438         return eswtTempFontAscent;
   399      * Returns the width of the given character in pixels.
   442      * Returns the width of the given character in pixels.
   400      *
   443      *
   401      * @param c the character
   444      * @param c the character
   402      * @return the width
   445      * @return the width
   403      */
   446      */
   404     public int charWidth(char c) {
   447     public int charWidth(char c)
       
   448     {
   405         return stringWidth(String.valueOf(c));
   449         return stringWidth(String.valueOf(c));
   406     }
   450     }
   407 
   451 
   408     /**
   452     /**
   409      * Returns the width of the given character array's region in pixels.
   453      * Returns the width of the given character array's region in pixels.
   411      * @param c characters
   455      * @param c characters
   412      * @param offset starting position
   456      * @param offset starting position
   413      * @param length the length
   457      * @param length the length
   414      * @return the width
   458      * @return the width
   415      */
   459      */
   416     public int charsWidth(char[] c, int offset, int length) {
   460     public int charsWidth(char[] c, int offset, int length)
       
   461     {
   417         return stringWidth(new String(c, offset, length));
   462         return stringWidth(new String(c, offset, length));
   418     }
   463     }
   419 
   464 
   420     /**
   465     /**
   421      * Returns string width in pixels.
   466      * Returns string width in pixels.
   422      *
   467      *
   423      * @param string the string
   468      * @param string the string
   424      * @return the width
   469      * @return the width
   425      */
   470      */
   426     public int stringWidth(String string) {
   471     public int stringWidth(String string)
   427         if (string == null) {
   472     {
       
   473         if(string == null)
       
   474         {
   428             throw new NullPointerException(
   475             throw new NullPointerException(
   429                     MsgRepository.FONT_EXCEPTION_NULL_STRING);
   476                 MsgRepository.FONT_EXCEPTION_NULL_STRING);
   430         }
   477         }
   431         final String finalString = string;
   478         final String finalString = string;
   432         eswtTempFontWidth = 0;
   479         eswtTempFontWidth = 0;
   433         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
   480         ESWTUIThreadRunner.safeSyncExec(new Runnable()
   434             public void run() {
   481         {
       
   482             public void run()
       
   483             {
   435                 eswtGetFontUtils(eswtFont).getBoundingRect(tempFontRect, finalString);
   484                 eswtGetFontUtils(eswtFont).getBoundingRect(tempFontRect, finalString);
   436                 eswtTempFontWidth = tempFontRect[2];
   485                 eswtTempFontWidth = tempFontRect[2];
   437             }
   486             }
   438         });
   487         });
   439         return eswtTempFontWidth;
   488         return eswtTempFontWidth;
   440     }
   489     }
   441 
   490 
   442     /**
   491     /**
   443      * Get the static FontUtils instance setup with given font.
   492      * Get the static FontUtils instance setup with given font.
   444      */
   493      */
   445     private static FontUtils eswtGetFontUtils(org.eclipse.swt.graphics.Font font) {
   494     private static FontUtils eswtGetFontUtils(org.eclipse.swt.graphics.Font font)
   446         if (fontUtils == null) {
   495     {
       
   496         if(fontUtils == null)
       
   497         {
   447             fontUtils = new FontUtils(font.handle);
   498             fontUtils = new FontUtils(font.handle);
   448         }
   499         }
   449         else {
   500         else
       
   501         {
   450             fontUtils.setFont(font.handle);
   502             fontUtils.setFont(font.handle);
   451         }
   503         }
   452         return fontUtils;
   504         return fontUtils;
   453     }
   505     }
   454 
   506 
   458      * @param string the string
   510      * @param string the string
   459      * @param offset starting position
   511      * @param offset starting position
   460      * @param length the length
   512      * @param length the length
   461      * @return the width of the specified region
   513      * @return the width of the specified region
   462      */
   514      */
   463     public int substringWidth(String string, int offset, int length) {
   515     public int substringWidth(String string, int offset, int length)
       
   516     {
   464         return stringWidth(string.substring(offset, offset + length));
   517         return stringWidth(string.substring(offset, offset + length));
   465     }
   518     }
   466 
   519 
   467     /**
   520     /**
   468      * Get FontData of a eSWT control.
   521      * Get FontData of a eSWT control.
   469      *
   522      *
   470      * @param control eSWT control
   523      * @param control eSWT control
   471      * @return FontData
   524      * @return FontData
   472      */
   525      */
   473     private static FontData getFontData(final int specifier) {
   526     private static FontData getFontData(final int specifier)
   474         ESWTUIThreadRunner.syncExec(new Runnable() {
   527     {
   475             public void run() {
   528         ESWTUIThreadRunner.syncExec(new Runnable()
       
   529         {
       
   530             public void run()
       
   531             {
   476                 Control control = null;
   532                 Control control = null;
   477                 MobileShell shell = new MobileShell(ESWTUIThreadRunner
   533                 MobileShell shell = new MobileShell(ESWTUIThreadRunner
   478                         .getInstance().getDisplay(), SWT.NONE);
   534                                                     .getInstance().getDisplay(), SWT.NONE);
   479                 if (specifier == FONT_INPUT_TEXT) {
   535                 if(specifier == FONT_INPUT_TEXT)
       
   536                 {
   480                     control = new Text(shell, SWT.NONE);
   537                     control = new Text(shell, SWT.NONE);
   481                 }
   538                 }
   482                 else {
   539                 else
       
   540                 {
   483                     control = new Label(shell, SWT.NONE);
   541                     control = new Label(shell, SWT.NONE);
   484                 }
   542                 }
   485                 eswtTempFontData = control.getFont().getFontData()[0];
   543                 eswtTempFontData = control.getFont().getFontData()[0];
   486                 control.dispose();
   544                 control.dispose();
   487                 shell.dispose();
   545                 shell.dispose();
   495      *
   553      *
   496      * @param font eSWT font
   554      * @param font eSWT font
   497      * @return FontData
   555      * @return FontData
   498      */
   556      */
   499     private static FontData getFontData(
   557     private static FontData getFontData(
   500             final org.eclipse.swt.graphics.Font font) {
   558         final org.eclipse.swt.graphics.Font font)
   501         ESWTUIThreadRunner.syncExec(new Runnable() {
   559     {
   502             public void run() {
   560         ESWTUIThreadRunner.syncExec(new Runnable()
       
   561         {
       
   562             public void run()
       
   563             {
   503                 eswtTempFontData = font.getFontData()[0];
   564                 eswtTempFontData = font.getFontData()[0];
   504             }
   565             }
   505         });
   566         });
   506         return eswtTempFontData;
   567         return eswtTempFontData;
   507     }
   568     }
   510      * Construct FontData with the given specifiers.
   571      * Construct FontData with the given specifiers.
   511      *
   572      *
   512      * @return FontData
   573      * @return FontData
   513      */
   574      */
   514     private static FontData getFontData(final int face, final int style,
   575     private static FontData getFontData(final int face, final int style,
   515             final int size) {
   576                                         final int size)
   516         ESWTUIThreadRunner.syncExec(new Runnable() {
   577     {
   517             public void run() {
   578         ESWTUIThreadRunner.syncExec(new Runnable()
       
   579         {
       
   580             public void run()
       
   581             {
   518                 int underlined = 0;
   582                 int underlined = 0;
   519                 if ((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED) {
   583                 if((style & Font.STYLE_UNDERLINED) == Font.STYLE_UNDERLINED)
       
   584                 {
   520                     underlined = 1;
   585                     underlined = 1;
   521                 }
   586                 }
   522                 /* FontData constructor format:
   587                 /* FontData constructor format:
   523                  *  version|name|height|style|underline|
   588                  *  version|name|height|style|underline|
   524                  *  overline|strike|stretch|pitch|qt-style|weight|strategy|
   589                  *  overline|strike|stretch|pitch|qt-style|weight|strategy|
   542     /**
   607     /**
   543      * Returns System default font's FontData instance.
   608      * Returns System default font's FontData instance.
   544      *
   609      *
   545      * @return system FontData
   610      * @return system FontData
   546      */
   611      */
   547     private static FontData getSystemFontData() {
   612     private static FontData getSystemFontData()
   548         if (eswtSystemFontData == null) {
   613     {
   549             ESWTUIThreadRunner.syncExec(new Runnable() {
   614         if(eswtSystemFontData == null)
   550                 public void run() {
   615         {
       
   616             ESWTUIThreadRunner.syncExec(new Runnable()
       
   617             {
       
   618                 public void run()
       
   619                 {
   551                     eswtSystemFontData = ESWTUIThreadRunner.getInstance()
   620                     eswtSystemFontData = ESWTUIThreadRunner.getInstance()
   552                             .getDisplay().getSystemFont().getFontData()[0];
   621                                          .getDisplay().getSystemFont().getFontData()[0];
   553                 }
   622                 }
   554             });
   623             });
   555         }
   624         }
   556         return eswtSystemFontData;
   625         return eswtSystemFontData;
   557     }
   626     }
   560      * Get eSWT font name from LCDUI face parameter.
   629      * Get eSWT font name from LCDUI face parameter.
   561      *
   630      *
   562      * @param face font's face type
   631      * @param face font's face type
   563      * @return font name
   632      * @return font name
   564      */
   633      */
   565     private static String mapFaceToFontName(int face) {
   634     private static String mapFaceToFontName(int face)
       
   635     {
   566         // TODO: how to map face values to names
   636         // TODO: how to map face values to names
   567         switch (face) {
   637         switch(face)
   568             case Font.FACE_MONOSPACE:
   638         {
   569             case Font.FACE_PROPORTIONAL:
   639         case Font.FACE_MONOSPACE:
   570             case Font.FACE_SYSTEM:
   640         case Font.FACE_PROPORTIONAL:
   571             default:
   641         case Font.FACE_SYSTEM:
   572                 return getSystemFontData().getName();
   642         default:
       
   643             return getSystemFontData().getName();
   573         }
   644         }
   574     }
   645     }
   575 
   646 
   576     /**
   647     /**
   577      * Get face of a given eSWT font name.
   648      * Get face of a given eSWT font name.
   578      *
   649      *
   579      * @param eswtFontName font's name
   650      * @param eswtFontName font's name
   580      * @return font face
   651      * @return font face
   581      */
   652      */
   582     private static int mapFontNameToFace(String eswtFontName) {
   653     private static int mapFontNameToFace(String eswtFontName)
       
   654     {
   583         // TODO: how to map names to face values
   655         // TODO: how to map names to face values
   584         if (eswtFontName.equals(getSystemFontData().getName())) {
   656         if(eswtFontName.equals(getSystemFontData().getName()))
       
   657         {
   585             return Font.FACE_SYSTEM;
   658             return Font.FACE_SYSTEM;
   586         }
   659         }
   587         else {
   660         else
       
   661         {
   588             // Font.FACE_MONOSPACE:
   662             // Font.FACE_MONOSPACE:
   589             // Font.FACE_PROPORTIONAL:
   663             // Font.FACE_PROPORTIONAL:
   590             return 0;
   664             return 0;
   591         }
   665         }
   592     }
   666     }
   596      * 0 maps to system's default font size, others map directly.
   670      * 0 maps to system's default font size, others map directly.
   597      *
   671      *
   598      * @param size font's size
   672      * @param size font's size
   599      * @return font height
   673      * @return font height
   600      */
   674      */
   601     private static int mapSizeToHeight(int size) {
   675     private static int mapSizeToHeight(int size)
       
   676     {
   602         //retreive the system default height
   677         //retreive the system default height
   603         int defHeight = getSystemFontData().getHeight();
   678         int defHeight = getSystemFontData().getHeight();
   604         
   679 
   605         if (size == Font.SIZE_SMALL) {
   680         if(size == Font.SIZE_SMALL)
       
   681         {
   606             //calculate the small size height as a ratio of system default medium size and round off the value to an integer
   682             //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 );
   683             return (int)Math.floor((defHeight * (3f/4)) + 0.5f);
   608         }
   684         }
   609         else if (size == Font.SIZE_LARGE) {
   685         else if(size == Font.SIZE_LARGE)
       
   686         {
   610             //calculate the large height as a ratio of system default medium size and round off the value to an integer
   687             //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 );
   688             return (int)Math.floor((defHeight * (4.5f/4)) + 0.5f);
   612         }
   689         }
   613         else {
   690         else
       
   691         {
   614             //return the system default height for medium size which is generally 12 but 7 in symbian for qt
   692             //return the system default height for medium size which is generally 12 but 7 in symbian for qt
   615             return defHeight;
   693             return defHeight;
   616         }
   694         }
   617     }
   695     }
   618 
   696 
   620      * Get LCDUI size from eSWT font height
   698      * Get LCDUI size from eSWT font height
   621      *
   699      *
   622      * @param height font's height
   700      * @param height font's height
   623      * @return font size
   701      * @return font size
   624      */
   702      */
   625     private static int mapHeightToSize(int height) {
   703     private static int mapHeightToSize(int height)
       
   704     {
   626         int defHeight = getSystemFontData().getHeight();
   705         int defHeight = getSystemFontData().getHeight();
   627         if (height < defHeight) {
   706         if(height < defHeight)
       
   707         {
   628             return Font.SIZE_SMALL;
   708             return Font.SIZE_SMALL;
   629         }
   709         }
   630         else if (height > defHeight) {
   710         else if(height > defHeight)
       
   711         {
   631             return Font.SIZE_LARGE;
   712             return Font.SIZE_LARGE;
   632         }
   713         }
   633         else {
   714         else
       
   715         {
   634             return Font.SIZE_MEDIUM;
   716             return Font.SIZE_MEDIUM;
   635         }
   717         }
   636     }
   718     }
   637 
   719 
   638     /**
   720     /**
   639      * Return eSWT style.
   721      * Return eSWT style.
   640      *
   722      *
   641      * @param style Font's LCDUI style.
   723      * @param style Font's LCDUI style.
   642      * @return Font's eSWT style.
   724      * @return Font's eSWT style.
   643      */
   725      */
   644     private static int mapStyleToESWTStyle(int style) {
   726     private static int mapStyleToESWTStyle(int style)
       
   727     {
   645         int retStyle = SWT.NORMAL;
   728         int retStyle = SWT.NORMAL;
   646         if ((style & Font.STYLE_BOLD) == Font.STYLE_BOLD) {
   729         if((style & Font.STYLE_BOLD) == Font.STYLE_BOLD)
       
   730         {
   647             retStyle |= SWT.BOLD;
   731             retStyle |= SWT.BOLD;
   648         }
   732         }
   649         if ((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC) {
   733         if((style & Font.STYLE_ITALIC) == Font.STYLE_ITALIC)
       
   734         {
   650             retStyle |= SWT.ITALIC;
   735             retStyle |= SWT.ITALIC;
   651         }
   736         }
   652         return retStyle;
   737         return retStyle;
   653     }
   738     }
   654 
   739 
   656      * Return LCDUI style.
   741      * Return LCDUI style.
   657      *
   742      *
   658      * @param eswtStyle Font's eSWT style.
   743      * @param eswtStyle Font's eSWT style.
   659      * @return Font's LCDUI style.
   744      * @return Font's LCDUI style.
   660      */
   745      */
   661     private static int mapESWTStyleToStyle(int eswtStyle) {
   746     private static int mapESWTStyleToStyle(int eswtStyle)
       
   747     {
   662         int retStyle = Font.STYLE_PLAIN;
   748         int retStyle = Font.STYLE_PLAIN;
   663         if ((eswtStyle & SWT.BOLD) != 0) {
   749         if((eswtStyle & SWT.BOLD) != 0)
       
   750         {
   664             retStyle |= Font.STYLE_BOLD;
   751             retStyle |= Font.STYLE_BOLD;
   665         }
   752         }
   666         if ((eswtStyle & SWT.ITALIC) != 0) {
   753         if((eswtStyle & SWT.ITALIC) != 0)
       
   754         {
   667             retStyle |= Font.STYLE_ITALIC;
   755             retStyle |= Font.STYLE_ITALIC;
   668         }
   756         }
   669         return retStyle;
   757         return retStyle;
   670     }
   758     }
   671 
   759