{"size": 2097, "ext": "js", "max_stars_repo_path": "src/renderer/components/panels/processing/layers/label.js", "max_stars_repo_name": "GaryCXJk/charoster-app", "max_stars_repo_stars_event_min_datetime": "2022-02-26T10:22:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-26T10:22:20.000Z", "max_issues_repo_path": "src/renderer/components/panels/processing/layers/label.js", "max_issues_repo_name": "GaryCXJk/charoster-app", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/renderer/components/panels/processing/layers/label.js", "max_forks_repo_name": "GaryCXJk/charoster-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.4464285714, "max_line_length": 191, "alphanum_fraction": 0.6886027659}
import createWaiter from "../../../../../helpers/create-waiter";
import { clearObject } from "../../../../../helpers/object-helper";

const renderedLabels = {};

export const clearImageLabels = () => {
  Object.values(renderedLabels).forEach((url) => {
    URL.revokeObjectURL(url);
  });
  clearObject(renderedLabels);
};

export const getLabel = async (type, entity, imageId = null) => {
  let displayLabel;
  if (imageId) {
    const imageInfo = await window.packs.getImageInfo(type, imageId);
    displayLabel = displayLabel ?? imageInfo.allCapsDisplayName ?? (imageInfo.displayName ? imageInfo.displayName.toUpperCase() : null);
  }
  displayLabel = displayLabel ?? entity.allCapsDisplayName ?? (entity.displayName ? entity.displayName.toUpperCase() : null) ?? entity.allCapsName ?? (entity.name ?? entity.id).toUpperCase();
  return displayLabel;
};

export const imageLabel = async ({
  label = '',
  fontFamily = 'Montserrat',
  fontWeight = '900',
  fontColor = 'white',
}) => {
  let id = `${label}>>${fontWeight} ${fontFamily}, ${fontColor}`;
  if (!renderedLabels[id]) {
    const waiter = createWaiter();
    renderedLabels[id] = waiter;
    await document.fonts.ready;
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    context.font = `${fontWeight} 144px "${fontFamily}"`;
    context.fillStyle = fontColor;
    const metric = context.measureText(label);
    const labelY = Math.ceil(metric.actualBoundingBoxAscent);
    const labelWidth = Math.ceil(metric.width);
    const labelHeight = Math.ceil(metric.actualBoundingBoxAscent + metric.actualBoundingBoxDescent);

    canvas.width = labelWidth;
    canvas.height = labelHeight;
    context.font = `${fontWeight} 144px "${fontFamily}"`;
    context.fillStyle = fontColor;
    context.fillText(label, 0, labelY);
    const blob = await new Promise((resolve) => canvas.toBlob(resolve));
    renderedLabels[id] = URL.createObjectURL(blob);
    waiter.resolve();
  } else if (renderedLabels[id] instanceof Promise) {
    await renderedLabels[id];
  }
  return renderedLabels[id];
};
