import { Rectangle } from '../../../maths/shapes/Rectangle.mjs';
import { Texture } from '../../renderers/shared/texture/Texture.mjs';
import { AbstractBitmapFont } from './AbstractBitmapFont.mjs';

class BitmapFont extends AbstractBitmapFont {
  constructor(options) {
    super();
    const { textures, data } = options;
    Object.keys(data.pages).forEach((key) => {
      const pageData = data.pages[parseInt(key, 10)];
      const texture = textures[pageData.id];
      this.pages.push({ texture });
    });
    Object.keys(data.chars).forEach((key) => {
      const charData = data.chars[key];
      const textureSource = textures[charData.page].source;
      const frame = new Rectangle(
        charData.x / textureSource.width,
        charData.y / textureSource.height,
        charData.width / textureSource.width,
        charData.height / textureSource.height
      );
      const texture = new Texture({
        source: textureSource,
        layout: {
          frame
        }
      });
      this.chars[key] = {
        id: key.codePointAt(0),
        xOffset: charData.xOffset,
        yOffset: charData.yOffset,
        xAdvance: charData.xAdvance,
        kerning: charData.kerning ?? {},
        texture
      };
    });
    this.baseRenderedFontSize = data.fontSize;
    const writable = this;
    writable.baseMeasurementFontSize = data.fontSize;
    writable.fontMetrics = {
      ascent: 0,
      descent: 0,
      fontSize: data.fontSize
    };
    writable.baseLineOffset = data.baseLineOffset;
    writable.lineHeight = data.lineHeight;
    writable.fontFamily = data.fontFamily;
    writable.distanceField = data.distanceField ?? {
      type: "none",
      range: 0
    };
  }
  destroy() {
    super.destroy();
    for (let i = 0; i < this.pages.length; i++) {
      const { texture } = this.pages[i];
      texture.destroy(true);
    }
    this.pages = null;
  }
}

export { BitmapFont };
//# sourceMappingURL=BitmapFont.mjs.map
