Fix ImageDocument's reference size
[framework/web/webkit-efl.git] / Source / WebCore / html / ImageDocument.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
23  */
24
25 #include "config.h"
26 #include "ImageDocument.h"
27
28 #include "CachedImage.h"
29 #include "DocumentLoader.h"
30 #include "EventListener.h"
31 #include "EventNames.h"
32 #include "Frame.h"
33 #include "FrameLoaderClient.h"
34 #include "FrameView.h"
35 #include "HTMLHtmlElement.h"
36 #include "HTMLImageElement.h"
37 #include "HTMLNames.h"
38 #include "LocalizedStrings.h"
39 #include "MouseEvent.h"
40 #include "NotImplemented.h"
41 #include "Page.h"
42 #include "RawDataDocumentParser.h"
43 #include "Settings.h"
44
45 using std::min;
46
47 namespace WebCore {
48
49 using namespace HTMLNames;
50
51 class ImageEventListener : public EventListener {
52 public:
53     static PassRefPtr<ImageEventListener> create(ImageDocument* document) { return adoptRef(new ImageEventListener(document)); }
54     static const ImageEventListener* cast(const EventListener* listener)
55     {
56         return listener->type() == ImageEventListenerType
57             ? static_cast<const ImageEventListener*>(listener)
58             : 0;
59     }
60
61     virtual bool operator==(const EventListener& other);
62
63 private:
64     ImageEventListener(ImageDocument* document)
65         : EventListener(ImageEventListenerType)
66         , m_doc(document)
67     {
68     }
69
70     virtual void handleEvent(ScriptExecutionContext*, Event*);
71
72     ImageDocument* m_doc;
73 };
74     
75 class ImageDocumentParser : public RawDataDocumentParser {
76 public:
77     static PassRefPtr<ImageDocumentParser> create(ImageDocument* document)
78     {
79         return adoptRef(new ImageDocumentParser(document));
80     }
81
82     ImageDocument* document() const
83     {
84         return static_cast<ImageDocument*>(RawDataDocumentParser::document());
85     }
86     
87 private:
88     ImageDocumentParser(ImageDocument* document)
89         : RawDataDocumentParser(document)
90     {
91     }
92
93     virtual void appendBytes(DocumentWriter*, const char*, size_t);
94     virtual void finish();
95 };
96
97 class ImageDocumentElement : public HTMLImageElement {
98 public:
99     static PassRefPtr<ImageDocumentElement> create(ImageDocument*);
100
101 private:
102     ImageDocumentElement(ImageDocument* document)
103         : HTMLImageElement(imgTag, document)
104         , m_imageDocument(document)
105     {
106     }
107
108     virtual ~ImageDocumentElement();
109     virtual void didMoveToNewDocument(Document* oldDocument) OVERRIDE;
110
111     ImageDocument* m_imageDocument;
112 };
113
114 inline PassRefPtr<ImageDocumentElement> ImageDocumentElement::create(ImageDocument* document)
115 {
116     return adoptRef(new ImageDocumentElement(document));
117 }
118
119 // --------
120
121 static float pageZoomFactor(const Document* document)
122 {
123     Frame* frame = document->frame();
124     return frame ? frame->pageZoomFactor() : 1;
125 }
126
127 void ImageDocumentParser::appendBytes(DocumentWriter*, const char*, size_t)
128 {
129     Frame* frame = document()->frame();
130     Settings* settings = frame->settings();
131     if (!frame->loader()->client()->allowImage(!settings || settings->areImagesEnabled(), document()->url()))
132         return;
133
134     CachedImage* cachedImage = document()->cachedImage();
135     cachedImage->data(frame->loader()->documentLoader()->mainResourceData(), false);
136
137     document()->imageUpdated();
138 }
139
140 void ImageDocumentParser::finish()
141 {
142     if (!isStopped() && document()->imageElement()) {
143         CachedImage* cachedImage = document()->cachedImage();
144         RefPtr<SharedBuffer> data = document()->frame()->loader()->documentLoader()->mainResourceData();
145
146         // If this is a multipart image, make a copy of the current part, since the resource data
147         // will be overwritten by the next part.
148         if (document()->frame()->loader()->documentLoader()->isLoadingMultipartContent())
149             data = data->copy();
150
151         cachedImage->data(data.release(), true);
152         cachedImage->finish();
153
154         cachedImage->setResponse(document()->frame()->loader()->documentLoader()->response());
155
156         // Report the natural image size in the page title, regardless of zoom
157         // level.
158         IntSize size = cachedImage->imageSizeForRenderer(document()->imageElement()->renderer(), 1.0f);
159         if (size.width()) {
160             // Compute the title, we use the decoded filename of the resource, falling
161             // back on the (decoded) hostname if there is no path.
162             String fileName = decodeURLEscapeSequences(document()->url().lastPathComponent());
163             if (fileName.isEmpty())
164                 fileName = document()->url().host();
165             document()->setTitle(imageTitle(fileName, size));
166         }
167
168         document()->imageUpdated();
169     }
170
171     document()->finishedParsing();
172 }
173     
174 // --------
175
176 ImageDocument::ImageDocument(Frame* frame, const KURL& url)
177     : HTMLDocument(frame, url)
178     , m_imageElement(0)
179     , m_imageSizeIsKnown(false)
180     , m_didShrinkImage(false)
181     , m_shouldShrinkImage(shouldShrinkToFit())
182 {
183     setCompatibilityMode(QuirksMode);
184     lockCompatibilityMode();
185 }
186     
187 PassRefPtr<DocumentParser> ImageDocument::createParser()
188 {
189     return ImageDocumentParser::create(this);
190 }
191
192 void ImageDocument::createDocumentStructure()
193 {
194     ExceptionCode ec;
195     
196     RefPtr<Element> rootElement = Document::createElement(htmlTag, false);
197     appendChild(rootElement, ec);
198     static_cast<HTMLHtmlElement*>(rootElement.get())->insertedByParser();
199
200     if (frame() && frame()->loader())
201         frame()->loader()->dispatchDocumentElementAvailable();
202     
203     RefPtr<Element> body = Document::createElement(bodyTag, false);
204     body->setAttribute(styleAttr, "margin: 0px;");
205     
206     rootElement->appendChild(body, ec);
207     
208     RefPtr<ImageDocumentElement> imageElement = ImageDocumentElement::create(this);
209     
210     imageElement->setAttribute(styleAttr, "-webkit-user-select: none");        
211     imageElement->setLoadManually(true);
212     imageElement->setSrc(url().string());
213     
214     body->appendChild(imageElement, ec);
215     
216     if (shouldShrinkToFit()) {
217         // Add event listeners
218         RefPtr<EventListener> listener = ImageEventListener::create(this);
219         if (DOMWindow* domWindow = this->domWindow())
220             domWindow->addEventListener("resize", listener, false);
221         imageElement->addEventListener("click", listener.release(), false);
222     }
223
224     m_imageElement = imageElement.get();
225 }
226
227 float ImageDocument::scale() const
228 {
229     if (!m_imageElement)
230         return 1.0f;
231
232     FrameView* view = frame()->view();
233     if (!view)
234         return 1;
235
236     LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
237 #if ENABLE(TIZEN_VIEWPORT_META_TAG)
238     // FIXME: view->width() and view->height() are frame's rect size. And it's calculated after layout.
239     // But ImageDocument's loading can be begun before frame's rect size is fixed.
240     // So, we use layoutWidth() here.
241     LayoutSize windowSize = LayoutSize(view->layoutWidth(), view->layoutHeight());
242 #else
243     LayoutSize windowSize = LayoutSize(view->width(), view->height());
244 #endif
245     
246     float widthScale = (float)windowSize.width() / imageSize.width();
247     float heightScale = (float)windowSize.height() / imageSize.height();
248
249     return min(widthScale, heightScale);
250 }
251
252 void ImageDocument::resizeImageToFit()
253 {
254     if (!m_imageElement)
255         return;
256
257     LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
258
259     float scale = this->scale();
260     m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale));
261     m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale));
262     
263     m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-in", false);
264 }
265
266 void ImageDocument::imageClicked(int x, int y)
267 {
268     if (!m_imageSizeIsKnown || imageFitsInWindow())
269         return;
270
271     m_shouldShrinkImage = !m_shouldShrinkImage;
272     
273     if (m_shouldShrinkImage)
274         windowSizeChanged();
275     else {
276         restoreImageSize();
277         
278         updateLayout();
279         
280         float scale = this->scale();
281         
282         int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width() / 2);
283         int scrollY = static_cast<int>(y / scale - (float)frame()->view()->height() / 2);
284         
285         frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY));
286     }
287 }
288
289 void ImageDocument::imageUpdated()
290 {
291     ASSERT(m_imageElement);
292     
293     if (m_imageSizeIsKnown)
294         return;
295
296     if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this)).isEmpty())
297         return;
298     
299     m_imageSizeIsKnown = true;
300     
301     if (shouldShrinkToFit()) {
302         // Force resizing of the image
303         windowSizeChanged();
304     }
305 }
306
307 void ImageDocument::restoreImageSize()
308 {
309     if (!m_imageElement || !m_imageSizeIsKnown)
310         return;
311     
312     LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
313     m_imageElement->setWidth(imageSize.width());
314     m_imageElement->setHeight(imageSize.height());
315     
316     if (imageFitsInWindow())
317         m_imageElement->removeInlineStyleProperty(CSSPropertyCursor);
318     else
319         m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-out", false);
320         
321     m_didShrinkImage = false;
322 }
323
324 bool ImageDocument::imageFitsInWindow() const
325 {
326     if (!m_imageElement)
327         return true;
328
329     FrameView* view = frame()->view();
330     if (!view)
331         return true;
332
333     LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->renderer(), pageZoomFactor(this));
334 #if ENABLE(TIZEN_VIEWPORT_META_TAG)
335     // FIXME: view->width() and view->height() are frame's rect size. And it's calculated after layout.
336     // But ImageDocument's loading can be begun before frame's rect size is fixed.
337     // So, we use layoutWidth() here.
338     LayoutSize windowSize = LayoutSize(view->layoutWidth(), view->layoutHeight());
339 #else
340     LayoutSize windowSize = LayoutSize(view->width(), view->height());
341 #endif
342     
343     return imageSize.width() <= windowSize.width() && imageSize.height() <= windowSize.height();    
344 }
345
346 void ImageDocument::windowSizeChanged()
347 {
348     if (!m_imageElement || !m_imageSizeIsKnown)
349         return;
350
351     bool fitsInWindow = imageFitsInWindow();
352     
353     // If the image has been explicitly zoomed in, restore the cursor if the image fits
354     // and set it to a zoom out cursor if the image doesn't fit
355     if (!m_shouldShrinkImage) {
356         if (fitsInWindow)
357             m_imageElement->removeInlineStyleProperty(CSSPropertyCursor);
358         else
359             m_imageElement->setInlineStyleProperty(CSSPropertyCursor, "-webkit-zoom-out", false);
360         return;
361     }
362     
363     if (m_didShrinkImage) {
364         // If the window has been resized so that the image fits, restore the image size
365         // otherwise update the restored image size.
366         if (fitsInWindow)
367             restoreImageSize();
368         else
369             resizeImageToFit();
370     } else {
371         // If the image isn't resized but needs to be, then resize it.
372         if (!fitsInWindow) {
373             resizeImageToFit();
374             m_didShrinkImage = true;
375         }
376     }
377 }
378
379 CachedImage* ImageDocument::cachedImage()
380
381     if (!m_imageElement)
382         createDocumentStructure();
383     
384     return m_imageElement->cachedImage();
385 }
386
387 bool ImageDocument::shouldShrinkToFit() const
388 {
389     return frame()->page()->settings()->shrinksStandaloneImagesToFit() &&
390         frame()->page()->mainFrame() == frame();
391 }
392
393 // --------
394
395 void ImageEventListener::handleEvent(ScriptExecutionContext*, Event* event)
396 {
397     if (event->type() == eventNames().resizeEvent)
398         m_doc->windowSizeChanged();
399     else if (event->type() == eventNames().clickEvent && event->isMouseEvent()) {
400         MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
401         m_doc->imageClicked(mouseEvent->x(), mouseEvent->y());
402     }
403 }
404
405 bool ImageEventListener::operator==(const EventListener& listener)
406 {
407     if (const ImageEventListener* imageEventListener = ImageEventListener::cast(&listener))
408         return m_doc == imageEventListener->m_doc;
409     return false;
410 }
411
412 // --------
413
414 ImageDocumentElement::~ImageDocumentElement()
415 {
416     if (m_imageDocument)
417         m_imageDocument->disconnectImageElement();
418 }
419
420 void ImageDocumentElement::didMoveToNewDocument(Document* oldDocument)
421 {
422     if (m_imageDocument) {
423         m_imageDocument->disconnectImageElement();
424         m_imageDocument = 0;
425     }
426     HTMLImageElement::didMoveToNewDocument(oldDocument);
427 }
428
429 }