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

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: Address review comments. 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
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 SkString resourceName = SkPDFResourceDict::getResourceName(
439 fContentStream->writeDecAsText(state.fShaderIndex); 440 SkPDFResourceDict::kPattern_ResourceType,
440 fContentStream->writeText(" SCN /P"); 441 state.fShaderIndex);
441 fContentStream->writeDecAsText(state.fShaderIndex); 442 fContentStream->writeText("/Pattern CS /Pattern cs /");
edisonn 2013/07/17 18:38:22 I have seen this code in another cl, consider re f
ducky 2013/07/17 19:51:25 Yes, this is in the transparency gradients CL, whi
443 fContentStream->writeText(resourceName.c_str());
444 fContentStream->writeText(" SCN /");
445 fContentStream->writeText(resourceName.c_str());
442 fContentStream->writeText(" scn\n"); 446 fContentStream->writeText(" scn\n");
443 currentEntry()->fShaderIndex = state.fShaderIndex; 447 currentEntry()->fShaderIndex = state.fShaderIndex;
444 } 448 }
445 } else { 449 } else {
446 if (state.fColor != currentEntry()->fColor || 450 if (state.fColor != currentEntry()->fColor ||
447 currentEntry()->fShaderIndex >= 0) { 451 currentEntry()->fShaderIndex >= 0) {
448 emit_pdf_color(state.fColor, fContentStream); 452 emit_pdf_color(state.fColor, fContentStream);
449 fContentStream->writeText("RG "); 453 fContentStream->writeText("RG ");
450 emit_pdf_color(state.fColor, fContentStream); 454 emit_pdf_color(state.fColor, fContentStream);
451 fContentStream->writeText("rg\n"); 455 fContentStream->writeText("rg\n");
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 fLastMarginContentEntry = contentEntry; 1131 fLastMarginContentEntry = contentEntry;
1128 } 1132 }
1129 } 1133 }
1130 1134
1131 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) { 1135 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) {
1132 // A ScopedContentEntry only exists during the course of a draw call, so 1136 // A ScopedContentEntry only exists during the course of a draw call, so
1133 // this can't be called while a ScopedContentEntry exists. 1137 // this can't be called while a ScopedContentEntry exists.
1134 fDrawingArea = drawingArea; 1138 fDrawingArea = drawingArea;
1135 } 1139 }
1136 1140
1137 SkPDFDict* SkPDFDevice::getResourceDict() { 1141 SkPDFResourceDict* SkPDFDevice::getResourceDict() {
1138 if (NULL == fResourceDict) { 1142 if (NULL == fResourceDict) {
1139 fResourceDict = SkNEW(SkPDFDict); 1143 fResourceDict = SkNEW(SkPDFResourceDict);
1140 1144
1141 if (fGraphicStateResources.count()) { 1145 if (fGraphicStateResources.count()) {
1142 SkAutoTUnref<SkPDFDict> extGState(new SkPDFDict());
1143 for (int i = 0; i < fGraphicStateResources.count(); i++) { 1146 for (int i = 0; i < fGraphicStateResources.count(); i++) {
1144 SkString nameString("G"); 1147 fResourceDict->insertResourceAsRef(
1145 nameString.appendS32(i); 1148 SkPDFResourceDict::kExtGState_ResourceType,
1146 extGState->insert( 1149 i, fGraphicStateResources[i]);
1147 nameString.c_str(),
1148 new SkPDFObjRef(fGraphicStateResources[i]))->unref();
1149 } 1150 }
1150 fResourceDict->insert("ExtGState", extGState.get());
1151 } 1151 }
1152 1152
1153 if (fXObjectResources.count()) { 1153 if (fXObjectResources.count()) {
1154 SkAutoTUnref<SkPDFDict> xObjects(new SkPDFDict());
1155 for (int i = 0; i < fXObjectResources.count(); i++) { 1154 for (int i = 0; i < fXObjectResources.count(); i++) {
1156 SkString nameString("X"); 1155 fResourceDict->insertResourceAsRef(
1157 nameString.appendS32(i); 1156 SkPDFResourceDict::kXObject_ResourceType,
1158 xObjects->insert( 1157 i, fXObjectResources[i]);
1159 nameString.c_str(),
1160 new SkPDFObjRef(fXObjectResources[i]))->unref();
1161 } 1158 }
1162 fResourceDict->insert("XObject", xObjects.get());
1163 } 1159 }
1164 1160
1165 if (fFontResources.count()) { 1161 if (fFontResources.count()) {
1166 SkAutoTUnref<SkPDFDict> fonts(new SkPDFDict());
1167 for (int i = 0; i < fFontResources.count(); i++) { 1162 for (int i = 0; i < fFontResources.count(); i++) {
1168 SkString nameString("F"); 1163 fResourceDict->insertResourceAsRef(
1169 nameString.appendS32(i); 1164 SkPDFResourceDict::kFont_ResourceType,
1170 fonts->insert(nameString.c_str(), 1165 i, fFontResources[i]);
1171 new SkPDFObjRef(fFontResources[i]))->unref();
1172 } 1166 }
1173 fResourceDict->insert("Font", fonts.get());
1174 } 1167 }
1175 1168
1176 if (fShaderResources.count()) { 1169 if (fShaderResources.count()) {
1177 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict()); 1170 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict());
1178 for (int i = 0; i < fShaderResources.count(); i++) { 1171 for (int i = 0; i < fShaderResources.count(); i++) {
1179 SkString nameString("P"); 1172 fResourceDict->insertResourceAsRef(
1180 nameString.appendS32(i); 1173 SkPDFResourceDict::kPattern_ResourceType,
1181 patterns->insert(nameString.c_str(), 1174 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 } 1175 }
1217 } 1176 }
1218 } 1177 }
1219 for (int i = 0; i < fXObjectResources.count(); i++) { 1178 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 } 1179 }
1253 1180
1254 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const { 1181 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1255 return fFontResources; 1182 return fFontResources;
1256 } 1183 }
1257 1184
1258 SkPDFArray* SkPDFDevice::copyMediaBox() const { 1185 SkPDFArray* SkPDFDevice::copyMediaBox() const {
1259 // should this be a singleton? 1186 // should this be a singleton?
1260 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0))); 1187 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0)));
1261 1188
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after
1775 return result; 1702 return result;
1776 } 1703 }
1777 1704
1778 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, 1705 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
1779 ContentEntry* contentEntry) { 1706 ContentEntry* contentEntry) {
1780 SkTypeface* typeface = paint.getTypeface(); 1707 SkTypeface* typeface = paint.getTypeface();
1781 if (contentEntry->fState.fFont == NULL || 1708 if (contentEntry->fState.fFont == NULL ||
1782 contentEntry->fState.fTextSize != paint.getTextSize() || 1709 contentEntry->fState.fTextSize != paint.getTextSize() ||
1783 !contentEntry->fState.fFont->hasGlyph(glyphID)) { 1710 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
1784 int fontIndex = getFontResourceIndex(typeface, glyphID); 1711 int fontIndex = getFontResourceIndex(typeface, glyphID);
1785 contentEntry->fContent.writeText("/F"); 1712 contentEntry->fContent.writeText("/");
1786 contentEntry->fContent.writeDecAsText(fontIndex); 1713 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
edisonn 2013/07/17 18:38:22 could there be no font at all?
ducky 2013/07/17 19:51:25 SkPDFResourceDict::getResourceName essentially enc
1714 SkPDFResourceDict::kFont_ResourceType,
1715 fontIndex).c_str());
1787 contentEntry->fContent.writeText(" "); 1716 contentEntry->fContent.writeText(" ");
1788 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent); 1717 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent);
1789 contentEntry->fContent.writeText(" Tf\n"); 1718 contentEntry->fContent.writeText(" Tf\n");
1790 contentEntry->fState.fFont = fFontResources[fontIndex]; 1719 contentEntry->fState.fFont = fFontResources[fontIndex];
1791 } 1720 }
1792 } 1721 }
1793 1722
1794 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { 1723 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
1795 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID )); 1724 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID ));
1796 int resourceIndex = fFontResources.find(newFont.get()); 1725 int resourceIndex = fFontResources.find(newFont.get());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1837 } 1766 }
1838 1767
1839 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, 1768 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
1840 SkCanvas::Config8888) { 1769 SkCanvas::Config8888) {
1841 return false; 1770 return false;
1842 } 1771 }
1843 1772
1844 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { 1773 bool SkPDFDevice::allowImageFilter(SkImageFilter*) {
1845 return false; 1774 return false;
1846 } 1775 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698