Skip to content

Commit

Permalink
#2175 - Adjust the rendering of PNG and SVG formats for ACS style (#…
Browse files Browse the repository at this point in the history
…2434)

Co-authored-by: Aliakasndr Dziarkach <[email protected]>
  • Loading branch information
AliaksandrDziarkach and Aliakasndr Dziarkach authored Sep 30, 2024
1 parent 55473ab commit 45da810
Show file tree
Hide file tree
Showing 34 changed files with 499 additions and 159 deletions.
57 changes: 50 additions & 7 deletions api/c/indigo-renderer/src/indigo_render2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ void indigoRenderGetCommentPosition(Array<char>& value)
value.readString("bottom", true);
}

static void indigoSetBondLength(float value)
{
Indigo& self = indigoGetInstance();
self.layout_options.setBondLengthPx(value);
}

static void indigoGetBondLength(float& value)
{
Indigo& self = indigoGetInstance();
value = self.layout_options.getBondLengthPx();
}

RenderCdxmlContext& getCdxmlContext()
{
RenderParams& rp = indigoRendererGetInstance().renderParams;
Expand Down Expand Up @@ -437,6 +449,24 @@ CEXPORT int indigoRendererDispose(const qword id)
INDIGO_END(-1);
}

static void setParams(RenderParams& rp, LayoutOptions& layout_options)
{
rp.cnvOpt.bondLength = layout_options.bondLength;
rp.cnvOpt.bondLengthUnit = layout_options.bondLengthUnit;
rp.rOpt.ppi = layout_options.ppi;
rp.rOpt.bond_length_px = layout_options.bondLength > EPSILON ? layout_options.getBondLengthPx() : LayoutOptions::DEFAULT_BOND_LENGTH_PX;
if (rp.cnvOpt.outputSheetWidth > 0)
{
rp.cnvOpt.maxHeight = -1;
rp.cnvOpt.maxWidth = UnitsOfMeasure::convertInchesToPx(rp.cnvOpt.outputSheetWidth, layout_options.ppi);
}
else if (rp.cnvOpt.outputSheetHeight > 0)
{
rp.cnvOpt.maxHeight = UnitsOfMeasure::convertInchesToPx(rp.cnvOpt.outputSheetHeight, layout_options.ppi);
rp.cnvOpt.maxWidth = -1;
}
}

