82d89fbbed8228cc2a50bb30794486115502c390dd5115224761bd4ece93badb70af03198b9dac50c420947c2a184977c7ba1905d682bdc815ffd50ac0137e 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { applyFontInfo } from './domFontInfo.js';
  6. export class CharWidthRequest {
  7. constructor(chr, type) {
  8. this.chr = chr;
  9. this.type = type;
  10. this.width = 0;
  11. }
  12. fulfill(width) {
  13. this.width = width;
  14. }
  15. }
  16. class DomCharWidthReader {
  17. constructor(bareFontInfo, requests) {
  18. this._bareFontInfo = bareFontInfo;
  19. this._requests = requests;
  20. this._container = null;
  21. this._testElements = null;
  22. }
  23. read() {
  24. // Create a test container with all these test elements
  25. this._createDomElements();
  26. // Add the container to the DOM
  27. document.body.appendChild(this._container);
  28. // Read character widths
  29. this._readFromDomElements();
  30. // Remove the container from the DOM
  31. document.body.removeChild(this._container);
  32. this._container = null;
  33. this._testElements = null;
  34. }
  35. _createDomElements() {
  36. const container = document.createElement('div');
  37. container.style.position = 'absolute';
  38. container.style.top = '-50000px';
  39. container.style.width = '50000px';
  40. const regularDomNode = document.createElement('div');
  41. applyFontInfo(regularDomNode, this._bareFontInfo);
  42. container.appendChild(regularDomNode);
  43. const boldDomNode = document.createElement('div');
  44. applyFontInfo(boldDomNode, this._bareFontInfo);
  45. boldDomNode.style.fontWeight = 'bold';
  46. container.appendChild(boldDomNode);
  47. const italicDomNode = document.createElement('div');
  48. applyFontInfo(italicDomNode, this._bareFontInfo);
  49. italicDomNode.style.fontStyle = 'italic';
  50. container.appendChild(italicDomNode);
  51. const testElements = [];
  52. for (const request of this._requests) {
  53. let parent;
  54. if (request.type === 0 /* CharWidthRequestType.Regular */) {
  55. parent = regularDomNode;
  56. }
  57. if (request.type === 2 /* CharWidthRequestType.Bold */) {
  58. parent = boldDomNode;
  59. }
  60. if (request.type === 1 /* CharWidthRequestType.Italic */) {
  61. parent = italicDomNode;
  62. }
  63. parent.appendChild(document.createElement('br'));
  64. const testElement = document.createElement('span');
  65. DomCharWidthReader._render(testElement, request);
  66. parent.appendChild(testElement);
  67. testElements.push(testElement);
  68. }
  69. this._container = container;
  70. this._testElements = testElements;
  71. }
  72. static _render(testElement, request) {
  73. if (request.chr === ' ') {
  74. let htmlString = '\u00a0';
  75. // Repeat character 256 (2^8) times
  76. for (let i = 0; i < 8; i++) {
  77. htmlString += htmlString;
  78. }
  79. testElement.innerText = htmlString;
  80. }
  81. else {
  82. let testString = request.chr;
  83. // Repeat character 256 (2^8) times
  84. for (let i = 0; i < 8; i++) {
  85. testString += testString;
  86. }
  87. testElement.textContent = testString;
  88. }
  89. }
  90. _readFromDomElements() {
  91. for (let i = 0, len = this._requests.length; i < len; i++) {
  92. const request = this._requests[i];
  93. const testElement = this._testElements[i];
  94. request.fulfill(testElement.offsetWidth / 256);
  95. }
  96. }
  97. }
  98. export function readCharWidths(bareFontInfo, requests) {
  99. const reader = new DomCharWidthReader(bareFontInfo, requests);
  100. reader.read();
  101. }