backout font measurement change, it is breaking tests
This commit is contained in:
parent
5bf4fcba9b
commit
f2a4756c77
68
fonts.js
68
fonts.js
@ -22,7 +22,14 @@ var kMaxWaitForFontFace = 1000;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
var Fonts = (function Fonts() {
|
var Fonts = (function Fonts() {
|
||||||
|
var kScalePrecision = 40;
|
||||||
var fonts = [];
|
var fonts = [];
|
||||||
|
|
||||||
|
if (!isWorker) {
|
||||||
|
var ctx = document.createElement('canvas').getContext('2d');
|
||||||
|
ctx.scale(1 / kScalePrecision, 1);
|
||||||
|
}
|
||||||
|
|
||||||
var fontCount = 0;
|
var fontCount = 0;
|
||||||
|
|
||||||
function FontInfo(name, data, properties) {
|
function FontInfo(name, data, properties) {
|
||||||
@ -35,6 +42,7 @@ var Fonts = (function Fonts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var current;
|
var current;
|
||||||
|
var measureCache;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
registerFont: function fonts_registerFont(fontName, data, properties) {
|
registerFont: function fonts_registerFont(fontName, data, properties) {
|
||||||
@ -49,6 +57,28 @@ var Fonts = (function Fonts() {
|
|||||||
},
|
},
|
||||||
lookupById: function fonts_lookupById(id) {
|
lookupById: function fonts_lookupById(id) {
|
||||||
return fonts[id];
|
return fonts[id];
|
||||||
|
},
|
||||||
|
setActive: function fonts_setActive(fontName, fontObj, size) {
|
||||||
|
// |current| can be null is fontName is a built-in font
|
||||||
|
// (e.g. "sans-serif")
|
||||||
|
if (fontObj && (current = fonts[fontObj.id])) {
|
||||||
|
var sizes = current.sizes;
|
||||||
|
if (!(measureCache = sizes[size]))
|
||||||
|
measureCache = sizes[size] = Object.create(null);
|
||||||
|
} else {
|
||||||
|
measureCache = null
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.font = (size * kScalePrecision) + 'px "' + fontName + '"';
|
||||||
|
},
|
||||||
|
measureText: function fonts_measureText(text) {
|
||||||
|
var width;
|
||||||
|
if (measureCache && (width = measureCache[text]))
|
||||||
|
return width;
|
||||||
|
width = ctx.measureText(text).width / kScalePrecision;
|
||||||
|
if (measureCache)
|
||||||
|
measureCache[text] = width;
|
||||||
|
return width;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
@ -334,14 +364,6 @@ function getUnicodeRangeFor(value) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
var MeasureText = {
|
|
||||||
currentCache: null,
|
|
||||||
currentFont: null,
|
|
||||||
currentSize: 0,
|
|
||||||
ctx: null,
|
|
||||||
sizes: Object.create(null)
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 'Font' is the class the outside world should use, it encapsulate all the font
|
* 'Font' is the class the outside world should use, it encapsulate all the font
|
||||||
* decoding logics whatever type it is (assuming the font type is supported).
|
* decoding logics whatever type it is (assuming the font type is supported).
|
||||||
@ -351,8 +373,6 @@ var MeasureText = {
|
|||||||
* type1Font.bind();
|
* type1Font.bind();
|
||||||
*/
|
*/
|
||||||
var Font = (function() {
|
var Font = (function() {
|
||||||
var kScalePrecision = 40;
|
|
||||||
|
|
||||||
var constructor = function font_constructor(name, file, properties) {
|
var constructor = function font_constructor(name, file, properties) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.textMatrix = properties.textMatrix || IDENTITY_MATRIX;
|
this.textMatrix = properties.textMatrix || IDENTITY_MATRIX;
|
||||||
@ -1090,7 +1110,7 @@ var Font = (function() {
|
|||||||
return rule;
|
return rule;
|
||||||
},
|
},
|
||||||
|
|
||||||
charsToUnicode: function fonts_charsToUnicode(chars) {
|
charsToUnicode: function fonts_chars2Unicode(chars) {
|
||||||
var charsCache = this.charsCache;
|
var charsCache = this.charsCache;
|
||||||
|
|
||||||
// if we translated this string before, just grab it from the cache
|
// if we translated this string before, just grab it from the cache
|
||||||
@ -1128,32 +1148,6 @@ var Font = (function() {
|
|||||||
|
|
||||||
// Enter the translated string into the cache
|
// Enter the translated string into the cache
|
||||||
return charsCache[chars] = str;
|
return charsCache[chars] = str;
|
||||||
},
|
|
||||||
|
|
||||||
measureText: function fonts_measureText(text, size) {
|
|
||||||
if (MeasureText.currentFont != this ||
|
|
||||||
MeasureText.currentSize != size) {
|
|
||||||
var ctx = MeasureText.ctx;
|
|
||||||
if (!ctx) {
|
|
||||||
ctx = MeasureText.ctx = document.createElement('canvas').getContext('2d');
|
|
||||||
ctx.scale(1 / kScalePrecision, 1);
|
|
||||||
}
|
|
||||||
ctx.font = (size * kScalePrecision) + 'px "' + this.loadedName + '"';
|
|
||||||
MeasureText.currentFont = this;
|
|
||||||
MeasureText.currentSize = size;
|
|
||||||
var cache = MeasureText.sizes[size];
|
|
||||||
if (!cache)
|
|
||||||
cache = MeasureText.sizes[size] = Object.create(null);
|
|
||||||
MeasureText.currentCache = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = size + "$" + text;
|
|
||||||
var width = MeasureText.currentCache[key];
|
|
||||||
if (width)
|
|
||||||
return width;
|
|
||||||
var ctx = MeasureText.ctx;
|
|
||||||
width = ctx.measureText(text).width / kScalePrecision;
|
|
||||||
return MeasureText.currentCache[key] = width;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
13
pdf.js
13
pdf.js
@ -3972,6 +3972,7 @@ var CanvasGraphics = (function() {
|
|||||||
this.ctx.$setFont(fontName, size);
|
this.ctx.$setFont(fontName, size);
|
||||||
} else {
|
} else {
|
||||||
this.ctx.font = size + 'px "' + fontName + '"';
|
this.ctx.font = size + 'px "' + fontName + '"';
|
||||||
|
Fonts.setActive(fontName, fontObj, size);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setTextRenderingMode: function(mode) {
|
setTextRenderingMode: function(mode) {
|
||||||
@ -4017,14 +4018,16 @@ var CanvasGraphics = (function() {
|
|||||||
ctx.$showText(current.y, text);
|
ctx.$showText(current.y, text);
|
||||||
} else {
|
} else {
|
||||||
ctx.translate(current.x, -1 * current.y);
|
ctx.translate(current.x, -1 * current.y);
|
||||||
var font = current.font;
|
var font = this.current.font;
|
||||||
ctx.transform.apply(ctx, font.textMatrix);
|
if (font) {
|
||||||
text = font.charsToUnicode(text);
|
ctx.transform.apply(ctx, font.textMatrix);
|
||||||
|
text = font.charsToUnicode(text);
|
||||||
|
}
|
||||||
ctx.fillText(text, 0, 0);
|
ctx.fillText(text, 0, 0);
|
||||||
current.x += font.measureText(text, current.fontSize);
|
current.x += Fonts.measureText(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.restore();
|
this.ctx.restore();
|
||||||
},
|
},
|
||||||
showSpacedText: function(arr) {
|
showSpacedText: function(arr) {
|
||||||
for (var i = 0; i < arr.length; ++i) {
|
for (var i = 0; i < arr.length; ++i) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user