2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +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/. */
|
2009-10-31 02:13:41 +03:00
|
|
|
|
|
|
|
#ifndef GFX_FT2FONTBASE_H
|
|
|
|
#define GFX_FT2FONTBASE_H
|
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxFont.h"
|
2019-10-02 00:56:30 +03:00
|
|
|
#include "gfxFontEntry.h"
|
2012-05-17 02:30:10 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2017-04-07 00:41:02 +03:00
|
|
|
#include "mozilla/gfx/UnscaledFontFreeType.h"
|
2017-12-06 16:51:39 +03:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2009-10-31 02:13:41 +03:00
|
|
|
|
2019-10-02 00:56:30 +03:00
|
|
|
class gfxFT2FontEntryBase : public gfxFontEntry {
|
2019-10-02 17:04:45 +03:00
|
|
|
public:
|
|
|
|
explicit gfxFT2FontEntryBase(const nsACString& aName) : gfxFontEntry(aName) {}
|
2019-10-02 00:56:30 +03:00
|
|
|
|
|
|
|
struct CmapCacheSlot {
|
|
|
|
CmapCacheSlot() : mCharCode(0), mGlyphIndex(0) {}
|
|
|
|
|
|
|
|
uint32_t mCharCode;
|
|
|
|
uint32_t mGlyphIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
CmapCacheSlot* GetCmapCacheSlot(uint32_t aCharCode);
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { kNumCmapCacheSlots = 256 };
|
|
|
|
|
|
|
|
mozilla::UniquePtr<CmapCacheSlot[]> mCmapCache;
|
|
|
|
};
|
|
|
|
|
2009-10-31 02:13:41 +03:00
|
|
|
class gfxFT2FontBase : public gfxFont {
|
|
|
|
public:
|
2017-04-07 00:41:02 +03:00
|
|
|
gfxFT2FontBase(
|
|
|
|
const RefPtr<mozilla::gfx::UnscaledFontFreeType>& aUnscaledFont,
|
2019-09-16 20:12:38 +03:00
|
|
|
RefPtr<mozilla::gfx::SharedFTFace>&& aFTFace, gfxFontEntry* aFontEntry,
|
2019-09-16 20:15:10 +03:00
|
|
|
const gfxFontStyle* aFontStyle, int aLoadFlags, bool aEmbolden);
|
2009-10-31 02:13:41 +03:00
|
|
|
virtual ~gfxFT2FontBase();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t GetGlyph(uint32_t aCharCode);
|
2019-04-11 15:36:51 +03:00
|
|
|
bool ProvidesGetGlyph() const override { return true; }
|
2014-11-17 12:59:50 +03:00
|
|
|
virtual uint32_t GetGlyph(uint32_t unicode,
|
2015-03-21 19:28:04 +03:00
|
|
|
uint32_t variation_selector) override;
|
2019-04-11 15:36:51 +03:00
|
|
|
bool ProvidesGlyphWidths() const override { return true; }
|
|
|
|
int32_t GetGlyphWidth(uint16_t aGID) override;
|
2019-09-20 19:30:21 +03:00
|
|
|
bool GetGlyphBounds(uint16_t aGID, gfxRect* aBounds, bool aTight) override;
|
2009-10-31 02:13:41 +03:00
|
|
|
|
2019-04-11 15:36:51 +03:00
|
|
|
FontType GetType() const override { return FONT_TYPE_FT2; }
|
2012-05-17 02:30:10 +04:00
|
|
|
|
2019-10-02 17:04:45 +03:00
|
|
|
bool ShouldRoundXOffset(cairo_t* aCairo) const override;
|
|
|
|
|
2018-08-02 00:39:05 +03:00
|
|
|
static void SetupVarCoords(FT_MM_Var* aMMVar,
|
2017-12-07 16:22:49 +03:00
|
|
|
const nsTArray<gfxFontVariation>& aVariations,
|
2019-09-16 20:07:59 +03:00
|
|
|
FT_Face aFTFace);
|
2019-09-15 06:01:02 +03:00
|
|
|
|
2019-09-16 20:12:38 +03:00
|
|
|
FT_Face LockFTFace();
|
|
|
|
void UnlockFTFace();
|
|
|
|
|
2017-07-27 06:24:48 +03:00
|
|
|
private:
|
2019-09-20 19:30:21 +03:00
|
|
|
uint32_t GetCharExtents(char aChar, gfxFloat* aWidth,
|
|
|
|
gfxRect* aBounds = nullptr);
|
2018-03-01 01:02:25 +03:00
|
|
|
|
2019-09-20 19:30:21 +03:00
|
|
|
// Get advance (and optionally bounds) of a single glyph from FreeType,
|
2019-09-16 20:15:10 +03:00
|
|
|
// and return true, or return false if we failed.
|
|
|
|
bool GetFTGlyphExtents(uint16_t aGID, int32_t* aWidth,
|
2019-09-22 22:56:00 +03:00
|
|
|
mozilla::gfx::IntRect* aBounds = nullptr);
|
2014-09-30 10:37:40 +04:00
|
|
|
|
2019-09-16 10:44:20 +03:00
|
|
|
protected:
|
2019-09-16 20:15:10 +03:00
|
|
|
void InitMetrics();
|
2019-09-16 10:44:20 +03:00
|
|
|
const Metrics& GetHorizontalMetrics() override;
|
2019-09-20 19:30:21 +03:00
|
|
|
FT_Vector GetEmboldenStrength(FT_Face aFace);
|
2019-09-15 06:01:02 +03:00
|
|
|
|
2019-09-16 20:12:38 +03:00
|
|
|
RefPtr<mozilla::gfx::SharedFTFace> mFTFace;
|
|
|
|
|
2009-10-31 02:13:41 +03:00
|
|
|
Metrics mMetrics;
|
2019-09-16 20:15:10 +03:00
|
|
|
int mFTLoadFlags;
|
2017-12-06 16:51:39 +03:00
|
|
|
bool mEmbolden;
|
2019-09-16 19:46:27 +03:00
|
|
|
gfxFloat mFTSize;
|
2017-12-06 16:51:39 +03:00
|
|
|
|
2017-12-06 17:42:42 +03:00
|
|
|
// For variation/multiple-master fonts, this will be an array of the values
|
|
|
|
// for each axis, as specified by mStyle.variationSettings (or the font's
|
|
|
|
// default for axes not present in variationSettings). Values here are in
|
|
|
|
// freetype's 16.16 fixed-point format, and clamped to the valid min/max
|
|
|
|
// range reported by the face.
|
|
|
|
nsTArray<FT_Fixed> mCoords;
|
|
|
|
|
2019-09-22 22:56:00 +03:00
|
|
|
// Store cached glyph metrics for reasonably small glyph sizes. The bounds
|
|
|
|
// are stored unscaled to losslessly compress 26.6 fixed point to an int16_t.
|
|
|
|
// Larger glyphs are handled directly via GetFTGlyphExtents.
|
|
|
|
struct GlyphMetrics {
|
|
|
|
// Set the X coord to INT16_MIN to signal the bounds are invalid, or
|
|
|
|
// INT16_MAX to signal that the bounds would overflow an int16_t.
|
|
|
|
enum { INVALID = INT16_MIN, LARGE = INT16_MAX };
|
|
|
|
|
|
|
|
GlyphMetrics() : mAdvance(0), mX(INVALID), mY(0), mWidth(0), mHeight(0) {}
|
|
|
|
|
|
|
|
bool HasValidBounds() const { return mX != INVALID; }
|
|
|
|
bool HasCachedBounds() const { return mX != LARGE; }
|
|
|
|
|
|
|
|
// If the bounds can fit in an int16_t, set them. Otherwise, leave the
|
|
|
|
// bounds invalid to signal that GetFTGlyphExtents should be queried
|
|
|
|
// directly.
|
|
|
|
void SetBounds(const mozilla::gfx::IntRect& aBounds) {
|
|
|
|
if (aBounds.x > INT16_MIN && aBounds.x < INT16_MAX &&
|
|
|
|
aBounds.y > INT16_MIN && aBounds.y < INT16_MAX &&
|
|
|
|
aBounds.width <= UINT16_MAX && aBounds.height <= UINT16_MAX) {
|
|
|
|
mX = aBounds.x;
|
|
|
|
mY = aBounds.y;
|
|
|
|
mWidth = aBounds.width;
|
|
|
|
mHeight = aBounds.height;
|
|
|
|
} else {
|
|
|
|
mX = LARGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::gfx::IntRect GetBounds() const {
|
|
|
|
return mozilla::gfx::IntRect(mX, mY, mWidth, mHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mAdvance;
|
|
|
|
int16_t mX;
|
|
|
|
int16_t mY;
|
|
|
|
uint16_t mWidth;
|
|
|
|
uint16_t mHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
const GlyphMetrics& GetCachedGlyphMetrics(
|
|
|
|
uint16_t aGID, mozilla::gfx::IntRect* aBounds = nullptr);
|
|
|
|
|
|
|
|
mozilla::UniquePtr<nsDataHashtable<nsUint32HashKey, GlyphMetrics>>
|
|
|
|
mGlyphMetrics;
|
2009-10-31 02:13:41 +03:00
|
|
|
};
|
|
|
|
|
2019-09-16 20:07:59 +03:00
|
|
|
// Helper classes used for clearing out user font data when FT font
|
|
|
|
// face is destroyed. Since multiple faces may use the same data, be
|
|
|
|
// careful to assure that the data is only cleared out when all uses
|
|
|
|
// expire. The font entry object contains a refptr to FTUserFontData and
|
|
|
|
// each FT face created from that font entry contains a refptr to that
|
|
|
|
// same FTUserFontData object.
|
|
|
|
|
|
|
|
class FTUserFontData final
|
|
|
|
: public mozilla::gfx::SharedFTFaceRefCountedData<FTUserFontData> {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FTUserFontData)
|
|
|
|
|
|
|
|
FTUserFontData(const uint8_t* aData, uint32_t aLength)
|
|
|
|
: mFontData(aData), mLength(aLength) {}
|
|
|
|
|
|
|
|
const uint8_t* FontData() const { return mFontData; }
|
|
|
|
|
|
|
|
already_AddRefed<mozilla::gfx::SharedFTFace> CloneFace(
|
|
|
|
int aFaceIndex = 0) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FTUserFontData() {
|
|
|
|
if (mFontData) {
|
|
|
|
free((void*)mFontData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t* mFontData;
|
|
|
|
uint32_t mLength;
|
|
|
|
};
|
|
|
|
|
2009-10-31 02:13:41 +03:00
|
|
|
#endif /* GFX_FT2FONTBASE_H */
|