diff --git a/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/services/ImageServices.java b/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/services/ImageServices.java index c1271b865..304e45848 100644 --- a/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/services/ImageServices.java +++ b/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/services/ImageServices.java @@ -16,6 +16,8 @@ import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.eclipse.acceleo.annotations.api.documentation.Documentation; import org.eclipse.acceleo.annotations.api.documentation.Example; @@ -23,7 +25,13 @@ import org.eclipse.acceleo.annotations.api.documentation.ServiceProvider; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.URIConverter; +import org.obeonetwork.m2doc.element.MElement; import org.obeonetwork.m2doc.element.MImage; +import org.obeonetwork.m2doc.element.MList; +import org.obeonetwork.m2doc.element.MParagraph; +import org.obeonetwork.m2doc.element.MTable; +import org.obeonetwork.m2doc.element.MTable.MCell; +import org.obeonetwork.m2doc.element.MTable.MRow; import org.obeonetwork.m2doc.element.PictureType; import org.obeonetwork.m2doc.element.impl.MImageAWTImpl; import org.obeonetwork.m2doc.element.impl.MImageImpl; @@ -323,4 +331,57 @@ public MImage rotate(MImage image, Integer angle) throws IOException { return new MImageAWTImpl(rotated, image.getURI()); } + // @formatter:off + @Documentation( + value = "Fits all Images of the given MElement in the given rectangle width and height.", + params = { + @Param(name = "element", value = "The MElement"), + @Param(name = "width", value = "The width to fit"), + @Param(name = "height", value = "The height to fit"), + @Param(name = "zoomIn", value = "The image will be zoomed in if smaller"), + }, + result = "the MElement with resized images", + examples = { + @Example(expression = "myImage.fitAll(200, 300, false)", result = "will fit all images in a rectangle (width=200, height=300) if the original image size is smaller it will not be zoomed in"), + } + ) + // @formatter:on + public MElement fitAll(MElement element, Integer width, Integer height, boolean zoomIn) { + final MElement res; + + if (element instanceof MList) { + final List newElements = new ArrayList<>(); + for (MElement e : (MList) element) { + newElements.add(fitAll(e, width, height, zoomIn)); + } + ((MList) element).clear(); + ((MList) element).addAll(newElements); + res = element; + } else if (element instanceof MParagraph) { + final MElement newContent = fitAll(((MParagraph) element).getContents(), width, height, zoomIn); + if (newContent != ((MParagraph) element).getContents()) { + ((MParagraph) element).setContents(newContent); + } + res = element; + } else if (element instanceof MTable) { + for (MRow row : ((MTable) element).getRows()) { + for (MCell cell : row.getCells()) { + final MElement newContent = fitAll(cell.getContents(), width, height, zoomIn); + if (newContent != cell.getContents()) { + cell.setContents(newContent); + } + } + } + res = element; + } else if (element instanceof MImage) { + final MImage image = (MImage) element; + fit(image, width, height, zoomIn); + res = image; + } else { + res = element; + } + + return res; + } + } diff --git a/tests/org.obeonetwork.m2doc.tests/resources/dh1.gif b/tests/org.obeonetwork.m2doc.tests/resources/dh1.gif new file mode 100644 index 000000000..6de1205cf Binary files /dev/null and b/tests/org.obeonetwork.m2doc.tests/resources/dh1.gif differ diff --git a/tests/org.obeonetwork.m2doc.tests/src/org/obeonetwork/m2doc/tests/services/MImageTestServices.java b/tests/org.obeonetwork.m2doc.tests/src/org/obeonetwork/m2doc/tests/services/MImageTestServices.java new file mode 100644 index 000000000..5de211acc --- /dev/null +++ b/tests/org.obeonetwork.m2doc.tests/src/org/obeonetwork/m2doc/tests/services/MImageTestServices.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2023 Obeo. + * All rights reserved. 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 + * http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Obeo - initial API and implementation + * + *******************************************************************************/ +package org.obeonetwork.m2doc.tests.services; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.URIConverter; +import org.obeonetwork.m2doc.element.MElement; +import org.obeonetwork.m2doc.element.MImage; +import org.obeonetwork.m2doc.element.MList; +import org.obeonetwork.m2doc.element.impl.MImageImpl; +import org.obeonetwork.m2doc.element.impl.MListImpl; +import org.obeonetwork.m2doc.element.impl.MParagraphImpl; +import org.obeonetwork.m2doc.element.impl.MTextImpl; + +/** + * Test services for {@link MImage}. + * + * @author Yvan Lussaud + */ +public class MImageTestServices { + + public MElement getMElements(Object object) { + final MList res = new MListImpl(); + + final MParagraphImpl paragraph1 = new MParagraphImpl(new MTextImpl("Some text", null), null); + res.add(paragraph1); + final MImage image = new MImageImpl(URIConverter.INSTANCE, URI.createURI("resources/dh1.gif")); + final MParagraphImpl paragraph2 = new MParagraphImpl(image, null); + res.add(paragraph2); + final MParagraphImpl paragraph3 = new MParagraphImpl(new MTextImpl("More text", null), null); + res.add(paragraph3); + + return res; + } + +}