1#include "html.h"
2#include "el_image.h"
3#include "render_image.h"
4
5litehtml::el_image::el_image(const document::ptr& doc) : html_tag(doc)
6{
7 m_css.set_display(display_inline_block);
8}
9
10void litehtml::el_image::get_content_size( size& sz, int max_width )
11{
12 get_document()->container()->get_image_size(src: m_src.c_str(), baseurl: 0, sz);
13}
14
15bool litehtml::el_image::is_replaced() const
16{
17 return true;
18}
19
20void litehtml::el_image::parse_attributes()
21{
22 m_src = get_attr(name: "src", def: "");
23
24 const char* attr_height = get_attr(name: "height");
25 if(attr_height)
26 {
27 m_style.add_property(name: _height_, val: attr_height);
28 }
29 const char* attr_width = get_attr(name: "width");
30 if(attr_width)
31 {
32 m_style.add_property(name: _width_, val: attr_width);
33 }
34}
35
36void litehtml::el_image::draw(uint_ptr hdc, int x, int y, const position *clip, const std::shared_ptr<render_item> &ri)
37{
38 position pos = ri->pos();
39 pos.x += x;
40 pos.y += y;
41
42 position el_pos = pos;
43 el_pos += ri->get_paddings();
44 el_pos += ri->get_borders();
45
46 // draw standard background here
47 if (el_pos.does_intersect(val: clip))
48 {
49 const background* bg = get_background();
50 if (bg)
51 {
52 std::vector<background_paint> bg_paint;
53 init_background_paint(pos, bg_paint, bg, ri);
54
55 get_document()->container()->draw_background(hdc, bg: bg_paint);
56 }
57 }
58
59 // draw image as background
60 if(pos.does_intersect(val: clip))
61 {
62 if (pos.width > 0 && pos.height > 0) {
63 background_paint bg;
64 bg.image = m_src;
65 bg.clip_box = pos;
66 bg.origin_box = pos;
67 bg.border_box = pos;
68 bg.border_box += ri->get_paddings();
69 bg.border_box += ri->get_borders();
70 bg.repeat = background_repeat_no_repeat;
71 bg.image_size.width = pos.width;
72 bg.image_size.height = pos.height;
73 bg.border_radius = css().get_borders().radius.calc_percents(width: bg.border_box.width, height: bg.border_box.height);
74 bg.position_x = pos.x;
75 bg.position_y = pos.y;
76 get_document()->container()->draw_background(hdc, bg: {bg});
77 }
78 }
79
80 // draw borders
81 if (el_pos.does_intersect(val: clip))
82 {
83 position border_box = pos;
84 border_box += ri->get_paddings();
85 border_box += ri->get_borders();
86
87 borders bdr = css().get_borders();
88 bdr.radius = css().get_borders().radius.calc_percents(width: border_box.width, height: border_box.height);
89
90 get_document()->container()->draw_borders(hdc, borders: bdr, draw_pos: border_box, root: is_root());
91 }
92}
93
94void litehtml::el_image::compute_styles(bool recursive)
95{
96 html_tag::compute_styles(recursive);
97
98 if(!m_src.empty())
99 {
100 if(!css().get_height().is_predefined() && !css().get_width().is_predefined())
101 {
102 get_document()->container()->load_image(src: m_src.c_str(), baseurl: nullptr, redraw_on_ready: true);
103 } else
104 {
105 get_document()->container()->load_image(src: m_src.c_str(), baseurl: nullptr, redraw_on_ready: false);
106 }
107 }
108}
109
110litehtml::string litehtml::el_image::dump_get_name()
111{
112 return "img src=\"" + m_src + "\"";
113}
114
115std::shared_ptr<litehtml::render_item> litehtml::el_image::create_render_item(const std::shared_ptr<render_item>& parent_ri)
116{
117 auto ret = std::make_shared<render_item_image>(args: shared_from_this());
118 ret->parent(par: parent_ri);
119 return ret;
120}
121

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qttools/src/assistant/qlitehtml/src/3rdparty/litehtml/src/el_image.cpp