Commit 542a4a0d authored by andersca's avatar andersca
Browse files

Reviewed by Geoff.

        <rdar://problem/3663808> Resize large images to fit in the browser window
        
        * WebCore.exp:
        Add new settings method.
        
        * loader/ImageDocument.cpp:
        (WebCore::ImageEventListener::ImageEventListener):
        New class that handles resize events and click events and forwards them to the image document.
        
        (WebCore::ImageTokenizer::ImageTokenizer):
        (WebCore::ImageTokenizer::writeRawData):
        (WebCore::ImageTokenizer::finish):
        (WebCore::ImageDocument::ImageDocument):
        (WebCore::ImageDocument::createDocumentStructure):
        (WebCore::ImageDocument::cachedImage):
        Let the ImageDocument class keep track of its document structure.
        
        (WebCore::ImageDocument::scale):
        Returns the scale that should be used when resizing the image.
        
        (WebCore::ImageDocument::resizeImage):
        Update the image size.
        
        (WebCore::ImageDocument::imageClicked):
        Toggle between resized and not resized. When restoring the size, scroll the image so that the area under
        the mouse stays the same.
        
        (WebCore::ImageDocument::imageChanged):
        When the image size is known, resize the image if needed.
        
        (WebCore::ImageDocument::restoreImageSize):
        Restore the image size.
        
        (WebCore::ImageDocument::imageNeedsResizing):
        Return whether the image is too big for its window or not.
        
        (WebCore::ImageDocument::windowSizeChanged):
        Called when the window's size changes. Determine if the window fits or not and resize it if it doesn't fit.
        
        (WebCore::ImageEventListener::handleEvent):
        Forward events to the image document.
        
        * page/Settings.cpp:
        (WebCore::Settings::Settings):
        (WebCore::Settings::setEnableAutomaticImageResizing):
        * page/Settings.h:
        (WebCore::Settings::enableAutomaticImageResizing):
        Add new setting.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@21686 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 1500e83b
2007-05-23 Anders Carlsson <andersca@apple.com>
Reviewed by Geoff.
<rdar://problem/3663808> Resize large images to fit in the browser window
* WebCore.exp:
Add new settings method.
* loader/ImageDocument.cpp:
(WebCore::ImageEventListener::ImageEventListener):
New class that handles resize events and click events and forwards them to the image document.
(WebCore::ImageTokenizer::ImageTokenizer):
(WebCore::ImageTokenizer::writeRawData):
(WebCore::ImageTokenizer::finish):
(WebCore::ImageDocument::ImageDocument):
(WebCore::ImageDocument::createDocumentStructure):
(WebCore::ImageDocument::cachedImage):
Let the ImageDocument class keep track of its document structure.
(WebCore::ImageDocument::scale):
Returns the scale that should be used when resizing the image.
(WebCore::ImageDocument::resizeImage):
Update the image size.
(WebCore::ImageDocument::imageClicked):
Toggle between resized and not resized. When restoring the size, scroll the image so that the area under
the mouse stays the same.
(WebCore::ImageDocument::imageChanged):
When the image size is known, resize the image if needed.
(WebCore::ImageDocument::restoreImageSize):
Restore the image size.
(WebCore::ImageDocument::imageNeedsResizing):
Return whether the image is too big for its window or not.
(WebCore::ImageDocument::windowSizeChanged):
Called when the window's size changes. Determine if the window fits or not and resize it if it doesn't fit.
(WebCore::ImageEventListener::handleEvent):
Forward events to the image document.
* page/Settings.cpp:
(WebCore::Settings::Settings):
(WebCore::Settings::setEnableAutomaticImageResizing):
* page/Settings.h:
(WebCore::Settings::enableAutomaticImageResizing):
Add new setting.
2007-05-23 Anders Carlsson <andersca@apple.com>
 
