private void LayoutTextAboveOrBelowImage(Rectangle totalArea, bool textFirst, Size textSize, Size imageSize, out Rectangle textRect, out Rectangle imageRect)
{
int element_spacing = 0; // Spacing between the Text and the Image
int total_height = textSize.Height + element_spacing + imageSize.Height;
if (textFirst)
element_spacing += 2;
if (textSize.Width > totalArea.Width)
textSize.Width = totalArea.Width;
// If the there isn't enough room and we're text first, cut out the image
if (total_height > totalArea.Height && textFirst)
{
imageSize = Size.Empty;
total_height = totalArea.Height;
}
int excess_height = totalArea.Height - total_height;
int offset = 0;
Rectangle final_text_rect;
Rectangle final_image_rect;
VerticalAlignment v_text = GetVerticalAlignment(TextAlign);
VerticalAlignment v_image = GetVerticalAlignment(ImageAlign);
if (v_image == VerticalAlignment.Top)
offset = 0;
else if (v_image == VerticalAlignment.Bottom && v_text == VerticalAlignment.Bottom)
offset = excess_height;
else if (v_image == VerticalAlignment.Center && (v_text == VerticalAlignment.Top || v_text == VerticalAlignment.Center))
offset += excess_height / 3;
else
offset += 2 * (excess_height / 3);
if (textFirst)
{
final_text_rect = new Rectangle(AlignInRectangle(totalArea, textSize, TextAlign).Left, totalArea.Top + offset, textSize.Width, textSize.Height);
final_image_rect = new Rectangle(AlignInRectangle(totalArea, imageSize, ImageAlign).Left, final_text_rect.Bottom + element_spacing, imageSize.Width, imageSize.Height);
}
else
{
final_image_rect = new Rectangle(AlignInRectangle(totalArea, imageSize, ImageAlign).Left, totalArea.Top + offset, imageSize.Width, imageSize.Height);
final_text_rect = new Rectangle(AlignInRectangle(totalArea, textSize, TextAlign).Left, final_image_rect.Bottom + element_spacing, textSize.Width, textSize.Height);
if (final_text_rect.Bottom > totalArea.Bottom)
final_text_rect.Y = totalArea.Top;
}
textRect = final_text_rect;
imageRect = final_image_rect;
}