我正在尝试在我的 Node.js 服务器上生成包含从右到左文本的图像。
我目前正在使用node-canvas(基于“Cairo”),但欢迎建议,以防其他库可以实现这一点。
需要明确的是:
- 我需要支持才能从右向左书写文本
- 实际的字体和字体的方向都与这个问题无关。 E.G:我不介意在我的软件中倒着写单词,只要它们在图像中从右到左显示即可。 (例如,不支持希伯来语,如果我写“תודה”这个词,它将被写成“התוד”。这个我可以处理,问题是段落方向。)
最佳答案
这是解决问题的方法:
smartReverse: function(str) { // Reverse a string considering english letters and and numbers.
return str // by taking it and
.split(/\s+/) // splitting it into words
.reverse() // and reversing the word order
.map(function(word) {
// and then changing each word
return (/^[0-9\.]+$/.test(word) || /^[a-zA-Z0-9?><;,{}[\]\-_+=!@#$%\^&*|']+$/.test(word)) ? // if it's a number or an english word
word : // into itself
word.split("").reverse().join(""); // or otherwise into its reverse
})
.join(" ") // put it back together
;
}
关于node.js - 使用 Node Canvas 从右到左编写段落,基于开罗的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34431353/