skia2/src/core/SkFontDescriptor.h

84 lines
2.7 KiB
C
Raw Normal View History

/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkFontDescriptor_DEFINED
#define SkFontDescriptor_DEFINED
#include "SkFixed.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTypeface.h"
class SkFontData {
public:
/** Makes a copy of the data in 'axis'. */
SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed axis[],int axisCount)
: fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
{
for (int i = 0; i < axisCount; ++i) {
fAxis[i] = axis[i];
}
}
SkFontData(const SkFontData& that)
: fStream(that.fStream->duplicate())
, fIndex(that.fIndex)
, fAxisCount(that.fAxisCount)
, fAxis(fAxisCount)
{
for (int i = 0; i < fAxisCount; ++i) {
fAxis[i] = that.fAxis[i];
}
}
bool hasStream() const { return fStream.get() != nullptr; }
std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
SkStreamAsset* getStream() { return fStream.get(); }
SkStreamAsset const* getStream() const { return fStream.get(); }
int getIndex() const { return fIndex; }
int getAxisCount() const { return fAxisCount; }
const SkFixed* getAxis() const { return fAxis.get(); }
private:
std::unique_ptr<SkStreamAsset> fStream;
int fIndex;
int fAxisCount;
SkAutoSTMalloc<4, SkFixed> fAxis;
};
class SkFontDescriptor : SkNoncopyable {
public:
SkFontDescriptor();
Make SkStream *not* ref counted. SkStream is a stateful object, so it does not make sense for it to have multiple owners. Make SkStream inherit directly from SkNoncopyable. Update methods which previously called SkStream::ref() (e.g. SkImageDecoder::buildTileIndex() and SkFrontBufferedStream::Create(), which required the existing owners to call SkStream::unref()) to take ownership of their SkStream parameters and delete when done (including on failure). Switch all SkAutoTUnref<SkStream>s to SkAutoTDelete<SkStream>s. In some cases this means heap allocating streams that were previously stack allocated. Respect ownership rules of SkTypeface::CreateFromStream() and SkImageDecoder::buildTileIndex(). Update the comments for exceptional methods which do not affect the ownership of their SkStream parameters (e.g. SkPicture::CreateFromStream() and SkTypeface::Deserialize()) to be explicit about ownership. Remove test_stream_life, which tested that buildTileIndex() behaved correctly when SkStream was a ref counted object. The test does not make sense now that it is not. In SkPDFStream, remove the SkMemoryStream member. Instead of using it, create a new SkMemoryStream to pass to fDataStream (which is now an SkAutoTDelete). Make other pdf rasterizers behave like SkPDFDocumentToBitmap. SkPDFDocumentToBitmap delete the SkStream, so do the same in the following pdf rasterizers: SkPopplerRasterizePDF SkNativeRasterizePDF SkNoRasterizePDF Requires a change to Android, which currently treats SkStreams as ref counted objects. Review URL: https://codereview.chromium.org/849103004
2015-01-21 20:09:53 +00:00
// Does not affect ownership of SkStream.
static bool Deserialize(SkStream*, SkFontDescriptor* result);
void serialize(SkWStream*);
SkFontStyle getStyle() { return fStyle; }
void setStyle(SkFontStyle style) { fStyle = style; }
const char* getFamilyName() const { return fFamilyName.c_str(); }
const char* getFullName() const { return fFullName.c_str(); }
const char* getPostscriptName() const { return fPostscriptName.c_str(); }
bool hasFontData() const { return fFontData.get() != nullptr; }
std::unique_ptr<SkFontData> detachFontData() { return std::move(fFontData); }
void setFamilyName(const char* name) { fFamilyName.set(name); }
void setFullName(const char* name) { fFullName.set(name); }
void setPostscriptName(const char* name) { fPostscriptName.set(name); }
/** Set the font data only if it is necessary for serialization. */
void setFontData(std::unique_ptr<SkFontData> data) { fFontData = std::move(data); }
private:
SkString fFamilyName;
SkString fFullName;
SkString fPostscriptName;
std::unique_ptr<SkFontData> fFontData;
SkFontStyle fStyle;
};
#endif // SkFontDescriptor_DEFINED