Index: src/pdf/SkPDFCanon.cpp |
diff --git a/src/pdf/SkPDFCanon.cpp b/src/pdf/SkPDFCanon.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..47affb5b291a0ed48e364aa078202bf3d194a0f0 |
--- /dev/null |
+++ b/src/pdf/SkPDFCanon.cpp |
@@ -0,0 +1,153 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkPDFCanon.h" |
+#include "SkPDFFont.h" |
+#include "SkPDFGraphicState.h" |
+#include "SkPDFShader.h" |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+SK_DECLARE_STATIC_MUTEX(gSkPDFCanonFontMutex); |
+SK_DECLARE_STATIC_MUTEX(gSkPDFCanonShaderMutex); |
+SK_DECLARE_STATIC_MUTEX(gSkPDFCanonPaintMutex); |
+ |
+SkBaseMutex& SkPDFCanon::GetFontMutex() { |
+ return gSkPDFCanonFontMutex; |
+} |
+SkBaseMutex& SkPDFCanon::GetShaderMutex() { |
+ return gSkPDFCanonShaderMutex; |
+} |
+SkBaseMutex& SkPDFCanon::GetPaintMutex() { |
+ return gSkPDFCanonPaintMutex; |
+} |
+ |
+SkPDFCanon* SkPDFCanon::Create() { |
+ return new SkPDFCanon; |
+} |
+void SkPDFCanon::Destroy(SkPDFCanon* ptr) { |
+ delete ptr; |
+} |
+SkPDFCanon::~SkPDFCanon() { |
+} |
+SK_DECLARE_STATIC_LAZY_PTR(SkPDFCanon, |
+ singleton, |
+ SkPDFCanon::Create, |
+ SkPDFCanon::Destroy); |
+ |
+SkPDFCanon& SkPDFCanon::GetCanon() { |
+ return *singleton.get(); |
+} |
+ |
+static void assert_mutex_held(SkBaseMutex* mutex) { |
+ SkDEBUGCODE(if (mutex) { mutex->assertHeld(); }) |
+} |
+ |
+SkPDFCanon::SkPDFCanon() |
+ : fFontMutex(&gSkPDFCanonFontMutex) |
+ , fShaderMutex(&gSkPDFCanonShaderMutex) |
+ , fPaintMutex(&gSkPDFCanonPaintMutex) { |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, |
+ uint16_t glyphID, |
+ SkPDFFont** relatedFontPtr) const { |
+ assert_mutex_held(fFontMutex); |
+ SkASSERT(relatedFontPtr); |
+ |
+ SkPDFFont* relatedFont = NULL; |
+ for (int i = 0; i < fFontRecords.count(); ++i) { |
+ SkPDFFont::Match match = SkPDFFont::IsMatch( |
+ fFontRecords[i].fFont, fFontRecords[i].fFontID, |
mtklein
2015/01/20 21:59:51
Might put all these arguments one per line, or at
hal.canary
2015/01/21 17:07:50
Done.
|
+ fFontRecords[i].fGlyphID, fontID, glyphID); |
+ if (SkPDFFont::kExact_Match == match) { |
+ return fFontRecords[i].fFont; |
+ } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { |
+ relatedFont = fFontRecords[i].fFont; |
+ } |
+ } |
+ *relatedFontPtr = relatedFont; // May still be NULL. |
+ return NULL; |
+} |
+ |
+void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { |
+ assert_mutex_held(fFontMutex); |
+ SkPDFCanon::FontRec* rec = fFontRecords.push(); |
+ rec->fFont = font; |
+ rec->fFontID = fontID; |
+ rec->fGlyphID = fGlyphID; |
+} |
+ |
+void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { |
+ assert_mutex_held(fFontMutex); |
+ for (int i = 0; i < fFontRecords.count(); i++) { |
+ if (fFontRecords[i].fFont == pdfFont) { |
+ fFontRecords.removeShuffle(i); |
+ return; |
+ } |
+ } |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+SkPDFShader* SkPDFCanon::findShader(const SkPDFShaderState& state) const { |
+ assert_mutex_held(fShaderMutex); |
+ for (int i = 0; i < fShaderRecords.count(); ++i) { |
+ if (fShaderRecords[i]->equals(state)) { |
+ return fShaderRecords[i]; |
+ } |
+ } |
+ return NULL; |
+} |
+ |
+void SkPDFCanon::addShader(SkPDFShader* shader) { |
+ assert_mutex_held(fShaderMutex); |
+ SkASSERT(shader); |
+ fShaderRecords.push(shader); |
+} |
+ |
+void SkPDFCanon::removeShader(SkPDFShader* pdfShader) { |
+ assert_mutex_held(fShaderMutex); |
+ for (int i = 0; i < fShaderRecords.count(); ++i) { |
+ if (fShaderRecords[i] == pdfShader) { |
+ fShaderRecords.removeShuffle(i); |
+ return; |
+ } |
+ } |
+ SkDEBUGFAIL("pdfShader not found"); |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+ |
+SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { |
+ assert_mutex_held(fPaintMutex); |
+ for (int i = 0; i < fGraphicStateRecords.count(); ++i) { |
+ if (fGraphicStateRecords[i]->equals(paint)) { |
+ return fGraphicStateRecords[i]; |
+ } |
+ } |
+ return NULL; |
+} |
+ |
+void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { |
+ assert_mutex_held(fPaintMutex); |
+ SkASSERT(state); |
+ fGraphicStateRecords.push(state); |
+} |
+ |
+void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { |
+ assert_mutex_held(fPaintMutex); |
+ for (int i = 0; i < fGraphicStateRecords.count(); ++i) { |
+ if (fGraphicStateRecords[i] == pdfGraphicState) { |
+ fGraphicStateRecords.removeShuffle(i); |
+ return; |
+ } |
+ } |
+ SkDEBUGFAIL("pdfGraphicState not found"); |
+} |