Skip to content

Commit

Permalink
refactor(text): split code related to font customization
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Oct 23, 2021
1 parent b2580d9 commit 3f8824d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 59 deletions.
65 changes: 6 additions & 59 deletions app/src/main/java/com/osfans/trime/ime/symbol/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.PaintDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import androidx.annotation.NonNull;

import com.osfans.trime.ime.core.Trime;
import com.osfans.trime.ime.enums.SymbolKeyboardType;
import com.osfans.trime.setup.Config;
import com.osfans.trime.util.GraphicUtils;

import timber.log.Timber;

// 这是滑动键盘顶部的view,展示了键盘布局的多个标签。
Expand All @@ -47,13 +46,12 @@ public class TabView extends View {

private int highlightIndex;
private TabTag[] tabTags;
private final GraphicUtils graphicUtils;

private PaintDrawable candidateHighlight;
private final Paint separatorPaint;
private final Paint candidatePaint;
private Typeface candidateFont;
private Typeface hanBFont;
private Typeface latinFont;
private int candidateTextColor, hilitedCandidateTextColor;
private int candidateViewHeight, commentHeight, candidateSpacing, candidatePadding;
private final boolean shouldShowComment = true;
Expand Down Expand Up @@ -81,8 +79,6 @@ public void reset(Context context) {
candidateViewHeight = config.getPixel("candidate_view_height");

candidateFont = config.getFont("candidate_font");
latinFont = config.getFont("latin_font");
hanBFont = config.getFont("hanb_font");

candidatePaint.setTextSize(candidateTextSize);
candidatePaint.setTypeface(candidateFont);
Expand All @@ -101,6 +97,7 @@ public TabView(Context context, AttributeSet attrs) {
separatorPaint = new Paint();
separatorPaint.setColor(Color.BLACK);

graphicUtils = new GraphicUtils(context);
reset(context);

setWillNotDraw(false);
Expand All @@ -125,34 +122,6 @@ public int getHightlightRight() {
return tabGeometries[highlightIndex].right;
}

private Typeface getFont(int codepoint, Typeface font) {
if (hanBFont != Typeface.DEFAULT && Character.isSupplementaryCodePoint(codepoint)) return hanBFont;
if (latinFont != Typeface.DEFAULT && codepoint < 0x2e80) return latinFont;
return font;
}

private void drawText(
String s, Canvas canvas, Paint paint, Typeface font, float center, float y) {
if (TextUtils.isEmpty(s)) return;
int codePoints = s.codePointCount(0, s.length());
float x = center - measureText(s, paint, font) / 2;
if (latinFont != Typeface.DEFAULT || (hanBFont != Typeface.DEFAULT && s.length() > codePoints)) {
int offset = 0;
while (offset < s.length()) {
int codePoint = s.codePointAt(offset);
int charCount = Character.charCount(codePoint);
int end = offset + charCount;
paint.setTypeface(getFont(codePoint, font));
canvas.drawText(s, offset, end, x, y, paint);
x += paint.measureText(s, offset, end);
offset = end;
}
} else {
paint.setTypeface(font);
canvas.drawText(s, x, y, paint);
}
}

private void drawCandidates(Canvas canvas) {
if (tabTags == null) return;

Expand All @@ -166,7 +135,7 @@ private void drawCandidates(Canvas canvas) {

candidatePaint.setColor(
isHighlighted(i) ? hilitedCandidateTextColor : candidateTextColor);
drawText(getTabText(i), canvas, candidatePaint, candidateFont, x, y);
graphicUtils.drawText(canvas, getTabText(i), x, y,candidatePaint, candidateFont);
// Draw the separator at the right edge of each candidate.
canvas.drawRect(
tabGeometries[i].right - candidateSpacing,
Expand Down Expand Up @@ -298,30 +267,8 @@ private String getTabText(int i) {
return "-1";
}

private float measureText(String s, Paint paint, Typeface font) {
if (TextUtils.isEmpty(s)) return 0;
float x = 0;
int codePoints = s.codePointCount(0, s.length());
if (latinFont != Typeface.DEFAULT || (hanBFont != Typeface.DEFAULT && s.length() > codePoints)) {
int offset = 0;
while (offset < s.length()) {
int codepoint = s.codePointAt(offset);
int charCount = Character.charCount(codepoint);
int end = offset + charCount;
paint.setTypeface(getFont(codepoint, font));
x += paint.measureText(s, offset, end);
offset = end;
}
paint.setTypeface(font);
} else {
paint.setTypeface(font);
x += paint.measureText(s);
}
return x;
}

private float getTabWidth(int i) {
String s = getTabText(i);
return s != null ? 2 * candidatePadding + measureText(s, candidatePaint, candidateFont) : 2 * candidatePadding;
return s != null ? 2 * candidatePadding + graphicUtils.measureText(candidatePaint, s, candidateFont) : 2 * candidatePadding;
}
}
76 changes: 76 additions & 0 deletions app/src/main/java/com/osfans/trime/util/GraphicUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.osfans.trime.util

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Typeface
import com.osfans.trime.setup.Config

class GraphicUtils(
context: Context
) {
private var hanBFont: Typeface = Typeface.DEFAULT
private var latinFont: Typeface = Typeface.DEFAULT

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

init {
val imeConfig = Config.get(context)
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
} else if (latinFont != Typeface.DEFAULT && codePoint < 0x2E80) {
latinFont
} else font
}

fun Paint.measureText(text: String, font: Typeface): Float {
if (text.isEmpty()) return 0.0f
val codePoints = text.codePointCount(0, text.length)
var x = 0.0f
if (latinFont != Typeface.DEFAULT
|| (hanBFont != Typeface.DEFAULT && text.length > codePoints)) {
var offset = 0
while (offset < text.length) {
val codePoint = text.codePointAt(offset)
val charCount = Character.charCount(codePoint)
this.typeface = determineTypeface(codePoint, font)
x += this.measureText(text, offset, offset + charCount)
offset += charCount
}
this.typeface = font
} else {
this.typeface = font
x = this.measureText(text)
}
return x
}

fun Canvas.drawText(text: String, centerX: Float, y: Float, paint: Paint, font: Typeface) {
if (text.isEmpty()) return
val codePoints = text.codePointCount(0, text.length)
var x = centerX - paint.measureText(text, font) / 2
if (latinFont != Typeface.DEFAULT
|| (hanBFont != Typeface.DEFAULT && text.length > codePoints)) {
var offset = 0
while (offset < text.length) {
val codePoint = text.codePointAt(offset)
val charCount = Character.charCount(codePoint)
paint.typeface = determineTypeface(codePoint, font)
this.drawText(text, offset, offset + charCount, x, y, paint)
x += paint.measureText(text, offset, offset + charCount)
offset += charCount
}
} else {
paint.typeface = font
this.drawText(text, x, y, paint)
}
}
}

0 comments on commit 3f8824d

Please sign in to comment.