001package conexp.fx.core.exporter;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025
026import java.io.File;
027import java.util.Map;
028
029import conexp.fx.core.context.MatrixContext;
030import conexp.fx.core.layout.AdditiveConceptLayout;
031
032
033public class PDFExporter<G, M> {
034
035  public static <G, M> void export(
036      MatrixContext<G, M> context,
037      Map<Integer, Integer> domainPermutation,
038      Map<Integer, Integer> codomainPermutation,
039      AdditiveConceptLayout<G, M> layout,
040      boolean exportArrows,
041      boolean exportLabels,
042      File file) {
043//    try {
044//      if (!file.exists()) {
045//        if (!file.getParentFile().exists())
046//          file.mkdirs();
047//        file.createNewFile();
048//      }
049//      final double width = 100d * layout.getCurrentBoundingBox().getWidth();
050//      final double minX = 100d * layout.getCurrentBoundingBox().getMinX();
051//      final double height = 100d * layout.getCurrentBoundingBox().getHeight();
052//      // final double unit = Math.min(160 / width, 230 / height);
053//      final int border = 100;
054//      final int circleSize = 20;
055//      final int textOffset = 5;
056//      final Document doc = new Document(PageSize.A4);
057//      FileOutputStream outputStream = new FileOutputStream(file);
058//      PdfWriter writer = PdfWriter.getInstance(doc, outputStream);
059//      doc.open();
060//      PdfContentByte content = writer.getDirectContent();
061//      final float dw = doc.getPageSize().getWidth();
062//      final float dh = doc.getPageSize().getHeight();
063//      final float cw = (float) width + border;
064//      final float ch = (float) height + border;
065//      final float wr = dw / cw;
066//      final float hr = dh / ch;
067//      final float f = (wr > hr ? hr : wr);
068//      Graphics2D gfx = content.createGraphics(dw, dh);
069//      gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
070//      gfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
071//      gfx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
072//      gfx.setBackground(Color.WHITE);
073//      gfx.setColor(Color.WHITE);
074//      gfx.fillRect(0, 0, (int) width + border, (int) height + border);
075//      gfx.setColor(Color.BLACK);
076//      for (int i = 0; i < layout.lattice.rowHeads().size(); i++) {
077//        Concept<G, M> conceptNode = layout.lattice.rowHeads().get(i);
078//        final double x = 100d * layout.positions.get(conceptNode).getX();
079//        final double y = 100d * layout.positions.get(conceptNode).getY();
080//        gfx.fillOval((int) (f * ((x - minX) - (3 * circleSize / 4) + (border / 2))), (int) (f * (y
081//            - (3 * circleSize / 4) + (border / 2))), circleSize, circleSize);
082//      }
083//      for (int i = 0; i < layout.lattice.rowHeads().size(); i++) {
084//        for (int j = 0; j < layout.lattice.rowHeads().size(); j++) {
085//          if (layout.lattice._contains(i, j)) {
086//            final int x1 =
087//                (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(i)).getX() - minX + (border / 2)));
088//            final int y1 =
089//                (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(i)).getY() + (border / 2)));
090//            final int x2 =
091//                (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(j)).getX() - minX + (border / 2)));
092//            final int y2 =
093//                (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(j)).getY() + (border / 2)));
094//            gfx.drawLine(x1, y1, x2, y2);
095//          }
096//        }
097//      }
098//      if (exportLabels)
099//        for (int i = 0; i < layout.lattice.rowHeads().size(); i++) {
100//          Concept<G, M> conceptNode = layout.lattice.rowHeads().get(i);
101//          String objLabels =
102//              layout.lattice
103//                  .objectLabels(conceptNode)
104//                  .toString()
105//                  .substring(1, layout.lattice.objectLabels(conceptNode).toString().length() - 1);
106//          String attLabels =
107//              layout.lattice
108//                  .attributeLabels(conceptNode)
109//                  .toString()
110//                  .substring(1, layout.lattice.attributeLabels(conceptNode).toString().length() - 1);
111//          final int owidth = (int) (f * gfx.getFontMetrics().stringWidth(objLabels));
112//          final int awidth = (int) (f * gfx.getFontMetrics().stringWidth(attLabels));
113//          final int theight = gfx.getFontMetrics().getHeight();
114//          final int x =
115//              (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(i)).getX() - minX + (border / 2)));
116//          final int ay =
117//              (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(i)).getY()
118//                  - (circleSize + textOffset) + (border / 2)));
119//          final int oy =
120//              (int) (f * (100d * layout.positions.get(layout.lattice.rowHeads().get(i)).getY()
121//                  + (circleSize + textOffset) + (border / 2) + (theight / 2)));
122//          gfx.drawString(objLabels, x - (owidth / 2), oy);
123//          gfx.drawString(attLabels, x - (awidth / 2), ay);
124//        }
125//      gfx.dispose();
126//      doc.close();
127//      outputStream.flush();
128//      outputStream.close();
129//    } catch (IOException | DocumentException e) {
130//      System.err.println("Unable to create or write PDFDocument to file " + file);
131//      e.printStackTrace();
132//    }
133  }
134}