Skip to content

Commit c3c5a7a

Browse files
committed
Added font customization
1 parent 0c46245 commit c3c5a7a

11 files changed

Lines changed: 138 additions & 64 deletions

File tree

96.2 KB
Binary file not shown.
674 KB
Binary file not shown.
327 KB
Binary file not shown.
93.7 KB
Binary file not shown.
58.5 KB
Binary file not shown.

codeview/src/main/java/io/github/kbiakov/codeview/adapters/AbstractCodeAdapter.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.kbiakov.codeview.adapters
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5+
import android.graphics.Typeface
56
import android.support.v7.widget.RecyclerView
67
import android.view.LayoutInflater
78
import android.view.View
@@ -159,8 +160,6 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
159160
ui(onUpdated)
160161
}
161162

162-
private fun monoTypeface() = MonoFontCache.getInstance(context).typeface
163-
164163
// - View holder
165164

166165
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@@ -169,12 +168,12 @@ abstract class AbstractCodeAdapter<T> : RecyclerView.Adapter<AbstractCodeAdapter
169168
lineView.setBackgroundColor(options.theme.bgContent.color())
170169

171170
val tvLineNum = lineView.findViewById(R.id.tv_line_num) as TextView
172-
tvLineNum.typeface = monoTypeface()
171+
tvLineNum.typeface = options.font
173172
tvLineNum.setTextColor(options.theme.numColor.color())
174173
tvLineNum.setBackgroundColor(options.theme.bgNum.color())
175174

176175
val tvLineContent = lineView.findViewById(R.id.tv_line_content) as TextView
177-
tvLineContent.typeface = monoTypeface()
176+
tvLineContent.typeface = options.font
178177

