LayoutImage.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 (renders_as_alt_text()) {
  33. context.painter().set_font(Font::default_font());
  34. StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
  35. auto alt = node().alt();
  36. if (alt.is_empty())
  37. alt = node().src();
  38. context.painter().draw_text(rect(), alt, TextAlignment::Center, style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black), TextElision::Right);
  39. } else {
  40. context.painter().draw_scaled_bitmap(rect(), *node().bitmap(), node().bitmap()->rect());
  41. }
  42. LayoutReplaced::render(context);
  43. }
  44. bool LayoutImage::renders_as_alt_text() const
  45. {
  46. return !node().bitmap();
  47. }