如何从Canvas中的宽度测量字体重量

问题描述

我正在开发一个应用程序,其中的画布以特定的文本呈现。字体粗细和颜色不同。我能够获得颜色,但是我必须以某种方式计算字体粗细。画布正在使用ctx.measureText(str).width我有关于字体的数据。

{
   "alphaIsShape":false,"fontSize":1,"fontSizeScale":0.0625,"textMatrix":[
      12,12,607.2912,515.4544
   ],"textMatrixScale":12,"fontMatrix":[
      0.001,0.001,0
   ],"leading":0,"x":0,"y":0,"lineX":0,"lineY":0,"charSpacing":0,"wordSpacing":0,"textHScale":1,"textRenderingMode":0,"textRise":0,"fillColor":"rgb(255,120,48)","strokeColor":"#000000","patternFill":false,"fillAlpha":1,"strokeAlpha":1,"linewidth":1,"activeSMask":null,"resumeSMaskCtx":null,"transferMaps":null,"fontDirection":1,"font":{
      "compiledGlyphs":{
         
      },"ascent":1.224,"bBox":[
         -84,-267,1565,1224
      ],"composite":false,"data":null,"defaultWidth":0,"descent":-0.267,"fallbackName":"sans-serif","fontMatrix":[
         0.001,0
      ],"fontType":"TYPE1C","isMonospace":false,"isSerifFont":false,"isType3Font":false,"loadedname":"g_d0_f1","mimetype":"font/opentype","missingFile":false,"name":"CKHZSJ+Fordantenna-Regular","subtype":"Type1C","type":"Type1","vertical":false,"isEvalSupported":true,"disableFontFace":false,"ignoreErrors":true,"fontRegistry":null,"attached":true
   }
}

我也尝试使用以下公式来计算它,但是该值不是很准确。

((ctx.measureText(character).width * 1000) / fontSize) * fontSizeScale

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)