179178
val holder = ViewHolder(lineView)
180179
holder.setIsRecyclable(false)
@@ -292,6 +291,7 @@ data class Options(
292291
var code: String = "",
293292
var language: String? = null,
294293
var theme: ColorThemeData = ColorTheme.DEFAULT.theme(),
294+
var font: Typeface = FontCache.get(context).getTypeface(context),
295295
var shadows: Boolean = false,
296296
var shortcut: Boolean = false,
297297
var shortcutNote: String = context.getString(R.string.show_all),
@@ -331,6 +331,24 @@ data class Options(
331331
withTheme(theme)
332332
}
333333

334+
fun withFont(fontPath: String): Options {
335+
this.font = FontCache.get(context).getTypeface(context, fontPath)
336+
return this
337+
}
338+
339+
fun withFont(font: Font): Options {
340+
this.font = FontCache.get(context).getTypeface(context, font)
341+
return this
342+
}
343+
344+
fun setFont(fontPath: String) {
345+
withFont(fontPath)
346+
}
347+
348+
fun setFont(font: Font) {
349+
withFont(font)
350+
}
351+
334352
fun withShadows(): Options {
335353
this.shadows = true
336354
return this

codeview/src/main/java/io/github/kbiakov/codeview/highlight/CodeHighlighter.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import java.util.*
1414
object CodeHighlighter {
1515

1616
private val LT_BRACE = "<".toRegex()
17-
private val LT_REGULAR = "&lt;"
18-
private val LT_TMP = "^"
17+
private const val LT_REGULAR = "&lt;"
18+
private const val LT_TMP = "^"
1919

2020
private val parser = PrettifyParser()
2121

@@ -38,7 +38,6 @@ object CodeHighlighter {
3838
val content = parseContent(source, it)
3939
highlighted.append(content.withFontParams(color))
4040
}
41-
4241
return highlighted.toString()
4342
}
4443

@@ -205,6 +204,24 @@ data class SyntaxColors(
205204
val attrName: Int = 0x268BD2,
206205
val attrValue: Int = 0x269186)
207206

207+
/**
208+
* Font presets.
209+
*/
210+
enum class Font {
211+
Consolas,
212+
CourierNew,
213+
DejaVuSansMono,
214+
DroidSansMonoSlashed,
215+
Inconsolata,
216+
Monaco;
217+
218+
companion object {
219+
val Default = DroidSansMonoSlashed
220+
}
221+
}
222+
223+
// - Helpers
224+
208225
/**
209226
* @return Converted hex int to color by adding alpha-channel
210227
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.github.kbiakov.codeview.highlight;
2+
3+
import android.content.Context;
4+
import android.graphics.Typeface;
5+
import android.util.Log;
6+
7+
import java.util.Map;
8+
import java.util.WeakHashMap;
9+
10+
import io.github.kbiakov.codeview.adapters.AbstractCodeAdapter;
11+
12+
/**
13+
* Font cache.
14+
*
15+
* @see AbstractCodeAdapter
16+
* @author Kirill Biakov
17+
*/
18+
public class FontCache {
19+
20+
private static volatile FontCache instance;
21+
22+
public static FontCache get(Context context) {
23+
FontCache localInstance = instance;
24+
if (localInstance == null) {
25+
synchronized (FontCache.class) {
26+
localInstance = instance;
27+
if (localInstance == null) {
28+
instance = localInstance = new FontCache(context);
29+
}
30+
}
31+
}
32+
return localInstance;
33+
}
34+
35+
private Map<String, Typeface> fonts;
36+
37+
private FontCache(final Context context) {
38+
this.fonts = new WeakHashMap<String, Typeface>() {{
39+
String fontPath = getLocalFontPath(Font.Companion.getDefault());
40+
put(fontPath, loadFont(context, fontPath));
41+
}};
42+
}
43+
44+
private static String getLocalFontPath(Font font) {
45+
return String.format("%s.ttf", getLocalFontPath(font.name()));
46+
}
47+
48+
private static String getLocalFontPath(String fontName) {
49+
return String.format("fonts/%s", fontName);
50+
}
51+
52+
private static Typeface loadFont(Context context, String fontPath) {
53+
return Typeface.createFromAsset(context.getAssets(), fontPath);
54+
}
55+
56+
// - Public methods
57+
58+
public Typeface getTypeface(Context context) {
59+
return getTypeface(context, Font.Companion.getDefault());
60+
}
61+
62+
public Typeface getTypeface(Context context, Font font) {
63+
return getTypeface(context, getLocalFontPath(font));
64+
}
65+
66+
public Typeface getLocalTypeface(Context context, String fontPath) {
67+
return getTypeface(context, getLocalFontPath(fontPath));
68+
}
69+
70+
public Typeface getTypeface(Context context, String fontPath) {
71+
Typeface font = fonts.get(fontPath);
72+
if (font != null) {
73+
return font;
74+
} else {
75+
font = loadFont(context, fontPath);
76+
fonts.put(fontPath, font);
77+
return font;
78+
}
79+
}
80+
}

codeview/src/main/java/io/github/kbiakov/codeview/highlight/MonoFontCache.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

codeview/src/main/java/io/github/kbiakov/codeview/views/LineDiffView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.view.LayoutInflater
66
import android.widget.RelativeLayout
77
import android.widget.TextView
88
import io.github.kbiakov.codeview.R
9-
import io.github.kbiakov.codeview.highlight.MonoFontCache
9+
import io.github.kbiakov.codeview.highlight.FontCache
1010

1111
/**
1212
* @class CodeDiffView
@@ -43,7 +43,7 @@ class LineDiffView : RelativeLayout {
4343
val diffView = LineDiffView(context)
4444
diffView.tvLineDiff.text = if (model.isAddition) "+" else "-"
4545
diffView.tvLineContent.text = model.content
46-
diffView.tvLineContent.typeface = MonoFontCache.getInstance(context).typeface
46+
diffView.tvLineContent.typeface = FontCache.get(context).getTypeface(context)
4747

4848
diffView.setBackgroundColor(ContextCompat.getColor(context,
4949
if (model.isAddition)

0 commit comments

Comments
 (0)