我正在尝试做一些基于句子中字符数量的动态规划。英语字母表中哪个字母在屏幕上占像素最多?


当前回答

这取决于字体。我会用你最熟悉的编程语言创建一个小程序,把字母表中的每个字母画成n乘以m的位图。用白色初始化每个像素。然后,在你画完每个字母后,数一数白色像素的数量,并保存这个数字。你找到的最大的数字就是你要找的数字。

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

其他回答

我认为字母W是最宽的。

想知道真正最长的字形,而不仅仅是猜测? 我说的不仅仅是字母、数字和常用符号(!, @等等)。我指的是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中最长的字符! 请注意,在大多数字体上,最长的字形是﷽,但有些字体(特别是单行字体)与运行程序时使用的字体重叠。

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

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));

那么程序化的解决方案呢?

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 < capsIndex + 26; 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); } } for(var i = smallIndex; i < smallIndex + 26; 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';

或者如果你想要一个宽度的映射,包含不止上面描述的alpha(数字)字符(就像我在非浏览器环境中需要的那样)

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 */ }