FontFaceSetIterator.cpp (2306B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/FontFaceSetIterator.h" 8 9 #include "js/Array.h" // JS::NewArrayObject 10 11 namespace mozilla { 12 namespace dom { 13 14 NS_IMPL_CYCLE_COLLECTION(FontFaceSetIterator, mFontFaceSet) 15 16 FontFaceSetIterator::FontFaceSetIterator(FontFaceSet* aFontFaceSet, 17 bool aIsKeyAndValue) 18 : mFontFaceSet(aFontFaceSet), 19 mNextIndex(0), 20 mIsKeyAndValue(aIsKeyAndValue) { 21 MOZ_COUNT_CTOR(FontFaceSetIterator); 22 } 23 24 FontFaceSetIterator::~FontFaceSetIterator() { 25 MOZ_COUNT_DTOR(FontFaceSetIterator); 26 } 27 28 bool FontFaceSetIterator::WrapObject(JSContext* aCx, 29 JS::Handle<JSObject*> aGivenProto, 30 JS::MutableHandle<JSObject*> aReflector) { 31 return FontFaceSetIterator_Binding::Wrap(aCx, this, aGivenProto, aReflector); 32 } 33 34 void FontFaceSetIterator::Next(JSContext* aCx, 35 FontFaceSetIteratorResult& aResult, 36 ErrorResult& aRv) { 37 if (!mFontFaceSet) { 38 aResult.mDone = true; 39 return; 40 } 41 42 // Skip over non-Author origin fonts (GetFontFaceAt returns nullptr 43 // for those). 44 FontFace* face; 45 while (!(face = mFontFaceSet->GetFontFaceAt(mNextIndex++))) { 46 if (mNextIndex >= mFontFaceSet->SizeIncludingNonAuthorOrigins()) { 47 break; // this iterator is done 48 } 49 } 50 51 if (!face) { 52 aResult.mValue.setUndefined(); 53 aResult.mDone = true; 54 mFontFaceSet = nullptr; 55 return; 56 } 57 58 JS::Rooted<JS::Value> value(aCx); 59 if (!ToJSValue(aCx, face, &value)) { 60 aRv.Throw(NS_ERROR_FAILURE); 61 return; 62 } 63 64 if (mIsKeyAndValue) { 65 JS::RootedValueArray<2> values(aCx); 66 values[0].set(value); 67 values[1].set(value); 68 69 JS::Rooted<JSObject*> array(aCx); 70 array = JS::NewArrayObject(aCx, values); 71 if (array) { 72 aResult.mValue.setObject(*array); 73 } 74 } else { 75 aResult.mValue = value; 76 } 77 78 aResult.mDone = false; 79 } 80 81 } // namespace dom 82 } // namespace mozilla