2013-07-19 19:08:33 +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/. */
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
#ifndef GFX_SVG_GLYPHS_WRAPPER_H
|
|
|
|
#define GFX_SVG_GLYPHS_WRAPPER_H
|
|
|
|
|
|
|
|
#include "gfxFontUtils.h"
|
2014-09-29 17:15:19 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2012-09-06 08:57:54 +04:00
|
|
|
#include "nsString.h"
|
2012-09-06 08:58:47 +04:00
|
|
|
#include "nsClassHashtable.h"
|
2012-09-06 08:57:54 +04:00
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "gfxPattern.h"
|
2012-09-06 08:58:46 +04:00
|
|
|
#include "mozilla/gfx/UserData.h"
|
2016-07-22 14:07:39 +03:00
|
|
|
#include "mozilla/SVGContextPaint.h"
|
2013-08-19 17:08:45 +04:00
|
|
|
#include "nsRefreshDriver.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
|
|
|
|
class nsIDocument;
|
|
|
|
class nsIContentViewer;
|
|
|
|
class nsIPresShell;
|
2013-08-19 17:08:45 +04:00
|
|
|
class gfxSVGGlyphs;
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2013-10-08 03:15:59 +04:00
|
|
|
namespace mozilla {
|
2016-07-22 16:56:09 +03:00
|
|
|
class SVGContextPaint;
|
2013-10-08 03:15:59 +04:00
|
|
|
namespace dom {
|
|
|
|
class Element;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-09-06 08:57:54 +04:00
|
|
|
|
|
|
|
/**
|
2012-09-06 08:58:47 +04:00
|
|
|
* Wraps an SVG document contained in the SVG table of an OpenType font.
|
|
|
|
* There may be multiple SVG documents in an SVG table which we lazily parse
|
|
|
|
* so we have an instance of this class for every document in the SVG table
|
|
|
|
* which contains a glyph ID which has been used
|
|
|
|
* Finds and looks up elements contained in the SVG document which have glyph
|
|
|
|
* mappings to be drawn by gfxSVGGlyphs
|
2012-09-06 08:57:54 +04:00
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class gfxSVGGlyphsDocument final : public nsAPostRefreshObserver
|
2012-09-06 08:58:47 +04:00
|
|
|
{
|
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
|
|
|
public:
|
2013-08-19 17:08:45 +04:00
|
|
|
gfxSVGGlyphsDocument(const uint8_t *aBuffer, uint32_t aBufLen,
|
|
|
|
gfxSVGGlyphs *aSVGGlyphs);
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
Element *GetGlyphElement(uint32_t aGlyphId);
|
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
~gfxSVGGlyphsDocument();
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void DidRefresh() override;
|
2012-09-06 08:58:47 +04:00
|
|
|
|
2016-05-03 14:14:35 +03:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
private:
|
2013-05-16 20:29:20 +04:00
|
|
|
nsresult ParseDocument(const uint8_t *aBuffer, uint32_t aBufLen);
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
nsresult SetupPresentation();
|
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
void FindGlyphElements(Element *aElement);
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
void InsertGlyphId(Element *aGlyphElement);
|
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
// Weak so as not to create a cycle. mOwner owns us so this can't dangle.
|
|
|
|
gfxSVGGlyphs* mOwner;
|
2012-09-06 08:58:47 +04:00
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
nsCOMPtr<nsIContentViewer> mViewer;
|
|
|
|
nsCOMPtr<nsIPresShell> mPresShell;
|
|
|
|
|
|
|
|
nsBaseHashtable<nsUint32HashKey, Element*, Element*> mGlyphIdMap;
|
2013-06-10 16:25:12 +04:00
|
|
|
|
2015-11-19 12:26:54 +03:00
|
|
|
nsCString mSVGGlyphsDocumentURI;
|
2012-09-06 08:58:47 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by |gfxFontEntry| to represent the SVG table of an OpenType font.
|
|
|
|
* Handles lazy parsing of the SVG documents in the table, looking up SVG glyphs
|
|
|
|
* and rendering SVG glyphs.
|
|
|
|
* Each |gfxFontEntry| owns at most one |gfxSVGGlyphs| instance.
|
|
|
|
*/
|
|
|
|
class gfxSVGGlyphs
|
|
|
|
{
|
2012-09-06 08:57:54 +04:00
|
|
|
private:
|
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
|
|
|
public:
|
2012-09-06 08:58:47 +04:00
|
|
|
/**
|
|
|
|
* @param aSVGTable The SVG table from the OpenType font
|
2013-05-16 20:29:20 +04:00
|
|
|
*
|
|
|
|
* The gfxSVGGlyphs object takes over ownership of the blob references
|
|
|
|
* that are passed in, and will hb_blob_destroy() them when finished;
|
|
|
|
* the caller should -not- destroy these references.
|
2012-09-06 08:58:47 +04:00
|
|
|
*/
|
2013-08-19 17:08:45 +04:00
|
|
|
gfxSVGGlyphs(hb_blob_t *aSVGTable, gfxFontEntry *aFontEntry);
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
/**
|
2013-08-19 17:08:45 +04:00
|
|
|
* Releases our references to the SVG table and cleans up everything else.
|
2012-09-06 08:58:47 +04:00
|
|
|
*/
|
2013-05-16 20:29:20 +04:00
|
|
|
~gfxSVGGlyphs();
|
2012-09-06 08:58:47 +04:00
|
|
|
|
2013-08-19 17:08:45 +04:00
|
|
|
/**
|
|
|
|
* This is called when the refresh driver has ticked.
|
|
|
|
*/
|
|
|
|
void DidRefresh();
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
/**
|
|
|
|
* Find the |gfxSVGGlyphsDocument| containing an SVG glyph for |aGlyphId|.
|
|
|
|
* If |aGlyphId| does not map to an SVG document, return null.
|
|
|
|
* If a |gfxSVGGlyphsDocument| has not been created for the document, create one.
|
|
|
|
*/
|
|
|
|
gfxSVGGlyphsDocument *FindOrCreateGlyphsDocument(uint32_t aGlyphId);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
/**
|
|
|
|
* Return true iff there is an SVG glyph for |aGlyphId|
|
|
|
|
*/
|
2012-09-06 08:57:54 +04:00
|
|
|
bool HasSVGGlyph(uint32_t aGlyphId);
|
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
/**
|
|
|
|
* Render the SVG glyph for |aGlyphId|
|
2013-05-16 06:35:12 +04:00
|
|
|
* @param aContextPaint Information on text context paints.
|
2016-07-22 16:56:09 +03:00
|
|
|
* See |SVGContextPaint|.
|
2012-09-06 08:58:47 +04:00
|
|
|
*/
|
2017-05-18 23:03:41 +03:00
|
|
|
void RenderGlyph(gfxContext *aContext, uint32_t aGlyphId,
|
2016-07-22 16:56:09 +03:00
|
|
|
mozilla::SVGContextPaint* aContextPaint);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
/**
|
|
|
|
* Get the extents for the SVG glyph associated with |aGlyphId|
|
|
|
|
* @param aSVGToAppSpace The matrix mapping the SVG glyph space to the
|
|
|
|
* target context space
|
|
|
|
*/
|
2012-09-06 08:58:46 +04:00
|
|
|
bool GetGlyphExtents(uint32_t aGlyphId, const gfxMatrix& aSVGToAppSpace,
|
|
|
|
gfxRect *aResult);
|
|
|
|
|
2016-05-03 14:14:35 +03:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
private:
|
2012-09-06 08:58:47 +04:00
|
|
|
Element *GetGlyphElement(uint32_t aGlyphId);
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2012-09-06 08:58:47 +04:00
|
|
|
nsClassHashtable<nsUint32HashKey, gfxSVGGlyphsDocument> mGlyphDocs;
|
|
|
|
nsBaseHashtable<nsUint32HashKey, Element*, Element*> mGlyphIdMap;
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2013-05-16 20:29:20 +04:00
|
|
|
hb_blob_t *mSVGData;
|
2017-06-30 03:52:43 +03:00
|
|
|
|
|
|
|
// pointer to the font entry that owns this gfxSVGGlyphs object
|
|
|
|
gfxFontEntry* MOZ_NON_OWNING_REF mFontEntry;
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2013-05-16 20:29:20 +04:00
|
|
|
const struct Header {
|
|
|
|
mozilla::AutoSwap_PRUint16 mVersion;
|
2013-08-19 17:08:43 +04:00
|
|
|
mozilla::AutoSwap_PRUint32 mDocIndexOffset;
|
|
|
|
mozilla::AutoSwap_PRUint32 mColorPalettesOffset;
|
2012-09-06 08:58:47 +04:00
|
|
|
} *mHeader;
|
2012-09-06 08:57:54 +04:00
|
|
|
|
2013-08-19 17:08:43 +04:00
|
|
|
struct IndexEntry {
|
2013-05-16 20:29:20 +04:00
|
|
|
mozilla::AutoSwap_PRUint16 mStartGlyph;
|
|
|
|
mozilla::AutoSwap_PRUint16 mEndGlyph;
|
|
|
|
mozilla::AutoSwap_PRUint32 mDocOffset;
|
|
|
|
mozilla::AutoSwap_PRUint32 mDocLength;
|
2013-08-19 17:08:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct DocIndex {
|
|
|
|
mozilla::AutoSwap_PRUint16 mNumEntries;
|
|
|
|
IndexEntry mEntries[1]; /* actual length = mNumEntries */
|
|
|
|
} *mDocIndex;
|
2012-09-06 08:58:47 +04:00
|
|
|
|
|
|
|
static int CompareIndexEntries(const void *_a, const void *_b);
|
2012-09-06 08:57:54 +04:00
|
|
|
};
|
|
|
|
|
2016-07-21 16:33:11 +03:00
|
|
|
/**
|
2016-07-22 14:07:39 +03:00
|
|
|
* XXX This is a complete hack and should die (see bug 1291494).
|
|
|
|
*
|
|
|
|
* This class is used when code fails to pass through an SVGContextPaint from
|
|
|
|
* the context in which we are painting. In that case we create one of these
|
|
|
|
* as a fallback and have it wrap the gfxContext's current gfxPattern and
|
|
|
|
* pretend that that is the paint context's fill pattern. In some contexts
|
|
|
|
* that will be the case, in others it will not. As we convert more code to
|
|
|
|
* Moz2D the less likely it is that this hack will work. It will also make
|
|
|
|
* converting to Moz2D harder.
|
2012-09-06 08:58:46 +04:00
|
|
|
*/
|
2016-07-22 16:56:09 +03:00
|
|
|
class SimpleTextContextPaint : public mozilla::SVGContextPaint
|
2012-09-06 08:58:46 +04:00
|
|
|
{
|
2012-09-06 08:58:47 +04:00
|
|
|
private:
|
2015-09-25 03:50:46 +03:00
|
|
|
static const mozilla::gfx::Color sZero;
|
2012-09-06 08:58:47 +04:00
|
|
|
|
2012-09-12 09:13:12 +04:00
|
|
|
static gfxMatrix SetupDeviceToPatternMatrix(gfxPattern *aPattern,
|
|
|
|
const gfxMatrix& aCTM)
|
|
|
|
{
|
|
|
|
if (!aPattern) {
|
|
|
|
return gfxMatrix();
|
|
|
|
}
|
|
|
|
gfxMatrix deviceToUser = aCTM;
|
2014-07-11 11:06:39 +04:00
|
|
|
if (!deviceToUser.Invert()) {
|
|
|
|
return gfxMatrix(0, 0, 0, 0, 0, 0); // singular
|
|
|
|
}
|
2012-09-12 09:13:12 +04:00
|
|
|
return deviceToUser * aPattern->GetMatrix();
|
|
|
|
}
|
|
|
|
|
2016-07-22 14:07:39 +03:00
|
|
|
public:
|
2013-05-16 06:35:12 +04:00
|
|
|
SimpleTextContextPaint(gfxPattern *aFillPattern, gfxPattern *aStrokePattern,
|
2012-09-12 09:13:12 +04:00
|
|
|
const gfxMatrix& aCTM) :
|
2012-09-06 08:58:47 +04:00
|
|
|
mFillPattern(aFillPattern ? aFillPattern : new gfxPattern(sZero)),
|
2012-09-12 09:13:12 +04:00
|
|
|
mStrokePattern(aStrokePattern ? aStrokePattern : new gfxPattern(sZero))
|
2012-09-06 08:58:46 +04:00
|
|
|
{
|
2012-09-12 09:13:12 +04:00
|
|
|
mFillMatrix = SetupDeviceToPatternMatrix(aFillPattern, aCTM);
|
|
|
|
mStrokeMatrix = SetupDeviceToPatternMatrix(aStrokePattern, aCTM);
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
already_AddRefed<gfxPattern>
|
2017-03-21 05:12:23 +03:00
|
|
|
GetFillPattern(const DrawTarget* aDrawTarget,
|
|
|
|
float aOpacity,
|
2017-03-24 22:19:18 +03:00
|
|
|
const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) override {
|
2012-09-06 08:58:46 +04:00
|
|
|
if (mFillPattern) {
|
2012-09-12 09:13:12 +04:00
|
|
|
mFillPattern->SetMatrix(aCTM * mFillMatrix);
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxPattern> fillPattern = mFillPattern;
|
2017-05-18 23:03:50 +03:00
|
|
|
return fillPattern.forget();
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
already_AddRefed<gfxPattern>
|
2017-03-21 05:12:23 +03:00
|
|
|
GetStrokePattern(const DrawTarget* aDrawTarget,
|
|
|
|
float aOpacity,
|
2017-03-24 22:19:18 +03:00
|
|
|
const gfxMatrix& aCTM,
|
2017-05-18 23:03:50 +03:00
|
|
|
imgDrawingParams& aImgParams) override {
|
2012-09-06 08:58:46 +04:00
|
|
|
if (mStrokePattern) {
|
2012-09-12 09:13:12 +04:00
|
|
|
mStrokePattern->SetMatrix(aCTM * mStrokeMatrix);
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxPattern> strokePattern = mStrokePattern;
|
2017-05-18 23:03:50 +03:00
|
|
|
return strokePattern.forget();
|
2012-09-06 08:58:46 +04:00
|
|
|
}
|
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
float GetFillOpacity() const override {
|
2012-09-06 08:58:47 +04:00
|
|
|
return mFillPattern ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
|
2017-05-18 23:03:50 +03:00
|
|
|
float GetStrokeOpacity() const override {
|
2012-09-06 08:58:47 +04:00
|
|
|
return mStrokePattern ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
|
2012-09-06 08:58:46 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxPattern> mFillPattern;
|
|
|
|
RefPtr<gfxPattern> mStrokePattern;
|
2012-09-06 08:58:46 +04:00
|
|
|
|
2012-09-12 09:13:12 +04:00
|
|
|
// Device space to pattern space transforms
|
2012-09-06 08:58:46 +04:00
|
|
|
gfxMatrix mFillMatrix;
|
|
|
|
gfxMatrix mStrokeMatrix;
|
|
|
|
};
|
|
|
|
|
2012-09-06 08:57:54 +04:00
|
|
|
#endif
|