如何检查字符是否将显示在浏览器中 [英] How to check if a character will display in the browser

查看:145
本文介绍了如何检查字符是否将显示在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要显示⬀,⤴或a等特殊字符,如何查看用户的浏览器是否可以显示?我不想让角色出来作为▯;

If I want to display a special character like ⬀, ⤴ or ➶, How can I see if the user's browser can display it? I don't want the character to come out as ▯; if it would, I'd rather display a normal ↗.

那么如何确保我发出的任何字符都能正确显示?

So how can I ensure that whatever character I emit is displayed correctly?

我找到此回答这个问题,不幸的是,在我的情况下不工作。

诀窍是把一个不存在于DOM树中的字符,所以你知道它不会显示,并将其宽度与所需字符的宽度进行比较。如果宽度相同,那么您期望的字符也是未定义的,它也不会显示。

I found this answer to this question, which unfortunately doesn't work in my situation.
The trick there is to put a character that doesn't exist in the DOM tree, so you know it won't display, and to compare its width to the width of your desired character. If the widths are the same, chances are your desired character is also undefined and it won't display either.

但是,这个技巧在所有情况下都不起作用。链接答案对非显示字符使用U + FFFD,但在某些设备(在我的情况下,Windows Mobile手机)中,U + FFFD是一个定义字符,其宽度不同于用于未定义字符的字形 vs▯)。此外,已知非字符的编码点,如U + FFFE,显示为问号(?)而不是▯,所以它们也有不同的宽度。

However, this trick doesn't work in all situations. The linked answer uses U+FFFD for a non-displaying character, but on some devices (in my case, a Windows Mobile phone) the U+FFFD is a defined character, that has a different width than the glyph used for undefined characters (� vs ▯). Also, codepoints that are known non-characters, such as U+FFFE, are displayed as question marks (?) rather than ▯s, so those have different widths too.

所以我的问题是,我可以比较我的角色对比,所以我会知道它不会是一个不可显示的字符。

So my question is, what can I compare my character against so that I'll know it will not be a non-displayable character?

推荐答案

使用多个字符



假设您不使用等宽字体,
使用不同语言的3+个字符,

Use more than one character

Say you're not using a monospace font, Use 3+ characters from different languages that chances are no one have them all installed.

然后在可以渲染它们的环境中测量它们的宽度,并将这些数据提供给您的应用程序。

Then measure their width in an environment that can render them and provide this data to your app.

例如:


  • A比B

  • C

一个简单的算法是:


  1. 在用户环境中收集A,B和C的宽度。

    使用您描述的技术。

  1. Gather A, B and C's width in users environment.
    Use the technique you described.

比B大,B大于C.

后退到安全字符。

If A is wider than B, B wider than C.
Fallback to safe character.

如果一个或所有这些都不在右

使用那个作为▯。

If One or All of these isn't in right order.
Use that one as ▯.



结论



我知道这是基本的,但这是我能想到的最好的方法。

Conclusion

I understand this is basic, but this is best way I can think of.

这篇关于如何检查字符是否将显示在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