CEXPORT int indigoRender(int object, int output)
{
INDIGO_BEGIN
Expand All @@ -447,6 +477,8 @@ CEXPORT int indigoRender(int object, int output)
rp.clearArrays();
rp.smart_layout = self.smart_layout;

setParams(rp, indigoGetInstance().layout_options);

IndigoObject& obj = self.getObject(object);

if (IndigoBaseMolecule::is(obj))
Expand Down Expand Up @@ -502,6 +534,8 @@ CEXPORT int indigoRenderGrid(int objects, int* refAtoms, int nColumns, int outpu
RenderParams& rp = indigoRendererGetInstance().renderParams;
rp.clearArrays();

setParams(rp, indigoGetInstance().layout_options);

PtrArray<IndigoObject>& objs = IndigoArray::cast(self.getObject(objects)).objects;
if (rp.rOpt.cdxml_context.get() != NULL)
{
Expand Down Expand Up @@ -690,12 +724,6 @@ void IndigoRenderer::setOptionsHandlers()
#define cdxmlContext getCdxmlContext()
#define indigo indigoGetInstance()

rp.cnvOpt.bondLength = indigo.layout_options.bondLength;
rp.cnvOpt.bondLengthUnit = indigo.layout_options.bondLengthUnit;
rp.rOpt.reactionComponentMarginSize = indigo.layout_options.reactionComponentMarginSize;
rp.rOpt.reactionComponentMarginSizeUnit = indigo.layout_options.reactionComponentMarginSizeUnit;
rp.rOpt.ppi = indigo.layout_options.ppi;

mgr->setOptionHandlerInt("render-comment-offset", SETTER_GETTER_INT_OPTION(rp.cnvOpt.commentOffset));
mgr->setOptionHandlerInt("render-image-width", SETTER_GETTER_INT_OPTION(rp.cnvOpt.width));
mgr->setOptionHandlerInt("render-image-height", SETTER_GETTER_INT_OPTION(rp.cnvOpt.height));
Expand Down Expand Up @@ -723,7 +751,7 @@ void IndigoRenderer::setOptionsHandlers()
mgr->setOptionHandlerBool("render-highlighted-labels-visible", SETTER_GETTER_BOOL_OPTION(rp.rOpt.highlightedLabelsVisible));
mgr->setOptionHandlerBool("render-bold-bond-detection", SETTER_GETTER_BOOL_OPTION(rp.rOpt.boldBondDetection));

mgr->setOptionHandlerFloat("render-bond-length", SETTER_GETTER_FLOAT_OPTION(rp.cnvOpt.bondLength));
mgr->setOptionHandlerFloat("render-bond-length", indigoSetBondLength, indigoGetBondLength);
mgr->setOptionHandlerFloat("render-relative-thickness", SET_POSITIVE_FLOAT_OPTION(rp.relativeThickness, "relative thickness must be positive"));
mgr->setOptionHandlerFloat("render-bond-line-width", SET_POSITIVE_FLOAT_OPTION(rp.bondLineWidthFactor, "bond line width factor must be positive"));
mgr->setOptionHandlerFloat("render-comment-font-size", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.commentFontFactor));
Expand Down Expand Up @@ -760,5 +788,20 @@ void IndigoRenderer::setOptionsHandlers()
mgr->setOptionHandlerString("render-cdxml-title-face", SETTER_GETTER_STR_OPTION(cdxmlContext.titleFace));

mgr->setOptionHandlerVoid("reset-render-options", indigoRenderResetOptions);

// ACS style options
mgr->setOptionHandlerFloat("render-font-size", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.fontSize));
mgr->setOptionHandlerString("render-font-size-unit", SETTER_GETTER_UNIT_OPTION(rp.rOpt.fontSizeUnit));
mgr->setOptionHandlerFloat("render-font-size-sub", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.fontSizeSub));
mgr->setOptionHandlerString("render-font-size-sub-unit", SETTER_GETTER_UNIT_OPTION(rp.rOpt.fontSizeSubUnit));
mgr->setOptionHandlerFloat("render-bond-thickness", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.bondThickness));
mgr->setOptionHandlerString("render-bond-thickness-unit", SETTER_GETTER_UNIT_OPTION(rp.rOpt.bondThicknessUnit));
mgr->setOptionHandlerFloat("render-bond-spacing", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.bondSpacing));
mgr->setOptionHandlerFloat("render-stereo-bond-width", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.stereoBondWidth));
mgr->setOptionHandlerString("render-stereo-bond-width-unit", SETTER_GETTER_UNIT_OPTION(rp.rOpt.stereoBondWidthUnit));
mgr->setOptionHandlerFloat("render-hash-spacing", SETTER_GETTER_FLOAT_OPTION(rp.rOpt.hashSpacing));
mgr->setOptionHandlerString("render-hash-spacing-unit", SETTER_GETTER_UNIT_OPTION(rp.rOpt.hashSpacingUnit));
mgr->setOptionHandlerFloat("render-output-sheet-width", SETTER_GETTER_FLOAT_OPTION(rp.cnvOpt.outputSheetWidth));
mgr->setOptionHandlerFloat("render-output-sheet-height", SETTER_GETTER_FLOAT_OPTION(rp.cnvOpt.outputSheetHeight));
}
}
1 change: 0 additions & 1 deletion api/c/indigo/src/indigo_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ class DLLEXPORT Indigo

int layout_max_iterations = 0; // default is zero -- no limit
bool smart_layout = false;
float layout_horintervalfactor = ReactionLayout::DEFAULT_HOR_INTERVAL_FACTOR;
bool layout_preserve_existing = false;

int layout_orientation = 0;
Expand Down
14 changes: 9 additions & 5 deletions api/c/indigo/src/indigo_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
#include "layout/reaction_layout.h"
#include "reaction/base_reaction.h"

#ifdef _WIN32
#pragma warning(push, 4)
#endif

