Skip to content

Commit

Permalink
Handle 'res' and 'asset' prefixes in ttf path to search in resources …
Browse files Browse the repository at this point in the history
…and assets folders
  • Loading branch information
DayS committed Aug 3, 2015
1 parent e24abea commit eb500cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

public final class Iconify {

private static final String FONTAWESOME_TTF_FILE = "fontawesome-webfont-4.4.0.ttf";
private static final String FONTAWESOME_TTF_FILE = "res:fontawesome-webfont-4.4.0.ttf";

public static final String TAG = Iconify.class.getSimpleName();

Expand Down Expand Up @@ -805,7 +805,7 @@ public String getPrefix() {

@Override
public IconValue iconFrom(String value) {
return IconValue.valueOf(value.replaceAll("-", "_"));
return IconValue.valueOf(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,41 @@ static File resourceToFile(Context context, String resourceName) throws IOExcept
Log.e(Iconify.TAG, "Font folder creation failed");
throw new IllegalStateException("Cannot create Iconify font destination folder");
}
File outPath = new File(f, resourceName);

String filename = resourceName;
int separatorIndex = resourceName.indexOf(':');
if (separatorIndex != -1) {
filename = resourceName.substring(separatorIndex + 1);
}

File outPath = new File(f, filename);
if (outPath.exists()) return outPath;

BufferedOutputStream bos = null;
InputStream inputStream = null;
try {
inputStream = Iconify.class.getClassLoader().getResourceAsStream(resourceName);
if (resourceName.startsWith("asset:")) {
inputStream = context.getAssets().open(filename);
copy(inputStream, outPath);
return outPath;
}

inputStream = Iconify.class.getClassLoader().getResourceAsStream(filename);
copy(inputStream, outPath);
return outPath;
} finally {
closeQuietly(inputStream);
}
}

private static void copy(InputStream inputStream, File outputFile) throws IOException {
BufferedOutputStream bos = null;
try {
byte[] buffer = new byte[inputStream.available()];
bos = new BufferedOutputStream(new FileOutputStream(outPath));
int l = 0;
bos = new BufferedOutputStream(new FileOutputStream(outputFile));
int l;
while ((l = inputStream.read(buffer)) > 0) {
bos.write(buffer, 0, l);
}
return outPath;
} finally {
closeQuietly(bos);
closeQuietly(inputStream);
Expand Down Expand Up @@ -116,7 +137,7 @@ public static <T extends Enum<T> & BaseIconValue> StringBuilder replaceIcons(T i

String iconString = text.substring(startIndex + 1, endIndex - 1);
try {
BaseIconValue value = icon.iconFrom(iconString);
BaseIconValue value = icon.iconFrom(iconString.replaceAll("-", "_"));
String iconValue = String.valueOf(value.character());

text = text.replace(startIndex, endIndex, iconValue);
Expand Down

0 comments on commit eb500cc

Please sign in to comment.