简体   繁体   English

固定宽度字符的确切宽度和高度

[英]exact width and height of fixed-width character

I am working on a text widget that needs to have fixed with font for displaying tables and such. 我正在处理一个文本小部件,该小部件需要固定字体才能显示表格等。 To be able to count the amount of character that fit into a given window I need to have the height and width of the font that I am using. 为了能够计算适合给定窗口的字符数,我需要具有所用字体的高度和宽度。 I whipped up a quick function that calculates it by creating an element with a single character and get the height and width of that element. 我创建了一个快速函数,该函数通过创建一个具有单个字符的元素来进行计算并获得该元素的高度和宽度。 The problem is that this width and height are integer not floats. 问题在于此宽度和高度是整数而不是浮点数。 For instance if I call this function on chrome with 1em fontSize and fontFamily monospace . 例如,如果我在带有1em fontSize和fontFamily monospace chrome上调用此函数。 I get width 8 and height 15. The height value is correct but the width is actually something like 7.14... This doesn't seem much but when you start to have 100 characters on a line you can see that the offset becomes more than 80 pixels. 我得到宽度8和高度15。高度值是正确的,但宽度实际上是7.14左右……这似乎并不多,但是当一行上开始有100个字符时,您可以看到偏移量大于80像素

Is there a way to get the accurate values in some reliable way? 有没有办法以某种可靠的方式获得准确的值?

and please no jquery.. 而且请不要使用jQuery。

here is the function: 这是函数:

function getCharDimensions(fontFamily, fontSize) {
    var body = document.getElementsByTagName('body');
    var el = document.createElement("span");
    el.style.position = "absolute";
    el.style.top = "-100%"
    el.style.fontFamily = fontFamily;
    el.style.fontSize = fontSize;
    el.innerHTML = "x";
    body[0].appendChild(el);
    var dimensions = {
        height: el.offsetHeight,
        width: el.offsetWidth
    };
    el.parentNode.removeChild(el);
    return dimensions;
}

You are not consideering the tracking and the kerning of the typography. 您不考虑排版的跟踪和字距调整。 All characteres have a tracking defined to adjust it to other characters and be more readable. 所有字符都具有定义的跟踪,以将其调整为其他字符并且更具可读性。

In this case this is not the only thing that affects you, it's because CSS can't read decimal pixels in a normal screen with device pixel width 1. So 1.5px doesn't exists. 在这种情况下,这并不是唯一影响您的因素,这是因为CSS无法在设备像素宽度为1的普通屏幕中读取小数像素。因此1.5px不存在。

If you detect a pattern you can apply to the font size and remove the tracking in your letter, and you can apply the 7.14px because you can make a sum with it and it will be more accuracy than now. 如果检测到图案,则可以应用字体大小并删除字母中的跟踪,并且可以应用7.14px因为您可以用它求和,并且比现在更准确。

Good luck! 祝好运!

EDIT 编辑

As a improvement, you can define the character you are looking for the width and height, because an i have less width than a m (yes, I know that you are playing with monospace, but this is a good improvement). 作为一项改进,您可以定义要查找的宽度和高度的字符,因为i宽度小于m宽度(是的,我知道您正在使用等宽字符,但这是一个很好的改进)。

 function getCharDimensions(character, fontFamily, fontSize) {
       //stuff
       el.innerHTML = character;
       //stuff
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM