getEffectiveChildSizeForImage function
Get effective child size for image position calculation for rendering alignment overlay for images.
Except for fitWidth and fitHeight, it returns child size calculated based on the alignment and image scale.
However, if image fit is fitWidth or fitHeight, image is scaled internally
regardless of given PaintModel.scaleX
/PaintModel.scaleY
value to
fit either on width or height. Since this is internal, we need to account
for it because the effective width and child height are no longer depend
on given PaintModel.scaleX
/PaintModel.scaleY
value. For this case, we
manually calculate the effective child size by calculating the linear
scale factor based on node size and image aspect ratio.
This method must be called before calling convertAlignmentToPosition
and convertPositionToAlignment
to get the correct childWidth and
childHeight for these methods.
Implementation
SizeC getEffectiveChildSizeForImage(BaseNode node, PaintModel paint,
{double? scaleX, double? scaleY}) {
final double modifiedScaleX = (scaleX ?? paint.scaleX).abs();
final double modifiedScaleY = (scaleY ?? paint.scaleY).abs();
final double width;
if (paint.fit == Fit.fitWidth) {
width = node.middleBoxGlobal.width;
} else if (paint.fit == Fit.fitHeight && paint.hasImageSourceSize) {
final aspectRatio = paint.sourceWidth! / paint.sourceHeight!;
width = aspectRatio * node.middleBoxGlobal.height;
} else {
width = (paint.sourceWidth ?? node.middleBoxGlobal.width) / modifiedScaleX;
}
final double height;
if (paint.fit == Fit.fitHeight) {
height = node.middleBoxGlobal.height;
} else if (paint.fit == Fit.fitWidth && paint.hasImageSourceSize) {
final aspectRatio = paint.sourceWidth! / paint.sourceHeight!;
height = node.middleBoxGlobal.width / aspectRatio;
} else {
height =
(paint.sourceHeight ?? node.middleBoxGlobal.height) / modifiedScaleY;
}
return SizeC(width, height);
}