
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/


/**
 * AUTO-GENERATED FILE. DO NOT MODIFY.
 */

import { DefaultFamily } from '../../data/Constant.js';
// eslint-disable-next-line no-undef
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
function findFontSize(text, maxWidth, font, tolerance, maxHeight, high) {
  if (font === void 0) {
    font = DefaultFamily;
  }
  if (tolerance === void 0) {
    tolerance = 8;
  }
  if (high === void 0) {
    high = 1000;
  }
  var low = 2;
  var lines = text.split('\n');
  var linesLength = lines.length;
  var maxLengthText = lines[0];
  for (var index = 1; index < lines.length; index++) {
    var element = lines[index];
    if (element.length > maxLengthText.length) {
      maxLengthText = element;
    }
  }
  while (low <= high) {
    var size = Math.floor((low + high) / 2);
    ctx.font = size + "px " + font;
    var isInHeight = size * linesLength <= maxHeight;
    var width = ctx.measureText(maxLengthText).width;
    if (maxWidth > width && maxWidth - width <= tolerance && isInHeight) {
      // 如果宽度差在容许范围内，则返回当前字体大小
      return size;
    } else if (width < maxWidth && isInHeight) {
      // 如果当前大小下的文本宽度小于最大宽度，则尝试更大的字体
      low = size + 1;
    } else {
      // 如果超出了最大宽度，则尝试更小的字体
      high = size - 1;
    }
  }
  // 返回最后一次满足条件的字体大小
  return high;
}
var boxFontSizeMapping = {
  huge: 24,
  large: 18,
  normal: 14,
  small: 10,
  tiny: 8
};
function getFontSize(size, text, maxWidth, font, maxHeight) {
  // const tolerance = Math.min(maxWidth / 5, 4);
  if (typeof size === 'number') {
    return size;
  }
  return boxFontSizeMapping[size] || findFontSize(text, maxWidth, font, 2, maxHeight);
}
export { findFontSize, getFontSize };