Skip to content

Commit 1113634

Browse files
ambarpkra
authored andcommitted
improve full-width characters rendering (#358)
* add option for width * refine textWidth calculation * add documentation and test
1 parent 6b3b03e commit 1113634

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ The following are the default input options.
114114
useGlobalCache: false, // use common <defs> for all equations?
115115
linebreaks: false, // automatic linebreaking
116116
equationNumbers: "none", // automatic equation numbering ("none", "AMS" or "all")
117+
cjkCharWidth: 13, // width of CJK character
117118

118119
math: "", // the math string to typeset
119120
format: "TeX", // the input format (TeX, inline-TeX, AsciiMath, or MathML)

lib/main.js

Lines changed: 7 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

@@ -47,6 +48,7 @@ var defaults = {
4748
useGlobalCache: false, // use common <defs> for all equations?
4849
linebreaks: false, // do linebreaking?
4950
equationNumbers: "none", // or "AMS" or "all"
51+
cjkCharWidth: 13, // width of CJK character
5052

5153
math: "", // the math to typeset
5254
format: "TeX", // the input format (TeX, inline-TeX, AsciiMath, or MathML)
@@ -309,7 +311,11 @@ function ConfigureMathJax() {
309311
if (def["font-weight"] === "") delete def["font-weight"];
310312
this.SUPER(arguments).Init.call(this,def);
311313
SVG.addText(this.element,text);
312-
var bbox = {width: text.length * 8.5, height: 18, y: -12};
314+
// tweaking font fallback behavior: https://github.com/mathjax/MathJax-node/issues/299
315+
var textWidth = text.split('')
316+
.map(function(c) { return isFullwidthCodePoint(c.codePointAt()) ? data.cjkCharWidth : 8.5 })
317+
.reduce(function(a, b) { return a + b }, 0);
318+
var bbox = {width: textWidth, height: 18, y: -12};
313319
scale *= 1000/SVG.em;
314320
this.element.setAttribute("font-family","monospace");
315321
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)