How to correctly calculate width of unicode text?

blov · · 448 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In an attempt to improve visuals of below-mentioned 9t multi-file tail watcher, I tried three methods of calculating width to pad lines for best alignment: naive len(string), utf8.RuneCountInString(string) and <a href="https://github.com/mattn/go-runewidth" rel="nofollow">mattn/go-runewidth.</a>StringWidth(). None of which gave satisfactory result:<br/> <a href="http://i.imgur.com/JRD6JNr.png" rel="nofollow">http://i.imgur.com/JRD6JNr.png</a> len(string) and StringWidth(string)<br/> <a href="http://i.imgur.com/YFXkAyx.png" rel="nofollow">http://i.imgur.com/YFXkAyx.png</a> RuneCountInString(string) </p> <p>Any suggestions about how to handle this, or is this font-related, and without pinging font engine for width of a glyph there is no way to get this information?</p> <hr/>**评论:**<br/><br/>epiris: <pre><p>Strings are simply a sequence of abritrary bytes, to count these bytes you may use len(). As you know UTF8 code points may be encoded into a sequence bytes, in Go they are called runes. You may use the utf8 packages RuneCountInString method as you demonstrated above to count them.</p> <p>This is pretty simple and it seems you already got it right. What is not simple is alignment in the context of your screenshot. First I&#39;ll say I don&#39;t recognize your terminal emulator, but it almost seems like some hanky JavaScript web GUI terminal emulator in a emulator thing. If that&#39;s the case alignment isn&#39;t even worth trying to pursue, just count runes and be sure your implementation is correct. Check the results without multi byte sequences. Then make sure all your multi byte sequences are valid. Then verify your monospace font is truly valid by using another known correct reader with your inputs. Finally try with a different terminal emulator. At some point you will find where the problem is. Have &#34;fun&#34;.. lol</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

448 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传