Skip to content

Commit

Permalink
Better font loader
Browse files Browse the repository at this point in the history
+ Better resources cache implement.
+ Better font loader
+ Fix image size in ImageView
  • Loading branch information
GabrielBRDeveloper committed May 22, 2024
1 parent 33907bc commit 892b4c0
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 66 deletions.
2 changes: 1 addition & 1 deletion res/ex-layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
width="wrap_content"
height="wrap_content"
textSize="14sp"
font="bold"
textStyle="bold"
text="Hello world"/>

<TextView
Expand Down
5 changes: 5 additions & 0 deletions res/font/roboto.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<font>
<font bold="true">@fonts/Roboto/Roboto-Bold</font>
<font>@fonts/Roboto/Roboto-Regular</font>
</font>
7 changes: 5 additions & 2 deletions src/br/nullexcept/mux/graphics/fonts/Typeface.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

public class Typeface {
protected static final float SCALE = 1024.0f;

public static final int STYLE_NORMAL = 0;
public static final int STYLE_BOLD = 1;
public static final int STYLE_ITALIC = 2;

public static Typeface DEFAULT;
public static Typeface DEFAULT_BOLD;

protected final float ascent;
protected final float descent;
Expand Down Expand Up @@ -40,7 +44,6 @@ public class Typeface {
this.lineHeight = lineHeight[0];
NVGGlyphPosition.create();
NVGGlyphPosition.Buffer b = NVGGlyphPosition.create(1);
ByteBuffer bf = MemoryUtil.memByteBuffer(b.address(),b.sizeof());
for (int i = 0; i < this.bounds.length; i++){
String character = String.valueOf((char)i);
NanoVG.nvgTextGlyphPositions(context,0,0,character,b);
Expand Down
5 changes: 0 additions & 5 deletions src/br/nullexcept/mux/res/AttributeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ public interface AttributeList {

void searchBoolean(String name, Function<Boolean> apply);

void searchFont(String name, Function<Typeface> apply);

String[] names();

String getRawValue(String name);

default Typeface getFont(String name) {
return null;
}

default CharSequence getText(String name) {
return "";
Expand Down
18 changes: 0 additions & 18 deletions src/br/nullexcept/mux/res/FallbackAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,6 @@ public void searchBoolean(String name, Function<Boolean> apply) {
searchRaw(name, value -> apply.call(Parser.parseBoolean(value)));
}

@Override
public void searchFont(String name, Function<Typeface> apply) {
Typeface font = getFont(name);
if (font != null) {
apply.call(font);
}
}

@Override
public Typeface getFont(String name) {
if (contains(name)) {
return Parser.parseFont(resources, resolve(map.get(name)));
} else if (fallback != null) {
return fallback.getFont(name);
}
return null;
}

@Override
public String[] names() {
return map.keySet().toArray(new String[0]);
Expand Down
30 changes: 4 additions & 26 deletions src/br/nullexcept/mux/res/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,10 @@ private static Bitmap openBitmap(String path) {
public static Drawable parseDrawable(Resources resources, String value) {
if (value.startsWith("#")){
return new ColorDrawable(Color.parseColor(value));
} else if (value.startsWith("@")){
value = value.substring(1);
} else {
if (value.startsWith("@")) {
value = value.substring(1);
}
if (Resources.Manager.exists(value+".xml")){
return inflateXmlDrawable(resources, resources.requestXml(value));
} else {
Expand Down Expand Up @@ -277,28 +279,4 @@ public static ColorStateList parseColorState(Resources resources, String value)
Log.error(LOG_TAG, "Invalid color value: "+value);
return new ColorStateList(Color.RED);
}

public static Typeface parseFont(Resources res, String name) {

{
String low = name.toLowerCase().trim();
switch (low) {
case "default":
case "normal":
return Typeface.DEFAULT;
case "bold":
return Typeface.DEFAULT_BOLD;
}
}

Typeface font = res.requestFont(Resources.fixPath(name, "font"));
if (font == null) {
font = res.requestFont(Resources.fixPath(name, "fonts"));
if (font == null && name.contains(":")) {
String[] pats = name.split(":");
return parseFont(res, pats[0]+"/"+pats[1]);
}
}
return font;
}
}
35 changes: 35 additions & 0 deletions src/br/nullexcept/mux/res/ResourceCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package br.nullexcept.mux.res;

import br.nullexcept.mux.graphics.fonts.Typeface;
import br.nullexcept.mux.lang.xml.XmlElement;

import java.util.HashMap;

class ResourceCache {
private static final HashMap<String, XmlElement> xmlCache = new HashMap<>();
private static final HashMap<String, Typeface> fontCache = new HashMap<>();

public static XmlElement obtainXml(String path) {
return xmlCache.get(path);
}

public static boolean hasXml(String path) {
return xmlCache.containsKey(path);
}

public static Typeface obtainTypeface(String name) {
return fontCache.get(name);
}

public static boolean hasFont(String path) {
return fontCache.containsKey(path);
}

public static void store(String path, Typeface typeface) {
fontCache.put(path, typeface);
}

public static void store(String path, XmlElement xml) {
xmlCache.put(path, xml);
}
}
44 changes: 34 additions & 10 deletions src/br/nullexcept/mux/res/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

public final class Resources {
private DisplayMetrics metrics;
private static final HashMap<String, XmlElement> cache = new HashMap<>();
private static final HashMap<String, Typeface> fonts = new HashMap<>();

private final HashMap<String, StylePreset> styles = new HashMap<>();

Expand All @@ -38,7 +36,6 @@ public final class Resources {
static {
Manager = new ResourcesManager();
Typeface.DEFAULT = TypefaceFactory.create(Manager.openDocument("fonts/Roboto/Roboto-Regular.ttf"));
Typeface.DEFAULT_BOLD = TypefaceFactory.create(Manager.openDocument("fonts/Roboto/Roboto-Bold.ttf"));
}

public Resources(Context ctx){
Expand Down Expand Up @@ -123,22 +120,24 @@ public DisplayMetrics getDisplayMetrics() {
}

Typeface requestFont(String path) {
if (fonts.containsKey(path)) {
return fonts.get(path);
if (ResourceCache.hasFont(path)) {
return ResourceCache.obtainTypeface(path);
}

String pth = path + ".ttf";
if (Manager.exists(path)) {
if (Manager.exists(pth)) {
Typeface typeface = TypefaceFactory.create(Manager.openDocument(pth));
fonts.put(path, typeface);
ResourceCache.store(path, typeface);
return typeface;
}
return null;
}

XmlElement requestXml(String path){
if (cache.containsKey(path)){
return cache.get(path);
if (ResourceCache.hasXml(path)){
return ResourceCache.obtainXml(path);
}
cache.put(path, XmlElement.parse(Resources.Manager.openDocument(path+".xml")));
ResourceCache.store(path, XmlElement.parse(Resources.Manager.openDocument(path+".xml")));
return requestXml(path);
}

Expand All @@ -154,6 +153,31 @@ public Drawable getDrawable(String id) {
return Parser.parseDrawable(this, fixPath(id, "drawable"));
}

public Typeface getFont(String family, int style) {
if (family.toLowerCase().equals("default")) {
family = "roboto";
}
XmlElement fonts = requestXml(fixPath(family, "font"));
Typeface font = Typeface.DEFAULT;

boolean bold = (style & Typeface.STYLE_BOLD) != 0;
boolean italic = (style & Typeface.STYLE_ITALIC) != 0;
for (XmlElement child: fonts.children()) {
if (child.has("bold") && !String.valueOf(bold).equals(child.attr("bold"))) {
continue;
}
if (child.has("italic") && !String.valueOf(italic).equals(child.attr("italic"))) {
continue;
}
String value = child.value().trim();
if (value.length() > 0) {
return requestFont(fixPath(value, "raw"));
}
}

return font;
}

public static class DisplayMetrics {
public final double pm;
public final double pt;
Expand Down
3 changes: 2 additions & 1 deletion src/br/nullexcept/mux/view/AttrList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public class AttrList {
public static final String textColor = "textColor";
public static final String text = "text";
public static final String textSize = "textSize";
public static final String typeface = "font";
public static final String fontFamily = "fontFamily";
public static final String textStyle = "textStyle";
public static final String width="width";
public static final String height="height";
public static final String background = "background";
Expand Down
4 changes: 2 additions & 2 deletions src/br/nullexcept/mux/widget/ImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public void setImageDrawable(Drawable image) {
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
float w = getWidth()-getPaddingLeft()-getPaddingRight();
float h = getHeight()-getPaddingTop()-getPaddingBottom();
float w = getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
float h = getMeasuredHeight()-getPaddingTop()-getPaddingBottom();
float iw, ih;

if (image != null){
Expand Down
26 changes: 25 additions & 1 deletion src/br/nullexcept/mux/widget/TextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import br.nullexcept.mux.view.View;
import br.nullexcept.mux.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TextView extends View {
private final Paint paint = new Paint();
private ColorStateList textColor = new ColorStateList(Color.RED);
Expand All @@ -26,7 +30,27 @@ public TextView(Context context, AttributeList attrs) {
attrs.searchText(AttrList.text, this::setText);
attrs.searchColorList(AttrList.textColor, value -> textColor = value);
attrs.searchDimension(AttrList.textSize, this::setTextSize);
attrs.searchFont(AttrList.typeface, this::setTypeface);

String[] fontFamily = new String[]{"default"};
int[] fontStyle = new int[1];

attrs.searchRaw(AttrList.fontFamily, value -> fontFamily[0] = value);
attrs.searchRaw(AttrList.textStyle, value -> {
List<String> values = Arrays.asList(value.toLowerCase().split("\\|"));
if (values.contains("italic")) {
fontStyle[0] |= Typeface.STYLE_ITALIC;
}
if (values.contains("bold")) {
fontStyle[0] |= Typeface.STYLE_BOLD;
}
});

Typeface font = context.getResources().getFont(fontFamily[0], fontStyle[0]);
if (font != null) {
setTypeface(font);
} else {
System.err.println("Font is null");
}
}

@Override
Expand Down

0 comments on commit 892b4c0

Please sign in to comment.