From 87d13930ef9b65bf4119843c8a41452ff639fc93 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 19 Oct 2019 09:31:52 +0200 Subject: LibHTML: Allow loading of PNG's directly into the HtmlView When loading a URL that ends in ".png", we now construct a simple DOM document to contain the image. It also shows the image dimensions in the document title. Because we use to load the image into the synthetic document, we end up loading the image resource twice. This issue will go away once we have a smarter, caching, loader mechanism. --- Libraries/LibHTML/HtmlView.cpp | 45 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) (limited to 'Libraries/LibHTML') diff --git a/Libraries/LibHTML/HtmlView.cpp b/Libraries/LibHTML/HtmlView.cpp index 3e9abd0e34..ef99039562 100644 --- a/Libraries/LibHTML/HtmlView.cpp +++ b/Libraries/LibHTML/HtmlView.cpp @@ -1,10 +1,15 @@ +#include #include +#include #include #include #include #include #include +#include #include +#include +#include #include #include #include @@ -222,6 +227,36 @@ void HtmlView::reload() load(main_frame().document()->url()); } +static RefPtr create_image_document(const ByteBuffer& data, const URL& url) +{ + auto document = adopt(*new Document); + document->set_url(url); + + auto bitmap = load_png_from_memory(data.data(), data.size()); + ASSERT(bitmap); + + auto html_element = create_element(document, "html"); + document->append_child(html_element); + + auto head_element = create_element(document, "head"); + html_element->append_child(head_element); + auto title_element = create_element(document, "title"); + head_element->append_child(title_element); + + auto basename = FileSystemPath(url.path()).basename(); + auto title_text = adopt(*new Text(document, String::format("%s [%dx%d]", basename.characters(), bitmap->width(), bitmap->height()))); + title_element->append_child(title_text); + + auto body_element = create_element(document, "body"); + html_element->append_child(body_element); + + auto image_element = create_element(document, "img"); + image_element->set_attribute("src", url.to_string()); + body_element->append_child(image_element); + + return document; +} + void HtmlView::load(const URL& url) { dbg() << "HtmlView::load: " << url; @@ -238,10 +273,14 @@ void HtmlView::load(const URL& url) ASSERT_NOT_REACHED(); } - auto document = parse_html(data, url); - + RefPtr document; + if (url.path().ends_with(".png")) { + document = create_image_document(data, url); + } else { + document = parse_html(data, url); + } + ASSERT(document); set_document(document); - if (on_title_change) on_title_change(document->title()); }); -- cgit v1.2.3