import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

import java.io.PrintStream;
import java.util.*;

/**
 * Standalone SWT text-measurement snippet — NO Archi/coArchi/GEF dependency.
 *
 * Purpose: isolate whether org.eclipse.swt.graphics.GC#textExtent() (the
 * primitive Archi's diagram figures ultimately use to lay out text) returns
 * DIFFERENT values headless (Xvfb, Linux) vs. on a real display (e.g.
 * Windows desktop), for the *same* font name/size/text. If it does, that is
 * conclusive, minimal, reproducible evidence of a platform/headless
 * discrepancy in SWT itself — exactly what the Archi maintainer asked for
 * before this can be escalated upstream to the Eclipse Platform SWT project.
 *
 * HOW TO USE:
 *
 * 1) Find the SWT jar that ships inside the SAME Archi installation you're
 *    comparing (do this separately on Linux/headless and on Windows/desktop
 *    — they are DIFFERENT jars, org.eclipse.swt.gtk.linux.x86_64_* vs.
 *    org.eclipse.swt.win32.win32.x86_64_*):
 *
 *        find /opt/Archi/plugins -iname "org.eclipse.swt.*.jar"
 *
 * 2) Compile (Linux, inside the container):
 *
 *        javac -cp "$(find /opt/Archi/plugins -iname 'org.eclipse.swt.gtk.linux.x86_64_*.jar')" \
 *              TextExtentTest.java
 *
 *    Compile (Windows, against the SWT jar from a Windows Archi install):
 *
 *        javac -cp "C:\Program Files\Archi\plugins\org.eclipse.swt.win32.win32.x86_64_*.jar" TextExtentTest.java
 *
 * 3) Run (Linux, headless under Xvfb — same as entrypoint.sh does for Archi):
 *
 *        xvfb-run -a java -cp ".:$(find /opt/Archi/plugins -iname 'org.eclipse.swt.gtk.linux.x86_64_*.jar')" \
 *              TextExtentTest
 *
 *    Run (Windows, normal desktop — real display, no Xvfb):
 *
 *        java -cp ".;C:\Program Files\Archi\plugins\org.eclipse.swt.win32.win32.x86_64_*.jar" TextExtentTest
 *
 * 4) Compare the two output tables side by side. Identical numbers for every
 *    row = SWT itself is consistent, and the bug is elsewhere in Archi's own
 *    layout code. Different numbers = confirmed SWT/platform-level
 *    discrepancy, worth reporting to eclipse-platform/eclipse.platform.swt
 *    with this exact snippet + both outputs attached.
 */
public class TextExtentTest {

    // Real strings pulled from the affected model (the ones that were
    // overflowing their figure in the Archi-generated report), plus a
    // couple of ASCII control cases for sanity-checking.
    static final String[] TEST_STRINGS = {
        "Компонент",
        "финансово-экономического",
        "ОООС «Ванильный Бюджет» (новая версия)",
        "топик 20 \nзапрос на формирование УГПП по",
        "192.168.231.150 \ngoogle.com",
        "Hello World",                     // ASCII sanity check
        "AAAAAAAAAAAAAAAAAAAA",             // fixed-width-ish sanity check
    };

    static final String[] FONT_NAMES = { "Segoe UI", "DejaVu Sans", "Sans" };
    static final int FONT_SIZE = 9;

    public static void main(String[] args) throws Exception {
        System.setOut(new PrintStream(System.out, true, "UTF-8"));

        Display display = new Display();
        try {
            Image dummy = new Image(display, 10, 10);
            GC gc = new GC(dummy);

            System.out.println("SWT platform: " + SWT.getPlatform());
            System.out.println("Font size requested: " + FONT_SIZE);
            System.out.println();
            System.out.printf("%-20s | %-45s | %-6s | %-6s%n", "font_requested", "text", "width", "height");
            System.out.println("-".repeat(90));

            for (String fontName : FONT_NAMES) {
                Font font = new Font(display, fontName, FONT_SIZE, SWT.NORMAL);
                gc.setFont(font);

                // Report what SWT actually resolved the font to (name may
                // differ from what was requested if substitution happened)
                FontData[] fd = gc.getFont().getFontData();
                String resolvedName = fd.length > 0 ? fd[0].getName() : "?";
                int resolvedHeight = fd.length > 0 ? fd[0].getHeight() : -1;

                for (String text : TEST_STRINGS) {
                    // textExtent handles embedded \n by measuring the widest
                    // line and total height across lines, same primitive
                    // Archi's figure layout would use internally
                    Point extent = gc.textExtent(text);
                    String shortText = text.replace("\n", "\\n");
                    if (shortText.length() > 43) shortText = shortText.substring(0, 40) + "...";
                    System.out.printf("%-20s | %-45s | %-6d | %-6d%n",
                            fontName + " (resolved: " + resolvedName + " " + resolvedHeight + "pt)",
                            shortText, extent.x, extent.y);
                }
                font.dispose();
                System.out.println();
            }

            gc.dispose();
            dummy.dispose();
        } finally {
            display.dispose();
        }
    }
}
