private void LayoutTextBeforeOrAfterImage(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_width = textSize.Width + element_spacing + imageSize.Width;
if (!textFirst)
element_spacing += 2;
// If the text is too big, chop it down to the size we have available to it
if (total_width > totalArea.Width)
{
textSize.Width = totalArea.Width - element_spacing - imageSize.Width;
total_width = totalArea.Width;
}
int excess_width = totalArea.Width - total_width;
int offset = 0;
Rectangle final_text_rect;
Rectangle final_image_rect;
HorizontalAlignment h_text = GetHorizontalAlignment(TextAlign);
HorizontalAlignment h_image = GetHorizontalAlignment(ImageAlign);
if (h_image == HorizontalAlignment.Left)
offset = 0;
else if (h_image == HorizontalAlignment.Right && h_text == HorizontalAlignment.Right)
offset = excess_width;
else if (h_image == HorizontalAlignment.Center && (h_text == HorizontalAlignment.Left || h_text == HorizontalAlignment.Center))
offset += excess_width / 3;
else
offset += 2 * (excess_width / 3);
if (textFirst)
{
final_text_rect = new Rectangle(totalArea.Left + offset, AlignInRectangle(totalArea, textSize, TextAlign).Top, textSize.Width, textSize.Height);
final_image_rect = new Rectangle(final_text_rect.Right + element_spacing, AlignInRectangle(totalArea, imageSize, ImageAlign).Top, imageSize.Width, imageSize.Height);
}
else
{
final_image_rect = new Rectangle(totalArea.Left + offset, AlignInRectangle(totalArea, imageSize, ImageAlign).Top, imageSize.Width, imageSize.Height);
final_text_rect = new Rectangle(final_image_rect.Right + element_spacing, AlignInRectangle(totalArea, textSize, TextAlign).Top, textSize.Width, textSize.Height);
}
textRect = final_text_rect;
imageRect = final_image_rect;
}