0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00

Implement default font with override font

This commit is contained in:
chajadan 2014-01-06 11:18:01 -08:00
parent d3c534f8a7
commit f793ee4f50
9 changed files with 186 additions and 39 deletions

View File

@ -29,7 +29,7 @@
The <build> is only present for alpha and beta releases (e.g., 2.0.4alpha2 or 2.0.4beta4), developer builds do
not have a build number (e.g., 2.0.4dev) and official releases only have three components (e.g., 2.0.4).
The version code is deerived from the version name as follows:
The version code is derived from the version name as follows:
AAbbCCtDD
where AA is 2-digit decimal number (with leading zeros omitted) representing the major version; bb is a 2-digit
decimal number representing the minor version; CC is a 2-digit decimal number representing the maintenance

View File

@ -154,7 +154,9 @@
<string name="fix_hebrew_instructions">This setting bypasses the RTL algorithm of Android to show Hebrew text with vowels correctly.\n\nFor this to work, you need to download font Tohu.ttf and copy it manually to:\n%s/fonts/\n\nYou won\'t have to modify your cards.</string>
<string name="fix_hebrew_download_font">Download Font</string>
<string name="default_font">Default font</string>
<string name="default_font_summ">The font used for text that has no font formatting specified. Custom fonts should be installed on the SD card in a folder called \'fonts\' in your AnkiDroid folder.</string>
<string name="default_font_summ">The font used for text that has no font formatting specified. Custom fonts should be installed in a folder called \'fonts\' in your AnkiDroid folder.</string>
<string name="override_font">Override font</string>
<string name="override_font_summ">A font that will be used instead of any fonts specified, including the default font. Custom fonts should be installed in a folder called \'fonts\' in your AnkiDroid folder.</string>
<string name="language">Language</string>
<string name="language_summ">Select the language AnkiDroid should use. At the moment: XXX</string>
<string name="language_system">System Language</string>
@ -313,7 +315,7 @@
<string name="pref_enable_tibetan">Enable Tibetan Support</string>
<string name="pref_enable_tibetan_summ">Enables tibetan typeface for card browser and card editor. Typeface should be provided in /mnt/sdard/fonts/DDC_Uchen.ttf.</string>
<string name="pref_browser_editor_font">Browser and Editor font</string>
<string name="pref_browser_editor_font_summ">Default font to be used in Card Browser and Card Editor. Custom fonts should be installed on the SD card in a folder called \'fonts\' in your AnkiDroid folder.</string>
<string name="pref_browser_editor_font_summ">Default font to be used in Card Browser and Card Editor. Custom fonts should be installed in a folder called \'fonts\' in your AnkiDroid folder.</string>
<string name="deck_conf_cram_filter">Filter</string>
<string name="deck_conf_cram_options">Options</string>

View File

@ -194,7 +194,13 @@
android:defaultValue=""
android:key="defaultFont"
android:summary="@string/default_font_summ"
android:title="@string/default_font" />
android:title="@string/default_font"
android:shouldDisableView="true" />
<ListPreference
android:defaultValue=""
android:key="overrideFont"
android:summary="@string/override_font_summ"
android:title="@string/override_font" />
<com.hlidskialf.android.preference.SeekBarPreference
android:defaultValue="100"
android:key="relativeDisplayFontSize"

View File

