function buildLabel()

in packages/ketcher-core/src/application/render/restruct/reatom.ts [868:979]


function buildLabel(
  atom: ReAtom,
  paper: any,
  ps: Vec2,
  options: any,
  atomId: number,
  sgroup?: SGroup,
): {
  rightMargin: number;
  leftMargin: number;
  label: ElemAttr;
} {
  const {
    atomColoring,
    font,
    fontszInPx,
    currentlySelectedMonomerAttachmentPoint,
    connectedMonomerAttachmentPoints,
    usageInMacromolecule,
  } = options;
  // eslint-disable-line max-statements
  const label: any = {
    text: getLabelText(atom.a, atomId, sgroup),
  };
  let tooltip: string | null = null;
  if (!label.text) {
    label.text = 'R#';
  }

  if (label.text === atom.a.label) {
    const element = Elements.get(label.text);
    if (atomColoring && element) {
      atom.color = ElementColor[label.text] || '#000';
    }
  }

  const shouldStyleLabel = usageInMacromolecule !== undefined;
  const isMonomerAttachmentPoint = attachmentPointNames.includes(label.text);
  const isMonomerAttachmentPointSelected =
    currentlySelectedMonomerAttachmentPoint === label.text;
  const isMonomerAttachmentPointUsed =
    connectedMonomerAttachmentPoints?.includes(label.text);

  const { color, fill, stroke } = util.useLabelStyles(
    isMonomerAttachmentPointSelected,
    isMonomerAttachmentPointUsed,
    usageInMacromolecule,
  );

  if (isMonomerAttachmentPoint && shouldStyleLabel) {
    atom.color = color;
  }

  if (label.text?.length > MAX_LABEL_LENGTH) {
    tooltip = label.text;
    label.text = `${label.text?.substring(0, 8)}...`;
  }

  const { previewOpacity } = options;

  label.path = paper.text(ps.x, ps.y, label.text).attr({
    font,
    'font-size': fontszInPx,
    fill: atom.color,
    'font-style': atom.a.pseudo ? 'italic' : '',
    'fill-opacity': atom.a.isPreview ? previewOpacity : 1,
  });

  if (isMonomerAttachmentPoint && shouldStyleLabel) {
    const backgroundSize = fontszInPx * 2;

    label.background = paper
      .rect(
        ps.x - backgroundSize / 2,
        ps.y - backgroundSize / 2,
        backgroundSize,
        backgroundSize,
        10,
      )
      .attr({ fill })
      .attr({ stroke });
  }
  if (tooltip) {
    addTooltip(label.path.node, tooltip);
  }

  label.rbb = util.relBox(label.path.getBBox());
  draw.recenterText(label.path, label.rbb);
  let rightMargin =
    (label.rbb.width / 2) * (options.zoom > 1 ? 1 : options.zoom); //
  let leftMargin =
    (-label.rbb.width / 2) * (options.zoom > 1 ? 1 : options.zoom);

  if (atom.a.atomList !== null) {
    const xShift =
      ((atom.hydrogenOnTheLeft ? -1 : 1) *
        (label.rbb.width - label.rbb.height)) /
      2;
    pathAndRBoxTranslate(
      label.path,
      label.rbb,
      xShift,

      0,
    );
    rightMargin += xShift;
    leftMargin += xShift;
  }

  atom.label = label;
  return { label, rightMargin, leftMargin };
}