LayoutImage.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 (renders_as_alt_text()) {
  15. auto& font = Font::default_font();
  16. auto alt = node().alt();
  17. if (alt.is_empty())
  18. alt = node().src();
  19. rect().set_width(font.width(alt) + 16);
  20. rect().set_height(font.glyph_height() + 16);
  21. } else {
  22. rect().set_width(node().bitmap()->width());
  23. rect().set_height(node().bitmap()->height());
  24. }
  25. LayoutReplaced::layout();
  26. }
  27. void LayoutImage::render(RenderingContext& context)
  28. {
  29. if (renders_as_alt_text()) {
  30. context.painter().set_font(Font::default_font());
  31. StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
  32. auto alt = node().alt();
  33. if (alt.is_empty())
  34. alt = node().src();
  35. context.painter().draw_text(rect(), alt, TextAlignment::Center, Color::White);
  36. } else {
  37. context.painter().draw_scaled_bitmap(rect(), *node().bitmap(), node().bitmap()->rect());
  38. }
  39. LayoutReplaced::render(context);
  40. }
  41. bool LayoutImage::renders_as_alt_text() const
  42. {
  43. return !node().bitmap();
  44. }