Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2015 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkPDFCanon.h" | |
| 9 #include "SkPDFFont.h" | |
| 10 #include "SkPDFGraphicState.h" | |
| 11 #include "SkPDFShader.h" | |
| 12 | |
| 13 //////////////////////////////////////////////////////////////////////////////// | |
| 14 | |
| 15 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonFontMutex); | |
| 16 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonShaderMutex); | |
| 17 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonPaintMutex); | |
| 18 | |
| 19 SkBaseMutex& SkPDFCanon::GetFontMutex() { | |
| 20 return gSkPDFCanonFontMutex; | |
| 21 } | |
| 22 SkBaseMutex& SkPDFCanon::GetShaderMutex() { | |
| 23 return gSkPDFCanonShaderMutex; | |
| 24 } | |
| 25 SkBaseMutex& SkPDFCanon::GetPaintMutex() { | |
| 26 return gSkPDFCanonPaintMutex; | |
| 27 } | |
| 28 | |
| 29 SkPDFCanon* SkPDFCanon::Create() { | |
| 30 return new SkPDFCanon; | |
| 31 } | |
| 32 void SkPDFCanon::Destroy(SkPDFCanon* ptr) { | |
| 33 delete ptr; | |
| 34 } | |
| 35 SkPDFCanon::~SkPDFCanon() { | |
| 36 } | |
| 37 SK_DECLARE_STATIC_LAZY_PTR(SkPDFCanon, | |
| 38 singleton, | |
| 39 SkPDFCanon::Create, | |
| 40 SkPDFCanon::Destroy); | |
| 41 | |
| 42 SkPDFCanon& SkPDFCanon::GetCanon() { | |
| 43 return *singleton.get(); | |
| 44 } | |
| 45 | |
| 46 static void assert_mutex_held(SkBaseMutex* mutex) { | |
| 47 SkDEBUGCODE(if (mutex) { mutex->assertHeld(); }) | |
| 48 } | |
| 49 | |
| 50 SkPDFCanon::SkPDFCanon() | |
| 51 : fFontMutex(&gSkPDFCanonFontMutex) | |
| 52 , fShaderMutex(&gSkPDFCanonShaderMutex) | |
| 53 , fPaintMutex(&gSkPDFCanonPaintMutex) { | |
| 54 } | |
| 55 | |
| 56 //////////////////////////////////////////////////////////////////////////////// | |
| 57 | |
| 58 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, | |
| 59 uint16_t glyphID, | |
| 60 SkPDFFont** relatedFontPtr) const { | |
| 61 assert_mutex_held(fFontMutex); | |
| 62 SkASSERT(relatedFontPtr); | |
| 63 | |
| 64 SkPDFFont* relatedFont = NULL; | |
| 65 for (int i = 0; i < fFontRecords.count(); ++i) { | |
| 66 SkPDFFont::Match match = SkPDFFont::IsMatch( | |
| 67 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.
| |
| 68 fFontRecords[i].fGlyphID, fontID, glyphID); | |
| 69 if (SkPDFFont::kExact_Match == match) { | |
| 70 return fFontRecords[i].fFont; | |
| 71 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { | |
| 72 relatedFont = fFontRecords[i].fFont; | |
| 73 } | |
| 74 } | |
| 75 *relatedFontPtr = relatedFont; // May still be NULL. | |
| 76 return NULL; | |
| 77 } | |
| 78 | |
| 79 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { | |
| 80 assert_mutex_held(fFontMutex); | |
| 81 SkPDFCanon::FontRec* rec = fFontRecords.push(); | |
| 82 rec->fFont = font; | |
| 83 rec->fFontID = fontID; | |
| 84 rec->fGlyphID = fGlyphID; | |
| 85 } | |
| 86 | |
| 87 void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { | |
| 88 assert_mutex_held(fFontMutex); | |
| 89 for (int i = 0; i < fFontRecords.count(); i++) { | |
| 90 if (fFontRecords[i].fFont == pdfFont) { | |
| 91 fFontRecords.removeShuffle(i); | |
| 92 return; | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 //////////////////////////////////////////////////////////////////////////////// | |
| 98 | |
| 99 SkPDFShader* SkPDFCanon::findShader(const SkPDFShaderState& state) const { | |
| 100 assert_mutex_held(fShaderMutex); | |
| 101 for (int i = 0; i < fShaderRecords.count(); ++i) { | |
| 102 if (fShaderRecords[i]->equals(state)) { | |
| 103 return fShaderRecords[i]; | |
| 104 } | |
| 105 } | |
| 106 return NULL; | |
| 107 } | |
| 108 | |
| 109 void SkPDFCanon::addShader(SkPDFShader* shader) { | |
| 110 assert_mutex_held(fShaderMutex); | |
| 111 SkASSERT(shader); | |
| 112 fShaderRecords.push(shader); | |
| 113 } | |
| 114 | |
| 115 void SkPDFCanon::removeShader(SkPDFShader* pdfShader) { | |
| 116 assert_mutex_held(fShaderMutex); | |
| 117 for (int i = 0; i < fShaderRecords.count(); ++i) { | |
| 118 if (fShaderRecords[i] == pdfShader) { | |
| 119 fShaderRecords.removeShuffle(i); | |
| 120 return; | |
| 121 } | |
| 122 } | |
| 123 SkDEBUGFAIL("pdfShader not found"); | |
| 124 } | |
| 125 | |
| 126 //////////////////////////////////////////////////////////////////////////////// | |
| 127 | |
| 128 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { | |
| 129 assert_mutex_held(fPaintMutex); | |
| 130 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { | |
| 131 if (fGraphicStateRecords[i]->equals(paint)) { | |
| 132 return fGraphicStateRecords[i]; | |
| 133 } | |
| 134 } | |
| 135 return NULL; | |
| 136 } | |
| 137 | |
| 138 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { | |
| 139 assert_mutex_held(fPaintMutex); | |
| 140 SkASSERT(state); | |
| 141 fGraphicStateRecords.push(state); | |
| 142 } | |
| 143 | |
| 144 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { | |
| 145 assert_mutex_held(fPaintMutex); | |
| 146 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { | |
| 147 if (fGraphicStateRecords[i] == pdfGraphicState) { | |
| 148 fGraphicStateRecords.removeShuffle(i); | |
| 149 return; | |
| 150 } | |
| 151 } | |
| 152 SkDEBUGFAIL("pdfGraphicState not found"); | |
| 153 } | |
| OLD | NEW |