Skip to content

Commit 12da8ba

Browse files
committed
fix full-width characters rendering
1 parent fa4d045 commit 12da8ba

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var fs = require('fs');
3131
var path = require('path');
3232
var url = require('url');
3333
var jsdom = require('jsdom').jsdom;
34+
var isFullwidthCodePoint = require('is-fullwidth-code-point');
3435

3536
require('./patch/jsdom.js').patch(jsdom); // Fix some bugs in jsdom
3637

@@ -309,7 +310,10 @@ function ConfigureMathJax() {
309310
if (def["font-weight"] === "") delete def["font-weight"];
310311
this.SUPER(arguments).Init.call(this,def);
311312
SVG.addText(this.element,text);
312-
var bbox = {width: text.length * 8.5, height: 18, y: -12};
313+
var textWidth = text.split('')
314+
.map(function(c) { return isFullwidthCodePoint(c.codePointAt()) ? 13 : 8.5 })
315+
.reduce(function(a, b) { return a + b }, 0);
316+
var bbox = {width: textWidth, height: 18, y: -12};
313317
scale *= 1000/SVG.em;
314318
this.element.setAttribute("font-family","monospace");
315319
this.element.setAttribute("transform","scale("+scale+") matrix(1 0 0 -1 0 0)");

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"url": "git://github.com/mathjax/MathJax-node.git"
2424
},
2525
"dependencies": {
26+
"is-fullwidth-code-point": "^2.0.0",
2627
"jsdom": "7.0 - 9.12",
2728
"mathjax": "*"
2829
},

test/svg-full-width-chars.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var tape = require('tape');
2+
var mjAPI = require("../lib/main.js");
3+
4+
tape('the SVG output should renders full-width characters correctly', function(t) {
5+
t.plan(1);
6+
7+
mjAPI.start();
8+
var tex = '\\text{拾零}^i';
9+
10+
mjAPI.typeset({
11+
math: tex,
12+
format: "TeX",
13+
svg: true
14+
}, function(data) {
15+
t.ok(data.width, '5.133ex', 'Width is correct');
16+
});
17+
});

0 commit comments

Comments
 (0)