Fonts 如何比较不同权重的OpenType字体,看它们是否相同?
我有一堆不同权重和风格的OpenType字体文件(例如,ComicSans100.otf、ComicSans200.otf、ComicSans300.otf、TimeNewRoman.otf和TimeNewRomanItalic.otf)。提供这些文件的人不确定不同字体的重量和样式是否被修改。例如,Fonts 如何比较不同权重的OpenType字体,看它们是否相同?,fonts,opentype,ttx-fonttools,Fonts,Opentype,Ttx Fonttools,我有一堆不同权重和风格的OpenType字体文件(例如,ComicSans100.otf、ComicSans200.otf、ComicSans300.otf、TimeNewRoman.otf和TimeNewRomanItalic.otf)。提供这些文件的人不确定不同字体的重量和样式是否被修改。例如,ComicSans400.otf中的字符来自权重为400的ComicSans100.otf,但经过调整后看起来更好 我想知道是否有办法确保,如果我使用ComicSans100.otf并将其应用为400
ComicSans400.otf
中的字符来自权重为400的ComicSans100.otf
,但经过调整后看起来更好
我想知道是否有办法确保,如果我使用ComicSans100.otf并将其应用为400的权重,所有字符将与ComicSans400.otf中的字符看起来相同
我问这个问题的原因是我想在Android应用程序中使用这些字体。每种字体都会增加应用程序的大小。这里有一个简单直观的方法来验证两种字体是否产生相同的字符
文本视图定义一个ConstraintLayout
:tv1
,tv2
,
tv3
和tv4
。按如下方式定义文本颜色和字体。你
正在相互检查font1和font2。(确保文本视图的背景是透明的
tv1
:红色,字体1
tv2
:蓝色,font2
tv3
:蓝色,font2
tv4
:红色,字体1
tv1
置于tv2
之上,将tv3
置于tv4
之上文本视图中。查找与文本颜色不匹配的任何颜色
顶部TextView
。对于tv1
,在tv2
顶部,您应该看到所有红色
没有蓝色。对于tv3
,在tv4
上方,您应该看到所有蓝色和蓝色
没有红色
<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>
如果要检查的字符可能很多,则上述方法将很困难且容易出错。更自动化的方法是定义两个如上所述的文本视图,使用相同的文本加载它们,但使用两种字体进行测试
MainActivity.java
下面是一段简短的代码,它获取两个文本视图,逐像素比较它们,并记录它们是否不同或相同
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();
}
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终文本视图tv1、tv2;
tv1=findViewById(R.id.tv1);
tv2=findViewById(R.id.tv2);
//获取要签入字符串的所有字符。
字符串s=getTextToCheck();
tv1.setText(s);
tv2.setText(s);
最终约束layout mLayout=findViewById(R.id.layout);
mLayout.post(新的Runnable(){
@凌驾
公开募捐{
位图Bitmap=Bitmap.createBitmap(mLayout.getWidth(),mLayout.getHeight(),
位图.Config.ARGB_8888);
画布=新画布(位图);
绘制(画布);
比较(位图、getViewRect(tv1)、getViewRect(tv2));
}
});
}
专用空比较器(位图、Rect rect1、Rect rect2){
int x1=rect1.left;
int x2=rect2.left;
如果(rect1.width()!=rect2.width()){
Log.i(“CompareFonts”,“手动分析字体的工具是
要进行比较,请先并排打开类似字体:
您可以看到我的两种字体是A750标准字体(左)和A750粗体字体(右)
接下来,要将粗体与增加权重的常规进行比较,请单击“编辑->全选”,然后单击“元素->样式->更改权重”,您可以直观地比较结果
对于我的示例字体,有一个巨大的差异,所以我将保留这两种字体
在“元素->比较字体”下还有一个比较两种字体的自动功能“,但我认为这不是你想要的。我们在谈论多少种不同的字体?你需要一个全自动的解决方案吗?在这个解决方案中,你的输出类似于字体a与字体B匹配/不匹配?@DanielLerps我有大约12种不同的字体。所以自动化不是必要的。:)我不明白你的意思“如果我使用ComicSans100.otf并将其应用为400的权重”。除非它是可变宽度字体,否则您不会“为字体指定权重”",一种字体只有一个权重,你可以分配你想要的所有内容,但充其量它什么也不做。你需要将每种字体分别绑定到每个权重。没有这个,充其量什么也不会发生,事情也不会改变,但最坏的情况是你现在正在强制使用假加粗/减淡。@Mike'Pomax'Kamermans感谢你的评论。我想我的理解是关于字体如何工作的定义是不正确的。我认为我可以为字体分配权重以增加其“最粗体”。我已经研究了Android框架,但我无法通过编程方式增加字体的权重。因此,这证实了您的说法。:)也许我应该删除我的问题,但同时麦加主义者要比较“cheticamp”中的两种字体非常好。只有当您有一系列字体时,您才能这样做,因为操作系统知道它们,或者因为您指定了多个@font-face
规则,所有这些规则都用于相同的字体系列
,但具有不同的权重/变体/etc属性,针对每个变体指向不同的字体资源。对于