void ImageDocument::RestoreImage() { if (!mImageContent) { return; } // Keep image content alive while changing the attributes. nsCOMPtr<Element> imageContent = mImageContent; imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::width, true); imageContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::height, true); if (ImageIsOverflowing()) { if (!mImageIsOverflowingVertically) { SetModeClass(eOverflowingHorizontalOnly); } else { SetModeClass(eOverflowingVertical); } } else { SetModeClass(eNone); } mImageIsResized = false; UpdateTitleAndCharset(); }
void ImageDocument::ShrinkToFit() { if (!mImageContent) { return; } if (GetZoomLevel() != mOriginalZoomLevel && mImageIsResized && !nsContentUtils::IsChildOfSameType(this)) { return; } // Keep image content alive while changing the attributes. nsCOMPtr<nsIContent> imageContent = mImageContent; nsCOMPtr<nsIDOMHTMLImageElement> image = do_QueryInterface(mImageContent); image->SetWidth(std::max(1, NSToCoordFloor(GetRatio() * mImageWidth))); image->SetHeight(std::max(1, NSToCoordFloor(GetRatio() * mImageHeight))); // The view might have been scrolled when zooming in, scroll back to the // origin now that we're showing a shrunk-to-window version. ScrollImageTo(0, 0, false); if (!mImageContent) { // ScrollImageTo flush destroyed our content. return; } SetModeClass(eShrinkToFit); mImageIsResized = true; UpdateTitleAndCharset(); }
nsresult ImageDocument::OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage) { int32_t oldWidth = mImageWidth; int32_t oldHeight = mImageHeight; // Styles have not yet been applied, so we don't know the final size. For now, // default to the image's intrinsic size. aImage->GetWidth(&mImageWidth); aImage->GetHeight(&mImageHeight); // Multipart images send size available for each part; ignore them if it // doesn't change our size. (We may not even support changing size in // multipart images in the future.) if (oldWidth == mImageWidth && oldHeight == mImageHeight) { return NS_OK; } nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod(this, &ImageDocument::DefaultCheckOverflowing); nsContentUtils::AddScriptRunner(runnable); UpdateTitleAndCharset(); return NS_OK; }
nsresult ImageDocument::CreateSyntheticDocument() { // Synthesize an html document that refers to the image nsresult rv = MediaDocument::CreateSyntheticDocument(); NS_ENSURE_SUCCESS(rv, rv); // Add the image element Element* body = GetBodyElement(); if (!body) { NS_WARNING("no body on image document!"); return NS_ERROR_FAILURE; } nsCOMPtr<nsINodeInfo> nodeInfo; nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::img, nullptr, kNameSpaceID_XHTML, nsIDOMNode::ELEMENT_NODE); mImageContent = NS_NewHTMLImageElement(nodeInfo.forget()); if (!mImageContent) { return NS_ERROR_OUT_OF_MEMORY; } nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(mImageContent); NS_ENSURE_TRUE(imageLoader, NS_ERROR_UNEXPECTED); nsAutoCString src; mDocumentURI->GetSpec(src); NS_ConvertUTF8toUTF16 srcString(src); // Make sure not to start the image load from here... // imageLoader->SetLoadingEnabled(false); mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::src, srcString, false); mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, srcString, false); //Pale Moon: implement mechanism for custom background color. if (!mBackgroundColor.IsEmpty()) { nsCSSValue color; nsCSSParser parser; if (parser.ParseColorString(mBackgroundColor, nullptr, 0, color)) { nsAutoString styleAttr(NS_LITERAL_STRING("background-color: ")); styleAttr.Append(mBackgroundColor); body->SetAttr(kNameSpaceID_None, nsGkAtoms::style, styleAttr, false); } } body->AppendChildTo(mImageContent, false); // imageLoader->SetLoadingEnabled(true); // PM UpdateTitleAndCharset(); return NS_OK; }
nsresult ImageDocument::OnStartContainer(imgIRequest* aRequest, imgIContainer* aImage) { aImage->GetWidth(&mImageWidth); aImage->GetHeight(&mImageHeight); nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableMethod(this, &ImageDocument::DefaultCheckOverflowing); nsContentUtils::AddScriptRunner(runnable); UpdateTitleAndCharset(); return NS_OK; }
void ImageDocument::ShrinkToFit() { if (!mImageContent) { return; } if (GetZoomLevel() != mOriginalZoomLevel && mImageIsResized && !nsContentUtils::IsChildOfSameType(this)) { // If we're zoomed, so that we don't maintain the invariant that // mImageIsResized if and only if its displayed width/height fit in // mVisibleWidth/mVisibleHeight, then we may need to switch to/from the // overflowingVertical class here, because our viewport size may have // changed and we don't plan to adjust the image size to compensate. Since // mImageIsResized it has a "height" attribute set, and we can just get the // displayed image height by getting .height on the HTMLImageElement. HTMLImageElement* img = HTMLImageElement::FromContent(mImageContent); uint32_t imageHeight = img->Height(); nsDOMTokenList* classList = img->ClassList(); ErrorResult ignored; if (imageHeight > mVisibleHeight) { classList->Add(NS_LITERAL_STRING("overflowingVertical"), ignored); } else { classList->Remove(NS_LITERAL_STRING("overflowingVertical"), ignored); } ignored.SuppressException(); return; } // Keep image content alive while changing the attributes. nsCOMPtr<Element> imageContent = mImageContent; nsCOMPtr<nsIDOMHTMLImageElement> image = do_QueryInterface(imageContent); image->SetWidth(std::max(1, NSToCoordFloor(GetRatio() * mImageWidth))); image->SetHeight(std::max(1, NSToCoordFloor(GetRatio() * mImageHeight))); // The view might have been scrolled when zooming in, scroll back to the // origin now that we're showing a shrunk-to-window version. ScrollImageTo(0, 0, false); if (!mImageContent) { // ScrollImageTo flush destroyed our content. return; } SetModeClass(eShrinkToFit); mImageIsResized = true; UpdateTitleAndCharset(); }
nsresult ImageDocument::OnStartContainer(imgIRequest* aRequest, imgIContainer* aImage) { // Styles have not yet been applied, so we don't know the final size. For now, // default to the image's intrinsic size. aImage->GetWidth(&mImageWidth); aImage->GetHeight(&mImageHeight); nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableMethod(this, &ImageDocument::DefaultCheckOverflowing); nsContentUtils::AddScriptRunner(runnable); UpdateTitleAndCharset(); return NS_OK; }
nsresult ImageDocument::OnLoadComplete(imgIRequest* aRequest, nsresult aStatus) { UpdateTitleAndCharset(); // mImageContent can be null if the document is already destroyed if (NS_FAILED(aStatus) && mStringBundle && mImageContent) { nsAutoCString src; mDocumentURI->GetSpec(src); NS_ConvertUTF8toUTF16 srcString(src); const char16_t* formatString[] = { srcString.get() }; nsXPIDLString errorMsg; mStringBundle->FormatStringFromName(u"InvalidImage", formatString, 1, getter_Copies(errorMsg)); mImageContent->SetAttr(kNameSpaceID_None, nsGkAtoms::alt, errorMsg, false); } return NS_OK; }