Skip to content

Commit

Permalink
refactor(data, ime, util): reform how to get typeface for the theme l…
Browse files Browse the repository at this point in the history
…ayout
  • Loading branch information
WhiredPlanck committed Dec 13, 2022
1 parent 0240385 commit 37a305c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 37 deletions.
14 changes: 0 additions & 14 deletions app/src/main/java/com/osfans/trime/data/theme/Config.java
Expand Up @@ -23,7 +23,6 @@
import android.graphics.Color;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.NinePatchDrawable;
Expand Down Expand Up @@ -496,10 +495,6 @@ public Drawable getDrawable(@NonNull Map<?, ?> m, String k) {
return null;
}

public static Object getValue(@NonNull Map<?, ?> m, String k, Object o) {
return m.containsKey(k) ? m.get(k) : o;
}

public float getLiquidFloat(String key) {
if (liquidKeyboard != null) {
if (liquidKeyboard.containsKey(key)) {
Expand Down Expand Up @@ -649,15 +644,6 @@ public void initEnterLabels() {
if (!mEnterLabels.containsKey("send")) mEnterLabels.put("send", defaultEnterLabel);
}

public Typeface getFont(String key) {
final String name = style.getString(key);
if (name != null) {
final File f = new File(DataManager.getDataDir("fonts"), name);
if (f.exists()) return Typeface.createFromFile(f);
}
return Typeface.DEFAULT;
}

// 返回drawable。参数可以是颜色或者图片。如果参数缺失,返回null
private Drawable drawableObject(Object o) {
if (o == null) return null;
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/osfans/trime/data/theme/FontManager.kt
@@ -0,0 +1,19 @@
package com.osfans.trime.data.theme

import android.graphics.Typeface
import com.osfans.trime.data.DataManager
import java.io.File

object FontManager {
val fontDir = File(DataManager.userDataDir, "fonts")

@JvmStatic
fun getTypeface(fontFileName: String): Typeface {
val f = File(fontDir, fontFileName)
return if (f.exists()) {
Typeface.createFromFile(f)
} else {
Typeface.DEFAULT
}
}
}
Expand Up @@ -46,6 +46,7 @@
import com.osfans.trime.R;
import com.osfans.trime.data.AppPrefs;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.databinding.KeyboardKeyPreviewBinding;
import com.osfans.trime.ime.enums.KeyEventType;
import com.osfans.trime.ime.lifecycle.CoroutineScopeJava;
Expand Down Expand Up @@ -393,11 +394,11 @@ public void reset() {
mPreviewText.setTextSize(mPreviewTextSizeLarge);
mShowPreview = getPrefs().getKeyboard().getPopupKeyPressEnabled();

mPaint.setTypeface(config.getFont("key_font"));
mPaintSymbol.setTypeface(config.getFont("symbol_font"));
mPaint.setTypeface(FontManager.getTypeface(config.style.getString("key_font")));
mPaintSymbol.setTypeface(FontManager.getTypeface(config.style.getString("symbol_font")));
mPaintSymbol.setColor(key_symbol_color);
mPaintSymbol.setTextSize(mSymbolSize);
mPreviewText.setTypeface(config.getFont("preview_font"));
mPreviewText.setTypeface(FontManager.getTypeface(config.style.getString("preview_font")));

mEnterLabels = config.getmEnterLabels();
enterLabelMode = config.style.getInt("enter_label_mode");
Expand Down
Expand Up @@ -13,6 +13,7 @@
import androidx.recyclerview.widget.RecyclerView;
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.databinding.LiquidKeyItemBinding;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -110,8 +111,8 @@ public ViewHolder(@NonNull LiquidKeyItemBinding binding) {
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
final Rime.RimeCandidate candidate = mCandidates.get(position);

final Typeface candidateFont = theme.getFont("candidate_font");
final Typeface commentFont = theme.getFont("comment_font");
final Typeface candidateFont = FontManager.getTypeface(theme.style.getString("candidate_font"));
final Typeface commentFont = FontManager.getTypeface(theme.style.getString("comment_font"));
holder.candidate.setTypeface(candidateFont);
holder.comment.setTypeface(commentFont);

Expand Down
Expand Up @@ -13,6 +13,7 @@ import com.osfans.trime.R
import com.osfans.trime.data.db.CollectionHelper
import com.osfans.trime.data.db.DatabaseBean
import com.osfans.trime.data.theme.Config
import com.osfans.trime.data.theme.FontManager
import com.osfans.trime.databinding.SimpleKeyItemBinding
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -62,7 +63,7 @@ class FlexibleAdapter(
val bean = mBeans[position]
simpleKeyText.apply {
text = bean.text
typeface = theme.getFont("long_text_font")
typeface = FontManager.getTypeface(theme.style.getString("long_text_font"))
when (val textColor = theme.getLiquidColor("long_text_color")) {
null -> setTextColor(theme.getLiquidColor("key_text_color"))
else -> setTextColor(textColor)
Expand Down
Expand Up @@ -9,6 +9,7 @@
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.databinding.SimpleKeyItemBinding;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.simpleKeyText.setText(bean.getLabel());
holder.simpleKeyText.setTextColor(theme.getLiquidColor("key_text_color"));
holder.simpleKeyText.setTypeface(theme.getFont("key_font"));
holder.simpleKeyText.setTypeface(FontManager.getTypeface(theme.style.getString("key_font")));
holder.simpleKeyText.setGravity(Gravity.CENTER);
holder.simpleKeyText.setEllipsize(TextUtils.TruncateAt.MARQUEE);

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/osfans/trime/ime/symbol/TabView.java
Expand Up @@ -31,6 +31,7 @@
import android.view.ViewGroup.LayoutParams;
import androidx.annotation.NonNull;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.ime.enums.SymbolKeyboardType;
import com.osfans.trime.util.DimensionsKt;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void reset() {
int candidateTextSize = (int) DimensionsKt.dp2px(config.style.getFloat("candidate_text_size"));
candidateViewHeight = (int) DimensionsKt.dp2px(config.style.getFloat("candidate_view_height"));

candidateFont = config.getFont("candidate_font");
candidateFont = FontManager.getTypeface(config.style.getString("candidate_font"));

candidatePaint.setTextSize(candidateTextSize);
candidatePaint.setTypeface(candidateFont);
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/osfans/trime/ime/text/Candidate.java
Expand Up @@ -36,6 +36,7 @@
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.AppPrefs;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.util.DimensionsKt;
import com.osfans.trime.util.GraphicUtils;
Expand Down Expand Up @@ -105,9 +106,9 @@ public void reset() {
candidateViewHeight = (int) DimensionsKt.dp2px(config.style.getFloat("candidate_view_height"));
commentHeight = (int) DimensionsKt.dp2px(config.style.getFloat("comment_height"));

candidateFont = config.getFont("candidate_font");
commentFont = config.getFont("comment_font");
symbolFont = config.getFont("symbol_font");
candidateFont = FontManager.getTypeface(config.style.getString("candidate_font"));
commentFont = FontManager.getTypeface(config.style.getString("comment_font"));
symbolFont = FontManager.getTypeface(config.style.getString("symbol_font"));

candidatePaint.setTextSize(candidate_text_size);
candidatePaint.setTypeface(candidateFont);
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/osfans/trime/ime/text/Composition.java
Expand Up @@ -41,6 +41,7 @@
import androidx.appcompat.widget.AppCompatTextView;
import com.osfans.trime.core.Rime;
import com.osfans.trime.data.theme.Config;
import com.osfans.trime.data.theme.FontManager;
import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.ime.keyboard.Event;
import com.osfans.trime.util.ConfigGetter;
Expand Down Expand Up @@ -273,10 +274,10 @@ public void reset() {
sticky_lines_land = config.style.getInt("layout/sticky_lines_land");
movable = config.style.getString("layout/movable");
all_phrases = config.style.getBoolean("layout/all_phrases");
tfLabel = config.getFont("label_font");
tfText = config.getFont("text_font");
tfCandidate = config.getFont("candidate_font");
tfComment = config.getFont("comment_font");
tfLabel = FontManager.getTypeface(config.style.getString("label_font"));
tfText = FontManager.getTypeface(config.style.getString("text_font"));
tfCandidate = FontManager.getTypeface(config.style.getString("candidate_font"));
tfComment = FontManager.getTypeface(config.style.getString("comment_font"));
}

private Object getAlign(Map<String, Object> m) {
Expand Down
12 changes: 4 additions & 8 deletions app/src/main/java/com/osfans/trime/util/GraphicUtils.kt
Expand Up @@ -4,22 +4,18 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import com.osfans.trime.data.theme.Config
import com.osfans.trime.data.theme.FontManager

class GraphicUtils {
private var hanBFont: Typeface = Typeface.DEFAULT
private var latinFont: Typeface = Typeface.DEFAULT
private val theme = Config.get()
private val hanBFont = FontManager.getTypeface(theme.style.getString(HAN_B_FONT))
private val latinFont = FontManager.getTypeface(theme.style.getString(LATIN_FONT))

companion object {
const val HAN_B_FONT = "hanb_font"
const val LATIN_FONT = "latin_font"
}

init {
val imeConfig = Config.get()
hanBFont = imeConfig.getFont(HAN_B_FONT)
latinFont = imeConfig.getFont(LATIN_FONT)
}

private fun determineTypeface(codePoint: Int, font: Typeface): Typeface {
return if (hanBFont != Typeface.DEFAULT && Character.isSupplementaryCodePoint(codePoint)) {
hanBFont
Expand Down

0 comments on commit 37a305c

Please sign in to comment.