summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/Layout/LayoutImage.cpp
blob: 2344afd713f942aeafa9d8b72dd4a48b8b0d5d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <LibDraw/Font.h>
#include <LibDraw/StylePainter.h>
#include <LibGUI/GPainter.h>
#include <LibHTML/Layout/LayoutImage.h>

LayoutImage::LayoutImage(const HTMLImageElement& element, NonnullRefPtr<StyleProperties> style)
    : LayoutReplaced(element, move(style))
{
}

LayoutImage::~LayoutImage()
{
}

void LayoutImage::layout()
{
    if (renders_as_alt_text()) {
        auto& font = Font::default_font();
        rect().set_width(font.width(node().alt()) + 16);
        rect().set_height(font.glyph_height() + 16);
    } else {
        rect().set_width(node().bitmap()->width());
        rect().set_height(node().bitmap()->height());
    }

    LayoutReplaced::layout();
}

void LayoutImage::render(RenderingContext& context)
{
    if (renders_as_alt_text()) {
        context.painter().set_font(Font::default_font());
        StylePainter::paint_frame(context.painter(), rect(), FrameShape::Container, FrameShadow::Sunken, 2);
        context.painter().draw_text(rect(), node().alt(), TextAlignment::Center, Color::White);
    } else {
        context.painter().draw_scaled_bitmap(rect(), *node().bitmap(), node().bitmap()->rect());
    }
    LayoutReplaced::render(context);
}

bool LayoutImage::renders_as_alt_text() const
{
    return !node().bitmap();
}