Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript计算给定高度的字体宽度_Javascript_Html - Fatal编程技术网

使用javascript计算给定高度的字体宽度

使用javascript计算给定高度的字体宽度,javascript,html,Javascript,Html,我希望开发一个工具,用于计算给定高度的宽度。它将接受字体系列和字体大小/高度,并返回输入文本的总宽度。 我比较的工具,但宽度似乎是不一样的。下面列出的8英寸高/字体大小的结果很少,输入的单词是“自定义”:- 1英寸=96像素 在我针对这些字体系列的工具中: Bhatoshine——宽21.46英寸 麦迪纳清洁型——西20.14英寸 布鲁克林灯光——西28.38英寸 学院——西经24.61英寸 在Inkscape桌面工具中: 巴托辛——宽21.278英寸 麦迪纳清洁型——17.015“W 布鲁克林

我希望开发一个工具,用于计算给定高度的宽度。它将接受字体系列和字体大小/高度,并返回输入文本的总宽度。 我比较的工具,但宽度似乎是不一样的。下面列出的8英寸高/字体大小的结果很少,输入的单词是“自定义”:- 1英寸=96像素

在我针对这些字体系列的工具中:

Bhatoshine——宽21.46英寸

麦迪纳清洁型——西20.14英寸

布鲁克林灯光——西28.38英寸

学院——西经24.61英寸

在Inkscape桌面工具中:

巴托辛——宽21.278英寸

麦迪纳清洁型——17.015“W

布鲁克林灯光——西38.237英寸

学院——西34.857英寸

我根据这个例子创建了这个工具

我无法理解他们用来计算这个的机制。或者这种差异是因为桌面和网页中字体的行为

任何帮助都将不胜感激


谢谢

如果这有帮助,请参见下面的内容。我正在使用PHP函数尝试以下操作:-

function CalculateWidth($text, $InchHeight, $font)
    {

     $height_px = $InchHeight*96;
     $height_px_min = $height_px - 10;
     $height_px_max = $height_px + 10;  
     $OutputArray = array();

     for($i=0;$i<10000;$i++){
        $box = imagettfbbox($i, 0, $font, $text);
      $width = abs($box[2] - $box[0]);
      $height = abs($box[7] - $box[1]);
      if($height > $height_px_min && $height <= $height_px_max){
        $OutputArray[]= number_format($width/96, 2);
      }

 }
 // REMOVE EMPTY VALUES
  $OutputArray = array_filter($OutputArray);

 //CALCULATE AVG OF ALL VALUES
 return $average = number_format(array_sum($OutputArray)/count($OutputArray),2);
}
函数计算宽度($text,$InchHeight,$font)
{
$height_px=$InchHeight*96;
$height_px_min=$height_px-10;
$height_px_max=$height_px+10;
$OutputArray=array();

对于($i=0;$i$height\u px\u min&&$height,我可以在变量中进行一些修正,至少可以达到我想要的效果。