From 8d506212a8ae8584564bc032e8b78e89812c2ef1 Mon Sep 17 00:00:00 2001 From: Yury Delendik Date: Sun, 16 Sep 2012 14:38:30 -0500 Subject: [PATCH] Adds heuristic for monospace font detection --- src/evaluator.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/evaluator.js b/src/evaluator.js index 59fec61a6..bd3ccd38f 100644 --- a/src/evaluator.js +++ b/src/evaluator.js @@ -825,21 +825,41 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { } } + // Heuristic: detection of monospace font by checking all non-zero widths + var isMonospace = true, firstWidth = defaultWidth; + for (var glyph in glyphsWidths) { + var glyphWidth = glyphsWidths[glyph]; + if (!glyphWidth) + continue; + if (!firstWidth) { + firstWidth = glyphWidth; + continue; + } + if (firstWidth != glyphWidth) { + isMonospace = false; + break; + } + } + if (isMonospace) + properties.flags |= FontFlags.FixedPitch; + properties.defaultWidth = defaultWidth; properties.widths = glyphsWidths; }, getBaseFontMetrics: function PartialEvaluator_getBaseFontMetrics(name) { - var defaultWidth = 0, widths = []; + var defaultWidth = 0, widths = [], monospace = false; var glyphWidths = Metrics[stdFontMap[name] || name]; if (isNum(glyphWidths)) { defaultWidth = glyphWidths; + monospace = true; } else { widths = glyphWidths; } return { defaultWidth: defaultWidth, + monospace: monospace, widths: widths }; }, @@ -893,6 +913,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() { var fontNameWoStyle = baseFontName.split('-')[0]; var flags = (serifFonts[fontNameWoStyle] || (fontNameWoStyle.search(/serif/gi) != -1) ? FontFlags.Serif : 0) | + (metrics.monospace ? FontFlags.FixedPitch : 0) | (symbolsFonts[fontNameWoStyle] ? FontFlags.Symbolic : FontFlags.Nonsymbolic);