@ -108,6 +108,13 @@ public class AnkiDroidApp extends Application {
*/
public static final int CHECK_DB_AT_VERSION = 40;
/**
* The latest package version number that included changes to the preferences
* that requires handling. All collections being upgraded to (or after) this version
* must update preferences.
*/
public static final int CHECK_PREFERENCES_AT_VERSION = 20100108;
/**
* On application creation.
*/

View File

@ -1,6 +1,7 @@
package com.ichi2.anki;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.Log;
@ -19,6 +20,8 @@ public class AnkiFont {
private String mFamily;
private List<String> mAttributes;
private String mPath;
private Boolean mIsDefault;
private Boolean mIsOverride;
private static final String fAssetPathPrefix = "/android_asset/fonts/";
private static Set<String> corruptFonts = new HashSet<String>();
@ -27,7 +30,10 @@ public class AnkiFont {
mFamily = family;
mAttributes = attributes;
mPath = path;
}
mIsDefault = false;
mIsOverride = false;
}
/**
* Factory for AnkiFont creation.
@ -51,6 +57,7 @@ public class AnkiFont {
// unable to create typeface
return null;
}
if (tf.isBold() || name.toLowerCase().contains("bold")) {
attributes.add("font-weight: bolder;");
family = family.replaceFirst("(?i)-?Bold", "");
@ -77,11 +84,22 @@ public class AnkiFont {
attributes.add("font-stretch: expanded;");
family = family.replaceFirst("(?i)-?Expanded", "");
family = family.replaceFirst("(?i)-?Wide(r)?", "");
} else {
attributes.add("font-stretch: normal;");
}
family = family.replaceFirst("(?i)-?Regular", "");
return new AnkiFont(name, family, attributes, path);
AnkiFont createdFont = new AnkiFont(name, family, attributes, path);
// determine if override font or default font
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(ctx);
String overrideFont = preferences.getString("overrideFont", "");
if (overrideFont.equalsIgnoreCase(name)) {
createdFont.setAsOverride();
} else {
String defaultFont = preferences.getString("defaultFont", "");
if (defaultFont.equalsIgnoreCase(name)) {
createdFont.setAsDefault();
}
}
return createdFont;
}
public String getDeclaration() {
@ -89,10 +107,23 @@ public class AnkiFont {
sb.append(getCSS()).append(" src: url(\"file://").append(mPath).append("\");}");
return sb.toString();
}
public String getCSS() {
StringBuilder sb = new StringBuilder("font-family: \"").append(mFamily).append("\" !important;");
public String getCSS() {
StringBuilder sb = new StringBuilder("font-family: \"").append(mFamily);
if (mIsOverride) {
sb.append("\" !important;");
} else {
sb.append("\";");
}
for (String attr : mAttributes) {
sb.append(" ").append(attr);
if (mIsOverride) {
if (sb.charAt(sb.length() - 1) == ';') {
sb.deleteCharAt(sb.length() - 1);
sb.append(" !important;");
} else {
Log.d(AnkiDroidApp.TAG, "AnkiFont.getCSS() - unable to set a font attribute important while override is set.");
}
}
}
return sb.toString();
}
@ -123,4 +154,12 @@ public class AnkiFont {
return null;
}
}
private void setAsDefault() {
mIsDefault = true;
mIsOverride = false;
}
private void setAsOverride() {
mIsOverride = true;
mIsDefault = false;
}
}

View File

@ -1288,13 +1288,13 @@ public class DeckPicker extends FragmentActivity {
// like to run on all collections. A missing version number is assumed to be a fresh
// installation of AnkiDroid and we don't run the check.
int current = AnkiDroidApp.getPkgVersionCode();
int previous;
int previousTemp; // a non-final variable, for intermediate calculations
if (!preferences.contains("lastUpgradeVersion")) {
// Fresh install
previous = current;
previousTemp = current;
} else {
try {
previous = preferences.getInt("lastUpgradeVersion", current);
previousTemp = preferences.getInt("lastUpgradeVersion", current);
} catch (ClassCastException e) {
// Previous versions stored this as a string.
String s = preferences.getString("lastUpgradeVersion", "");
@ -1302,20 +1302,27 @@ public class DeckPicker extends FragmentActivity {
// We manually set the version here, but anything older will force a DB
// check.
if (s.equals("2.0.2")) {
previous = 40;
previousTemp = 40;
} else {
previous = 0;
previousTemp = 0;
}
}
}
final int previous = previousTemp;
preferences.edit().putInt("lastUpgradeVersion", current).commit();
if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) {
if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION ||
previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new Listener() {
@Override
public void onPostExecute(DeckTask task, TaskData result) {
mOpenCollectionHandler.onPostExecute(result);
integrityCheck();
if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) {
integrityCheck();
}
if (previous < AnkiDroidApp.CHECK_PREFERENCES_AT_VERSION) {
upgradePreferences(previous);
}
}
@Override
@ -1332,7 +1339,16 @@ public class DeckPicker extends FragmentActivity {
}
}
private void upgradePreferences(int previousVersionCode) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
if (previousVersionCode < 20100108) {
preferences.edit().putString("overrideFont", preferences.getString("defaultFont", "")).commit();
preferences.edit().putString("defaultFont", "").commit();
}
}
protected void sendKey(int keycode) {
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keycode));
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keycode));

View File

@ -186,9 +186,6 @@ public class Info extends Activity {
sb.append(
String.format(content[4], res.getString(R.string.licence_wiki),
res.getString(R.string.link_source))).append("<br/><br/>");
sb.append(
String.format(content[5])).append("<br/><br/>");
sb.append("</body></html>");
mWebView.loadDataWithBaseURL("", sb.toString(), "text/html", "utf-8", null);
((Button) findViewById(R.id.info_continue)).setText(res.getString(R.string.info_rate));

View File

@ -340,15 +340,30 @@ public class Preferences extends PreferenceActivity implements OnSharedPreferenc
/** Initializes the list of custom fonts shown in the preferences. */
private void initializeCustomFontsDialog() {
ListPreference customFontsPreference = (ListPreference) getPreferenceScreen().findPreference("defaultFont");
customFontsPreference.setEntries(getCustomFonts("System default"));
customFontsPreference.setEntryValues(getCustomFonts(""));
ListPreference defaultFontPreference = (ListPreference) getPreferenceScreen().findPreference("defaultFont");
ListPreference overrideFontPreference = (ListPreference) getPreferenceScreen().findPreference("overrideFont");
if (defaultFontPreference != null) {
defaultFontPreference.setEntries(getCustomFonts("System default"));
defaultFontPreference.setEntryValues(getCustomFonts(""));
}
if (overrideFontPreference != null) {
overrideFontPreference.setEntries(getCustomFonts("None"));
overrideFontPreference.setEntryValues(getCustomFonts(""));
}
onOverrideFontChange(overrideFontPreference, defaultFontPreference);
ListPreference browserEditorCustomFontsPreference = (ListPreference) getPreferenceScreen().findPreference("browserEditorFont");
browserEditorCustomFontsPreference.setEntries(getCustomFonts("System default"));
browserEditorCustomFontsPreference.setEntryValues(getCustomFonts("", true));
}
private void onOverrideFontChange(ListPreference overrideFontPreference, ListPreference defaultFontPreference) {
if (overrideFontPreference != null && defaultFontPreference != null) {
Boolean overrideIsSet = !overrideFontPreference.getValue().isEmpty();
defaultFontPreference.setEnabled(!overrideIsSet);
}
}
@Override
protected void onPause() {
super.onPause();
@ -471,6 +486,12 @@ public class Preferences extends PreferenceActivity implements OnSharedPreferenc
date.set(Calendar.HOUR_OF_DAY, hours);
mCol.setCrt(date.getTimeInMillis() / 1000);
mCol.setMod();
} else if (key.equals("overrideFont")) {
ListPreference overrideFontPreference =
(ListPreference) getPreferenceScreen().findPreference("overrideFont");
ListPreference defaultFontPreference =
(ListPreference) getPreferenceScreen().findPreference("defaultFont");
onOverrideFontChange(overrideFontPreference, defaultFontPreference);
}
if (Arrays.asList(mShowValueInSummList).contains(key)) {
updateListPreference(key);

View File

@ -26,17 +26,22 @@ import android.text.TextUtils;
import com.ichi2.anki.AnkiDroidApp;
import com.ichi2.anki.AnkiFont;
import com.ichi2.anki.reviewer.ReviewerExt;
import com.ichi2.libanki.Utils;
import com.ichi2.themes.Themes;
public class CustomFontsReviewerExt implements ReviewerExt {
private final String mCustomStyle;
private String mDefaultFontStyle;
private String mOverrideFontStyle;
private String mThemeFontStyle;
private String mDominantFontStyle;
private final boolean mSupportsQuickUpdate;
public CustomFontsReviewerExt(Context context) {
Map<String, AnkiFont> customFontsMap = getCustomFontsMap(context);
mCustomStyle = getCustomDefaultFontStyle(context, customFontsMap) + getCustomFontsStyle(customFontsMap);
mCustomStyle = getCustomFontsStyle(customFontsMap) + getDominantFontStyle(context, customFontsMap);
mSupportsQuickUpdate = customFontsMap.size() == 0;
}
@ -68,27 +73,81 @@ public class CustomFontsReviewerExt implements ReviewerExt {
return builder.toString();
}
/** Returns the CSS used to set the default font. */
private static String getCustomDefaultFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
if (defaultFont != null) {
return "BODY, .card { " + defaultFont.getCSS() + " }\n";
} else {
String defaultFontName = Themes.getReviewerFontName();
if (TextUtils.isEmpty(defaultFontName)) {
return "";
/**
* Returns the CSS used to set the theme font.
* @return the font style, or the empty string if no font is set
*/
private String getThemeFontStyle() {
if (mThemeFontStyle == null) {
String themeFontName = Themes.getReviewerFontName();
if (TextUtils.isEmpty(themeFontName)) {
mThemeFontStyle = "";
} else {
return String.format(
mThemeFontStyle = String.format(
"BODY {"
+ "font-family: '%s';"
+ "font-weight: normal;"
+ "font-style: normal;"
+ "font-stretch: normal;"
+ "}\n", defaultFontName);
+ "}\n", themeFontName);
}
}
return mThemeFontStyle;
}
/**
* Returns the CSS used to set the default font.
* @return the default font style, or the empty string if no default font is set
*/
private String getDefaultFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
if (mDefaultFontStyle == null) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
if (defaultFont != null) {
mDefaultFontStyle = "BODY { " + defaultFont.getCSS() + " }\n";
} else {
mDefaultFontStyle = "";
}
}
return mDefaultFontStyle;
}
/**
* Returns the CSS used to set the override font.
* @return the override font style, or the empty string if no override font is set
*/
private String getOverrideFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
if (mOverrideFontStyle == null) {
SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
AnkiFont overrideFont = customFontsMap.get(preferences.getString("overrideFont", null));
if (overrideFont != null) {
mOverrideFontStyle = "BODY, .card, * { " + overrideFont.getCSS() + " }\n";
} else {
mOverrideFontStyle = "";
}
}
return mOverrideFontStyle;
}
/**
* Returns the CSS that determines font choice in a global fashion.
* @return the font style, or the empty string if none applies
*/
private String getDominantFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
if (mDominantFontStyle == null) {
mDominantFontStyle = getOverrideFontStyle(context, customFontsMap);
if (mDominantFontStyle.isEmpty()) {
mDominantFontStyle = getDefaultFontStyle(context, customFontsMap);
if (mDominantFontStyle.isEmpty()) {
mDominantFontStyle = getThemeFontStyle();
}
}
}
return mDominantFontStyle;
}