mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-07 22:44:25 +08:00
Swig MolDraw2D cairo (#5128)
* MolDraw2DCairo working in SWIG * C# test
This commit is contained in:
@@ -45,6 +45,12 @@
|
||||
#include <utility>
|
||||
%}
|
||||
|
||||
#ifdef RDK_BUILD_CAIRO_SUPPORT
|
||||
%{
|
||||
#include <GraphMol/MolDraw2D/MolDraw2DCairo.h>
|
||||
%}
|
||||
#endif
|
||||
|
||||
%template(Int_String_Map) std::map< int, std::string >;
|
||||
|
||||
#ifdef SWIGJAVA
|
||||
@@ -74,6 +80,47 @@
|
||||
%include <GraphMol/MolDraw2D/MolDraw2DHelpers.h>
|
||||
%include <GraphMol/MolDraw2D/MolDraw2D.h>
|
||||
%include <GraphMol/MolDraw2D/MolDraw2DSVG.h>
|
||||
#ifdef RDK_BUILD_CAIRO_SUPPORT
|
||||
|
||||
#ifdef SWIGJAVA
|
||||
%typemap(jni) std::string RDKit::MolDraw2DCairo::toByteArray "jbyteArray"
|
||||
%typemap(jtype) std::string RDKit::MolDraw2DCairo::toByteArray "byte[]"
|
||||
%typemap(jstype) std::string RDKit::MolDraw2DCairo::toByteArray "byte[]"
|
||||
%typemap(javaout) std::string RDKit::MolDraw2DCairo::toByteArray {
|
||||
return $jnicall;
|
||||
}
|
||||
%typemap(out) std::string RDKit::MolDraw2DCairo::toByteArray {
|
||||
$result = JCALL1(NewByteArray, jenv, $1.size());
|
||||
JCALL4(SetByteArrayRegion, jenv, $result, 0, $1.size(), (const jbyte*)$1.c_str());
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SWIGCSHARP
|
||||
%template(UChar_Vect) std::vector<unsigned char>;
|
||||
#endif
|
||||
|
||||
%include <GraphMol/MolDraw2D/MolDraw2DCairo.h>
|
||||
|
||||
#ifdef SWIGJAVA
|
||||
%extend RDKit::MolDraw2DCairo {
|
||||
const std::string toByteArray() {
|
||||
return ($self)->getDrawingText();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SWIGCSHARP
|
||||
%extend RDKit::MolDraw2DCairo {
|
||||
const std::vector<unsigned char> getImage() {
|
||||
const auto text = ($self)->getDrawingText();
|
||||
const std::vector<unsigned char> image(text.begin(), text.end());
|
||||
return image;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif // RDK_BUILD_CAIRO_SUPPORT
|
||||
|
||||
%include <GraphMol/MolDraw2D/MolDraw2DUtils.h>
|
||||
|
||||
%inline %{
|
||||
|
||||
@@ -49,6 +49,10 @@ endif()
|
||||
if(RDK_USE_BOOST_IOSTREAMS)
|
||||
SET(CMAKE_SWIG_FLAGS "-DRDK_USE_BOOST_IOSTREAMS" ${CMAKE_SWIG_FLAGS} )
|
||||
endif()
|
||||
if (RDK_BUILD_CAIRO_SUPPORT)
|
||||
SET(CMAKE_SWIG_FLAGS "-DRDK_BUILD_CAIRO_SUPPORT" ${CMAKE_SWIG_FLAGS} )
|
||||
endif()
|
||||
|
||||
|
||||
FILE(GLOB SWIG_SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/../*.i")
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using GraphMolWrap;
|
||||
using Xunit;
|
||||
|
||||
namespace RdkitTests;
|
||||
|
||||
public class MolDraw2DCairoTest
|
||||
{
|
||||
[Fact]
|
||||
public void TestDrawMolecule()
|
||||
{
|
||||
var mol = RWMol.MolFromSmiles("c1ccc(C)c(C)c1C");
|
||||
var drawer = new MolDraw2DCairo(300, 300, -1, -1, true);
|
||||
drawer.drawOptions().addAtomIndices = true;
|
||||
drawer.drawMolecule(mol);
|
||||
drawer.finishDrawing();
|
||||
var png1 = drawer.getImage().ToArray();
|
||||
drawer.writeDrawingText("test.png");
|
||||
byte[] png2 = File.ReadAllBytes("test.png");
|
||||
Assert.Equal(png1.Length, png2.Length);
|
||||
for (int i=0; i<png1.Length; i++) {
|
||||
Assert.Equal(png1[i], png2[i]);
|
||||
}
|
||||
File.Delete("test.png");
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,9 @@ endif()
|
||||
if(RDK_USE_BOOST_IOSTREAMS)
|
||||
SET(CMAKE_SWIG_FLAGS "-DRDK_USE_BOOST_IOSTREAMS" ${CMAKE_SWIG_FLAGS} )
|
||||
endif()
|
||||
|
||||
if (RDK_BUILD_CAIRO_SUPPORT)
|
||||
SET(CMAKE_SWIG_FLAGS "-DRDK_BUILD_CAIRO_SUPPORT" ${CMAKE_SWIG_FLAGS} )
|
||||
endif()
|
||||
|
||||
|
||||
# enable this line to build the ErrorGenerator class for testing handling of C++ errors in the JNI layer
|
||||
@@ -177,6 +179,10 @@ if(NOT RDK_USE_BOOST_IOSTREAMS)
|
||||
LIST(REMOVE_ITEM JAVA_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src-test/org/RDKit/GzStreamTests.java")
|
||||
endif()
|
||||
|
||||
if (NOT RDK_BUILD_CAIRO_SUPPORT)
|
||||
LIST(REMOVE_ITEM JAVA_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src-test/org/RDKit/MolDraw2DCairoTests.java")
|
||||
endif()
|
||||
|
||||
ADD_CUSTOM_COMMAND(
|
||||
OUTPUT ${CMAKE_JAVA_TEST_OUTDIR}/org/RDKit/WrapperTests.class
|
||||
COMMAND ${JAVA_COMPILE} ${DOCLINT_FLAGS} -d ${CMAKE_JAVA_TEST_OUTDIR} -cp "\"${CMAKE_CURRENT_SOURCE_DIR}/org.RDKit.jar${PATH_SEP}${JUNIT_JAR}\"" ${JAVA_TEST_FILES}
|
||||
@@ -407,7 +413,12 @@ ADD_TEST(JavaSubstanceGroupTests
|
||||
-cp "${JUNIT_JAR}${PATH_SEP}${CMAKE_JAVA_TEST_OUTDIR}${PATH_SEP}${CMAKE_CURRENT_SOURCE_DIR}/org.RDKit.jar"
|
||||
org.RDKit.SubstanceGroupTests)
|
||||
|
||||
|
||||
if (RDK_BUILD_CAIRO_SUPPORT)
|
||||
ADD_TEST(JavaMolDraw2DCairoTests
|
||||
java -Djava.library.path=${CMAKE_CURRENT_SOURCE_DIR}
|
||||
-cp "${JUNIT_JAR}${PATH_SEP}${CMAKE_JAVA_TEST_OUTDIR}${PATH_SEP}${CMAKE_CURRENT_SOURCE_DIR}/org.RDKit.jar"
|
||||
org.RDKit.MolDraw2DCairoTests)
|
||||
endif (RDK_BUILD_CAIRO_SUPPORT)
|
||||
|
||||
INSTALL(TARGETS GraphMolWrap
|
||||
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} )
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package org.RDKit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MolDraw2DCairoTests extends GraphMolTest{
|
||||
|
||||
@Test
|
||||
public void createImage() throws IOException {
|
||||
RWMol mol = RWMol.MolFromSmiles("c1ccc(C)c(C)c1C");
|
||||
MolDraw2DCairo drawer = new MolDraw2DCairo(300, 300, -1, -1, true);
|
||||
drawer.drawOptions().setAddAtomIndices(true);
|
||||
drawer.drawMolecule(mol);
|
||||
drawer.finishDrawing();
|
||||
byte[] png1 = drawer.toByteArray();
|
||||
// Files.write(Paths.get("test2.png"), png);
|
||||
drawer.writeDrawingText("test.png");
|
||||
byte[] png2 = Files.readAllBytes(Paths.get("test.png"));
|
||||
assertEquals(png1.length, png2.length);
|
||||
for (int i=0; i<png1.length; i++) {
|
||||
assertEquals(png1[i], png2[i]);
|
||||
}
|
||||
Files.delete(Paths.get("test.png"));
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
org.junit.runner.JUnitCore.main("org.RDKit.MolDraw2DCairoTests");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user