Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logos are cut off when displayed in the second or following column of a table #2752

Merged
merged 2 commits into from Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -179,7 +179,7 @@ else if (attribute.getType().getConverter() instanceof ImageConverter)
preview.setText(previewPlaceholderText);

ImageConverter conv = (ImageConverter) attribute.getType().getConverter();
Image img = ImageUtil.toImage(conv.toString(attribute.getValue()), 16, 16);
Image img = ImageUtil.toImage(conv.toString(attribute.getValue()), 0, 16, 16);
if (img != null)
preview.setImage(img);

Expand All @@ -206,7 +206,7 @@ public IStatus validate(Object value)
return Status.OK_STATUS;
}

Image img = ImageUtil.toImage(s, 16, 16);
Image img = ImageUtil.toImage(s, 0, 16, 16);

updatePreview(img);
return img == null ? Status.CANCEL_STATUS : Status.OK_STATUS;
Expand Down
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;

import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.graphics.Image;

import name.abuchen.portfolio.model.AttributeType.ImageConverter;
Expand All @@ -23,10 +24,11 @@ private ImageManager()

public Image getImage(Attributable target, AttributeType attr)
{
return getImage(target, attr, 16, 16);
int xOffset = Platform.OS_WIN32.equals(Platform.getOS()) ? 1 : 0;
return getImage(target, attr, xOffset, 16, 16);
}

public Image getImage(Attributable target, AttributeType attr, int width, int height)
public Image getImage(Attributable target, AttributeType attr, int xOffset, int width, int height)
{
if (target != null && target.getAttributes().exists(attr) && attr.getConverter() instanceof ImageConverter)
{
Expand All @@ -35,14 +37,14 @@ public Image getImage(Attributable target, AttributeType attr, int width, int he
return null;

String imgString = String.valueOf(imgObject);
String imgKey = imgString + width + height;
String imgKey = imgString + width + height + xOffset;
synchronized (imageCache)
{
Image img = imageCache.getOrDefault(imgKey, null);
if (img != null)
return img;

img = ImageUtil.toImage(imgString, width, height);
img = ImageUtil.toImage(imgString, xOffset, width, height);
if (img != null)
{
imageCache.put(imgKey, img);
Expand Down
Expand Up @@ -27,11 +27,13 @@ private static class ZoomingImageDataProvider implements ImageDataProvider
{
private int logicalWidth;
private int logicalHeight;
private int xOffset;
private ImageData fullSize;
private HashMap<Integer, ImageData> zoomLevels = new HashMap<Integer, ImageData>();

public ZoomingImageDataProvider(byte[] data, int logicalWidth, int logicalHeight)
public ZoomingImageDataProvider(byte[] data, int xOffset, int logicalWidth, int logicalHeight)
{
this.xOffset = xOffset;
this.logicalWidth = logicalWidth;
this.logicalHeight = logicalHeight;
this.fullSize = ImageUtil.toImageData(data);
Expand All @@ -47,7 +49,7 @@ public ImageData getImageData(int zoom)
float scaleW = 1f / fullSize.width * logicalWidth * (zoom / 100f);
float scaleH = 1f / fullSize.height * logicalHeight * (zoom / 100f);

imageData = ImageUtil.resize(fullSize, (int) (fullSize.width * scaleW), (int) (fullSize.height * scaleH),
imageData = ImageUtil.resize(fullSize, xOffset, (int) (fullSize.width * scaleW), (int) (fullSize.height * scaleH),
false);

zoomLevels.put(zoom, imageData);
Expand All @@ -56,7 +58,15 @@ public ImageData getImageData(int zoom)
}
}

public static Image toImage(String value, int logicalWidth, int logicalHeight)
/**
*
* @param value
* @param xOffset Additional transparent offset. Width of the resulting image is (xOffset + maxWidth)
* @param logicalWidth
* @param logicalHeight
* @return
*/
public static Image toImage(String value, int xOffset, int logicalWidth, int logicalHeight)
{
if (value == null || value.length() == 0)
return null;
Expand All @@ -73,7 +83,7 @@ public static Image toImage(String value, int logicalWidth, int logicalHeight)
if (buff == null || buff.length == 0)
return null;

return new Image(null, new ZoomingImageDataProvider(buff, logicalWidth, logicalHeight));
return new Image(null, new ZoomingImageDataProvider(buff, xOffset, logicalWidth, logicalHeight));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -119,13 +129,13 @@ public static String loadAndPrepare(String filename, int maxWidth, int maxHeight

if (imgData.width > maxWidth || imgData.height > maxHeight)
{
imgData = ImageUtil.resize(imgData, maxWidth, maxHeight, true);
imgData = ImageUtil.resize(imgData, 0, maxWidth, maxHeight, true);
data = ImageUtil.encode(imgData);
}
return BASE64PREFIX + Base64.getEncoder().encodeToString(data);
}

private static ImageData resize(ImageData image, int maxWidth, int maxHeight, boolean preserveRatio)
private static ImageData resize(ImageData image, int xOffset, int maxWidth, int maxHeight, boolean preserveRatio)
{
if (image.width == maxWidth && image.height == maxHeight)
return image;
Expand All @@ -148,7 +158,7 @@ private static ImageData resize(ImageData image, int maxWidth, int maxHeight, bo
if (posY + newHeight > imageHeight)
newWidth = imageHeight - posY;

ImageData imageData = getTransparentImage(imageWidth, imageHeight);
ImageData imageData = getTransparentImage(imageWidth + xOffset, imageHeight);

Image canvas = new Image(null, imageData);

Expand All @@ -157,7 +167,7 @@ private static ImageData resize(ImageData image, int maxWidth, int maxHeight, bo
gc.setInterpolation(SWT.HIGH);

Image source = new Image(null, image);
gc.drawImage(source, 0, 0, image.width, image.height, posX, posY, newWidth, newHeight);
gc.drawImage(source, 0, 0, image.width, image.height, posX+xOffset, posY, newWidth, newHeight);
gc.dispose();
source.dispose();

Expand Down