LayoutImage.cpp 1.2 KB

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