CEXPORT int indigoLayout(int object)
{
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(object);
int i;

if (IndigoBaseMolecule::is(obj))
{
Expand Down Expand Up @@ -79,7 +82,7 @@ CEXPORT int indigoLayout(int object)
catch (Exception e)
{
}
for (i = 1; i <= mol->rgroups.getRGroupCount(); i++)
for (int i = 1; i <= mol->rgroups.getRGroupCount(); i++)
{
RGroup& rgp = mol->rgroups.getRGroup(i);

Expand Down Expand Up @@ -112,9 +115,6 @@ CEXPORT int indigoLayout(int object)
ReactionLayout rl(rxn, self.smart_layout, self.layout_options);
rl.setMaxIterations(self.layout_max_iterations);
rl.setLayoutOrientation((LAYOUT_ORIENTATION)self.layout_orientation);
// TODO::ACS Why removed?
// rl.bond_length = LayoutOptions::DEFAULT_BOND_LENGTH;
// rl.reaction_margin_size = self.layout_horintervalfactor;
if (self.layout_preserve_existing)
rl.setPreserveMoleculeLayout(true);
rl.make();
Expand Down Expand Up @@ -197,3 +197,7 @@ CEXPORT int indigoClean2d(int object)
}
INDIGO_END(-1);
}

#ifdef _WIN32
#pragma warning(pop)
#endif
67 changes: 5 additions & 62 deletions api/c/indigo/src/indigo_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ static void indigoGetEmbeddingUniqueness(Array<char>& value)
static void indigoSetLayoutHorIntervalFactor(float value)
{
Indigo& self = indigoGetInstance();
self.layout_horintervalfactor = value;
self.layout_options.setMarginSizeInAngstroms(value);
}

static void indigoGetLayoutHorIntervalFactor(float& value)
{
Indigo& self = indigoGetInstance();
value = self.layout_horintervalfactor;
value = self.layout_options.getMarginSizeInAngstroms();
}

static void indigoSetAromaticityModel(const char* model)
Expand Down Expand Up @@ -238,6 +238,7 @@ static void indigoResetBasicOptions()
Indigo& self = indigoGetInstance();
self.standardize_options.reset();
self.ionize_options = IonizeOptions();
self.layout_options.reset();
self.init();
}

Expand All @@ -261,62 +262,6 @@ void indigoProductEnumeratorGetOneTubeMode(Array<char>& value)
value.readString("grid", true);
}

bool isEqual(const char* l, const char* r)
{
return strcmp(l, r) != 0;
}

IndigoOptionManager::optf_string_t indigoSetUnitsOfMeasure(UnitsOfMeasure::TYPE& result)
{
static auto func = [&result](const char* mode) {
if (isEqual(mode, "pt"))
{
result = UnitsOfMeasure::TYPE::PT;
}
else if (isEqual(mode, "px"))
{
result = UnitsOfMeasure::TYPE::PX;
}
else if (isEqual(mode, "inch"))
{
result = UnitsOfMeasure::TYPE::INCH;
}
else if (isEqual(mode, "cm"))
{
result = UnitsOfMeasure::TYPE::CM;
}
else
{
throw IndigoError("Invalid size unit, should be 'px', 'pt', 'inch' or 'all'");
}
};

return [](const char* mode) -> void { return func(mode); };
}

IndigoOptionManager::get_optf_string_t indigoGetUnitsOfMeasure(const UnitsOfMeasure::TYPE input)
{
static auto func = [input](Array<char>& result) {
switch (input)
{
case UnitsOfMeasure::TYPE::PT:
result.readString("pt", true);
break;
case UnitsOfMeasure::TYPE::PX:
result.readString("px", true);
break;
case UnitsOfMeasure::TYPE::INCH:
result.readString("inch", true);
break;
case UnitsOfMeasure::TYPE::CM:
result.readString("cm", true);
break;
}
};

return [](Array<char>& res) -> void { return func(res); };
}

