Skip to content

Commit

Permalink
Fixed #506 Add a fitAll() service that call fit() on all MImages of a…
Browse files Browse the repository at this point in the history
… MElement.
  • Loading branch information
ylussaud committed Oct 27, 2023
1 parent 3ca1877 commit 4c26979
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
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;
import org.eclipse.acceleo.annotations.api.documentation.Param;
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;
Expand Down Expand Up @@ -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<MElement> 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;
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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 <a href="mailto:[email protected]">Yvan Lussaud</a>
*/
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;
}

}

0 comments on commit 4c26979

Please sign in to comment.