英文字母中哪个字母占用的像素最多?

221

我正在尝试根据句子中字符数量进行一些动态编程。哪个英文字母在屏幕上占用的像素最多?


17
最多的像素?最宽?最高?哪种字体?你能具体说明吗? - Gregor Brandt
8
那不是取决于所使用的字体吗? - Roberto Aloi
1
哪种字体?小写,大写? - Rocket Ronnie
7
这要看字体,因为你要的是“占用最多像素”的那个。 - Gurzo
7
传统的排版智慧是用 M 或 W 来表示大写字母,用 m 表示小写字母。根据字体不同可能会有所差异,但在大多数情况下都能得到令人满意的效果。 - tripleee
显示剩余3条评论
15个回答

3

想知道真正最长的字形,不要只是瞎猜?
我说的不仅仅是字母、数字和常见符号(!、@等),而是UTF-16所有32,834个字符中最长的字形。
因此,我以@NK的答案为基础写了一个程序解决方案,并进行了修改:

var capsIndex = 65;
var smallIndex = 97;
var div = document.createElement('div');
div.style.float = 'left';
document.body.appendChild(div);
var highestWidth = 0;
var elem;

for(var i = capsIndex; i < 32834; i++) {
    div.innerText = String.fromCharCode(i);
    var computedWidth = window.getComputedStyle(div, null).getPropertyValue("width");
    if(highestWidth < parseFloat(computedWidth)) {
        highestWidth = parseFloat(computedWidth);
        elem = String.fromCharCode(i);
    }
}
div.innerHTML = '<b>' + elem + '</b>' + ' won';

运行此程序并等待(长时间等待),它将输出 ௌ won
这就是UTF-32中最长的字符!请注意,大多数字体中最长的字形是﷽,但某些字体(特别是等宽字体)会重叠字符,例如使用此程序运行的字体。


1
原始问题是关于英文字母的。您可以提出一个单独的问题并回答您的问题。 - user894319twitter
1
@user894319twitter 完成。 - Anonymous
1
愚蠢的管理员无缘无故删除了 https://stackoverflow.com/questions/67025564/which-utf-8-character-is-the-widest - user894319twitter
1
@user894319twitter 是的,他们删除了有关该删除的元帖,并在不久之后关闭了这个问题。只是好奇,如果那个问题被删除了,你是怎么看到它的? - Anonymous
我此刻只看到这个,没有任何诡计:页面未找到。这个问题因管理原因已从Stack Overflow移除。 - user894319twitter

1

这将取决于字体。我会使用你最熟悉的编程语言创建一个小程序,在其中将字母表中的每个字母绘制成n乘m大小的位图。将每个像素初始化为白色。然后在绘制每个字母后计算白色像素的数量,并保存该数字。您找到的最高数字就是您要寻找的。

编辑:如果您实际上只对哪个占用最大矩形感兴趣(但看起来您真正想要的不是像素),则可以使用各种API调用来查找大小,但这取决于您的编程语言。例如,在Java中,您将使用FontMetrics类。


0

这段代码将以数组的形式获取所有字符的宽度:

        const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var widths = [];
        
        var div = document.createElement('div');
        div.style.float = 'left';
        document.body.appendChild(div);
        
        var highestObservedWidth = 0;
        // widest characters (not just one)
        var answer = '';
        
        for (var i = 0; i < alphabet.length; i++) {
            div.innerText = alphabet[i];
            var computedWidthString = window.getComputedStyle(div, null).getPropertyValue("width");
            var computedWidth = parseFloat(computedWidthString.slice(0, -2));
    //      console.log(typeof(computedWidth));
            widths[i] = computedWidth;
            if(highestObservedWidth == computedWidth) {
                answer = answer + ', ' + div.innerText;
            }
            if(highestObservedWidth < computedWidth) {
                highestObservedWidth = computedWidth;
                answer = div.innerText;
            }
        }
        if (answer.length == 1) {
            div.innerHTML = ' Winner: ' + answer + '.';
        } else {
            div.innerHTML = ' Winners: ' + answer + '.';
        }
        div.innerHTML = div.innerHTML ;
//      console.log(widths);
//      console.log(widths.sort((a, b) => a - b));


0

或者,如果你想要一个宽度映射表,并包含除了上述描述的字母数字字符之外的更多字符(就像我在非浏览器环境中所需的那样)

const chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "\"", "#", "$", "%", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", " ", "&", ">", "<"]

const test = document.createElement('div')
test.id = "Test"
document.body.appendChild(test)
test.style.fontSize = 12

const result = {}

chars.forEach(char => {
  let newStr = ""
  for (let i = 0; i < 10; i++) {
    if (char === " ") {
      newStr += "&nbsp;"
    } else {
      newStr += char
    }  
  }
  test.innerHTML = newStr
  const width = (test.clientWidth)
  result[char] = width / 10
})

console.log('RESULT:', result)
#Test
{
    position: absolute;
    /* visibility: hidden; */
    height: auto;
    width: auto;
    white-space: nowrap; /* Thanks to Herb Caudill comment */
}


-2

这取决于字体。例如,交叉的零比普通的零占用更多的空间。

但如果要猜的话,我会选择X或B。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接