如何比较不同字重的OpenType字体是否相同?
How to compare OpenType fonts with different weight to see if they are the same?
我有一堆不同粗细和样式的 OpenType 字体文件(例如,ComicSans100.otf、ComicSans200.otf、ComicSans300.otf 和 TimeNewRoman.otf 和 TimesNewRomanItalic.otf).向我提供这些文件的人不确定是否修改了不同字体的粗细和样式。例如,ComicSans400.otf
中的字符来自 ComicSans100.otf
,权重为 400,但进行了调整以使其看起来更好。
我想知道是否有办法确保如果我采用 ComicSans100.otf 并为其应用 400 的权重,所有字符看起来都与 ComicSans400.otf 中的字符相同.
我问这个的原因是我想在 Android 应用程序中使用这些字体。而且每种字体都会增加应用程序的大小。
这是一种简单直观的方法来验证您的两种字体是否产生相同的字符。
用四个TextViews
定义一个ConstraintLayout
:tv1
, tv2
,
tv3
和 tv4
。如下定义文本颜色和字体。你
正在相互检查 font1 和 font2。 (确保 TextViews
的背景是透明的。
tv1
: 红色, font1
tv2
: 蓝色, font2
tv3
: 蓝色, font2
tv4
: 红色, font1
将 tv1
置于 tv2
之上,将 tv3
置于 tv4
之上。
将您要检查的所有字符放入每个 TextViews
。寻找与颜色不匹配的任何颜色
顶部 TextView
。对于 tv2
之上的 tv1
你应该看到所有红色
没有蓝色。对于 tv4
之上的 tv3
你应该看到所有的蓝色和
没有红色.
这可以是自动化的,但如果简单的设置和目视检查就足够了,那么可能不值得付出努力。一个有价值的自动化可能是寻找有问题的颜色的像素。
布局可能是这样的。这个简单的案例只是将默认字体视为粗体而不是粗体。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_blue_light"
android:textSize="20sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline" />
<TextView
android:id="@+id/tv4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_blue_light"
android:textSize="20sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.50" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是查看差异的输出。此快照来自 Android Studio 设计师。
如果要检查的字符可能很多,上述方法会比较困难,容易出错。一种更自动化的方法是像上面那样定义两个文本视图,用相同的文本加载它们,但使用将相互测试的两种字体。
MainActivity.java
下面是一小段代码,它采用两个 TextView 并逐个像素地比较它们并记录它们是否不同或相同。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv1, tv2;
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
// Get all characters to check into a string.
String s = getTextToCheck();
tv1.setText(s);
tv2.setText(s);
final ConstraintLayout mLayout = findViewById(R.id.layout);
mLayout.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(mLayout.getWidth(), mLayout.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mLayout.draw(canvas);
compareRects(bitmap, getViewRect(tv1), getViewRect(tv2));
}
});
}
private void compareRects(Bitmap bitmap, Rect rect1, Rect rect2) {
int x1 = rect1.left;
int x2 = rect2.left;
if (rect1.width() != rect2.width()) {
Log.i("CompareFonts", "<<<< TextView widths do not match");
}
if (rect1.height() != rect2.height()) {
Log.i("CompareFonts", "<<<< TextView heights do not match");
}
int totalPixels = 0;
int diffCount = 0;
while (x1 < rect1.right && x2 < rect2.right) {
int y1 = rect1.top;
int y2 = rect2.top;
while (y1 < rect1.bottom && y2 < rect2.bottom) {
int pixel1 = bitmap.getPixel(x1, y1);
int pixel2 = bitmap.getPixel(x2, y2);
if (pixel1 != pixel2) {
diffCount++;
totalPixels++;
} else if (pixel1 != 0) {
totalPixels++;
}
y1++;
y2++;
}
x1++;
x2++;
}
Log.i("CompareFonts", String.format(Locale.US, "<<<< Total pixels compared = %,d", totalPixels));
Log.i("CompareFonts", String.format(Locale.US, "<<<< Different pixel count = %,d (%%%.2f) ",
diffCount, (float) diffCount * 100 / totalPixels));
}
private Rect getViewRect(View view) {
Rect rect = new Rect();
rect.left = view.getLeft() + view.getPaddingLeft();
rect.right = view.getRight() - view.getPaddingRight();
rect.top = view.getTop() + view.getPaddingTop();
rect.bottom = view.getBottom() - view.getPaddingBottom();
return rect;
}
private String getTextToCheck() {
// Define any text to check. This is just the printable ASCII character set.
StringBuilder sb = new StringBuilder();
for (int i = 32; i <= 126; i++) {
sb.append((char) i);
}
return sb.toString();
}
}
手动分析字体的首选工具是FontForge。
要进行比较,请先并排打开相似的字体:
大家可以看到我的两种字体分别是A750Sant-Regular(左)和A750Sans-Bold(右)。
接下来,要比较粗体和加粗的常规,点击"Edit -> Select All",然后点击"Element -> Style -> Change Weight",你可以直观地比较结果。
对于我的示例字体,存在巨大差异,所以我会保留两者。
在"Element -> Compare Fonts"下还有一个比较两种字体的自动功能,但我认为这不是你要找的。
我有一堆不同粗细和样式的 OpenType 字体文件(例如,ComicSans100.otf、ComicSans200.otf、ComicSans300.otf 和 TimeNewRoman.otf 和 TimesNewRomanItalic.otf).向我提供这些文件的人不确定是否修改了不同字体的粗细和样式。例如,ComicSans400.otf
中的字符来自 ComicSans100.otf
,权重为 400,但进行了调整以使其看起来更好。
我想知道是否有办法确保如果我采用 ComicSans100.otf 并为其应用 400 的权重,所有字符看起来都与 ComicSans400.otf 中的字符相同.
我问这个的原因是我想在 Android 应用程序中使用这些字体。而且每种字体都会增加应用程序的大小。
这是一种简单直观的方法来验证您的两种字体是否产生相同的字符。
用四个
TextViews
定义一个ConstraintLayout
:tv1
,tv2
,tv3
和tv4
。如下定义文本颜色和字体。你 正在相互检查 font1 和 font2。 (确保TextViews
的背景是透明的。tv1
: 红色, font1tv2
: 蓝色, font2tv3
: 蓝色, font2tv4
: 红色, font1
将
tv1
置于tv2
之上,将tv3
置于tv4
之上。将您要检查的所有字符放入每个
TextViews
。寻找与颜色不匹配的任何颜色 顶部TextView
。对于tv2
之上的tv1
你应该看到所有红色 没有蓝色。对于tv4
之上的tv3
你应该看到所有的蓝色和 没有红色.
这可以是自动化的,但如果简单的设置和目视检查就足够了,那么可能不值得付出努力。一个有价值的自动化可能是寻找有问题的颜色的像素。
布局可能是这样的。这个简单的案例只是将默认字体视为粗体而不是粗体。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_blue_light"
android:textSize="20sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline" />
<TextView
android:id="@+id/tv4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="ABCEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="@android:color/holo_blue_light"
android:textSize="20sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.50" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是查看差异的输出。此快照来自 Android Studio 设计师。
如果要检查的字符可能很多,上述方法会比较困难,容易出错。一种更自动化的方法是像上面那样定义两个文本视图,用相同的文本加载它们,但使用将相互测试的两种字体。
MainActivity.java
下面是一小段代码,它采用两个 TextView 并逐个像素地比较它们并记录它们是否不同或相同。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv1, tv2;
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
// Get all characters to check into a string.
String s = getTextToCheck();
tv1.setText(s);
tv2.setText(s);
final ConstraintLayout mLayout = findViewById(R.id.layout);
mLayout.post(new Runnable() {
@Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(mLayout.getWidth(), mLayout.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mLayout.draw(canvas);
compareRects(bitmap, getViewRect(tv1), getViewRect(tv2));
}
});
}
private void compareRects(Bitmap bitmap, Rect rect1, Rect rect2) {
int x1 = rect1.left;
int x2 = rect2.left;
if (rect1.width() != rect2.width()) {
Log.i("CompareFonts", "<<<< TextView widths do not match");
}
if (rect1.height() != rect2.height()) {
Log.i("CompareFonts", "<<<< TextView heights do not match");
}
int totalPixels = 0;
int diffCount = 0;
while (x1 < rect1.right && x2 < rect2.right) {
int y1 = rect1.top;
int y2 = rect2.top;
while (y1 < rect1.bottom && y2 < rect2.bottom) {
int pixel1 = bitmap.getPixel(x1, y1);
int pixel2 = bitmap.getPixel(x2, y2);
if (pixel1 != pixel2) {
diffCount++;
totalPixels++;
} else if (pixel1 != 0) {
totalPixels++;
}
y1++;
y2++;
}
x1++;
x2++;
}
Log.i("CompareFonts", String.format(Locale.US, "<<<< Total pixels compared = %,d", totalPixels));
Log.i("CompareFonts", String.format(Locale.US, "<<<< Different pixel count = %,d (%%%.2f) ",
diffCount, (float) diffCount * 100 / totalPixels));
}
private Rect getViewRect(View view) {
Rect rect = new Rect();
rect.left = view.getLeft() + view.getPaddingLeft();
rect.right = view.getRight() - view.getPaddingRight();
rect.top = view.getTop() + view.getPaddingTop();
rect.bottom = view.getBottom() - view.getPaddingBottom();
return rect;
}
private String getTextToCheck() {
// Define any text to check. This is just the printable ASCII character set.
StringBuilder sb = new StringBuilder();
for (int i = 32; i <= 126; i++) {
sb.append((char) i);
}
return sb.toString();
}
}
手动分析字体的首选工具是FontForge。
要进行比较,请先并排打开相似的字体:
大家可以看到我的两种字体分别是A750Sant-Regular(左)和A750Sans-Bold(右)。
接下来,要比较粗体和加粗的常规,点击"Edit -> Select All",然后点击"Element -> Style -> Change Weight",你可以直观地比较结果。
对于我的示例字体,存在巨大差异,所以我会保留两者。
在"Element -> Compare Fonts"下还有一个比较两种字体的自动功能,但我认为这不是你要找的。