Bug 1072101 - Part 2: Implement FontFaceSet.{entries,values}. r=peterv

This commit is contained in:
Cameron McCormack 2015-03-31 14:05:33 +11:00
parent b7a4daf2fd
commit 161a8c2a7e
7 changed files with 161 additions and 6 deletions

View File

@ -473,6 +473,10 @@ DOMInterfaces = {
'wrapperCache': False, 'wrapperCache': False,
}, },
'FontFaceSetIterator': {
'wrapperCache': False,
},
'FormData': { 'FormData': {
'nativeType': 'nsFormData' 'nativeType': 'nsFormData'
}, },

View File

@ -10,6 +10,19 @@
* liability, trademark and document use rules apply. * liability, trademark and document use rules apply.
*/ */
// To implement FontFaceSet's iterator until we can use setlike.
dictionary FontFaceSetIteratorResult
{
required any value;
required boolean done;
};
// To implement FontFaceSet's iterator until we can use setlike.
[NoInterfaceObject]
interface FontFaceSetIterator {
[Throws] FontFaceSetIteratorResult next();
};
enum FontFaceSetLoadStatus { "loading", "loaded" }; enum FontFaceSetLoadStatus { "loading", "loaded" };
// Bug 1072762 is for the FontFaceSet constructor. // Bug 1072762 is for the FontFaceSet constructor.
@ -23,9 +36,9 @@ interface FontFaceSet : EventTarget {
boolean has(FontFace font); boolean has(FontFace font);
[Throws] boolean delete(FontFace font); [Throws] boolean delete(FontFace font);
void clear(); void clear();
// Iterator entries(); [NewObject] FontFaceSetIterator entries();
// Iterator keys(); // Iterator keys();
// Iterator values(); [NewObject] FontFaceSetIterator values();
// void forEach(ForEachCallback cb, optional any thisArg); // void forEach(ForEachCallback cb, optional any thisArg);
// FontFace iterator; // FontFace iterator;

View File

@ -12,6 +12,7 @@
#include "mozilla/dom/CSSFontFaceLoadEvent.h" #include "mozilla/dom/CSSFontFaceLoadEvent.h"
#include "mozilla/dom/CSSFontFaceLoadEventBinding.h" #include "mozilla/dom/CSSFontFaceLoadEventBinding.h"
#include "mozilla/dom/FontFaceSetBinding.h" #include "mozilla/dom/FontFaceSetBinding.h"
#include "mozilla/dom/FontFaceSetIterator.h"
#include "mozilla/dom/Promise.h" #include "mozilla/dom/Promise.h"
#include "mozilla/AsyncEventDispatcher.h" #include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
@ -328,25 +329,30 @@ FontFaceSet::Has(FontFace& aFontFace)
} }
FontFace* FontFace*
FontFaceSet::IndexedGetter(uint32_t aIndex, bool& aFound) FontFaceSet::GetFontFaceAt(uint32_t aIndex)
{ {
mPresContext->FlushUserFontSet(); mPresContext->FlushUserFontSet();
if (aIndex < mRuleFaces.Length()) { if (aIndex < mRuleFaces.Length()) {
aFound = true;
return mRuleFaces[aIndex].mFontFace; return mRuleFaces[aIndex].mFontFace;
} }
aIndex -= mRuleFaces.Length(); aIndex -= mRuleFaces.Length();
if (aIndex < mNonRuleFaces.Length()) { if (aIndex < mNonRuleFaces.Length()) {
aFound = true;
return mNonRuleFaces[aIndex].mFontFace; return mNonRuleFaces[aIndex].mFontFace;
} }
aFound = false;
return nullptr; return nullptr;
} }
FontFace*
FontFaceSet::IndexedGetter(uint32_t aIndex, bool& aFound)
{
FontFace* f = GetFontFaceAt(aIndex);
aFound = !!f;
return f;
}
uint32_t uint32_t
FontFaceSet::Size() FontFaceSet::Size()
{ {
@ -358,6 +364,18 @@ FontFaceSet::Size()
return std::min<size_t>(total, INT32_MAX); return std::min<size_t>(total, INT32_MAX);
} }
FontFaceSetIterator*
FontFaceSet::Entries()
{
return new FontFaceSetIterator(this, true);
}
FontFaceSetIterator*
FontFaceSet::Values()
{
return new FontFaceSetIterator(this, false);
}
static PLDHashOperator DestroyIterator(nsPtrHashKey<nsFontFaceLoader>* aKey, static PLDHashOperator DestroyIterator(nsPtrHashKey<nsFontFaceLoader>* aKey,
void* aUserArg) void* aUserArg)
{ {

View File

@ -158,6 +158,8 @@ public:
bool aWasAlternate, bool aWasAlternate,
nsresult aStatus) override; nsresult aStatus) override;
FontFace* GetFontFaceAt(uint32_t aIndex);
// -- Web IDL -------------------------------------------------------------- // -- Web IDL --------------------------------------------------------------
IMPL_EVENT_HANDLER(loading) IMPL_EVENT_HANDLER(loading)
@ -177,6 +179,8 @@ public:
bool Delete(FontFace& aFontFace, mozilla::ErrorResult& aRv); bool Delete(FontFace& aFontFace, mozilla::ErrorResult& aRv);
bool Has(FontFace& aFontFace); bool Has(FontFace& aFontFace);
uint32_t Size(); uint32_t Size();
mozilla::dom::FontFaceSetIterator* Entries();
mozilla::dom::FontFaceSetIterator* Values();
FontFace* IndexedGetter(uint32_t aIndex, bool& aFound); FontFace* IndexedGetter(uint32_t aIndex, bool& aFound);
uint32_t Length() { return Size(); } uint32_t Length() { return Size(); }

