Moved back the wide SVG images size compensation from TextureData to ImageComponent.

This commit is contained in:
Leon Styhre 2022-09-05 23:00:47 +02:00
parent 5d78412b9d
commit 01d410867c
2 changed files with 11 additions and 17 deletions

View file

@ -74,9 +74,12 @@ void ImageComponent::resize(bool rasterize)
glm::vec2 resizeScale {mTargetSize.x / mSize.x, mTargetSize.y / mSize.y};
if (resizeScale.x < resizeScale.y) {
// This will be mTargetSize.x. We can't exceed it, nor be lower than it.
// SVG rasterization is determined by height and rasterization is done in terms of
// pixels. If rounding is off enough in the rasterization step (for images with
// extreme aspect ratios), it can cause cutoff when the aspect ratio breaks.
// So we always make sure to round accordingly to avoid such issues.
mSize.x *= resizeScale.x;
mSize.y = std::min(mSize.y * resizeScale.x, mTargetSize.y);
mSize.y = floorf(std::min(mSize.y * resizeScale.x, mTargetSize.y));
}
else {
// This will be mTargetSize.y(). We can't exceed it.

View file

@ -108,31 +108,22 @@ bool TextureData::initSVGFromMemory(const std::string& fileData)
}
if (rasterize) {
float height {static_cast<float>(mHeight)};
const float aspectW {svgImage->width / static_cast<float>(mWidth)};
const float aspectH {svgImage->height / static_cast<float>(mHeight)};
if (mScalableNonAspect && aspectW != aspectH) {
// If the size property has been used to override the aspect ratio of the SVG image,
// then we need to rasterize at a lower resolution and let the GPU scale the texture.
// This is necessary as the rasterization always maintains the image aspect ratio.
// If the size property has been used to override the aspect ratio of the SVG image,
// then we need to rasterize at a lower resolution and let the GPU scale the texture.
// This is necessary as the rasterization always maintains the image aspect ratio.
if (mScalableNonAspect && aspectW != aspectH)
mWidth = static_cast<int>(std::round(svgImage->width / aspectH));
}
else {
// For very wide and short images we need to check that the target width is enough
// to fit the rasterized image, if not we'll decrease the height as needed.
const float requiredWidth {height * (svgImage->width / svgImage->height)};
if (std::round(requiredWidth) > static_cast<float>(mWidth))
height = std::floor(height * (static_cast<float>(mWidth) / requiredWidth));
}
std::vector<unsigned char> tempVector;
tempVector.reserve(mWidth * mHeight * 4);
NSVGrasterizer* rast {nsvgCreateRasterizer()};
nsvgRasterize(rast, svgImage, 0, 0, height / svgImage->height, tempVector.data(), mWidth,
mHeight, mWidth * 4);
nsvgRasterize(rast, svgImage, 0, 0, static_cast<float>(mHeight) / svgImage->height,
tempVector.data(), mWidth, mHeight, mWidth * 4);
nsvgDeleteRasterizer(rast);