2014-10-01 19:32:05 -07:00
|
|
|
/* -*- 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/FontFace.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/FontFaceBinding.h"
|
2014-10-01 19:32:06 -07:00
|
|
|
#include "mozilla/dom/FontFaceSet.h"
|
2014-10-01 19:32:05 -07:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-10-01 19:32:09 -07:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2014-10-01 19:32:09 -07:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2014-10-01 19:32:07 -07:00
|
|
|
#include "nsCSSParser.h"
|
2014-10-01 19:32:06 -07:00
|
|
|
#include "nsCSSRules.h"
|
2014-10-01 19:32:06 -07:00
|
|
|
#include "nsIDocument.h"
|
2014-10-01 19:32:07 -07:00
|
|
|
#include "nsStyleUtil.h"
|
2014-10-01 19:32:05 -07:00
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
// -- FontFaceBufferSource ---------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object that wraps a FontFace object and exposes its ArrayBuffer
|
|
|
|
* or ArrayBufferView data in a form the user font set can consume.
|
|
|
|
*/
|
|
|
|
class FontFaceBufferSource : public gfxFontFaceBufferSource
|
|
|
|
{
|
|
|
|
public:
|
2014-09-24 06:16:53 -07:00
|
|
|
explicit FontFaceBufferSource(FontFace* aFontFace)
|
2014-10-01 19:32:09 -07:00
|
|
|
: mFontFace(aFontFace) {}
|
|
|
|
virtual void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength);
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<FontFace> mFontFace;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFaceBufferSource::TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength)
|
|
|
|
{
|
2015-05-10 18:24:49 -07:00
|
|
|
MOZ_ASSERT(mFontFace, "only call TakeBuffer once on a given "
|
|
|
|
"FontFaceBufferSource object");
|
2014-10-01 19:32:09 -07:00
|
|
|
mFontFace->TakeBuffer(aBuffer, aLength);
|
2015-05-10 18:24:49 -07:00
|
|
|
mFontFace = nullptr;
|
2014-10-01 19:32:09 -07:00
|
|
|
}
|
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
// -- Utility functions ------------------------------------------------------
|
2014-10-01 19:32:09 -07:00
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
template<typename T>
|
|
|
|
static void
|
|
|
|
GetDataFrom(const T& aObject, uint8_t*& aBuffer, uint32_t& aLength)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aBuffer);
|
|
|
|
aObject.ComputeLengthAndData();
|
2015-02-18 20:51:06 -08:00
|
|
|
// We use malloc here rather than a FallibleTArray or fallible
|
|
|
|
// operator new[] since the gfxUserFontEntry will be calling free
|
2014-10-01 19:32:09 -07:00
|
|
|
// on it.
|
2015-02-18 20:51:06 -08:00
|
|
|
aBuffer = (uint8_t*) malloc(aObject.Length());
|
2014-10-01 19:32:09 -07:00
|
|
|
if (!aBuffer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy((void*) aBuffer, aObject.Data(), aObject.Length());
|
|
|
|
aLength = aObject.Length();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
// -- FontFace ---------------------------------------------------------------
|
2014-10-01 19:32:05 -07:00
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(FontFace)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(FontFace)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLoaded)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRule)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFontFaceSet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(FontFace)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLoaded)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRule)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFontFaceSet)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(FontFace)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2014-10-01 19:32:05 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FontFace)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(FontFace)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(FontFace)
|
|
|
|
|
2015-06-26 18:39:54 -07:00
|
|
|
FontFace::FontFace(nsISupports* aParent, FontFaceSet* aFontFaceSet)
|
2014-10-01 19:32:05 -07:00
|
|
|
: mParent(aParent)
|
2014-10-01 19:32:06 -07:00
|
|
|
, mStatus(FontFaceLoadStatus::Unloaded)
|
2014-10-01 19:32:09 -07:00
|
|
|
, mSourceType(SourceType(0))
|
|
|
|
, mSourceBuffer(nullptr)
|
|
|
|
, mSourceBufferLength(0)
|
2015-06-26 18:39:54 -07:00
|
|
|
, mFontFaceSet(aFontFaceSet)
|
2014-10-01 19:32:08 -07:00
|
|
|
, mInFontFaceSet(false)
|
2014-10-01 19:32:05 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(FontFace);
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aParent);
|
|
|
|
|
2014-11-10 19:53:55 -08:00
|
|
|
// If the pref is not set, don't create the Promise (which the page wouldn't
|
|
|
|
// be able to get to anyway) as it causes the window.FontFace constructor
|
|
|
|
// to be created.
|
|
|
|
if (global && FontFaceSet::PrefEnabled()) {
|
2014-10-01 19:32:07 -07:00
|
|
|
ErrorResult rv;
|
|
|
|
mLoaded = Promise::Create(global, rv);
|
|
|
|
}
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FontFace::~FontFace()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(FontFace);
|
2014-10-01 19:32:08 -07:00
|
|
|
|
|
|
|
SetUserFontEntry(nullptr);
|
2014-10-01 19:32:08 -07:00
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
if (mSourceBuffer) {
|
2015-03-31 22:29:55 -07:00
|
|
|
free(mSourceBuffer);
|
2014-10-01 19:32:09 -07:00
|
|
|
}
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
FontFace::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-01 19:32:05 -07:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
return FontFaceBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:06 -07:00
|
|
|
static FontFaceLoadStatus
|
|
|
|
LoadStateToStatus(gfxUserFontEntry::UserFontLoadState aLoadState)
|
|
|
|
{
|
|
|
|
switch (aLoadState) {
|
|
|
|
case gfxUserFontEntry::UserFontLoadState::STATUS_NOT_LOADED:
|
|
|
|
return FontFaceLoadStatus::Unloaded;
|
|
|
|
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADING:
|
|
|
|
return FontFaceLoadStatus::Loading;
|
|
|
|
case gfxUserFontEntry::UserFontLoadState::STATUS_LOADED:
|
|
|
|
return FontFaceLoadStatus::Loaded;
|
|
|
|
case gfxUserFontEntry::UserFontLoadState::STATUS_FAILED:
|
|
|
|
return FontFaceLoadStatus::Error;
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("invalid aLoadState value");
|
|
|
|
return FontFaceLoadStatus::Error;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:06 -07:00
|
|
|
already_AddRefed<FontFace>
|
2014-10-01 19:32:06 -07:00
|
|
|
FontFace::CreateForRule(nsISupports* aGlobal,
|
2015-06-26 18:39:54 -07:00
|
|
|
FontFaceSet* aFontFaceSet,
|
2014-10-09 00:03:56 -07:00
|
|
|
nsCSSFontFaceRule* aRule)
|
2014-10-01 19:32:06 -07:00
|
|
|
{
|
2014-10-01 19:32:07 -07:00
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject = do_QueryInterface(aGlobal);
|
|
|
|
|
2015-06-26 18:39:54 -07:00
|
|
|
nsRefPtr<FontFace> obj = new FontFace(aGlobal, aFontFaceSet);
|
2014-10-01 19:32:06 -07:00
|
|
|
obj->mRule = aRule;
|
2014-10-01 19:32:09 -07:00
|
|
|
obj->mSourceType = eSourceType_FontFaceRule;
|
2014-10-01 19:32:08 -07:00
|
|
|
obj->mInFontFaceSet = true;
|
2014-10-01 19:32:06 -07:00
|
|
|
return obj.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:05 -07:00
|
|
|
already_AddRefed<FontFace>
|
|
|
|
FontFace::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aFamily,
|
|
|
|
const StringOrArrayBufferOrArrayBufferView& aSource,
|
|
|
|
const FontFaceDescriptors& aDescriptors,
|
2014-10-01 19:32:06 -07:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsISupports* global = aGlobal.GetAsSupports();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global);
|
|
|
|
nsIDocument* doc = window->GetDoc();
|
|
|
|
if (!doc) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-26 18:39:54 -07:00
|
|
|
nsRefPtr<FontFace> obj = new FontFace(global, doc->Fonts());
|
2014-10-01 19:32:09 -07:00
|
|
|
if (!obj->SetDescriptors(aFamily, aDescriptors)) {
|
|
|
|
return obj.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
obj->InitializeSource(aSource);
|
2014-10-01 19:32:05 -07:00
|
|
|
return obj.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
void
|
2015-03-27 03:13:21 -07:00
|
|
|
FontFace::InitializeSource(const StringOrArrayBufferOrArrayBufferView& aSource)
|
2014-10-01 19:32:09 -07:00
|
|
|
{
|
2015-03-27 03:13:21 -07:00
|
|
|
if (aSource.IsString()) {
|
2014-10-01 19:32:09 -07:00
|
|
|
if (!ParseDescriptor(eCSSFontDesc_Src,
|
2015-03-27 03:13:21 -07:00
|
|
|
aSource.GetAsString(),
|
2014-10-01 19:32:09 -07:00
|
|
|
mDescriptors->mSrc)) {
|
|
|
|
if (mLoaded) {
|
2015-03-27 03:13:21 -07:00
|
|
|
// The SetStatus call we are about to do assumes that for
|
2014-10-01 19:32:09 -07:00
|
|
|
// FontFace objects with sources other than ArrayBuffer(View)s, that the
|
|
|
|
// mLoaded Promise is rejected with a network error. We get
|
|
|
|
// in here beforehand to set it to the required syntax error.
|
|
|
|
mLoaded->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
SetStatus(FontFaceLoadStatus::Error);
|
2014-10-01 19:32:09 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSourceType = eSourceType_URLs;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
mSourceType = FontFace::eSourceType_Buffer;
|
2014-10-01 19:32:09 -07:00
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
if (aSource.IsArrayBuffer()) {
|
|
|
|
GetDataFrom(aSource.GetAsArrayBuffer(),
|
|
|
|
mSourceBuffer, mSourceBufferLength);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aSource.IsArrayBufferView());
|
|
|
|
GetDataFrom(aSource.GetAsArrayBufferView(),
|
|
|
|
mSourceBuffer, mSourceBufferLength);
|
|
|
|
}
|
2014-10-01 19:32:09 -07:00
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
SetStatus(FontFaceLoadStatus::Loading);
|
|
|
|
DoLoad();
|
2014-10-01 19:32:09 -07:00
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:05 -07:00
|
|
|
void
|
|
|
|
FontFace::GetFamily(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
|
|
|
// Serialize the same way as in nsCSSFontFaceStyleDecl::GetPropertyValue.
|
|
|
|
nsCSSValue value;
|
|
|
|
GetDesc(eCSSFontDesc_Family, value);
|
|
|
|
|
|
|
|
aResult.Truncate();
|
2015-03-24 01:34:32 -07:00
|
|
|
|
|
|
|
if (value.GetUnit() == eCSSUnit_Null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
nsDependentString family(value.GetStringBufferValue());
|
2014-10-01 19:32:09 -07:00
|
|
|
if (!family.IsEmpty()) {
|
|
|
|
// The string length can be zero when the author passed an invalid
|
|
|
|
// family name or an invalid descriptor to the JS FontFace constructor.
|
|
|
|
nsStyleUtil::AppendEscapedCSSString(family, aResult);
|
|
|
|
}
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetFamily(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_Family, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetStyle(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
GetDesc(eCSSFontDesc_Style, eCSSProperty_font_style, aResult);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetStyle(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_Style, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetWeight(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
GetDesc(eCSSFontDesc_Weight, eCSSProperty_font_weight, aResult);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetWeight(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_Weight, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetStretch(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
GetDesc(eCSSFontDesc_Stretch, eCSSProperty_font_stretch, aResult);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetStretch(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_Stretch, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetUnicodeRange(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
2015-03-17 00:15:42 -07:00
|
|
|
// There is no eCSSProperty_unicode_range for us to pass in to GetDesc
|
|
|
|
// to get a serialized (possibly defaulted) value, but that function
|
|
|
|
// doesn't use the property ID for this descriptor anyway.
|
|
|
|
GetDesc(eCSSFontDesc_UnicodeRange, eCSSProperty_UNKNOWN, aResult);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetUnicodeRange(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_UnicodeRange, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetVariant(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
|
|
|
// XXX Just expose the font-variant descriptor as "normal" until we
|
|
|
|
// support it properly (bug 1055385).
|
|
|
|
aResult.AssignLiteral("normal");
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetVariant(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
|
|
|
// XXX Ignore assignments to variant until we support font-variant
|
|
|
|
// descriptors (bug 1055385).
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetFeatureSettings(nsString& aResult)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2015-03-17 00:15:42 -07:00
|
|
|
GetDesc(eCSSFontDesc_FontFeatureSettings, eCSSProperty_font_feature_settings,
|
|
|
|
aResult);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetFeatureSettings(const nsAString& aValue, ErrorResult& aRv)
|
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
SetDescriptor(eCSSFontDesc_FontFeatureSettings, aValue, aRv);
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
FontFaceLoadStatus
|
|
|
|
FontFace::Status()
|
|
|
|
{
|
2014-10-01 19:32:06 -07:00
|
|
|
return mStatus;
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
2014-10-01 19:32:07 -07:00
|
|
|
FontFace::Load(ErrorResult& aRv)
|
2014-10-01 19:32:05 -07:00
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
if (!mLoaded) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
// Calling Load on a FontFace constructed with an ArrayBuffer data source,
|
|
|
|
// or on one that is already loading (or has finished loading), has no
|
|
|
|
// effect.
|
|
|
|
if (mSourceType == eSourceType_Buffer ||
|
|
|
|
mStatus != FontFaceLoadStatus::Unloaded) {
|
2014-10-01 19:32:07 -07:00
|
|
|
return mLoaded;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
// Calling the user font entry's Load method will end up setting our
|
|
|
|
// status to Loading, but the spec requires us to set it to Loading
|
|
|
|
// here.
|
2014-10-01 19:32:07 -07:00
|
|
|
SetStatus(FontFaceLoadStatus::Loading);
|
|
|
|
|
2015-03-27 03:13:21 -07:00
|
|
|
DoLoad();
|
2014-10-01 19:32:09 -07:00
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
return mLoaded;
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
|
|
|
|
2015-06-26 18:41:10 -07:00
|
|
|
gfxUserFontEntry*
|
|
|
|
FontFace::CreateUserFontEntry()
|
2014-10-01 19:32:09 -07:00
|
|
|
{
|
|
|
|
if (!mUserFontEntry) {
|
2014-10-01 19:32:10 -07:00
|
|
|
MOZ_ASSERT(!HasRule(),
|
|
|
|
"Rule backed FontFace objects should already have a user font "
|
2014-10-01 19:32:09 -07:00
|
|
|
"entry by the time Load() can be called on them");
|
|
|
|
|
|
|
|
nsRefPtr<gfxUserFontEntry> newEntry =
|
|
|
|
mFontFaceSet->FindOrCreateUserFontEntryFromFontFace(this);
|
2015-06-26 18:41:10 -07:00
|
|
|
if (newEntry) {
|
|
|
|
SetUserFontEntry(newEntry);
|
2014-10-01 19:32:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 18:41:10 -07:00
|
|
|
return mUserFontEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::DoLoad()
|
|
|
|
{
|
|
|
|
if (!CreateUserFontEntry()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-01 19:32:09 -07:00
|
|
|
mUserFontEntry->Load();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:05 -07:00
|
|
|
Promise*
|
2014-10-01 19:32:07 -07:00
|
|
|
FontFace::GetLoaded(ErrorResult& aRv)
|
2014-10-01 19:32:05 -07:00
|
|
|
{
|
2015-06-26 18:39:54 -07:00
|
|
|
mFontFaceSet->FlushUserFontSet();
|
2014-10-01 19:32:07 -07:00
|
|
|
|
|
|
|
if (!mLoaded) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLoaded;
|
2014-10-01 19:32:05 -07:00
|
|
|
}
|
2014-10-01 19:32:06 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetStatus(FontFaceLoadStatus aStatus)
|
|
|
|
{
|
2014-10-01 19:32:07 -07:00
|
|
|
if (mStatus == aStatus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
if (aStatus < mStatus) {
|
|
|
|
// We're being asked to go backwards in status! Normally, this shouldn't
|
|
|
|
// happen. But it can if the FontFace had a user font entry that had
|
|
|
|
// loaded, but then was given a new one by FontFaceSet::InsertRuleFontFace
|
|
|
|
// if we used a local() rule. For now, just ignore the request to
|
|
|
|
// go backwards in status.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:06 -07:00
|
|
|
mStatus = aStatus;
|
2014-10-01 19:32:07 -07:00
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
if (mInFontFaceSet) {
|
|
|
|
mFontFaceSet->OnFontFaceStatusChanged(this);
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
if (!mLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStatus == FontFaceLoadStatus::Loaded) {
|
|
|
|
mLoaded->MaybeResolve(this);
|
|
|
|
} else if (mStatus == FontFaceLoadStatus::Error) {
|
2014-10-01 19:32:09 -07:00
|
|
|
if (mSourceType == eSourceType_Buffer) {
|
|
|
|
mLoaded->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
} else {
|
|
|
|
mLoaded->MaybeReject(NS_ERROR_DOM_NETWORK_ERR);
|
|
|
|
}
|
2014-10-01 19:32:07 -07:00
|
|
|
}
|
2014-10-01 19:32:06 -07:00
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
bool
|
|
|
|
FontFace::ParseDescriptor(nsCSSFontDesc aDescID,
|
|
|
|
const nsAString& aString,
|
|
|
|
nsCSSValue& aResult)
|
|
|
|
{
|
|
|
|
nsCSSParser parser;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mParent);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = global->PrincipalOrNull();
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mParent);
|
2015-05-05 02:55:28 -07:00
|
|
|
nsCOMPtr<nsIURI> docURI = window->GetDocumentURI();
|
2014-10-01 19:32:07 -07:00
|
|
|
nsCOMPtr<nsIURI> base = window->GetDocBaseURI();
|
|
|
|
|
|
|
|
if (!parser.ParseFontFaceDescriptor(aDescID, aString,
|
2015-05-05 02:55:28 -07:00
|
|
|
docURI, // aSheetURL
|
2014-10-01 19:32:07 -07:00
|
|
|
base,
|
|
|
|
principal,
|
|
|
|
aResult)) {
|
|
|
|
aResult.Reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::SetDescriptor(nsCSSFontDesc aFontDesc,
|
|
|
|
const nsAString& aValue,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-10-01 19:32:10 -07:00
|
|
|
NS_ASSERTION(!HasRule(),
|
|
|
|
"we don't handle rule backed FontFace objects yet");
|
|
|
|
if (HasRule()) {
|
2014-10-01 19:32:07 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSValue parsedValue;
|
|
|
|
if (!ParseDescriptor(aFontDesc, aValue, parsedValue)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDescriptors->Get(aFontDesc) = parsedValue;
|
|
|
|
|
|
|
|
// XXX Setting descriptors doesn't actually have any effect on FontFace
|
|
|
|
// objects that have started loading or have already been loaded.
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
bool
|
|
|
|
FontFace::SetDescriptors(const nsAString& aFamily,
|
|
|
|
const FontFaceDescriptors& aDescriptors)
|
|
|
|
{
|
2014-10-01 19:32:10 -07:00
|
|
|
MOZ_ASSERT(!HasRule());
|
2014-10-01 19:32:09 -07:00
|
|
|
MOZ_ASSERT(!mDescriptors);
|
|
|
|
|
|
|
|
mDescriptors = new CSSFontFaceDescriptors;
|
|
|
|
|
|
|
|
// Parse all of the mDescriptors in aInitializer, which are the values
|
|
|
|
// we got from the JS constructor.
|
|
|
|
if (!ParseDescriptor(eCSSFontDesc_Family,
|
|
|
|
aFamily,
|
|
|
|
mDescriptors->mFamily) ||
|
|
|
|
*mDescriptors->mFamily.GetStringBufferValue() == 0 ||
|
|
|
|
!ParseDescriptor(eCSSFontDesc_Style,
|
|
|
|
aDescriptors.mStyle,
|
|
|
|
mDescriptors->mStyle) ||
|
|
|
|
!ParseDescriptor(eCSSFontDesc_Weight,
|
|
|
|
aDescriptors.mWeight,
|
|
|
|
mDescriptors->mWeight) ||
|
|
|
|
!ParseDescriptor(eCSSFontDesc_Stretch,
|
|
|
|
aDescriptors.mStretch,
|
|
|
|
mDescriptors->mStretch) ||
|
|
|
|
!ParseDescriptor(eCSSFontDesc_UnicodeRange,
|
|
|
|
aDescriptors.mUnicodeRange,
|
|
|
|
mDescriptors->mUnicodeRange) ||
|
|
|
|
!ParseDescriptor(eCSSFontDesc_FontFeatureSettings,
|
|
|
|
aDescriptors.mFeatureSettings,
|
|
|
|
mDescriptors->mFontFeatureSettings)) {
|
|
|
|
// XXX Handle font-variant once we support it (bug 1055385).
|
|
|
|
|
|
|
|
// If any of the descriptors failed to parse, none of them should be set
|
|
|
|
// on the FontFace.
|
|
|
|
mDescriptors = new CSSFontFaceDescriptors;
|
|
|
|
|
|
|
|
if (mLoaded) {
|
|
|
|
mLoaded->MaybeReject(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatus(FontFaceLoadStatus::Error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
void
|
|
|
|
FontFace::GetDesc(nsCSSFontDesc aDescID, nsCSSValue& aResult) const
|
|
|
|
{
|
2014-10-01 19:32:10 -07:00
|
|
|
if (HasRule()) {
|
2014-10-01 19:32:09 -07:00
|
|
|
MOZ_ASSERT(mRule);
|
2014-10-01 19:32:07 -07:00
|
|
|
MOZ_ASSERT(!mDescriptors);
|
|
|
|
mRule->GetDesc(aDescID, aResult);
|
|
|
|
} else {
|
|
|
|
aResult = mDescriptors->Get(aDescID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::GetDesc(nsCSSFontDesc aDescID,
|
|
|
|
nsCSSProperty aPropID,
|
|
|
|
nsString& aResult) const
|
|
|
|
{
|
2015-03-17 00:15:42 -07:00
|
|
|
MOZ_ASSERT(aDescID == eCSSFontDesc_UnicodeRange ||
|
|
|
|
aPropID != eCSSProperty_UNKNOWN,
|
|
|
|
"only pass eCSSProperty_UNKNOWN for eCSSFontDesc_UnicodeRange");
|
|
|
|
|
2014-10-01 19:32:07 -07:00
|
|
|
nsCSSValue value;
|
|
|
|
GetDesc(aDescID, value);
|
|
|
|
|
|
|
|
aResult.Truncate();
|
|
|
|
|
|
|
|
// Fill in a default value for missing descriptors.
|
|
|
|
if (value.GetUnit() == eCSSUnit_Null) {
|
|
|
|
if (aDescID == eCSSFontDesc_UnicodeRange) {
|
|
|
|
aResult.AssignLiteral("U+0-10FFFF");
|
|
|
|
} else if (aDescID != eCSSFontDesc_Family &&
|
|
|
|
aDescID != eCSSFontDesc_Src) {
|
|
|
|
aResult.AssignLiteral("normal");
|
|
|
|
}
|
2015-03-17 00:15:42 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aDescID == eCSSFontDesc_UnicodeRange) {
|
|
|
|
// Since there's no unicode-range property, we can't use
|
|
|
|
// nsCSSValue::AppendToString to serialize this descriptor.
|
|
|
|
nsStyleUtil::AppendUnicodeRange(value, aResult);
|
2014-10-01 19:32:07 -07:00
|
|
|
} else {
|
|
|
|
value.AppendToString(aPropID, aResult, nsCSSValue::eNormalized);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
void
|
|
|
|
FontFace::SetUserFontEntry(gfxUserFontEntry* aEntry)
|
|
|
|
{
|
|
|
|
if (mUserFontEntry) {
|
|
|
|
mUserFontEntry->mFontFaces.RemoveElement(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mUserFontEntry = static_cast<Entry*>(aEntry);
|
|
|
|
if (mUserFontEntry) {
|
|
|
|
mUserFontEntry->mFontFaces.AppendElement(this);
|
|
|
|
|
|
|
|
// Our newly assigned user font entry might be in the process of or
|
|
|
|
// finished loading, so set our status accordingly. But only do so
|
|
|
|
// if we're not going "backwards" in status, which could otherwise
|
|
|
|
// happen in this case:
|
|
|
|
//
|
|
|
|
// new FontFace("ABC", "url(x)").load();
|
|
|
|
//
|
|
|
|
// where the SetUserFontEntry call (from the after-initialization
|
2014-10-01 19:32:09 -07:00
|
|
|
// DoLoad call) comes after the author's call to load(), which set mStatus
|
|
|
|
// to Loading.
|
2014-10-01 19:32:08 -07:00
|
|
|
FontFaceLoadStatus newStatus =
|
|
|
|
LoadStateToStatus(mUserFontEntry->LoadState());
|
|
|
|
if (newStatus > mStatus) {
|
|
|
|
SetStatus(newStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
bool
|
|
|
|
FontFace::GetFamilyName(nsString& aResult)
|
|
|
|
{
|
|
|
|
nsCSSValue value;
|
|
|
|
GetDesc(eCSSFontDesc_Family, value);
|
|
|
|
|
|
|
|
if (value.GetUnit() == eCSSUnit_String) {
|
|
|
|
nsString familyname;
|
|
|
|
value.GetStringValue(familyname);
|
|
|
|
aResult.Append(familyname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !aResult.IsEmpty();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
void
|
|
|
|
FontFace::DisconnectFromRule()
|
|
|
|
{
|
2014-10-01 19:32:10 -07:00
|
|
|
MOZ_ASSERT(HasRule());
|
2014-10-01 19:32:08 -07:00
|
|
|
|
|
|
|
// Make a copy of the descriptors.
|
|
|
|
mDescriptors = new CSSFontFaceDescriptors;
|
|
|
|
mRule->GetDescriptors(*mDescriptors);
|
|
|
|
mRule = nullptr;
|
|
|
|
mInFontFaceSet = false;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:09 -07:00
|
|
|
bool
|
|
|
|
FontFace::HasFontData() const
|
|
|
|
{
|
|
|
|
return mSourceType == eSourceType_Buffer && mSourceBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FontFace::TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(HasFontData());
|
|
|
|
|
|
|
|
aBuffer = mSourceBuffer;
|
|
|
|
aLength = mSourceBufferLength;
|
|
|
|
|
|
|
|
mSourceBuffer = nullptr;
|
|
|
|
mSourceBufferLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxFontFaceBufferSource>
|
|
|
|
FontFace::CreateBufferSource()
|
|
|
|
{
|
|
|
|
nsRefPtr<FontFaceBufferSource> bufferSource = new FontFaceBufferSource(this);
|
|
|
|
return bufferSource.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:32:06 -07:00
|
|
|
// -- FontFace::Entry --------------------------------------------------------
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
FontFace::Entry::SetLoadState(UserFontLoadState aLoadState)
|
|
|
|
{
|
|
|
|
gfxUserFontEntry::SetLoadState(aLoadState);
|
|
|
|
|
2014-10-01 19:32:08 -07:00
|
|
|
for (size_t i = 0; i < mFontFaces.Length(); i++) {
|
|
|
|
mFontFaces[i]->SetStatus(LoadStateToStatus(aLoadState));
|
2014-10-01 19:32:06 -07:00
|
|
|
}
|
|
|
|
}
|
2014-10-01 19:32:09 -07:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|