LayoutImage.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <LibDraw/Font.h>
  2. #include <LibDraw/StylePainter.h>
  3. #include <LibGUI/GPainter.h>
  4. #include <LibHTML/Layout/LayoutImage.h>
  5. LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style)
  6. : LayoutReplaced(element, move(style))
  7. {
  8. }
  9. LayoutImage::~LayoutImage()
  10. {
  11. }
  12. void LayoutImage::layout()
  13. {
  14. if (node().preferred_width() && node().preferred_height()) {
  15. rect().set_width(node().preferred_width());
  16. rect().set_height(node().preferred_height());
  17. } else if (renders_as_alt_text()) {
  18. auto& font = Font::default_font();
  19. auto alt = node().alt();
  20. if (alt.is_empty())
  21. alt = node().src();
  22. rect().set_width(font.width(alt) + 16);
  23. rect().set_height(font.glyph_height() + 16);
  24. } else {
  25. rect().set_width(16);
  26. rect().set_height(16);
  27. }
  28. LayoutReplaced::layout();
  29. }
  30. void LayoutImage::render(RenderingContext& context)
  31. {
  32. if (!is_visible())
  33. return;
  34. if (renders_as_alt_text()) {
  35. context.painter().set_font(Font::default_font());
  36. StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
  37. auto alt = node().alt();
  38. if (alt.is_empty())
  39. alt = node().src();
  40. context.painter().draw_text(rect(), alt, TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), TextElision::Right);
  41. } else {
  42. context.painter().draw_scaled_bitmap(rect(), *node().bitmap(), node().bitmap()->rect());
  43. }
  44. LayoutReplaced::render(context);
  45. }
  46. bool LayoutImage::renders_as_alt_text() const
  47. {
  48. return !node().bitmap();
  49. }