Skip to content

Commit

Permalink
#5685 - The diagonal bond in the molecule is displayed incorrect with…
Browse files Browse the repository at this point in the history
… ACS style
  • Loading branch information
Guch1g0v committed Oct 28, 2024
1 parent d6ddc42 commit 4e3b8f0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
40 changes: 35 additions & 5 deletions packages/ketcher-core/src/application/render/restruct/reatom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,35 @@ class ReAtom extends ReObject {
return this.getSelectionContour(render, false).attr(options.selectionStyle);
}

private isNeedShiftForCharge(showCharge: boolean, bondLength: number) {
const MIN_BOND_LENGTH = 24;
const isBondLengthTooShort = bondLength <= MIN_BOND_LENGTH;
const hasCharge = this.a.charge !== null && this.a.charge !== 0;
return showCharge && isBondLengthTooShort && hasCharge;
}

private getRatio(
renderOptions: RenderOptions,
bondLen: number | null,
): number {
const DEFAULT_BOND_LENGTH = 40;
const DEFAULT_SUB_FONT_SIZE = 13;
const subFontSize = renderOptions.fontszsubInPx || DEFAULT_SUB_FONT_SIZE;
if (!bondLen) return 1;
const showCharge = renderOptions.showCharge;

const isNeedShift = this.isNeedShiftForCharge(showCharge, bondLen);

if (!isNeedShift) {
return 1;
}

const DEFAULT_PROPORTION = DEFAULT_BOND_LENGTH / DEFAULT_SUB_FONT_SIZE;
const currentProportion = bondLen / subFontSize;
const ratio = currentProportion / DEFAULT_PROPORTION;
return ratio;
}

/**
* if atom is rendered as Abbreviation: O, NH, ...
* In this case we need to shift the bond render start position to free space for Atom,
Expand All @@ -214,21 +243,22 @@ class ReAtom extends ReObject {
renderOptions: RenderOptions,
direction: Vec2,
_atomPosition?: Vec2,
bondLen: number | null = null,
): Vec2 {
const atomPosition = Scale.modelToCanvas(
_atomPosition || this.a.pp,
renderOptions,
);
let atomSymbolShift = 0;
const exts = this.visel.exts;
const ratio = this.getRatio(renderOptions, bondLen);
for (let k = 0; k < exts.length; ++k) {
const box = exts[k].translate(atomPosition);

atomSymbolShift = Math.max(
atomSymbolShift,
util.shiftRayBox(atomPosition, direction, box),
);
const shiftRayBox = util.shiftRayBox(atomPosition, direction, box);
const shift = shiftRayBox * ratio;
atomSymbolShift = Math.max(atomSymbolShift, shift);
}

if (atomSymbolShift > 0) {
return atomPosition.addScaled(
direction,
Expand Down
11 changes: 9 additions & 2 deletions packages/ketcher-core/src/application/render/restruct/rebond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,20 @@ class ReBond extends ReObject {

if (!hb1?.dir || !hb2?.dir) return;

hb1.p = beginAtom.getShiftedSegmentPosition(options, hb1.dir, p1);
hb2.p = endAtom.getShiftedSegmentPosition(options, hb2.dir, p2);
bond.b.center = Vec2.lc2(p1, 0.5, p2, 0.5);
bond.b.len = Vec2.dist(
Scale.modelToCanvas(p1, render.options),
Scale.modelToCanvas(p2, render.options),
);

hb1.p = beginAtom.getShiftedSegmentPosition(
options,
hb1.dir,
p1,
bond.b.len,
);
hb2.p = endAtom.getShiftedSegmentPosition(options, hb2.dir, p2, bond.b.len);

bond.b.sb = options.lineWidth * 5;
/* eslint-disable no-mixed-operators */
bond.b.sa = Math.max(bond.b.sb, bond.b.len / 2 - options.lineWidth * 2);
Expand Down

0 comments on commit 4e3b8f0

Please sign in to comment.