void IndigoOptionHandlerSetter::setBasicOptionHandlers(const qword id)
{
auto mgr = sf::xlock_safe_ptr(indigoGetOptionManager(id));
Expand Down Expand Up @@ -442,10 +387,8 @@ void IndigoOptionHandlerSetter::setBasicOptionHandlers(const qword id)
mgr->setOptionHandlerBool("transform-layout", SETTER_GETTER_BOOL_OPTION(indigo.rpe_params.transform_is_layout));

mgr->setOptionHandlerFloat("bond-length", SET_POSITIVE_FLOAT_OPTION(indigo.layout_options.bondLength, "bond length must be positive"));
mgr->setOptionHandlerString("bond-length-unit", indigoSetUnitsOfMeasure(indigo.layout_options.bondLengthUnit),
indigoGetUnitsOfMeasure(indigo.layout_options.bondLengthUnit));
mgr->setOptionHandlerString("bond-length-unit", SETTER_GETTER_UNIT_OPTION(indigo.layout_options.bondLengthUnit));
mgr->setOptionHandlerFloat("reaction-component-margin-size", SETTER_GETTER_FLOAT_OPTION(indigo.layout_options.reactionComponentMarginSize));
mgr->setOptionHandlerString("reaction-component-margin-size-unit", indigoSetUnitsOfMeasure(indigo.layout_options.reactionComponentMarginSizeUnit),
indigoGetUnitsOfMeasure(indigo.layout_options.reactionComponentMarginSizeUnit));
mgr->setOptionHandlerString("reaction-component-margin-size-unit", SETTER_GETTER_UNIT_OPTION(indigo.layout_options.reactionComponentMarginSizeUnit));
mgr->setOptionHandlerInt("image-resolution", SET_POSITIVE_INT_OPTION(indigo.layout_options.ppi, "image resolution ppi must be positive"));
}
31 changes: 31 additions & 0 deletions api/c/indigo/src/option_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ using namespace indigo;
}, \
[](int32_t& value) { value = option; }

#define SETTER_GETTER_UNIT_OPTION(option) \
[](const char* mode) { \
if (strcmp(mode, "pt") == 0) \
option = UnitsOfMeasure::TYPE::PT; \
else if (strcmp(mode, "px") == 0) \
option = UnitsOfMeasure::TYPE::PX; \
else if (strcmp(mode, "inch") == 0) \
option = UnitsOfMeasure::TYPE::INCH; \
else if (strcmp(mode, "cm") == 0) \
option = UnitsOfMeasure::TYPE::CM; \
else \
throw IndigoError("Invalid size unit, should be 'px', 'pt', 'inch' or 'all'"); \
}, \
[](Array<char>& result) { \
switch (option) \
{ \
case UnitsOfMeasure::TYPE::PT: \
result.readString("pt", true); \
break; \
case UnitsOfMeasure::TYPE::PX: \
result.readString("px", true); \
break; \
case UnitsOfMeasure::TYPE::INCH: \
result.readString("inch", true); \
break; \
case UnitsOfMeasure::TYPE::CM: \
result.readString("cm", true); \
break; \
} \
}

class DLLEXPORT IndigoOptionManager
{
public:
Expand Down
2 changes: 1 addition & 1 deletion api/tests/integration/ref/basic/options.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bool
True
true
***** Float *****
1.400
0.800
float
21.333
21.333
2 changes: 1 addition & 1 deletion api/tests/integration/ref/layout/acs_style_reaction.py.out
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
Molecule #4: Success

*** 2389 wrong margin ***
acs_issue_2389.ket.ket:SUCCEED
acs_issue_2389.ket:SUCCEED
4 changes: 4 additions & 0 deletions api/tests/integration/ref/rendering/acs_style.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
****** Default rendering settings *****
acs_style_default.png rendering status: OK
****** Changed ACS settings *****
acs_style_changed.png rendering status: OK
4 changes: 2 additions & 2 deletions api/tests/integration/tests/layout/acs_style_reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def find_diff(a, b):
ket = rxn.json()
diff = find_diff(ket_ref, ket)
if not diff:
print(filename + ".ket:SUCCEED")
print(filename + ":SUCCEED")
else:
print(filename + ".ket:FAILED")
print(filename + ":FAILED")
print(diff)
Loading

0 comments on commit 45da810

Please sign in to comment.