use of com.codename1.ui.Font in project CodenameOne by codenameone.
the class Paint method breakText.
public int breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth) {
char[] chars = text.toCharArray();
Font f = getTypeface();
float tmp = 0;
if (f != null) {
int start = measureForwards ? 0 : chars.length - 1;
int inc = measureForwards ? 1 : -1;
float currWidth = 0f;
int clen = chars.length;
int wlen = measuredWidth != null ? measuredWidth.length : -1;
for (int i = start; (measureForwards && i < clen) || (!measureForwards && i >= 0); i += inc) {
tmp = f.charWidth(chars[i]);
if (currWidth + tmp > maxWidth) {
return i;
}
if (i < wlen) {
measuredWidth[i] = tmp;
}
currWidth += tmp;
}
} else {
throw new RuntimeException("Failed to get font");
}
return chars.length;
}
use of com.codename1.ui.Font in project CodenameOne by codenameone.
the class Paint method getCN1TextBounds.
void getCN1TextBounds(String string, int start, int count, Rectangle2D rect) {
Font f = getTypeface();
if (f != null) {
int w = f.substringWidth(string, start, count);
int h = f.getHeight();
rect.setBounds(0, 0, w, h);
}
}
use of com.codename1.ui.Font in project CodenameOne by codenameone.
the class Paint method measureText.
public float measureText(char[] chars, int start, int count) {
float out = 0f;
Font f = getTypeface();
if (f != null) {
int clen = chars.length;
for (int i = start; i < clen && i < start + count; i++) {
out += f.charWidth(chars[i]);
}
} else {
throw new RuntimeException("Failed to get font");
}
return out;
}
use of com.codename1.ui.Font in project CodenameOne by codenameone.
the class Paint method measureTextHeight.
float measureTextHeight(char[] chars, int start, int count) {
Font f = getTypeface();
float h = 0f;
if (f != null) {
int clen = chars.length;
for (int i = start; i < clen && i < start + count; i++) {
float nh = f.getHeight();
h = nh > h ? nh : h;
}
} else {
throw new RuntimeException("Failed to get font");
}
return h;
}
use of com.codename1.ui.Font in project CodenameOne by codenameone.
the class ComponentSelector method setFontSizePercent.
/**
* Sets the fonts size of all components in the found set as a percentage of the font
* size of the components' respective parents.
* @param sizePercentage Font size as a percentage of parent font size.
* @return
*/
public ComponentSelector setFontSizePercent(double sizePercentage) {
for (Component c : this) {
Component parent = c.getParent();
if (parent != null) {
float size = (float) (getEffectiveFontSize(parent) * sizePercentage / 100.0);
Style style = getStyle(c);
Font curr = style.getFont();
if (curr == null || !curr.isTTFNativeFont()) {
curr = c.getStyle().getFont();
}
if (curr == null || !curr.isTTFNativeFont()) {
parent = c.getParent();
while (parent != null && (curr == null || !curr.isTTFNativeFont())) {
curr = parent.getStyle().getFont();
parent = parent.getParent();
}
}
if (curr == null || !curr.isTTFNativeFont()) {
curr = Font.create("native:MainRegular");
}
if (curr != null && curr.isTTFNativeFont()) {
curr = curr.derive(size, 0);
style.setFont(curr);
}
}
}
return this;
}
Aggregations