Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: src/pdf/SkPDFDevice.cpp

Issue 18977002: Add SkPDFResourceDict class, refactor existing code (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Add SkPDFDict::getResourceName, and refactor into existing code. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/pdf/SkPDFDevice.h ('k') | src/pdf/SkPDFFormXObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkPDFDevice.h" 8 #include "SkPDFDevice.h"
9 9
10 #include "SkAnnotation.h" 10 #include "SkAnnotation.h"
11 #include "SkColor.h" 11 #include "SkColor.h"
12 #include "SkClipStack.h" 12 #include "SkClipStack.h"
13 #include "SkData.h" 13 #include "SkData.h"
14 #include "SkDraw.h" 14 #include "SkDraw.h"
15 #include "SkFontHost.h" 15 #include "SkFontHost.h"
16 #include "SkGlyphCache.h" 16 #include "SkGlyphCache.h"
17 #include "SkPaint.h" 17 #include "SkPaint.h"
18 #include "SkPath.h" 18 #include "SkPath.h"
19 #include "SkPDFFont.h" 19 #include "SkPDFFont.h"
20 #include "SkPDFFormXObject.h" 20 #include "SkPDFFormXObject.h"
21 #include "SkPDFGraphicState.h" 21 #include "SkPDFGraphicState.h"
22 #include "SkPDFImage.h" 22 #include "SkPDFImage.h"
23 #include "SkPDFResourceDict.h"
23 #include "SkPDFShader.h" 24 #include "SkPDFShader.h"
24 #include "SkPDFStream.h" 25 #include "SkPDFStream.h"
25 #include "SkPDFTypes.h" 26 #include "SkPDFTypes.h"
26 #include "SkPDFUtils.h" 27 #include "SkPDFUtils.h"
27 #include "SkRect.h" 28 #include "SkRect.h"
28 #include "SkString.h" 29 #include "SkString.h"
29 #include "SkTextFormatParams.h" 30 #include "SkTextFormatParams.h"
30 #include "SkTemplates.h" 31 #include "SkTemplates.h"
31 #include "SkTypefacePriv.h" 32 #include "SkTypefacePriv.h"
32 #include "SkTSet.h" 33 #include "SkTSet.h"
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 429
429 push(); 430 push();
430 SkPDFUtils::AppendTransform(matrix, fContentStream); 431 SkPDFUtils::AppendTransform(matrix, fContentStream);
431 currentEntry()->fMatrix = matrix; 432 currentEntry()->fMatrix = matrix;
432 } 433 }
433 434
434 void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) { 435 void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) {
435 // PDF treats a shader as a color, so we only set one or the other. 436 // PDF treats a shader as a color, so we only set one or the other.
436 if (state.fShaderIndex >= 0) { 437 if (state.fShaderIndex >= 0) {
437 if (state.fShaderIndex != currentEntry()->fShaderIndex) { 438 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
438 fContentStream->writeText("/Pattern CS /Pattern cs /P"); 439 fContentStream->writeText("/Pattern CS /Pattern cs /");
439 fContentStream->writeDecAsText(state.fShaderIndex); 440 fContentStream->writeText(SkPDFResourceDict::getResourceName(
vandebo (ex-Chrome) 2013/07/12 21:58:58 Since this is called twice with the same index, yo
ducky 2013/07/12 23:49:32 Done.
440 fContentStream->writeText(" SCN /P"); 441 SkPDFResourceDict::kPattern_ResourceType,
441 fContentStream->writeDecAsText(state.fShaderIndex); 442 state.fShaderIndex).c_str());
443 fContentStream->writeText(" SCN /");
444 fContentStream->writeText(SkPDFResourceDict::getResourceName(
445 SkPDFResourceDict::kPattern_ResourceType,
446 state.fShaderIndex).c_str());
442 fContentStream->writeText(" scn\n"); 447 fContentStream->writeText(" scn\n");
443 currentEntry()->fShaderIndex = state.fShaderIndex; 448 currentEntry()->fShaderIndex = state.fShaderIndex;
444 } 449 }
445 } else { 450 } else {
446 if (state.fColor != currentEntry()->fColor || 451 if (state.fColor != currentEntry()->fColor ||
447 currentEntry()->fShaderIndex >= 0) { 452 currentEntry()->fShaderIndex >= 0) {
448 emit_pdf_color(state.fColor, fContentStream); 453 emit_pdf_color(state.fColor, fContentStream);
449 fContentStream->writeText("RG "); 454 fContentStream->writeText("RG ");
450 emit_pdf_color(state.fColor, fContentStream); 455 emit_pdf_color(state.fColor, fContentStream);
451 fContentStream->writeText("rg\n"); 456 fContentStream->writeText("rg\n");
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 fLastMarginContentEntry = contentEntry; 1132 fLastMarginContentEntry = contentEntry;
1128 } 1133 }
1129 } 1134 }
1130 1135
1131 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) { 1136 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) {
1132 // A ScopedContentEntry only exists during the course of a draw call, so 1137 // A ScopedContentEntry only exists during the course of a draw call, so
1133 // this can't be called while a ScopedContentEntry exists. 1138 // this can't be called while a ScopedContentEntry exists.
1134 fDrawingArea = drawingArea; 1139 fDrawingArea = drawingArea;
1135 } 1140 }
1136 1141
1137 SkPDFDict* SkPDFDevice::getResourceDict() { 1142 SkPDFResourceDict* SkPDFDevice::getResourceDict() {
1138 if (NULL == fResourceDict) { 1143 if (NULL == fResourceDict) {
1139 fResourceDict = SkNEW(SkPDFDict); 1144 fResourceDict = SkNEW(SkPDFResourceDict);
1140 1145
1141 if (fGraphicStateResources.count()) { 1146 if (fGraphicStateResources.count()) {
1142 SkAutoTUnref<SkPDFDict> extGState(new SkPDFDict());
1143 for (int i = 0; i < fGraphicStateResources.count(); i++) { 1147 for (int i = 0; i < fGraphicStateResources.count(); i++) {
1144 SkString nameString("G"); 1148 fResourceDict->insertResourceAsRef(
1145 nameString.appendS32(i); 1149 SkPDFResourceDict::kExtGState_ResourceType,
1146 extGState->insert( 1150 i, fGraphicStateResources[i]);
1147 nameString.c_str(),
1148 new SkPDFObjRef(fGraphicStateResources[i]))->unref();
1149 } 1151 }
1150 fResourceDict->insert("ExtGState", extGState.get());
1151 } 1152 }
1152 1153
1153 if (fXObjectResources.count()) { 1154 if (fXObjectResources.count()) {
1154 SkAutoTUnref<SkPDFDict> xObjects(new SkPDFDict());
1155 for (int i = 0; i < fXObjectResources.count(); i++) { 1155 for (int i = 0; i < fXObjectResources.count(); i++) {
1156 SkString nameString("X"); 1156 fResourceDict->insertResourceAsRef(
1157 nameString.appendS32(i); 1157 SkPDFResourceDict::kXObject_ResourceType,
1158 xObjects->insert( 1158 i, fXObjectResources[i]);
1159 nameString.c_str(),
1160 new SkPDFObjRef(fXObjectResources[i]))->unref();
1161 } 1159 }
1162 fResourceDict->insert("XObject", xObjects.get());
1163 } 1160 }
1164 1161
1165 if (fFontResources.count()) { 1162 if (fFontResources.count()) {
1166 SkAutoTUnref<SkPDFDict> fonts(new SkPDFDict());
1167 for (int i = 0; i < fFontResources.count(); i++) { 1163 for (int i = 0; i < fFontResources.count(); i++) {
1168 SkString nameString("F"); 1164 fResourceDict->insertResourceAsRef(
1169 nameString.appendS32(i); 1165 SkPDFResourceDict::kFont_ResourceType,
1170 fonts->insert(nameString.c_str(), 1166 i, fFontResources[i]);
1171 new SkPDFObjRef(fFontResources[i]))->unref();
1172 } 1167 }
1173 fResourceDict->insert("Font", fonts.get());
1174 } 1168 }
1175 1169
1176 if (fShaderResources.count()) { 1170 if (fShaderResources.count()) {
1177 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict()); 1171 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict());
1178 for (int i = 0; i < fShaderResources.count(); i++) { 1172 for (int i = 0; i < fShaderResources.count(); i++) {
1179 SkString nameString("P"); 1173 fResourceDict->insertResourceAsRef(
1180 nameString.appendS32(i); 1174 SkPDFResourceDict::kPattern_ResourceType,
1181 patterns->insert(nameString.c_str(), 1175 i, fShaderResources[i]);
1182 new SkPDFObjRef(fShaderResources[i]))->unref();
1183 }
1184 fResourceDict->insert("Pattern", patterns.get());
1185 }
1186
1187 // For compatibility, add all proc sets (only used for output to PS
1188 // devices).
1189 const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"};
1190 SkAutoTUnref<SkPDFArray> procSets(new SkPDFArray());
1191 procSets->reserve(SK_ARRAY_COUNT(procs));
1192 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++)
1193 procSets->appendName(procs[i]);
1194 fResourceDict->insert("ProcSet", procSets.get());
1195 }
1196 return fResourceDict;
1197 }
1198
1199 void SkPDFDevice::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects,
1200 SkTSet<SkPDFObject*>* newResourceObjects,
1201 bool recursive) const {
1202 // TODO: reserve not correct if we need to recursively explore.
1203 newResourceObjects->setReserve(newResourceObjects->count() +
1204 fGraphicStateResources.count() +
1205 fXObjectResources.count() +
1206 fFontResources.count() +
1207 fShaderResources.count());
1208 for (int i = 0; i < fGraphicStateResources.count(); i++) {
1209 if (!knownResourceObjects.contains(fGraphicStateResources[i]) &&
1210 !newResourceObjects->contains(fGraphicStateResources[i])) {
1211 newResourceObjects->add(fGraphicStateResources[i]);
1212 fGraphicStateResources[i]->ref();
1213 if (recursive) {
1214 fGraphicStateResources[i]->getResources(knownResourceObjects,
1215 newResourceObjects);
1216 } 1176 }
1217 } 1177 }
1218 } 1178 }
1219 for (int i = 0; i < fXObjectResources.count(); i++) { 1179 return fResourceDict;
1220 if (!knownResourceObjects.contains(fXObjectResources[i]) &&
1221 !newResourceObjects->contains(fXObjectResources[i])) {
1222 newResourceObjects->add(fXObjectResources[i]);
1223 fXObjectResources[i]->ref();
1224 if (recursive) {
1225 fXObjectResources[i]->getResources(knownResourceObjects,
1226 newResourceObjects);
1227 }
1228 }
1229 }
1230 for (int i = 0; i < fFontResources.count(); i++) {
1231 if (!knownResourceObjects.contains(fFontResources[i]) &&
1232 !newResourceObjects->contains(fFontResources[i])) {
1233 newResourceObjects->add(fFontResources[i]);
1234 fFontResources[i]->ref();
1235 if (recursive) {
1236 fFontResources[i]->getResources(knownResourceObjects,
1237 newResourceObjects);
1238 }
1239 }
1240 }
1241 for (int i = 0; i < fShaderResources.count(); i++) {
1242 if (!knownResourceObjects.contains(fShaderResources[i]) &&
1243 !newResourceObjects->contains(fShaderResources[i])) {
1244 newResourceObjects->add(fShaderResources[i]);
1245 fShaderResources[i]->ref();
1246 if (recursive) {
1247 fShaderResources[i]->getResources(knownResourceObjects,
1248 newResourceObjects);
1249 }
1250 }
1251 }
1252 } 1180 }
1253 1181
1254 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const { 1182 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1255 return fFontResources; 1183 return fFontResources;
1256 } 1184 }
1257 1185
1258 SkPDFArray* SkPDFDevice::copyMediaBox() const { 1186 SkPDFArray* SkPDFDevice::copyMediaBox() const {
1259 // should this be a singleton? 1187 // should this be a singleton?
1260 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0))); 1188 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0)));
1261 1189
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1775 return result; 1703 return result;
1776 } 1704 }
1777 1705
1778 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, 1706 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
1779 ContentEntry* contentEntry) { 1707 ContentEntry* contentEntry) {
1780 SkTypeface* typeface = paint.getTypeface(); 1708 SkTypeface* typeface = paint.getTypeface();
1781 if (contentEntry->fState.fFont == NULL || 1709 if (contentEntry->fState.fFont == NULL ||
1782 contentEntry->fState.fTextSize != paint.getTextSize() || 1710 contentEntry->fState.fTextSize != paint.getTextSize() ||
1783 !contentEntry->fState.fFont->hasGlyph(glyphID)) { 1711 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
1784 int fontIndex = getFontResourceIndex(typeface, glyphID); 1712 int fontIndex = getFontResourceIndex(typeface, glyphID);
1785 contentEntry->fContent.writeText("/F"); 1713 contentEntry->fContent.writeText("/");
1786 contentEntry->fContent.writeDecAsText(fontIndex); 1714 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
vandebo (ex-Chrome) 2013/07/12 21:58:58 This statement looks kind of ugly. Do you think i
ducky 2013/07/12 23:49:32 I think it's going to look ugly either way, becaus
1715 SkPDFResourceDict::kFont_ResourceType,
1716 fontIndex).c_str());
1787 contentEntry->fContent.writeText(" "); 1717 contentEntry->fContent.writeText(" ");
1788 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent); 1718 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent);
1789 contentEntry->fContent.writeText(" Tf\n"); 1719 contentEntry->fContent.writeText(" Tf\n");
1790 contentEntry->fState.fFont = fFontResources[fontIndex]; 1720 contentEntry->fState.fFont = fFontResources[fontIndex];
1791 } 1721 }
1792 } 1722 }
1793 1723
1794 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { 1724 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
1795 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID )); 1725 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID ));
1796 int resourceIndex = fFontResources.find(newFont.get()); 1726 int resourceIndex = fFontResources.find(newFont.get());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1837 } 1767 }
1838 1768
1839 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, 1769 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
1840 SkCanvas::Config8888) { 1770 SkCanvas::Config8888) {
1841 return false; 1771 return false;
1842 } 1772 }
1843 1773
1844 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { 1774 bool SkPDFDevice::allowImageFilter(SkImageFilter*) {
1845 return false; 1775 return false;
1846 } 1776 }
OLDNEW
« no previous file with comments | « include/pdf/SkPDFDevice.h ('k') | src/pdf/SkPDFFormXObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698