Reviewed by Darin.
......@@ -446,6 +446,7 @@ __ZN7WebCore8Settings25setShouldPrintBackgroundsEb
__ZN7WebCore8Settings25setUserStyleSheetLocationERKNS_4KURLE
__ZN7WebCore8Settings26setDefaultTextEncodingNameERKNS_6StringE
__ZN7WebCore8Settings27setLoadsImagesAutomaticallyEb
__ZN7WebCore8Settings31setShrinksStandaloneImagesToFitEb
__ZN7WebCore8Settings34setNeedsAcrobatFrameReloadingQuirkEb
__ZN7WebCore8Settings40setJavaScriptCanOpenWindowsAutomaticallyEb
__ZN7WebCore8Settings41setUsesDashboardBackwardCompatibilityModeEb
......
......@@ -28,11 +28,17 @@
#include "CachedImage.h"
#include "DocumentLoader.h"
#include "Element.h"
#include "EventListener.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameView.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
#include "MouseEvent.h"
#include "Page.h"
#include "SegmentedString.h"
#include "Settings.h"
#include "Text.h"
#include "XMLTokenizer.h"
......@@ -40,13 +46,25 @@
#include "ImageDocumentMac.h"
#endif
using std::min;
namespace WebCore {
using namespace EventNames;
using namespace HTMLNames;
class ImageEventListener : public EventListener {
public:
ImageEventListener(ImageDocument* doc) : m_doc(doc) { }
virtual void handleEvent(Event*, bool isWindowEvent);
private:
ImageDocument* m_doc;
};
class ImageTokenizer : public Tokenizer {
public:
ImageTokenizer(Document* doc) : m_doc(doc), m_imageElement(0) {}
ImageTokenizer(ImageDocument* doc) : m_doc(doc) {}
virtual bool write(const SegmentedString&, bool appendData);
virtual void finish();
......@@ -54,11 +72,8 @@ public:
virtual bool wantsRawData() const { return true; }
virtual bool writeRawData(const char* data, int len);
void createDocumentStructure();
private:
Document* m_doc;
HTMLImageElement* m_imageElement;
ImageDocument* m_doc;
};
bool ImageTokenizer::write(const SegmentedString& s, bool appendData)
......@@ -67,43 +82,20 @@ bool ImageTokenizer::write(const SegmentedString& s, bool appendData)
return false;
}
void ImageTokenizer::createDocumentStructure()
{
ExceptionCode ec;
RefPtr<Element> rootElement = m_doc->createElementNS(xhtmlNamespaceURI, "html", ec);
m_doc->appendChild(rootElement, ec);
RefPtr<Element> body = m_doc->createElementNS(xhtmlNamespaceURI, "body", ec);
body->setAttribute(styleAttr, "margin: 0px;");
rootElement->appendChild(body, ec);
RefPtr<Element> imageElement = m_doc->createElementNS(xhtmlNamespaceURI, "img", ec);
m_imageElement = static_cast<HTMLImageElement *>(imageElement.get());
m_imageElement->setAttribute(styleAttr, "-webkit-user-select: none");
m_imageElement->setLoadManually(true);
m_imageElement->setSrc(m_doc->URL());
body->appendChild(imageElement, ec);
}
bool ImageTokenizer::writeRawData(const char* data, int len)
{
if (!m_imageElement)
createDocumentStructure();
CachedImage* cachedImage = m_imageElement->cachedImage();
CachedImage* cachedImage = m_doc->cachedImage();
cachedImage->data(m_doc->frame()->loader()->documentLoader()->mainResourceData(), false);
m_doc->imageChanged();
return false;
}
void ImageTokenizer::finish()
{
if (!m_parserStopped && m_imageElement) {
CachedImage* cachedImage = m_imageElement->cachedImage();
if (!m_parserStopped && m_doc->imageElement()) {
CachedImage* cachedImage = m_doc->cachedImage();
cachedImage->data(m_doc->frame()->loader()->documentLoader()->mainResourceData(), true);
cachedImage->finish();
......@@ -126,12 +118,169 @@ bool ImageTokenizer::isWaitingForScripts() const
ImageDocument::ImageDocument(DOMImplementation* implementation, Frame* frame)
: HTMLDocument(implementation, frame)
, m_imageElement(0)
, m_imageSizeIsKnown(false)
, m_didShrinkImage(false)
, m_shouldShrinkImage(true)
{
setParseMode(Compat);
}
Tokenizer* ImageDocument::createTokenizer()
{
return new ImageTokenizer(this);
}
void ImageDocument::createDocumentStructure()
{
ExceptionCode ec;
RefPtr<Element> rootElement = createElementNS(xhtmlNamespaceURI, "html", ec);
appendChild(rootElement, ec);
RefPtr<Element> body = createElementNS(xhtmlNamespaceURI, "body", ec);
body->setAttribute(styleAttr, "margin: 0px;");
rootElement->appendChild(body, ec);
RefPtr<Element> imageElement = createElementNS(xhtmlNamespaceURI, "img", ec);
m_imageElement = static_cast<HTMLImageElement *>(imageElement.get());
m_imageElement->setAttribute(styleAttr, "-webkit-user-select: none");
m_imageElement->setLoadManually(true);
m_imageElement->setSrc(URL());
body->appendChild(imageElement, ec);
if (!frame()->page()->settings()->shrinksStandaloneImagesToFit())
return;
// Add event listeners
RefPtr<EventListener> listener = new ImageEventListener(this);
addWindowEventListener("resize", listener, false);
m_imageElement->addEventListener("click", listener.release(), false);
}
float ImageDocument::scale() const
{
IntSize imageSize = m_imageElement->cachedImage()->imageSize();
IntSize windowSize = IntSize(frame()->view()->width(), frame()->view()->height());
float widthScale = (float)windowSize.width() / imageSize.width();
float heightScale = (float)windowSize.height() / imageSize.height();
return min(widthScale, heightScale);
}
void ImageDocument::resizeImageToFit()
{
IntSize imageSize = m_imageElement->cachedImage()->imageSize();
float scale = this->scale();
m_imageElement->setWidth(imageSize.width() * scale);
m_imageElement->setHeight(imageSize.height() * scale);
}
void ImageDocument::imageClicked(int x, int y)
{
if (!m_imageSizeIsKnown || imageFitsInWindow())
return;
m_shouldShrinkImage = !m_shouldShrinkImage;
if (m_shouldShrinkImage)
windowSizeChanged();
else {
restoreImageSize();
updateLayout();
float scale = this->scale();
int scrollX = x / scale - (float)frame()->view()->width() / 2;
int scrollY = y / scale - (float)frame()->view()->height() / 2;
frame()->view()->setContentsPos(scrollX, scrollY);
}
}
void ImageDocument::imageChanged()
{
ASSERT(m_imageElement);
if (m_imageSizeIsKnown)
return;
if (m_imageElement->cachedImage()->imageSize().isEmpty())
return;
m_imageSizeIsKnown = true;
if (!frame()->page()->settings()->shrinksStandaloneImagesToFit())
return;
// Force resizing of the image
windowSizeChanged();
}
void ImageDocument::restoreImageSize()
{
if (!m_imageSizeIsKnown)
return;
m_imageElement->setWidth(m_imageElement->cachedImage()->imageSize().width());
m_imageElement->setHeight(m_imageElement->cachedImage()->imageSize().height());
m_didShrinkImage = false;
}
bool ImageDocument::imageFitsInWindow() const
{
IntSize imageSize = m_imageElement->cachedImage()->imageSize();
IntSize windowSize = IntSize(frame()->view()->width(), frame()->view()->height());
return imageSize.width() <= windowSize.width() && imageSize.height() <= windowSize.height();
}
void ImageDocument::windowSizeChanged()
{
if (!m_imageSizeIsKnown || !m_shouldShrinkImage)
return;
bool fitsInWindow = imageFitsInWindow();
if (m_didShrinkImage) {
// If the window has been resized so that the image fits, restore the image size
// otherwise update the restored image size.
if (fitsInWindow)
restoreImageSize();
else
resizeImageToFit();
} else {
// If the image isn't resized but needs to be, then resize it.
if (!fitsInWindow) {
resizeImageToFit();
m_didShrinkImage = true;
}
}
}
CachedImage* ImageDocument::cachedImage()
{
if (!m_imageElement)
createDocumentStructure();
return m_imageElement->cachedImage();
}
void ImageEventListener::handleEvent(Event* event, bool isWindowEvent)
{
if (event->type() == resizeEvent)
m_doc->windowSizeChanged();
else if (event->type() == clickEvent) {
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
m_doc->imageClicked(mouseEvent->x(), mouseEvent->y());
}
}
}
......@@ -30,7 +30,8 @@ namespace WebCore {
class DOMImplementation;
class FrameView;
class HTMLImageElement;
class ImageDocument : public HTMLDocument
{
public:
......@@ -39,6 +40,30 @@ public:
virtual bool isImageDocument() const { return true; }
virtual Tokenizer* createTokenizer();
CachedImage* cachedImage();
HTMLImageElement* imageElement() const { return m_imageElement; }
void windowSizeChanged();
void imageChanged();
void imageClicked(int x, int y);
private:
void createDocumentStructure();
void resizeImageToFit();
void restoreImageSize();
bool imageFitsInWindow() const;
float scale() const;
HTMLImageElement* m_imageElement;
// Whether enough of the image has been loaded to determine its size
bool m_imageSizeIsKnown;
// Whether the image is shrunk to fit or not
bool m_didShrinkImage;
// Whether the image should be shrunk or not
bool m_shouldShrinkImage;
};
}
......
......@@ -46,6 +46,7 @@ Settings::Settings()
, m_textAreasAreResizable(false)
, m_usesDashboardBackwardCompatibilityMode(false)
, m_needsAcrobatFrameReloadingQuirk(false)
, m_shrinksStandaloneImagesToFit(true)
{
// A Frame may not have been created yet, so we initialize the AtomicString
// hash before trying to use it.
......@@ -223,4 +224,9 @@ void Settings::setDOMPasteAllowed(bool DOMPasteAllowed)
m_isDOMPasteAllowed = DOMPasteAllowed;
}
void Settings::setShrinksStandaloneImagesToFit(bool shrinksStandaloneImagesToFit)
{
m_shrinksStandaloneImagesToFit = shrinksStandaloneImagesToFit;
}
} // namespace WebCore
......@@ -117,6 +117,9 @@ namespace WebCore {
void setDOMPasteAllowed(bool);
bool isDOMPasteAllowed() const { return m_isDOMPasteAllowed; }
void setShrinksStandaloneImagesToFit(bool);
bool shrinksStandaloneImagesToFit() const { return m_shrinksStandaloneImagesToFit; }
private:
String m_defaultTextEncodingName;
KURL m_userStyleSheetLocation;
......@@ -141,7 +144,8 @@ namespace WebCore {
bool m_textAreasAreResizable : 1;
bool m_usesDashboardBackwardCompatibilityMode : 1;
bool m_needsAcrobatFrameReloadingQuirk : 1;
bool m_isDOMPasteAllowed: 1;
bool m_isDOMPasteAllowed : 1;
bool m_shrinksStandaloneImagesToFit : 1;
};
} // namespace WebCore
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment