2013-07-19 15:08:33 +00: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 04:57:54 +00:00
|
|
|
|
|
|
|
#ifndef GFX_SVG_GLYPHS_WRAPPER_H
|
|
|
|
#define GFX_SVG_GLYPHS_WRAPPER_H
|
|
|
|
|
|
|
|
#include "gfxFontUtils.h"
|
2014-09-29 13:15:19 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2012-09-06 04:57:54 +00:00
|
|
|
#include "nsString.h"
|
2012-09-06 04:58:47 +00:00
|
|
|
#include "nsClassHashtable.h"
|
2012-09-06 04:57:54 +00:00
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "gfxPattern.h"
|
2012-09-06 04:58:46 +00:00
|
|
|
#include "mozilla/gfx/UserData.h"
|
2016-07-22 11:07:39 +00:00
|
|
|
#include "mozilla/SVGContextPaint.h"
|
2013-08-19 13:08:45 +00:00
|
|
|
#include "nsRefreshDriver.h"
|
2013-10-07 23:15:59 +00:00
|
|
|
|
|
|
|
class nsIDocument;
|
|
|
|
class nsIContentViewer;
|
|
|
|
class nsIPresShell;
|
2013-08-19 13:08:45 +00:00
|
|
|
class gfxSVGGlyphs;
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2013-10-07 23:15:59 +00:00
|
|
|
namespace mozilla {
|
2016-07-22 13:56:09 +00:00
|
|
|
class SVGContextPaint;
|
2013-10-07 23:15:59 +00:00
|
|
|
namespace dom {
|
|
|
|
class Element;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2012-09-06 04:57:54 +00:00
|
|
|
|
|
|
|
/**
|
2012-09-06 04:58:47 +00: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 04:57:54 +00:00
|
|
|
*/
|
2015-03-21 16:28:04 +00:00
|
|
|
class gfxSVGGlyphsDocument final : public nsAPostRefreshObserver
|
2012-09-06 04:58:47 +00:00
|
|
|
{
|
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
|
|
|
public:
|
2013-08-19 13:08:45 +00:00
|
|
|
gfxSVGGlyphsDocument(const uint8_t *aBuffer, uint32_t aBufLen,
|
|
|
|
gfxSVGGlyphs *aSVGGlyphs);
|
2012-09-06 04:58:47 +00:00
|
|
|
|
|
|
|
Element *GetGlyphElement(uint32_t aGlyphId);
|
|
|
|
|
2013-08-19 13:08:45 +00:00
|
|
|
~gfxSVGGlyphsDocument();
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void DidRefresh() override;
|
2012-09-06 04:58:47 +00:00
|
|
|
|
2016-05-03 11:14:35 +00:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2012-09-06 04:58:47 +00:00
|
|
|
private:
|
2013-05-16 16:29:20 +00:00
|
|
|
nsresult ParseDocument(const uint8_t *aBuffer, uint32_t aBufLen);
|
2012-09-06 04:58:47 +00:00
|
|
|
|
|
|
|
nsresult SetupPresentation();
|
|
|
|
|
2013-08-19 13:08:43 +00:00
|
|
|
void FindGlyphElements(Element *aElement);
|
2012-09-06 04:58:47 +00:00
|
|
|
|
|
|
|
void InsertGlyphId(Element *aGlyphElement);
|
|
|
|
|
2013-08-19 13:08:45 +00:00
|
|
|
// Weak so as not to create a cycle. mOwner owns us so this can't dangle.
|
|
|
|
gfxSVGGlyphs* mOwner;
|
2012-09-06 04:58:47 +00:00
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
nsCOMPtr<nsIContentViewer> mViewer;
|
|
|
|
nsCOMPtr<nsIPresShell> mPresShell;
|
|
|
|
|
|
|
|
nsBaseHashtable<nsUint32HashKey, Element*, Element*> mGlyphIdMap;
|
2013-06-10 12:25:12 +00:00
|
|
|
|
2015-11-19 09:26:54 +00:00
|
|
|
nsCString mSVGGlyphsDocumentURI;
|
2012-09-06 04:58:47 +00: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 04:57:54 +00:00
|
|
|
private:
|
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
|
|
|
|
public:
|
2012-09-06 04:58:47 +00:00
|
|
|
/**
|
|
|
|
* @param aSVGTable The SVG table from the OpenType font
|
2013-05-16 16:29:20 +00: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 04:58:47 +00:00
|
|
|
*/
|
2013-08-19 13:08:45 +00:00
|
|
|
gfxSVGGlyphs(hb_blob_t *aSVGTable, gfxFontEntry *aFontEntry);
|
2012-09-06 04:58:47 +00:00
|
|
|
|
|
|
|
/**
|
2013-08-19 13:08:45 +00:00
|
|
|
* Releases our references to the SVG table and cleans up everything else.
|
2012-09-06 04:58:47 +00:00
|
|
|
*/
|
2013-05-16 16:29:20 +00:00
|
|
|
~gfxSVGGlyphs();
|
2012-09-06 04:58:47 +00:00
|
|
|
|
2013-08-19 13:08:45 +00:00
|
|
|
/**
|
|
|
|
* This is called when the refresh driver has ticked.
|
|
|
|
*/
|
|
|
|
void DidRefresh();
|
|
|
|
|
2012-09-06 04:58:47 +00: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 04:57:54 +00:00
|
|
|
|
2012-09-06 04:58:47 +00:00
|
|
|
/**
|
|
|
|
* Return true iff there is an SVG glyph for |aGlyphId|
|
|
|
|
*/
|
2012-09-06 04:57:54 +00:00
|
|
|
bool HasSVGGlyph(uint32_t aGlyphId);
|
|
|
|
|
2012-09-06 04:58:47 +00:00
|
|
|
/**
|
|
|
|
* Render the SVG glyph for |aGlyphId|
|
2013-05-16 02:35:12 +00:00
|
|
|
* @param aContextPaint Information on text context paints.
|
2016-07-22 13:56:09 +00:00
|
|
|
* See |SVGContextPaint|.
|
2012-09-06 04:58:47 +00:00
|
|
|
*/
|
2016-04-21 13:39:30 +00:00
|
|
|
bool RenderGlyph(gfxContext *aContext, uint32_t aGlyphId,
|
2016-07-22 13:56:09 +00:00
|
|
|
mozilla::SVGContextPaint* aContextPaint);
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2012-09-06 04:58:47 +00: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 04:58:46 +00:00
|
|
|
bool GetGlyphExtents(uint32_t aGlyphId, const gfxMatrix& aSVGToAppSpace,
|
|
|
|
gfxRect *aResult);
|
|
|
|
|
2016-05-03 11:14:35 +00:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2012-09-06 04:57:54 +00:00
|
|
|
private:
|
2012-09-06 04:58:47 +00:00
|
|
|
Element *GetGlyphElement(uint32_t aGlyphId);
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2012-09-06 04:58:47 +00:00
|
|
|
nsClassHashtable<nsUint32HashKey, gfxSVGGlyphsDocument> mGlyphDocs;
|
|
|
|
nsBaseHashtable<nsUint32HashKey, Element*, Element*> mGlyphIdMap;
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2013-05-16 16:29:20 +00:00
|
|
|
hb_blob_t *mSVGData;
|
2013-08-19 13:08:45 +00:00
|
|
|
gfxFontEntry *mFontEntry;
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2013-05-16 16:29:20 +00:00
|
|
|
const struct Header {
|
|
|
|
mozilla::AutoSwap_PRUint16 mVersion;
|
2013-08-19 13:08:43 +00:00
|
|
|
mozilla::AutoSwap_PRUint32 mDocIndexOffset;
|
|
|
|
mozilla::AutoSwap_PRUint32 mColorPalettesOffset;
|
2012-09-06 04:58:47 +00:00
|
|
|
} *mHeader;
|
2012-09-06 04:57:54 +00:00
|
|
|
|
2013-08-19 13:08:43 +00:00
|
|
|
struct IndexEntry {
|
2013-05-16 16:29:20 +00:00
|
|
|
mozilla::AutoSwap_PRUint16 mStartGlyph;
|
|
|
|
mozilla::AutoSwap_PRUint16 mEndGlyph;
|
|
|
|
mozilla::AutoSwap_PRUint32 mDocOffset;
|
|
|
|
mozilla::AutoSwap_PRUint32 mDocLength;
|
2013-08-19 13:08:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct DocIndex {
|
|
|
|
mozilla::AutoSwap_PRUint16 mNumEntries;
|
|
|
|
IndexEntry mEntries[1]; /* actual length = mNumEntries */
|
|
|
|
} *mDocIndex;
|
2012-09-06 04:58:47 +00:00
|
|
|
|
|
|
|
static int CompareIndexEntries(const void *_a, const void *_b);
|
2012-09-06 04:57:54 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 13:33:11 +00:00
|
|
|
/**
|
2016-07-22 11:07:39 +00: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 04:58:46 +00:00
|
|
|
*/
|
2016-07-22 13:56:09 +00:00
|
|
|
class SimpleTextContextPaint : public mozilla::SVGContextPaint
|
2012-09-06 04:58:46 +00:00
|
|
|
{
|
2012-09-06 04:58:47 +00:00
|
|
|
private:
|
2015-09-25 00:50:46 +00:00
|
|
|
static const mozilla::gfx::Color sZero;
|
2012-09-06 04:58:47 +00:00
|
|
|
|
2012-09-12 05:13:12 +00:00
|
|
|
static gfxMatrix SetupDeviceToPatternMatrix(gfxPattern *aPattern,
|
|
|
|
const gfxMatrix& aCTM)
|
|
|
|
{
|
|
|
|
if (!aPattern) {
|
|
|
|
return gfxMatrix();
|
|
|
|
}
|
|
|
|
gfxMatrix deviceToUser = aCTM;
|
2014-07-11 07:06:39 +00:00
|
|
|
if (!deviceToUser.Invert()) {
|
|
|
|
return gfxMatrix(0, 0, 0, 0, 0, 0); // singular
|
|
|
|
}
|
2012-09-12 05:13:12 +00:00
|
|
|
return deviceToUser * aPattern->GetMatrix();
|
|
|
|
}
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
public:
|
2013-05-16 02:35:12 +00:00
|
|
|
SimpleTextContextPaint(gfxPattern *aFillPattern, gfxPattern *aStrokePattern,
|
2012-09-12 05:13:12 +00:00
|
|
|
const gfxMatrix& aCTM) :
|
2012-09-06 04:58:47 +00:00
|
|
|
mFillPattern(aFillPattern ? aFillPattern : new gfxPattern(sZero)),
|
2012-09-12 05:13:12 +00:00
|
|
|
mStrokePattern(aStrokePattern ? aStrokePattern : new gfxPattern(sZero))
|
2012-09-06 04:58:46 +00:00
|
|
|
{
|
2012-09-12 05:13:12 +00:00
|
|
|
mFillMatrix = SetupDeviceToPatternMatrix(aFillPattern, aCTM);
|
|
|
|
mStrokeMatrix = SetupDeviceToPatternMatrix(aStrokePattern, aCTM);
|
2012-09-06 04:58:46 +00:00
|
|
|
}
|
|
|
|
|
2014-09-29 13:15:19 +00:00
|
|
|
already_AddRefed<gfxPattern> GetFillPattern(const DrawTarget* aDrawTarget,
|
|
|
|
float aOpacity,
|
2012-09-12 05:13:12 +00:00
|
|
|
const gfxMatrix& aCTM) {
|
2012-09-06 04:58:46 +00:00
|
|
|
if (mFillPattern) {
|
2012-09-12 05:13:12 +00:00
|
|
|
mFillPattern->SetMatrix(aCTM * mFillMatrix);
|
2012-09-06 04:58:46 +00:00
|
|
|
}
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<gfxPattern> fillPattern = mFillPattern;
|
2012-09-06 04:58:46 +00:00
|
|
|
return fillPattern.forget();
|
|
|
|
}
|
|
|
|
|
2014-09-29 13:15:19 +00:00
|
|
|
already_AddRefed<gfxPattern> GetStrokePattern(const DrawTarget* aDrawTarget,
|
|
|
|
float aOpacity,
|
2012-09-12 05:13:12 +00:00
|
|
|
const gfxMatrix& aCTM) {
|
2012-09-06 04:58:46 +00:00
|
|
|
if (mStrokePattern) {
|
2012-09-12 05:13:12 +00:00
|
|
|
mStrokePattern->SetMatrix(aCTM * mStrokeMatrix);
|
2012-09-06 04:58:46 +00:00
|
|
|
}
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<gfxPattern> strokePattern = mStrokePattern;
|
2012-09-06 04:58:46 +00:00
|
|
|
return strokePattern.forget();
|
|
|
|
}
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
float GetFillOpacity() const {
|
2012-09-06 04:58:47 +00:00
|
|
|
return mFillPattern ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
|
2016-07-22 11:07:39 +00:00
|
|
|
float GetStrokeOpacity() const {
|
2012-09-06 04:58:47 +00:00
|
|
|
return mStrokePattern ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
|
2012-09-06 04:58:46 +00:00
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<gfxPattern> mFillPattern;
|
|
|
|
RefPtr<gfxPattern> mStrokePattern;
|
2012-09-06 04:58:46 +00:00
|
|
|
|
2012-09-12 05:13:12 +00:00
|
|
|
// Device space to pattern space transforms
|
2012-09-06 04:58:46 +00:00
|
|
|
gfxMatrix mFillMatrix;
|
|
|
|
gfxMatrix mStrokeMatrix;
|
|
|
|
};
|
|
|
|
|
2012-09-06 04:57:54 +00:00
|
|
|
#endif
|