getBBoxDummyFill.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. let originalFn: () => DOMRect;
  20. const textToWidth = {
  21. paris: 200,
  22. tokyo: 300,
  23. beijing: 400,
  24. };
  25. export const SAMPLE_TEXT = Object.keys(textToWidth);
  26. export function addDummyFill() {
  27. // @ts-ignore - fix jsdom
  28. originalFn = SVGElement.prototype.getBBox;
  29. // @ts-ignore - fix jsdom
  30. SVGElement.prototype.getBBox = function getBBox() {
  31. let width =
  32. textToWidth[this.textContent as keyof typeof textToWidth] || 200;
  33. let height = 20;
  34. if (this.getAttribute('class') === 'test-class') {
  35. width /= 2;
  36. }
  37. if (this.style.fontFamily === 'Lobster') {
  38. width *= 1.25;
  39. }
  40. if (this.style.fontSize) {
  41. const size = Number(this.style.fontSize.replace('px', ''));
  42. const ratio = size / 20;
  43. width *= ratio;
  44. height *= ratio;
  45. }
  46. if (this.style.fontStyle === 'italic') {
  47. width *= 1.5;
  48. }
  49. if (this.style.fontWeight === '700') {
  50. width *= 2;
  51. }
  52. if (this.style.letterSpacing) {
  53. width *= 1.1;
  54. }
  55. return {
  56. x: 0,
  57. y: 0,
  58. width,
  59. height,
  60. top: 0,
  61. left: 0,
  62. right: 0,
  63. bottom: 0,
  64. };
  65. };
  66. }
  67. export function removeDummyFill() {
  68. // @ts-ignore - fix jsdom
  69. SVGElement.prototype.getBBox = originalFn;
  70. }