Updated loading of the system fonts (by listing all existing /system/fonts/*.ttf files),

removed adjustment of italic and bold selection for fonts with suffix "-Italic" or "-Bold"
master
Olga Miller 2024-12-29 17:01:16 +01:00
rodzic 53b1f39ca9
commit 5c50012f71
4 zmienionych plików z 74 dodań i 33 usunięć

Wyświetl plik

@ -18,10 +18,12 @@ package om.sstvencoder;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import androidx.annotation.ColorInt;
import androidx.fragment.app.DialogFragment;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
@ -35,6 +37,7 @@ import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
import om.sstvencoder.TextOverlay.Label;
@ -52,9 +55,7 @@ public class EditTextActivity extends AppCompatActivity
public static final String EXTRA = "EDIT_TEXT_EXTRA";
private Label mLabel;
private EditColorMode mEditColor;
private FontFamilySet mFontFamilySet;
private FontFamilySet.FontFamily mSelectedFontFamily;
private List<String> mFontFamilyNameList;
private List<String> mFontFilePathList;
private CheckBox mEditItalic, mEditBold, mEditOutline;
private int mClearTextButtonWidth;
@ -77,7 +78,6 @@ public class EditTextActivity extends AppCompatActivity
mEditBold.setChecked(mLabel.getBold());
mEditItalic.setChecked(mLabel.getItalic());
initFontFamilySpinner(mLabel.getFamilyName());
updateBoldAndItalic();
mEditOutline.setChecked(mLabel.getOutline());
initOutlineSizeSpinner(mLabel.getOutlineSize());
findViewById(R.id.edit_color).setBackgroundColor(mLabel.getForeColor());
@ -148,12 +148,20 @@ public class EditTextActivity extends AppCompatActivity
private void initFontFamilySpinner(String familyName) {
Spinner spinner = findViewById(R.id.edit_font_family);
spinner.setOnItemSelectedListener(this);
mFontFamilySet = new FontFamilySet(this);
mSelectedFontFamily = mFontFamilySet.getFontFamily(familyName);
mFontFamilyNameList = mFontFamilySet.getFontFamilyDisplayNameList();
mFontFilePathList = Utility.getSystemFontFilePaths();
mFontFilePathList.add(0, Label.DEFAULT_FONT);
List<String> fontFamilyNameList = getFontNames(mFontFilePathList);
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, mFontFamilyNameList));
spinner.setSelection(mFontFamilyNameList.indexOf(mSelectedFontFamily.displayName));
android.R.layout.simple_spinner_dropdown_item, fontFamilyNameList));
spinner.setSelection(mFontFilePathList.indexOf(familyName));
}
private static List<String> getFontNames(List<String> fontFilePathList) {
List<String> fontNameList = new ArrayList<>();
for (String path : fontFilePathList) {
fontNameList.add(Utility.getFileNameWithoutExtension(path));
}
return fontNameList;
}
private void initTextSizeSpinner(float textSize) {
@ -219,28 +227,7 @@ public class EditTextActivity extends AppCompatActivity
mLabel.setOutlineSize(positionToOutlineSize(position));
}
else if (parentId == R.id.edit_font_family) {
String displayName = mFontFamilyNameList.get(position);
mSelectedFontFamily = mFontFamilySet.getFontFamilyFromDisplayName(displayName);
mLabel.setFamilyName(mSelectedFontFamily.name);
updateBoldAndItalic();
}
}
private void updateBoldAndItalic() {
boolean bold = mSelectedFontFamily.bold;
mEditBold.setEnabled(bold);
findViewById(R.id.text_bold).setEnabled(bold);
if (!mEditBold.isEnabled()) {
mEditBold.setChecked(false);
mLabel.setBold(false);
}
boolean italic = mSelectedFontFamily.italic;
mEditItalic.setEnabled(italic);
findViewById(R.id.text_italic).setEnabled(italic);
if (!mEditItalic.isEnabled()) {
mEditItalic.setChecked(false);
mLabel.setItalic(false);
mLabel.setFamilyName(mFontFilePathList.get(position));
}
}

Wyświetl plik

@ -24,6 +24,7 @@ import java.lang.reflect.Modifier;
public class Label implements Serializable {
public static final float TEXT_SIZE_NORMAL = 2f;
public static final float OUTLINE_SIZE_NORMAL = 0.05f;
public static final String DEFAULT_FONT = "Default";
private String mText;
private float mTextSize, mOutlineSize;
private String mFamilyName;

Wyświetl plik

@ -22,6 +22,9 @@ import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import java.io.File;
import androidx.annotation.NonNull;
class LabelPainter {
@ -130,7 +133,13 @@ class LabelPainter {
private void setPaintSettings(float sizeFactor) {
mPaint.setAlpha(255);
mPaint.setTypeface(Typeface.create(mLabel.getFamilyName(), getTypeface()));
try {
Typeface tf = Typeface.create(
createTypefaceFromFontFile(),
createTypefaceFromFontAttributes());
mPaint.setTypeface(tf);
} catch (Exception ignore) {
}
setTextPaintSettings();
setSizePaintSettings(sizeFactor);
}
@ -151,7 +160,19 @@ class LabelPainter {
mPaint.setStrokeWidth(mLabel.getOutlineSize() * textSize);
}
private int getTypeface() {
private Typeface createTypefaceFromFontFile() {
Typeface typeface = null; // Typeface.DEFAULT
String fontFilePath = mLabel.getFamilyName();
if (!fontFilePath.equalsIgnoreCase(Label.DEFAULT_FONT)) {
File fontFile = new File(fontFilePath);
if (fontFile.exists() && fontFile.canRead())
typeface = Typeface.createFromFile(fontFilePath);
}
return typeface;
}
private int createTypefaceFromFontAttributes() {
int typeface = Typeface.NORMAL;
if (mLabel.getBold() && mLabel.getItalic())

Wyświetl plik

@ -28,10 +28,14 @@ import androidx.core.content.FileProvider;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
final class Utility {
static final String DIRECTORY_SYSTEM_FONTS = "/system/fonts";
@NonNull
static Rect getEmbeddedRect(int w, int h, int iw, int ih) {
Rect rect;
@ -113,4 +117,32 @@ final class Utility {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
static List<String> getSystemFontFilePaths() {
List<String> fontList = new ArrayList<>();
File fontsDir = new File(DIRECTORY_SYSTEM_FONTS);
String fontFileExtension = ".ttf";
if (fontsDir.exists() && fontsDir.isDirectory()) {
File[] files = fontsDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile() && file.getName().endsWith(fontFileExtension)) {
fontList.add(file.getAbsolutePath());
}
}
}
}
return fontList;
}
static String getFileNameWithoutExtension(String filePath) {
File file = new File(filePath);
String fileName = file.getName();
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
return fileName.substring(0, dotIndex);
}
return fileName;
}
}