public class Font extends CN
Codename One currently supports 3 font types:
createSystemFont(int, int, int)
.createTrueTypeFont(java.lang.String, java.lang.String)
.createTrueTypeFont(java.lang.String, java.lang.String)
.
WARNING: If you use a TTF file MAKE SURE not to delete the file when there MIGHT
be a reference to it. This can cause hard to track down issues!
IMPORTANT: due to copyright restrictions we cannot distribute Helvetica and thus can't simulate it.
In the simulator you will see Roboto as the fallback in some cases and not the device font unless you
are running on a Mac. Notice that the Roboto font from Google doesn't support all languages and thus
special characters might not work on the simulator but would work on the device.
The sample code below demonstrates a catalog of available fonts, the scr
The Font class also supports bitmap fonts but this support is strictly aimed at legacy applications. We no longer maintain that functionality.
BASELINE, BOTTOM, CENTER, CENTER_BEHAVIOR_CENTER, CENTER_BEHAVIOR_CENTER_ABSOLUTE, CENTER_BEHAVIOR_SCALE, CENTER_BEHAVIOR_TOTAL_BELOW, EAST, FACE_MONOSPACE, FACE_PROPORTIONAL, FACE_SYSTEM, LEFT, NATIVE_ITALIC_BLACK, NATIVE_ITALIC_BOLD, NATIVE_ITALIC_LIGHT, NATIVE_ITALIC_REGULAR, NATIVE_ITALIC_THIN, NATIVE_MAIN_BLACK, NATIVE_MAIN_BOLD, NATIVE_MAIN_LIGHT, NATIVE_MAIN_REGULAR, NATIVE_MAIN_THIN, NORTH, RIGHT, SIZE_LARGE, SIZE_MEDIUM, SIZE_SMALL, SOUTH, STYLE_BOLD, STYLE_ITALIC, STYLE_PLAIN, STYLE_UNDERLINED, TOP, WEST
DENSITY_2HD, DENSITY_4K, DENSITY_560, DENSITY_HD, DENSITY_HIGH, DENSITY_LOW, DENSITY_MEDIUM, DENSITY_VERY_HIGH, DENSITY_VERY_LOW, GALLERY_ALL, GALLERY_ALL_MULTI, GALLERY_IMAGE, GALLERY_IMAGE_MULTI, GALLERY_VIDEO, GALLERY_VIDEO_MULTI, PICKER_TYPE_CALENDAR, PICKER_TYPE_DATE, PICKER_TYPE_DATE_AND_TIME, PICKER_TYPE_DURATION, PICKER_TYPE_DURATION_HOURS, PICKER_TYPE_DURATION_MINUTES, PICKER_TYPE_STRINGS, PICKER_TYPE_TIME, SMS_BOTH, SMS_INTERACTIVE, SMS_NOT_SUPPORTED, SMS_SEAMLESS
Modifier and Type | Method and Description |
---|---|
void |
addContrast(byte value)
Deprecated.
bitmap font functionality is now deprecated
|
int |
charsWidth(char[] ch,
int offset,
int length)
Return the width of the given characters in this font instance
|
int |
charWidth(char ch)
Return the width of the specific character when rendered alone
|
static void |
clearBitmapCache()
Deprecated.
bitmap font functionality is now deprecated
|
static Font |
create(String lookup)
Creates a new font instance based on the platform specific string name of the
font.
|
static Font |
createBitmapFont(Image bitmap,
int[] cutOffsets,
int[] charWidth,
String charsets)
Deprecated.
bitmap font functionality is now deprecated
|
static Font |
createBitmapFont(String name,
Image bitmap,
int[] cutOffsets,
int[] charWidth,
String charsets)
Deprecated.
bitmap font functionality is now deprecated
|
static Font |
createSystemFont(int face,
int style,
int size)
Creates a system native font in a similar way to common MIDP fonts
|
static Font |
createTrueTypeFont(String fontName)
Shorthand for
createTrueTypeFont(name, name) which is useful
for cases such as native: fonts. |
static Font |
createTrueTypeFont(String fontName,
float sizeMm)
Shorthand for
createTrueTypeFont(name, name) &
derive(size) which is useful for cases such as native: fonts. |
static Font |
createTrueTypeFont(String fontName,
float size,
byte sizeUnit)
Shorthand for
createTrueTypeFont(name, name) &
derive(size) which is useful for cases such as native: fonts. |
static Font |
createTrueTypeFont(String fontName,
String fileName)
Creates a true type font with the given name/filename (font name might be different from the file name
and is required by some devices e.g.
|
Font |
derive(float sizePixels,
int weight)
Creates a font based on this truetype font with the given pixel, WARNING! This method
will only work in the case of truetype fonts!
Important some platforms e.g. |
Font |
derive(float size,
int weight,
byte unitType)
Creates a font based on this truetype font with the given pixel, WARNING! This method
will only work in the case of truetype fonts!
Important some platforms e.g. |
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
int |
getAscent()
The ascent is the amount by which the character ascends above the baseline.
|
static Font |
getBitmapFont(String fontName)
Deprecated.
bitmap font functionality is now deprecated
|
String |
getCharset()
Returns a string containing all the characters supported by this font.
|
static Font |
getDefaultFont()
Return the global default font instance
|
int |
getDescent()
The descent is the amount by which the character descends below the baseline
|
int |
getFace()
Return Optional operation returning the font face for system fonts
|
int |
getHeight()
Return the total height of the font
|
Object |
getNativeFont()
Returns the internal implementation specific font object
|
float |
getPixelSize()
Returns the size with which the font object was created in case of truetype fonts/derived fonts.
|
int |
getSize()
Return Optional operation returning the font size for system fonts
|
int |
getStyle()
Return Optional operation returning the font style for system fonts
|
static boolean |
isBitmapFontEnabled()
Indicates whether bitmap fonts should be enabled when loading or
the fallback system font should be used instead.
|
static boolean |
isCreationByStringSupported()
Returns true if the underlying platform allows creating a font based on a
user submitted string.
|
static boolean |
isNativeFontSchemeSupported()
Indicates whether the implementation supports loading a font "natively" to handle one of the common
native prefixes
|
static boolean |
isTrueTypeFileSupported()
Returns true if the underlying platform supports loading truetype fonts from
a file.
|
boolean |
isTTFNativeFont()
Indicates if this is a TTF native font that can be derived and manipulated.
|
static void |
setBitmapFontEnabled(boolean enabled)
Indicates whether bitmap fonts should be enabled by default when loading or
the fallback system font should be used instead.
|
static void |
setDefaultFont(Font f)
Sets the global default font instance
|
int |
stringWidth(String str)
Return the width of the given string in this font instance
|
int |
substringWidth(String str,
int offset,
int len)
Return the width of the given string subset in this font instance
|
addDefaultHeader, addEdtErrorHandler, addMessageListener, addNetworkErrorListener, addNetworkProgressListener, addToQueue, addToQueueAndWait, callSerially, callSeriallyAndWait, callSeriallyAndWait, callSeriallyOnIdle, canDial, canExecute, canForceOrientation, canInstallOnHomescreen, captureScreen, clearStorage, clearStorageCache, convertToPixels, convertToPixels, convertToPixels, convertToPixels, createSoftWeakRef, createStorageInputStream, createStorageOutputStream, createThread, delete, deleteStorageFile, deregisterPush, dial, execute, existsInFileSystem, existsInStorage, exitApplication, exitFullScreen, extractHardRef, flushStorageCache, getAppHomePath, getCachesDir, getCurrentForm, getDeviceDensity, getDisplayHeight, getDisplayWidth, getDragStartPercentage, getFileLastModifiedFile, getFileLength, getFileSystemRootAvailableSpace, getFileSystemRoots, getFileSystemRootSizeBytes, getFileSystemRootType, getPlatformName, getPluginSupport, getProperty, getResourceAsStream, getSharedJavascriptContext, getSMSSupport, hasCachesDir, hasCamera, invokeAndBlock, invokeWithoutBlocking, invokeWithoutBlockingWithResultSync, isDarkMode, isDesktop, isDirectory, isEdt, isEnableAsyncStackTraces, isFullScreenSupported, isHiddenFile, isInFullScreenMode, isMinimized, isNativePickerTypeSupported, isNativeShareSupported, isPortrait, isScreenSaverDisableSupported, isSimulator, isTablet, killAndWait, listFiles, listStorageEntries, lockOrientation, log, log, minimizeApplication, mkdir, onCanInstallOnHomescreen, openFileInputStream, openFileOutputStream, openFileOutputStream, openGallery, postMessage, promptInstallOnHomescreen, readObjectFromStorage, registerPush, removeEdtErrorHandler, removeMessageListener, removeNetworkErrorListener, removeNetworkProgressListener, renameFile, requestFullScreen, restoreMinimizedApplication, restoreToBookmark, scheduleBackgroundTask, sendLog, sendMessage, sendSMS, sendSMS, setBookmark, setDarkMode, setDragStartPercentage, setEnableAsyncStackTraces, setHiddenFile, setInterval, setProperty, setScreenSaverEnabled, setTimeout, share, share, showNativePicker, startThread, storageEntrySize, unlockOrientation, updateNetworkThreadCount, vibrate, writeObjectToStorage
public static Font getBitmapFont(String fontName)
fontName
- the font name is the logical name of the fontclearBitmapCache()
public static void clearBitmapCache()
public static boolean isTrueTypeFileSupported()
public static boolean isCreationByStringSupported()
public static boolean isNativeFontSchemeSupported()
public static Font createTrueTypeFont(String fontName)
createTrueTypeFont(name, name)
which is useful
for cases such as native: fonts. If a TTF file is passed this method will throw an exception!fontName
- the native font name. Notice that TTF file names are prohibitedpublic static Font createTrueTypeFont(String fontName, float sizeMm)
createTrueTypeFont(name, name)
&
derive(size)
which is useful for cases such as native: fonts.fontName
- the native font namesizeMm
- the size in mmpublic static Font createTrueTypeFont(String fontName, float size, byte sizeUnit)
createTrueTypeFont(name, name)
&
derive(size)
which is useful for cases such as native: fonts.fontName
- the native font namesize
- the size in the specified unit.sizeUnit
- The unit type of the size. One of Style.UNIT_TYPE_DIPS
,
* Style.UNIT_TYPE_PIXELS
,
* Style.UNIT_TYPE_REM
,
* Style.UNIT_TYPE_VW
,
* Style.UNIT_TYPE_VH
,
* Style.UNIT_TYPE_VMIN
,
* Style.UNIT_TYPE_VMAX
.public static Font createTrueTypeFont(String fontName, String fileName)
fontName
- the name of the fontfileName
- the file name of the font as it appears in the src directory of the project, it MUST end with the .ttf extension!public Font derive(float size, int weight, byte unitType)
size
- the size of the font in the specified unit type.weight
- PLAIN, BOLD or ITALIC weight based on the constants in this classunitType
- The unit type of the size. One of Style.UNIT_TYPE_DIPS
,
Style.UNIT_TYPE_PIXELS
,
Style.UNIT_TYPE_REM
,
Style.UNIT_TYPE_VW
,
Style.UNIT_TYPE_VH
,
Style.UNIT_TYPE_VMIN
,
Style.UNIT_TYPE_VMAX
.public Font derive(float sizePixels, int weight)
sizePixels
- the size of the font in pixelsweight
- PLAIN, BOLD or ITALIC weight based on the constants in this classpublic boolean isTTFNativeFont()
public static Font create(String lookup)
lookup
- a set of platform specific names delimited by commas, the first succefully
loaded font will be usedpublic void addContrast(byte value)
Notice that this method only works in one way, contrast cannot be decreased properly in a font and it should be cleared and reloaed with a Look and Feel switch.
value
- the value to increasepublic static Font createBitmapFont(String name, Image bitmap, int[] cutOffsets, int[] charWidth, String charsets)
name
- the name for the font in the cachebitmap
- a transparency map in red and black that indicates the characterscutOffsets
- character offsets matching the bitmap pixels and characters in the fontcharWidth
- The width of the character when drawing... this should not be confused with
the number of cutOffset[o + 1] - cutOffset[o]. They are completely different
since a character can be "wider" and "seep" into the next region. This is
especially true with italic characters all of which "lean" outside of their
bounds.charsets
- the set of characters in the fontpublic static Font createBitmapFont(Image bitmap, int[] cutOffsets, int[] charWidth, String charsets)
bitmap
- a transparency map in red and black that indicates the characterscutOffsets
- character offsets matching the bitmap pixels and characters in the fontcharWidth
- The width of the character when drawing... this should not be confused with
the number of cutOffset[o + 1] - cutOffset[o]. They are completely different
since a character can be "wider" and "seep" into the next region. This is
especially true with italic characters all of which "lean" outside of their
bounds.charsets
- the set of characters in the fontpublic static Font createSystemFont(int face, int style, int size)
face
- One of FACE_SYSTEM, FACE_PROPORTIONAL, FACE_MONOSPACEstyle
- one of STYLE_PLAIN, STYLE_ITALIC, STYLE_BOLDsize
- One of SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGEpublic int charsWidth(char[] ch, int offset, int length)
ch
- array of charactersoffset
- characters offsetslength
- characters lengthpublic int substringWidth(String str, int offset, int len)
str
- the given stringoffset
- the string offsetlen
- the len od stringpublic int stringWidth(String str)
str
- the given string *public int charWidth(char ch)
ch
- the specific characterpublic int getHeight()
public static Font getDefaultFont()
public static void setDefaultFont(Font f)
f
- the global default font instancepublic int getFace()
public int getSize()
public int getStyle()
public String getCharset()
public static void setBitmapFontEnabled(boolean enabled)
enabled
- true to enable bitmap font loading (if they exist in the resource)public static boolean isBitmapFontEnabled()
public Object getNativeFont()
public boolean equals(Object o)
public int getAscent()
public int getDescent()
public float getPixelSize()