View File

@ -0,0 +1,75 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "mozilla/dom/FontFaceSetIterator.h"
namespace mozilla {
namespace dom {
FontFaceSetIterator::FontFaceSetIterator(FontFaceSet* aFontFaceSet,
bool aIsKeyAndValue)
: mFontFaceSet(aFontFaceSet)
, mNextIndex(0)
, mIsKeyAndValue(aIsKeyAndValue)
{
MOZ_COUNT_CTOR(FontFaceSetIterator);
}
FontFaceSetIterator::~FontFaceSetIterator()
{
MOZ_COUNT_DTOR(FontFaceSetIterator);
}
bool
FontFaceSetIterator::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto,
JS::MutableHandle<JSObject*> aReflector)
{
return FontFaceSetIteratorBinding::Wrap(aCx, this, aGivenProto, aReflector);
}
void
FontFaceSetIterator::Next(JSContext* aCx, FontFaceSetIteratorResult& aResult,
ErrorResult& aRv)
{
if (!mFontFaceSet) {
aResult.mDone = true;
return;
}
FontFace* face = mFontFaceSet->GetFontFaceAt(mNextIndex++);
if (!face) {
aResult.mValue.setUndefined();
aResult.mDone = true;
mFontFaceSet = nullptr;
return;
}
JS::Rooted<JS::Value> value(aCx);
if (!ToJSValue(aCx, face, &value)) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
if (mIsKeyAndValue) {
JS::AutoValueArray<2> values(aCx);
values[0].set(value);
values[1].set(value);
JS::Rooted<JSObject*> array(aCx);
array = JS_NewArrayObject(aCx, values);
if (array) {
aResult.mValue.setObject(*array);
}
} else {
aResult.mValue = value;
}
aResult.mDone = false;
}
} // namespace dom
} // namespace mozilla

View File

@ -0,0 +1,39 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#ifndef mozilla_dom_FontFaceSetIterator_h
#define mozilla_dom_FontFaceSetIterator_h
#include "mozilla/dom/FontFaceSetBinding.h"
#include "mozilla/dom/NonRefcountedDOMObject.h"
namespace mozilla {
namespace dom {
class FontFaceSetIterator final : public NonRefcountedDOMObject
{
public:
FontFaceSetIterator(mozilla::dom::FontFaceSet* aFontFaceSet,
bool aIsKeyAndValue);
~FontFaceSetIterator();
bool WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto,
JS::MutableHandle<JSObject*> aReflector);
// WebIDL
void Next(JSContext* aCx, FontFaceSetIteratorResult& aResult,
mozilla::ErrorResult& aRv);
private:
nsRefPtr<FontFaceSet> mFontFaceSet;
uint32_t mNextIndex;
bool mIsKeyAndValue;
};
} // namespace dom
} // namespace mozilla
#endif // !defined(mozilla_dom_FontFaceSetIterator_h)

View File

@ -93,6 +93,7 @@ EXPORTS.mozilla.dom += [
'CSSValue.h', 'CSSValue.h',
'FontFace.h', 'FontFace.h',
'FontFaceSet.h', 'FontFaceSet.h',
'FontFaceSetIterator.h',
'MediaQueryList.h', 'MediaQueryList.h',
] ]
@ -120,6 +121,7 @@ UNIFIED_SOURCES += [
'Declaration.cpp', 'Declaration.cpp',
'ErrorReporter.cpp', 'ErrorReporter.cpp',
'FontFace.cpp', 'FontFace.cpp',
'FontFaceSetIterator.cpp',
'ImageLoader.cpp', 'ImageLoader.cpp',
'Loader.cpp', 'Loader.cpp',
'MediaQueryList.cpp', 'MediaQueryList.cpp',