mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 12:02:16 +02:00
[Kotlin Migration] ReviewerCustomFonts
Package: com.ichi2.anki.reviewer
This commit is contained in:
parent
f003a510db
commit
14801033c3
@ -43,7 +43,7 @@ permission notice:
|
|||||||
// Example of class name: "/com/ichi2/anki/UIUtils.kt"
|
// Example of class name: "/com/ichi2/anki/UIUtils.kt"
|
||||||
// Ensure that it starts with '/' (slash)
|
// Ensure that it starts with '/' (slash)
|
||||||
def source = Source.MAIN
|
def source = Source.MAIN
|
||||||
def className = "/com/ichi2/anki/reviewer/ReviewerCustomFonts.kt"
|
def className = ""
|
||||||
|
|
||||||
enum Source {
|
enum Source {
|
||||||
MAIN("/src/main/java"),
|
MAIN("/src/main/java"),
|
||||||
|
@ -14,150 +14,140 @@
|
|||||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
****************************************************************************************/
|
****************************************************************************************/
|
||||||
|
|
||||||
package com.ichi2.anki.reviewer;
|
package com.ichi2.anki.reviewer
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context
|
||||||
import android.content.SharedPreferences;
|
import android.text.TextUtils
|
||||||
import android.text.TextUtils;
|
import com.ichi2.anki.AnkiDroidApp
|
||||||
|
import com.ichi2.anki.AnkiFont
|
||||||
|
import com.ichi2.libanki.Utils
|
||||||
|
import com.ichi2.utils.HashUtil.HashMapInit
|
||||||
|
|
||||||
import com.ichi2.anki.AnkiDroidApp;
|
class ReviewerCustomFonts(context: Context) {
|
||||||
import com.ichi2.anki.AnkiFont;
|
private val mCustomStyle: String
|
||||||
import com.ichi2.libanki.Utils;
|
private var mDefaultFontStyle: String? = null
|
||||||
import com.ichi2.utils.HashUtil;
|
private var mOverrideFontStyle: String? = null
|
||||||
|
private var mThemeFontStyle: String? = null
|
||||||
import java.util.HashMap;
|
private var mDominantFontStyle: String? = null
|
||||||
import java.util.List;
|
fun updateCssStyle(cssStyle: StringBuilder) {
|
||||||
import java.util.Map;
|
cssStyle.append(mCustomStyle)
|
||||||
|
|
||||||
public class ReviewerCustomFonts {
|
|
||||||
|
|
||||||
private final String mCustomStyle;
|
|
||||||
private String mDefaultFontStyle;
|
|
||||||
private String mOverrideFontStyle;
|
|
||||||
private String mThemeFontStyle;
|
|
||||||
private String mDominantFontStyle;
|
|
||||||
|
|
||||||
public ReviewerCustomFonts(Context context) {
|
|
||||||
Map<String, AnkiFont> customFontsMap = getCustomFontsMap(context);
|
|
||||||
mCustomStyle = getCustomFontsStyle(customFontsMap) + getDominantFontStyle(context, customFontsMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCssStyle(StringBuilder cssStyle) {
|
|
||||||
cssStyle.append(mCustomStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the CSS used to handle custom fonts.
|
|
||||||
* <p>
|
|
||||||
* Custom fonts live in fonts directory in the directory used to store decks.
|
|
||||||
* <p>
|
|
||||||
* Each font is mapped to the font family by the same name as the name of the font without the extension.
|
|
||||||
*/
|
|
||||||
private static String getCustomFontsStyle(Map<String, AnkiFont> customFontsMap) {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for (AnkiFont font : customFontsMap.values()) {
|
|
||||||
builder.append(font.getDeclaration());
|
|
||||||
builder.append('\n');
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the CSS used to set the theme font.
|
* Returns the CSS used to set the theme font.
|
||||||
*
|
*
|
||||||
* @return the font style, or the empty string if no font is set
|
* @return the font style, or the empty string if no font is set
|
||||||
*/
|
*/
|
||||||
private String getThemeFontStyle() {
|
private val themeFontStyle: String?
|
||||||
if (mThemeFontStyle == null) {
|
get() {
|
||||||
String themeFontName = "OpenSans";
|
if (mThemeFontStyle == null) {
|
||||||
if (TextUtils.isEmpty(themeFontName)) {
|
val themeFontName = "OpenSans"
|
||||||
mThemeFontStyle = "";
|
mThemeFontStyle = if (TextUtils.isEmpty(themeFontName)) {
|
||||||
} else {
|
""
|
||||||
mThemeFontStyle = String.format(
|
} else {
|
||||||
"BODY {"
|
String.format(
|
||||||
+ "font-family: '%s';"
|
"""
|
||||||
+ "font-weight: normal;"
|
BODY {font-family: '%s';font-weight: normal;font-style: normal;font-stretch: normal;}
|
||||||
+ "font-style: normal;"
|
|
||||||
+ "font-stretch: normal;"
|
""".trimIndent(),
|
||||||
+ "}\n", themeFontName);
|
themeFontName
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return mThemeFontStyle
|
||||||
}
|
}
|
||||||
return mThemeFontStyle;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the CSS used to set the default font.
|
* Returns the CSS used to set the default font.
|
||||||
*
|
*
|
||||||
* @return the default font style, or the empty string if no default font is set
|
* @return the default font style, or the empty string if no default font is set
|
||||||
*/
|
*/
|
||||||
private String getDefaultFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
|
private fun getDefaultFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
|
||||||
if (mDefaultFontStyle == null) {
|
if (mDefaultFontStyle == null) {
|
||||||
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
|
val preferences = AnkiDroidApp.getSharedPrefs(context)
|
||||||
AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
|
val defaultFont = customFontsMap[preferences.getString("defaultFont", null)]
|
||||||
if (defaultFont != null) {
|
mDefaultFontStyle = if (defaultFont != null) {
|
||||||
mDefaultFontStyle = "BODY { " + defaultFont.getCSS(false) + " }\n";
|
"""BODY { ${defaultFont.getCSS(false)} }
|
||||||
|
"""
|
||||||
} else {
|
} else {
|
||||||
mDefaultFontStyle = "";
|
""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mDefaultFontStyle;
|
return mDefaultFontStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the CSS used to set the override font.
|
* Returns the CSS used to set the override font.
|
||||||
*
|
*
|
||||||
* @return the override font style, or the empty string if no override font is set
|
* @return the override font style, or the empty string if no override font is set
|
||||||
*/
|
*/
|
||||||
private String getOverrideFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
|
private fun getOverrideFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
|
||||||
if (mOverrideFontStyle == null) {
|
if (mOverrideFontStyle == null) {
|
||||||
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
|
val preferences = AnkiDroidApp.getSharedPrefs(context)
|
||||||
AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
|
val defaultFont = customFontsMap[preferences.getString("defaultFont", null)]
|
||||||
boolean overrideFont = "1".equals(preferences.getString("overrideFontBehavior", "0"));
|
val overrideFont = "1" == preferences.getString("overrideFontBehavior", "0")
|
||||||
if (defaultFont != null && overrideFont) {
|
mOverrideFontStyle = if (defaultFont != null && overrideFont) {
|
||||||
mOverrideFontStyle = "BODY, .card, * { " + defaultFont.getCSS(true) + " }\n";
|
"""BODY, .card, * { ${defaultFont.getCSS(true)} }
|
||||||
|
"""
|
||||||
} else {
|
} else {
|
||||||
mOverrideFontStyle = "";
|
""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mOverrideFontStyle;
|
return mOverrideFontStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the CSS that determines font choice in a global fashion.
|
* Returns the CSS that determines font choice in a global fashion.
|
||||||
*
|
*
|
||||||
* @return the font style, or the empty string if none applies
|
* @return the font style, or the empty string if none applies
|
||||||
*/
|
*/
|
||||||
private String getDominantFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
|
private fun getDominantFontStyle(context: Context, customFontsMap: Map<String?, AnkiFont>): String? {
|
||||||
if (mDominantFontStyle == null) {
|
if (mDominantFontStyle == null) {
|
||||||
mDominantFontStyle = getOverrideFontStyle(context, customFontsMap);
|
mDominantFontStyle = getOverrideFontStyle(context, customFontsMap)
|
||||||
if (TextUtils.isEmpty(mDominantFontStyle)) {
|
if (TextUtils.isEmpty(mDominantFontStyle)) {
|
||||||
mDominantFontStyle = getDefaultFontStyle(context, customFontsMap);
|
mDominantFontStyle = getDefaultFontStyle(context, customFontsMap)
|
||||||
if (TextUtils.isEmpty(mDominantFontStyle)) {
|
if (TextUtils.isEmpty(mDominantFontStyle)) {
|
||||||
mDominantFontStyle = getThemeFontStyle();
|
mDominantFontStyle = themeFontStyle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mDominantFontStyle;
|
return mDominantFontStyle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Returns a map from custom fonts names to the corresponding {@link AnkiFont} object.
|
* Returns the CSS used to handle custom fonts.
|
||||||
* <p>
|
* <p>
|
||||||
* The list of constructed lazily the first time is needed.
|
* Custom fonts live in fonts directory in the directory used to store decks.
|
||||||
*/
|
* <p>
|
||||||
private static Map<String, AnkiFont> getCustomFontsMap(Context context) {
|
* Each font is mapped to the font family by the same name as the name of the font without the extension.
|
||||||
List<AnkiFont> fonts = Utils.getCustomFonts(context);
|
*/
|
||||||
Map<String, AnkiFont> customFontsMap = HashUtil.HashMapInit(fonts.size());
|
private fun getCustomFontsStyle(customFontsMap: Map<String?, AnkiFont>): String {
|
||||||
for (AnkiFont f : fonts) {
|
val builder = StringBuilder()
|
||||||
customFontsMap.put(f.getName(), f);
|
for (font in customFontsMap.values) {
|
||||||
|
builder.append(font.declaration)
|
||||||
|
builder.append('\n')
|
||||||
|
}
|
||||||
|
return builder.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a map from custom fonts names to the corresponding {@link AnkiFont} object.
|
||||||
|
* <p>
|
||||||
|
* The list of constructed lazily the first time is needed.
|
||||||
|
*/
|
||||||
|
private fun getCustomFontsMap(context: Context): Map<String?, AnkiFont> {
|
||||||
|
val fonts = Utils.getCustomFonts(context)
|
||||||
|
val customFontsMap: MutableMap<String?, AnkiFont> = HashMapInit(fonts.size)
|
||||||
|
for (f in fonts) {
|
||||||
|
customFontsMap[f.name] = f
|
||||||
|
}
|
||||||
|
return customFontsMap
|
||||||
}
|
}
|
||||||
return customFontsMap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init {
|
||||||
|
val customFontsMap = getCustomFontsMap(context)
|
||||||
|
mCustomStyle = getCustomFontsStyle(customFontsMap) + getDominantFontStyle(context, customFontsMap)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user