2013-07-19 19:08:33 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
#include "gfxSVGGlyphs.h"
|
|
|
|
|
2019-04-16 05:47:26 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
|
|
|
#include "mozilla/LoadInfo.h"
|
|
|
|
#include "mozilla/NullPrincipal.h"
|
|
|
|
#include "mozilla/PresShell.h"
|
|
|
|
#include "mozilla/SMILAnimationController.h"
|
2016-07-22 14:07:39 +03:00
|
|
|
#include "mozilla/SVGContextPaint.h"
|
2019-04-16 05:47:26 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/FontTableURIProtocolHandler.h"
|
|
|
|
#include "mozilla/dom/ImageTracker.h"
|
|
|
|
#include "mozilla/dom/SVGDocument.h"
|
2012-09-06 08:57:54 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsICategoryManager.h"
|
|
|
|
#include "nsIDocumentLoaderFactory.h"
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2012-12-12 03:15:07 +04:00
|
|
|
#include "nsSVGUtils.h"
|
2013-06-10 16:25:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-19 17:08:45 +04:00
|
|
|
#include "gfxFont.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxContext.h"
|
2013-05-16 20:29:20 +04:00
|
|
|
#include "harfbuzz/hb.h"
|
2019-05-21 00:01:18 +03:00
|
|
|
#include "zlib.h"
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
#define SVG_CONTENT_TYPE NS_LITERAL_CSTRING("image/svg+xml")
|
|
|
|
#define UTF8_CHARSET NS_LITERAL_CSTRING("utf-8")
|
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
using namespace mozilla;
|
2019-01-02 16:05:23 +03:00
|
|
|
using mozilla::dom::Document;
|
|
|
|
using mozilla::dom::Element;
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
|
|
|
const mozilla::gfx::Color SimpleTextContextPaint::sZero;
|
2012-09-06 08:58:47 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
gfxSVGGlyphs::gfxSVGGlyphs(hb_blob_t* aSVGTable, gfxFontEntry* aFontEntry)
|
2013-08-19 17:08:45 +04:00
|
|
|
: mSVGData(aSVGTable), mFontEntry(aFontEntry) {
|
2013-08-19 17:08:43 +04:00
|
|
|
unsigned int length;
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* svgData = hb_blob_get_data(mSVGData, &length);
|
|
|
|
mHeader = reinterpret_cast<const Header*>(svgData);
|
2013-08-19 17:08:43 +04:00
|
|
|
mDocIndex = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
if (sizeof(Header) <= length && uint16_t(mHeader->mVersion) == 0 &&
|
|
|
|
uint64_t(mHeader->mDocIndexOffset) + 2 <= length) {
|
2019-05-01 11:47:10 +03:00
|
|
|
const DocIndex* docIndex =
|
|
|
|
reinterpret_cast<const DocIndex*>(svgData + mHeader->mDocIndexOffset);
|
2013-08-19 17:08:43 +04:00
|
|
|
// Limit the number of documents to avoid overflow
|
|
|
|
if (uint64_t(mHeader->mDocIndexOffset) + 2 +
|
|
|
|
uint16_t(docIndex->mNumEntries) * sizeof(IndexEntry) <=
|
|
|
|
length) {
|
|
|
|
mDocIndex = docIndex;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:58:47 +04:00
|
|
|
}
|
|
|
|
|
2013-05-16 20:29:20 +04:00
|
|
|
gfxSVGGlyphs::~gfxSVGGlyphs() { hb_blob_destroy(mSVGData); }
|
2012-09-06 08:58:47 +04:00
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
void gfxSVGGlyphs::DidRefresh() { mFontEntry->NotifyGlyphsChanged(); }
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
/*
|
|
|
|
* Comparison operator for finding a range containing a given glyph ID. Simply
|
|
|
|
* checks whether |key| is less (greater) than every element of |range|, in
|
|
|
|
* which case return |key| < |range| (|key| > |range|). Otherwise |key| is in
|
|
|
|
* |range|, in which case return equality.
|
|
|
|
* The total ordering here is guaranteed by
|
|
|
|
* (1) the index ranges being disjoint; and
|
|
|
|
* (2) the (sole) key always being a singleton, so intersection => containment
|
|
|
|
* (note that this is wrong if we have more than one intersection or two
|
|
|
|
* sets intersecting of size > 1 -- so... don't do that)
|
|
|
|
*/
|
2019-02-26 01:07:19 +03:00
|
|
|
/* static */
|
2019-05-01 11:47:10 +03:00
|
|
|
int gfxSVGGlyphs::CompareIndexEntries(const void* aKey, const void* aEntry) {
|
|
|
|
const uint32_t key = *(uint32_t*)aKey;
|
|
|
|
const IndexEntry* entry = (const IndexEntry*)aEntry;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-12 13:48:35 +04:00
|
|
|
if (key < uint16_t(entry->mStartGlyph)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (key > uint16_t(entry->mEndGlyph)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2012-09-06 08:58:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
gfxSVGGlyphsDocument* gfxSVGGlyphs::FindOrCreateGlyphsDocument(
|
2012-09-06 08:58:47 +04:00
|
|
|
uint32_t aGlyphId) {
|
2013-08-19 17:08:43 +04:00
|
|
|
if (!mDocIndex) {
|
|
|
|
// Invalid table
|
2013-08-19 17:08:43 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
IndexEntry* entry = (IndexEntry*)bsearch(
|
2013-08-19 17:08:43 +04:00
|
|
|
&aGlyphId, mDocIndex->mEntries, uint16_t(mDocIndex->mNumEntries),
|
2012-09-06 08:58:47 +04:00
|
|
|
sizeof(IndexEntry), CompareIndexEntries);
|
|
|
|
if (!entry) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
gfxSVGGlyphsDocument* result = mGlyphDocs.Get(entry->mDocOffset);
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
if (!result) {
|
2013-08-19 17:08:43 +04:00
|
|
|
unsigned int length;
|
2019-05-01 11:47:10 +03:00
|
|
|
const uint8_t* data = (const uint8_t*)hb_blob_get_data(mSVGData, &length);
|
2013-08-19 17:08:43 +04:00
|
|
|
if (entry->mDocOffset > 0 && uint64_t(mHeader->mDocIndexOffset) +
|
|
|
|
entry->mDocOffset +
|
|
|
|
entry->mDocLength <=
|
|
|
|
length) {
|
|
|
|
result = new gfxSVGGlyphsDocument(
|
|
|
|
data + mHeader->mDocIndexOffset + entry->mDocOffset,
|
2013-08-19 17:08:45 +04:00
|
|
|
entry->mDocLength, this);
|
2013-08-19 17:08:43 +04:00
|
|
|
mGlyphDocs.Put(entry->mDocOffset, result);
|
2012-09-06 08:58:47 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult gfxSVGGlyphsDocument::SetupPresentation() {
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
2017-08-21 13:01:27 +03:00
|
|
|
nsCString contractId;
|
2018-07-24 03:41:06 +03:00
|
|
|
nsresult rv = catMan->GetCategoryEntry("Gecko-Content-Viewers",
|
|
|
|
"image/svg+xml", contractId);
|
2012-09-06 08:58:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2017-08-21 13:01:27 +03:00
|
|
|
nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
|
|
|
|
do_GetService(contractId.get());
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ASSERTION(docLoaderFactory, "Couldn't get DocumentLoaderFactory");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentViewer> viewer;
|
2012-09-06 08:58:47 +04:00
|
|
|
rv = docLoaderFactory->CreateInstanceForDocument(nullptr, mDocument, nullptr,
|
|
|
|
getter_AddRefs(viewer));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2019-07-23 05:13:32 +03:00
|
|
|
rv = viewer->Init(nullptr, gfx::IntRect(0, 0, 1000, 1000));
|
2012-09-06 08:57:54 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = viewer->Open(nullptr, nullptr);
|
2012-09-06 08:58:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2019-04-16 05:47:26 +03:00
|
|
|
RefPtr<PresShell> presShell = viewer->GetPresShell();
|
2012-09-07 08:16:09 +04:00
|
|
|
if (!presShell->DidInitialize()) {
|
2018-02-16 22:34:46 +03:00
|
|
|
rv = presShell->Initialize();
|
2012-09-06 08:58:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2017-01-05 10:31:56 +03:00
|
|
|
mDocument->FlushPendingNotifications(FlushType::Layout);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2017-08-31 09:00:39 +03:00
|
|
|
if (mDocument->HasAnimationController()) {
|
2019-01-02 10:21:13 +03:00
|
|
|
mDocument->GetAnimationController()->Resume(SMILTimeContainer::PAUSE_IMAGE);
|
2013-08-19 17:08:45 +04:00
|
|
|
}
|
2016-10-13 10:04:19 +03:00
|
|
|
mDocument->ImageTracker()->SetAnimatingState(true);
|
2013-08-19 17:08:45 +04:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
mViewer = viewer;
|
|
|
|
mPresShell = presShell;
|
2013-08-19 17:08:45 +04:00
|
|
|
mPresShell->AddPostRefreshObserver(this);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
return NS_OK;
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
void gfxSVGGlyphsDocument::DidRefresh() { mOwner->DidRefresh(); }
|
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
/**
|
|
|
|
* Walk the DOM tree to find all glyph elements and insert them into the lookup
|
|
|
|
* table
|
|
|
|
* @param aElem The element to search from
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
void gfxSVGGlyphsDocument::FindGlyphElements(Element* aElem) {
|
|
|
|
for (nsIContent* child = aElem->GetLastChild(); child;
|
2012-09-06 08:57:54 +04:00
|
|
|
child = child->GetPreviousSibling()) {
|
|
|
|
if (!child->IsElement()) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-19 17:08:43 +04:00
|
|
|
FindGlyphElements(child->AsElement());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
InsertGlyphId(aElem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there exists an SVG glyph with the specified glyph id, render it and
|
|
|
|
* return true If no such glyph exists, or in the case of an error return false
|
|
|
|
* @param aContext The thebes aContext to draw to
|
|
|
|
* @param aGlyphId The glyph id
|
|
|
|
* @return true iff rendering succeeded
|
|
|
|
*/
|
2019-05-01 11:47:10 +03:00
|
|
|
void gfxSVGGlyphs::RenderGlyph(gfxContext* aContext, uint32_t aGlyphId,
|
|
|
|
SVGContextPaint* aContextPaint) {
|
2012-09-06 08:57:54 +04:00
|
|
|
gfxContextAutoSaveRestore aContextRestorer(aContext);
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* glyph = mGlyphIdMap.Get(aGlyphId);
|
2017-05-18 23:03:41 +03:00
|
|
|
MOZ_ASSERT(glyph, "No glyph element. Should check with HasSVGGlyph() first!");
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2018-04-21 19:48:29 +03:00
|
|
|
AutoSetRestoreSVGContextPaint autoSetRestore(
|
|
|
|
*aContextPaint, *glyph->OwnerDoc()->AsSVGDocument());
|
2016-07-21 16:33:11 +03:00
|
|
|
|
2017-05-18 23:03:41 +03:00
|
|
|
nsSVGUtils::PaintSVGGlyph(glyph, aContext);
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2012-09-06 08:58:46 +04:00
|
|
|
bool gfxSVGGlyphs::GetGlyphExtents(uint32_t aGlyphId,
|
2019-05-01 11:47:10 +03:00
|
|
|
const gfxMatrix& aSVGToAppSpace,
|
|
|
|
gfxRect* aResult) {
|
|
|
|
Element* glyph = mGlyphIdMap.Get(aGlyphId);
|
2012-09-06 08:58:46 +04:00
|
|
|
NS_ASSERTION(glyph,
|
|
|
|
"No glyph element. Should check with HasSVGGlyph() first!");
|
|
|
|
|
2012-12-12 03:15:07 +04:00
|
|
|
return nsSVGUtils::GetSVGGlyphExtents(glyph, aSVGToAppSpace, aResult);
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* gfxSVGGlyphs::GetGlyphElement(uint32_t aGlyphId) {
|
|
|
|
Element* elem;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
if (!mGlyphIdMap.Get(aGlyphId, &elem)) {
|
|
|
|
elem = nullptr;
|
2019-05-01 11:47:10 +03:00
|
|
|
if (gfxSVGGlyphsDocument* set = FindOrCreateGlyphsDocument(aGlyphId)) {
|
2012-09-06 08:58:47 +04:00
|
|
|
elem = set->GetGlyphElement(aGlyphId);
|
|
|
|
}
|
|
|
|
mGlyphIdMap.Put(aGlyphId, elem);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
bool gfxSVGGlyphs::HasSVGGlyph(uint32_t aGlyphId) {
|
2012-09-06 08:58:47 +04:00
|
|
|
return !!GetGlyphElement(aGlyphId);
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2016-05-03 14:14:35 +03:00
|
|
|
size_t gfxSVGGlyphs::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
// We don't include the size of mSVGData here, because (depending on the
|
|
|
|
// font backend implementation) it will either wrap a block of data owned
|
|
|
|
// by the system (and potentially shared), or a table that's in our font
|
|
|
|
// table cache and therefore already counted.
|
|
|
|
size_t result = aMallocSizeOf(this) +
|
|
|
|
mGlyphDocs.ShallowSizeOfExcludingThis(aMallocSizeOf) +
|
|
|
|
mGlyphIdMap.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = mGlyphDocs.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
result += iter.Data()->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* gfxSVGGlyphsDocument::GetGlyphElement(uint32_t aGlyphId) {
|
2012-09-06 08:58:47 +04:00
|
|
|
return mGlyphIdMap.Get(aGlyphId);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
gfxSVGGlyphsDocument::gfxSVGGlyphsDocument(const uint8_t* aBuffer,
|
2013-08-19 17:08:45 +04:00
|
|
|
uint32_t aBufLen,
|
2019-05-01 11:47:10 +03:00
|
|
|
gfxSVGGlyphs* aSVGGlyphs)
|
2013-08-19 17:08:45 +04:00
|
|
|
: mOwner(aSVGGlyphs) {
|
2019-05-21 00:01:18 +03:00
|
|
|
if (aBufLen >= 14 && aBuffer[0] == 31 && aBuffer[1] == 139) {
|
|
|
|
// It's a gzip-compressed document; decompress it before parsing.
|
|
|
|
// The original length (modulo 2^32) is found in the last 4 bytes
|
|
|
|
// of the data, stored in little-endian format. We read it as
|
|
|
|
// individual bytes to avoid possible alignment issues.
|
|
|
|
// (Note that if the original length was >2^32, then origLen here
|
|
|
|
// will be incorrect; but then the inflate() call will not return
|
|
|
|
// Z_STREAM_END and we'll bail out safely.)
|
|
|
|
size_t origLen = (size_t(aBuffer[aBufLen - 1]) << 24) +
|
|
|
|
(size_t(aBuffer[aBufLen - 2]) << 16) +
|
|
|
|
(size_t(aBuffer[aBufLen - 3]) << 8) +
|
|
|
|
size_t(aBuffer[aBufLen - 4]);
|
|
|
|
AutoTArray<uint8_t, 4096> outBuf;
|
|
|
|
if (outBuf.SetLength(origLen, mozilla::fallible)) {
|
|
|
|
z_stream s = {0};
|
|
|
|
s.next_in = const_cast<Byte*>(aBuffer);
|
|
|
|
s.avail_in = aBufLen;
|
|
|
|
s.next_out = outBuf.Elements();
|
|
|
|
s.avail_out = outBuf.Length();
|
|
|
|
// The magic number 16 here is the zlib flag to expect gzip format,
|
|
|
|
// see http://www.zlib.net/manual.html#Advanced
|
|
|
|
if (Z_OK == inflateInit2(&s, 16 + MAX_WBITS)) {
|
|
|
|
int result = inflate(&s, Z_FINISH);
|
|
|
|
if (Z_STREAM_END == result) {
|
|
|
|
MOZ_ASSERT(size_t(s.next_out - outBuf.Elements()) == origLen);
|
|
|
|
ParseDocument(outBuf.Elements(), outBuf.Length());
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to decompress SVG glyphs document");
|
|
|
|
}
|
|
|
|
inflateEnd(&s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to allocate memory for SVG glyphs document");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ParseDocument(aBuffer, aBufLen);
|
|
|
|
}
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
if (!mDocument) {
|
|
|
|
NS_WARNING("Could not parse SVG glyphs document");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Element* root = mDocument->GetRootElement();
|
2012-09-06 08:58:47 +04:00
|
|
|
if (!root) {
|
|
|
|
NS_WARNING("Could not parse SVG glyphs document");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
nsresult rv = SetupPresentation();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Couldn't setup presentation for SVG glyphs document");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
FindGlyphElements(root);
|
2012-09-06 08:58:47 +04:00
|
|
|
}
|
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
gfxSVGGlyphsDocument::~gfxSVGGlyphsDocument() {
|
|
|
|
if (mDocument) {
|
2016-06-18 19:03:11 +03:00
|
|
|
mDocument->OnPageHide(false, nullptr);
|
2013-08-19 17:08:45 +04:00
|
|
|
}
|
|
|
|
if (mPresShell) {
|
|
|
|
mPresShell->RemovePostRefreshObserver(this);
|
|
|
|
}
|
|
|
|
if (mViewer) {
|
2016-06-18 20:32:19 +03:00
|
|
|
mViewer->Close(nullptr);
|
2013-08-19 17:08:45 +04:00
|
|
|
mViewer->Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
static nsresult CreateBufferedStream(const uint8_t* aBuffer, uint32_t aBufLen,
|
|
|
|
nsCOMPtr<nsIInputStream>& aResult) {
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2019-02-25 22:11:20 +03:00
|
|
|
nsresult rv = NS_NewByteInputStream(
|
|
|
|
getter_AddRefs(stream),
|
2019-05-01 11:47:10 +03:00
|
|
|
MakeSpan(reinterpret_cast<const char*>(aBuffer), aBufLen),
|
2019-02-25 22:11:20 +03:00
|
|
|
NS_ASSIGNMENT_DEPEND);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsIInputStream> aBufferedStream;
|
|
|
|
if (!NS_InputStreamIsBuffered(stream)) {
|
2017-10-19 12:39:30 +03:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(aBufferedStream),
|
|
|
|
stream.forget(), 4096);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
stream = aBufferedStream;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
aResult = stream;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult gfxSVGGlyphsDocument::ParseDocument(const uint8_t* aBuffer,
|
2013-05-16 20:29:20 +04:00
|
|
|
uint32_t aBufLen) {
|
2012-09-06 08:57:54 +04:00
|
|
|
// Mostly pulled from nsDOMParser::ParseFromStream
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = CreateBufferedStream(aBuffer, aBufLen, stream);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-06-02 16:51:42 +03:00
|
|
|
mozilla::dom::FontTableURIProtocolHandler::GenerateURIString(
|
|
|
|
mSVGGlyphsDocumentURI);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), mSVGGlyphsDocumentURI);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-22 21:36:20 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
NullPrincipal::CreateWithoutOriginAttributes();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> document;
|
2018-05-11 20:46:15 +03:00
|
|
|
rv = NS_NewDOMDocument(getter_AddRefs(document),
|
2012-09-06 08:57:54 +04:00
|
|
|
EmptyString(), // aNamespaceURI
|
|
|
|
EmptyString(), // aQualifiedName
|
|
|
|
nullptr, // aDoctype
|
|
|
|
uri, uri, principal,
|
|
|
|
false, // aLoadedAsData
|
|
|
|
nullptr, // aEventObject
|
2018-02-25 19:28:14 +03:00
|
|
|
DocumentFlavorSVG);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-10-16 22:15:40 +04:00
|
|
|
rv = NS_NewInputStreamChannel(
|
|
|
|
getter_AddRefs(channel), uri,
|
|
|
|
nullptr, // aStream
|
|
|
|
principal, nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER, SVG_CONTENT_TYPE, UTF8_CHARSET);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
// Set this early because various decisions during page-load depend on it.
|
|
|
|
document->SetIsBeingUsedAsImage();
|
2018-01-15 17:33:25 +03:00
|
|
|
document->SetIsSVGGlyphsDocument();
|
2019-01-02 16:05:23 +03:00
|
|
|
document->SetReadyStateInternal(Document::READYSTATE_UNINITIALIZED);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
rv = document->StartDocumentLoad("external-resource", channel,
|
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aContainer
|
|
|
|
getter_AddRefs(listener), true /* aReset */);
|
|
|
|
if (NS_FAILED(rv) || !listener) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
rv = listener->OnStartRequest(channel);
|
2012-09-06 08:57:54 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
channel->Cancel(rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
nsresult status;
|
|
|
|
channel->GetStatus(&status);
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(status)) {
|
2019-02-28 02:42:27 +03:00
|
|
|
rv = listener->OnDataAvailable(channel, stream, 0, aBufLen);
|
2012-09-06 08:57:54 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
channel->Cancel(rv);
|
|
|
|
}
|
|
|
|
channel->GetStatus(&status);
|
|
|
|
}
|
|
|
|
|
2019-02-28 02:41:31 +03:00
|
|
|
rv = listener->OnStopRequest(channel, status);
|
2012-09-06 08:57:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
document.swap(mDocument);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void gfxSVGGlyphsDocument::InsertGlyphId(Element* aGlyphElement) {
|
2012-09-06 08:57:54 +04:00
|
|
|
nsAutoString glyphIdStr;
|
2013-08-19 17:08:43 +04:00
|
|
|
static const uint32_t glyphPrefixLength = 5;
|
|
|
|
// The maximum glyph ID is 65535 so the maximum length of the numeric part
|
|
|
|
// is 5.
|
|
|
|
if (!aGlyphElement->GetAttr(kNameSpaceID_None, nsGkAtoms::id, glyphIdStr) ||
|
|
|
|
!StringBeginsWith(glyphIdStr, NS_LITERAL_STRING("glyph")) ||
|
|
|
|
glyphIdStr.Length() > glyphPrefixLength + 5) {
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
uint32_t id = 0;
|
|
|
|
for (uint32_t i = glyphPrefixLength; i < glyphIdStr.Length(); ++i) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ch = glyphIdStr.CharAt(i);
|
2013-08-19 17:08:43 +04:00
|
|
|
if (ch < '0' || ch > '9') {
|
2012-09-06 08:57:54 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-19 17:08:43 +04:00
|
|
|
if (ch == '0' && i == glyphPrefixLength) {
|
2012-09-06 08:57:54 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-19 17:08:43 +04:00
|
|
|
id = id * 10 + (ch - '0');
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
mGlyphIdMap.Put(id, aGlyphElement);
|
2012-09-06 08:57:54 +04:00
|
|
|
}
|
|
|
|
|
2016-05-03 14:14:35 +03:00
|
|
|
size_t gfxSVGGlyphsDocument::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf aMallocSizeOf) const {
|
|
|
|
return aMallocSizeOf(this) +
|
|
|
|
mGlyphIdMap.ShallowSizeOfExcludingThis(aMallocSizeOf) +
|
|
|
|
mSVGGlyphsDocumentURI.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|