Skip to content

Commit

Permalink
[937] Add diagram to SVG service
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#937
Signed-off-by: Raphaël Pagé <raphael.page@obeo.fr>
  • Loading branch information
RaphaelPageObeo committed Jan 27, 2022
1 parent 0b0a0eb commit a06d410
Show file tree
Hide file tree
Showing 12 changed files with 1,145 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.dto;

import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramInput;

/**
* The input object for the export representation service.
*
* @author rpage
*/
public final class ExportRepresentationInput implements IDiagramInput {
private final UUID id;

private final String representationId;

public ExportRepresentationInput(UUID id, String representationId) {
this.id = Objects.requireNonNull(id);
this.representationId = Objects.requireNonNull(representationId);
}

@Override
public String getRepresentationId() {
return this.representationId;
}

@Override
public UUID getId() {
return this.id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.dto;

import java.text.MessageFormat;
import java.util.Objects;
import java.util.UUID;

import org.eclipse.sirius.components.core.api.IPayload;

/**
* The payload of the export representation handler.
*
* @author rpage
*/
public final class ExportRepresentationPayload implements IPayload {
private final UUID id;

private final String name;

private final String content;

public ExportRepresentationPayload(UUID id, String name, String svgExport) {
this.id = Objects.requireNonNull(id);
this.name = Objects.requireNonNull(name);
this.content = Objects.requireNonNull(svgExport);
}

@Override
public UUID getId() {
return this.id;
}

public String getName() {
return this.name;
}

public String getContent() {
return this.content;
}

@Override
public String toString() {
String pattern = "{0} '{'id: {1}, name: {2}, content: {3} '}'"; //$NON-NLS-1$
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.name, this.content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.export.api;

import org.eclipse.sirius.components.diagrams.Diagram;

/**
* Interface of the service used to export a diagram to SVG.
*
* @author rpage
*/
public interface ISVGDiagramExportService {
String export(Diagram diagram);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*******************************************************************************
* Copyright (c) 2022 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.collaborative.diagrams.export.svg;

import java.util.Optional;

import org.eclipse.sirius.components.diagrams.Label;
import org.eclipse.sirius.components.diagrams.LabelStyle;
import org.eclipse.sirius.components.diagrams.LineStyle;
import org.eclipse.sirius.components.diagrams.Node;
import org.eclipse.sirius.components.diagrams.Position;
import org.eclipse.sirius.components.diagrams.Size;
import org.springframework.stereotype.Service;

/**
* Contains methods used when exporting diagram elements.
*
* @author rpage
*/
@Service
@SuppressWarnings("checkstyle:MultipleStringLiterals")
public class DiagramElementExportService {
public StringBuilder exportLabel(Label label) {
StringBuilder labelExport = new StringBuilder();
Position position = label.getPosition();
Position alignment = label.getAlignment();
LabelStyle style = label.getStyle();

labelExport.append("<g "); //$NON-NLS-1$
labelExport.append("transform=\""); //$NON-NLS-1$
labelExport.append("translate(" + position.getX() + ", " + position.getY() + ") "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
labelExport.append("translate(" + alignment.getX() + ", " + alignment.getY() + ")\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
labelExport.append(">"); //$NON-NLS-1$

if (!(style.getIconURL().isEmpty())) {
labelExport.append(this.exportImageElement(style.getIconURL(), -20, -12, Optional.empty()));
}

labelExport.append(this.exportTextElement(label.getText(), style));

return labelExport.append("</g>"); //$NON-NLS-1$
}

public StringBuilder exportImageElement(String imageURL, int x, int y, Optional<Size> size) {
StringBuilder imageExport = new StringBuilder();

imageExport.append("<image "); //$NON-NLS-1$
imageExport.append("href=\"" + imageURL + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
imageExport.append("x=\"0\" "); //$NON-NLS-1$
imageExport.append("y=\"0\" "); //$NON-NLS-1$
size.ifPresent(it -> imageExport.append(this.addSizeParam(it)));

return imageExport.append("/>"); //$NON-NLS-1$
}

/**
* Export the g element containing the position of a diagram element.
*
* @param node
* The diagram element
* @return The g element containing its position
*/
public StringBuilder exportGNodeElement(Node node) {
StringBuilder gExport = new StringBuilder();

gExport.append("<g "); //$NON-NLS-1$
gExport.append("transform=\"translate(" + node.getPosition().getX() + ", " + node.getPosition().getY() + ")\" "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

return gExport.append(">"); //$NON-NLS-1$
}

public StringBuilder exportRectangleElement(Size size, Optional<Integer> borderRadius, Optional<String> color, Optional<String> borderColor, Optional<Integer> borderSize,
Optional<LineStyle> borderStyle) {
StringBuilder rectangle = new StringBuilder();

rectangle.append("<rect "); //$NON-NLS-1$
rectangle.append("x=\"0\" "); //$NON-NLS-1$
rectangle.append("y=\"0\" "); //$NON-NLS-1$
borderRadius.ifPresent(radius -> rectangle.append("rx=\"" + radius + "\" ")); //$NON-NLS-1$ //$NON-NLS-2$
rectangle.append(this.addSizeParam(size));
rectangle.append(this.exportRectangleStyleParam(color, borderColor, borderSize, borderStyle));

return rectangle.append("/> "); //$NON-NLS-1$
}

private StringBuilder addSizeParam(Size size) {
StringBuilder sizeParam = new StringBuilder();
sizeParam.append("width=\"" + size.getWidth() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
return sizeParam.append("height=\"" + size.getHeight() + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
}

private StringBuilder exportTextElement(String text, LabelStyle labelStyle) {
StringBuilder textExport = new StringBuilder();

textExport.append("<text "); //$NON-NLS-1$
textExport.append("style=\""); //$NON-NLS-1$
textExport.append("fill: " + labelStyle.getColor() + "; "); //$NON-NLS-1$ //$NON-NLS-2$
textExport.append(this.exportFont(labelStyle));
textExport.append("\">"); //$NON-NLS-1$

textExport.append(text);

return textExport.append("</text>"); //$NON-NLS-1$
}

private StringBuilder exportFont(LabelStyle labelStyle) {
StringBuilder textExport = new StringBuilder();

textExport.append("font-size: " + labelStyle.getFontSize() + "px; "); //$NON-NLS-1$ //$NON-NLS-2$
textExport.append("font-family: Arial, Helvetica, sans-serif; "); //$NON-NLS-1$

textExport.append("font-weight: "); //$NON-NLS-1$
if (labelStyle.isBold()) {
textExport.append("bold"); //$NON-NLS-1$
} else {
textExport.append("normal"); //$NON-NLS-1$
}
textExport.append("; "); //$NON-NLS-1$

textExport.append("font-style: "); //$NON-NLS-1$
if (labelStyle.isItalic()) {
textExport.append("italic"); //$NON-NLS-1$
} else {
textExport.append("normal"); //$NON-NLS-1$
}
textExport.append("; "); //$NON-NLS-1$

textExport.append("text-decoration: "); //$NON-NLS-1$
if (labelStyle.isUnderline() || labelStyle.isStrikeThrough()) {
if (labelStyle.isUnderline()) {
textExport.append("underline "); //$NON-NLS-1$
}
if (labelStyle.isStrikeThrough()) {
textExport.append("line-through"); //$NON-NLS-1$
}
} else {
textExport.append("none"); //$NON-NLS-1$
}
return textExport.append(";"); //$NON-NLS-1$
}

private StringBuilder exportRectangleStyleParam(Optional<String> color, Optional<String> borderColor, Optional<Integer> borderSize, Optional<LineStyle> borderStyle) {
StringBuilder styleExport = new StringBuilder();

styleExport.append("style=\""); //$NON-NLS-1$
color.ifPresent(it -> styleExport.append("fill: " + it + "; ")); //$NON-NLS-1$ //$NON-NLS-2$
borderColor.ifPresent(it -> styleExport.append("stroke: " + it + "; ")); //$NON-NLS-1$ //$NON-NLS-2$
borderSize.ifPresent(it -> styleExport.append("stroke-width: " + it + "px; ")); //$NON-NLS-1$ //$NON-NLS-2$
borderStyle.ifPresent(style -> styleExport.append(this.exportBorderStyle(style)));

return styleExport.append("\""); //$NON-NLS-1$
}

private StringBuilder exportBorderStyle(LineStyle borderStyle) {
StringBuilder style = new StringBuilder();
switch (borderStyle.toString()) {
case "Dash": //$NON-NLS-1$
style.append(" stroke-dasharray: 4, 4;"); //$NON-NLS-1$
break;
case "Dot": //$NON-NLS-1$
style.append(" stroke-dasharray: 2, 2;"); //$NON-NLS-1$
break;
case "Dash_Dot": //$NON-NLS-1$
style.append(" stroke-dasharray: 2, 4, 2;"); //$NON-NLS-1$
break;
default:
break;
}
return style;
}
}
Loading

0 comments on commit a06d410

Please sign in to comment.