font-metrics.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var util_1 = require("../core/util");
  4. var SAMPLE_TEXT = 'Hidden Text';
  5. var FontMetrics = /** @class */ (function () {
  6. function FontMetrics(document) {
  7. this._data = {};
  8. this._document = document;
  9. }
  10. FontMetrics.prototype.parseMetrics = function (fontFamily, fontSize) {
  11. var container = this._document.createElement('div');
  12. var img = this._document.createElement('img');
  13. var span = this._document.createElement('span');
  14. var body = this._document.body;
  15. container.style.visibility = 'hidden';
  16. container.style.fontFamily = fontFamily;
  17. container.style.fontSize = fontSize;
  18. container.style.margin = '0';
  19. container.style.padding = '0';
  20. body.appendChild(container);
  21. img.src = util_1.SMALL_IMAGE;
  22. img.width = 1;
  23. img.height = 1;
  24. img.style.margin = '0';
  25. img.style.padding = '0';
  26. img.style.verticalAlign = 'baseline';
  27. span.style.fontFamily = fontFamily;
  28. span.style.fontSize = fontSize;
  29. span.style.margin = '0';
  30. span.style.padding = '0';
  31. span.appendChild(this._document.createTextNode(SAMPLE_TEXT));
  32. container.appendChild(span);
  33. container.appendChild(img);
  34. var baseline = img.offsetTop - span.offsetTop + 2;
  35. container.removeChild(span);
  36. container.appendChild(this._document.createTextNode(SAMPLE_TEXT));
  37. container.style.lineHeight = 'normal';
  38. img.style.verticalAlign = 'super';
  39. var middle = img.offsetTop - container.offsetTop + 2;
  40. body.removeChild(container);
  41. return { baseline: baseline, middle: middle };
  42. };
  43. FontMetrics.prototype.getMetrics = function (fontFamily, fontSize) {
  44. var key = fontFamily + " " + fontSize;
  45. if (typeof this._data[key] === 'undefined') {
  46. this._data[key] = this.parseMetrics(fontFamily, fontSize);
  47. }
  48. return this._data[key];
  49. };
  50. return FontMetrics;
  51. }());
  52. exports.FontMetrics = FontMetrics;
  53. //# sourceMappingURL=font-metrics.js.map