OLD | NEW |
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 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 | 418 |
418 push(); | 419 push(); |
419 SkPDFUtils::AppendTransform(matrix, fContentStream); | 420 SkPDFUtils::AppendTransform(matrix, fContentStream); |
420 currentEntry()->fMatrix = matrix; | 421 currentEntry()->fMatrix = matrix; |
421 } | 422 } |
422 | 423 |
423 void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) { | 424 void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) { |
424 // PDF treats a shader as a color, so we only set one or the other. | 425 // PDF treats a shader as a color, so we only set one or the other. |
425 if (state.fShaderIndex >= 0) { | 426 if (state.fShaderIndex >= 0) { |
426 if (state.fShaderIndex != currentEntry()->fShaderIndex) { | 427 if (state.fShaderIndex != currentEntry()->fShaderIndex) { |
427 fContentStream->writeText("/Pattern CS /Pattern cs /P"); | 428 SkString resourceName = SkPDFResourceDict::getResourceName( |
428 fContentStream->writeDecAsText(state.fShaderIndex); | 429 SkPDFResourceDict::kPattern_ResourceType, |
429 fContentStream->writeText(" SCN /P"); | 430 state.fShaderIndex); |
430 fContentStream->writeDecAsText(state.fShaderIndex); | 431 fContentStream->writeText("/Pattern CS /Pattern cs /"); |
| 432 fContentStream->writeText(resourceName.c_str()); |
| 433 fContentStream->writeText(" SCN /"); |
| 434 fContentStream->writeText(resourceName.c_str()); |
431 fContentStream->writeText(" scn\n"); | 435 fContentStream->writeText(" scn\n"); |
432 currentEntry()->fShaderIndex = state.fShaderIndex; | 436 currentEntry()->fShaderIndex = state.fShaderIndex; |
433 } | 437 } |
434 } else { | 438 } else { |
435 if (state.fColor != currentEntry()->fColor || | 439 if (state.fColor != currentEntry()->fColor || |
436 currentEntry()->fShaderIndex >= 0) { | 440 currentEntry()->fShaderIndex >= 0) { |
437 emit_pdf_color(state.fColor, fContentStream); | 441 emit_pdf_color(state.fColor, fContentStream); |
438 fContentStream->writeText("RG "); | 442 fContentStream->writeText("RG "); |
439 emit_pdf_color(state.fColor, fContentStream); | 443 emit_pdf_color(state.fColor, fContentStream); |
440 fContentStream->writeText("rg\n"); | 444 fContentStream->writeText("rg\n"); |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 fLastMarginContentEntry = contentEntry; | 1120 fLastMarginContentEntry = contentEntry; |
1117 } | 1121 } |
1118 } | 1122 } |
1119 | 1123 |
1120 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) { | 1124 void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) { |
1121 // A ScopedContentEntry only exists during the course of a draw call, so | 1125 // A ScopedContentEntry only exists during the course of a draw call, so |
1122 // this can't be called while a ScopedContentEntry exists. | 1126 // this can't be called while a ScopedContentEntry exists. |
1123 fDrawingArea = drawingArea; | 1127 fDrawingArea = drawingArea; |
1124 } | 1128 } |
1125 | 1129 |
1126 SkPDFDict* SkPDFDevice::getResourceDict() { | 1130 SkPDFResourceDict* SkPDFDevice::getResourceDict() { |
1127 if (NULL == fResourceDict) { | 1131 if (NULL == fResourceDict) { |
1128 fResourceDict = SkNEW(SkPDFDict); | 1132 fResourceDict = SkNEW(SkPDFResourceDict); |
1129 | 1133 |
1130 if (fGraphicStateResources.count()) { | 1134 if (fGraphicStateResources.count()) { |
1131 SkAutoTUnref<SkPDFDict> extGState(new SkPDFDict()); | |
1132 for (int i = 0; i < fGraphicStateResources.count(); i++) { | 1135 for (int i = 0; i < fGraphicStateResources.count(); i++) { |
1133 SkString nameString("G"); | 1136 fResourceDict->insertResourceAsReference( |
1134 nameString.appendS32(i); | 1137 SkPDFResourceDict::kExtGState_ResourceType, |
1135 extGState->insert( | 1138 i, fGraphicStateResources[i]); |
1136 nameString.c_str(), | |
1137 new SkPDFObjRef(fGraphicStateResources[i]))->unref(); | |
1138 } | 1139 } |
1139 fResourceDict->insert("ExtGState", extGState.get()); | |
1140 } | 1140 } |
1141 | 1141 |
1142 if (fXObjectResources.count()) { | 1142 if (fXObjectResources.count()) { |
1143 SkAutoTUnref<SkPDFDict> xObjects(new SkPDFDict()); | |
1144 for (int i = 0; i < fXObjectResources.count(); i++) { | 1143 for (int i = 0; i < fXObjectResources.count(); i++) { |
1145 SkString nameString("X"); | 1144 fResourceDict->insertResourceAsReference( |
1146 nameString.appendS32(i); | 1145 SkPDFResourceDict::kXObject_ResourceType, |
1147 xObjects->insert( | 1146 i, fXObjectResources[i]); |
1148 nameString.c_str(), | |
1149 new SkPDFObjRef(fXObjectResources[i]))->unref(); | |
1150 } | 1147 } |
1151 fResourceDict->insert("XObject", xObjects.get()); | |
1152 } | 1148 } |
1153 | 1149 |
1154 if (fFontResources.count()) { | 1150 if (fFontResources.count()) { |
1155 SkAutoTUnref<SkPDFDict> fonts(new SkPDFDict()); | |
1156 for (int i = 0; i < fFontResources.count(); i++) { | 1151 for (int i = 0; i < fFontResources.count(); i++) { |
1157 SkString nameString("F"); | 1152 fResourceDict->insertResourceAsReference( |
1158 nameString.appendS32(i); | 1153 SkPDFResourceDict::kFont_ResourceType, |
1159 fonts->insert(nameString.c_str(), | 1154 i, fFontResources[i]); |
1160 new SkPDFObjRef(fFontResources[i]))->unref(); | |
1161 } | 1155 } |
1162 fResourceDict->insert("Font", fonts.get()); | |
1163 } | 1156 } |
1164 | 1157 |
1165 if (fShaderResources.count()) { | 1158 if (fShaderResources.count()) { |
1166 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict()); | 1159 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict()); |
1167 for (int i = 0; i < fShaderResources.count(); i++) { | 1160 for (int i = 0; i < fShaderResources.count(); i++) { |
1168 SkString nameString("P"); | 1161 fResourceDict->insertResourceAsReference( |
1169 nameString.appendS32(i); | 1162 SkPDFResourceDict::kPattern_ResourceType, |
1170 patterns->insert(nameString.c_str(), | 1163 i, fShaderResources[i]); |
1171 new SkPDFObjRef(fShaderResources[i]))->unref(); | |
1172 } | |
1173 fResourceDict->insert("Pattern", patterns.get()); | |
1174 } | |
1175 | |
1176 // For compatibility, add all proc sets (only used for output to PS | |
1177 // devices). | |
1178 const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; | |
1179 SkAutoTUnref<SkPDFArray> procSets(new SkPDFArray()); | |
1180 procSets->reserve(SK_ARRAY_COUNT(procs)); | |
1181 for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) | |
1182 procSets->appendName(procs[i]); | |
1183 fResourceDict->insert("ProcSet", procSets.get()); | |
1184 } | |
1185 return fResourceDict; | |
1186 } | |
1187 | |
1188 void SkPDFDevice::getResources(const SkTSet<SkPDFObject*>& knownResourceObjects, | |
1189 SkTSet<SkPDFObject*>* newResourceObjects, | |
1190 bool recursive) const { | |
1191 // TODO: reserve not correct if we need to recursively explore. | |
1192 newResourceObjects->setReserve(newResourceObjects->count() + | |
1193 fGraphicStateResources.count() + | |
1194 fXObjectResources.count() + | |
1195 fFontResources.count() + | |
1196 fShaderResources.count()); | |
1197 for (int i = 0; i < fGraphicStateResources.count(); i++) { | |
1198 if (!knownResourceObjects.contains(fGraphicStateResources[i]) && | |
1199 !newResourceObjects->contains(fGraphicStateResources[i])) { | |
1200 newResourceObjects->add(fGraphicStateResources[i]); | |
1201 fGraphicStateResources[i]->ref(); | |
1202 if (recursive) { | |
1203 fGraphicStateResources[i]->getResources(knownResourceObjects, | |
1204 newResourceObjects); | |
1205 } | 1164 } |
1206 } | 1165 } |
1207 } | 1166 } |
1208 for (int i = 0; i < fXObjectResources.count(); i++) { | 1167 return fResourceDict; |
1209 if (!knownResourceObjects.contains(fXObjectResources[i]) && | |
1210 !newResourceObjects->contains(fXObjectResources[i])) { | |
1211 newResourceObjects->add(fXObjectResources[i]); | |
1212 fXObjectResources[i]->ref(); | |
1213 if (recursive) { | |
1214 fXObjectResources[i]->getResources(knownResourceObjects, | |
1215 newResourceObjects); | |
1216 } | |
1217 } | |
1218 } | |
1219 for (int i = 0; i < fFontResources.count(); i++) { | |
1220 if (!knownResourceObjects.contains(fFontResources[i]) && | |
1221 !newResourceObjects->contains(fFontResources[i])) { | |
1222 newResourceObjects->add(fFontResources[i]); | |
1223 fFontResources[i]->ref(); | |
1224 if (recursive) { | |
1225 fFontResources[i]->getResources(knownResourceObjects, | |
1226 newResourceObjects); | |
1227 } | |
1228 } | |
1229 } | |
1230 for (int i = 0; i < fShaderResources.count(); i++) { | |
1231 if (!knownResourceObjects.contains(fShaderResources[i]) && | |
1232 !newResourceObjects->contains(fShaderResources[i])) { | |
1233 newResourceObjects->add(fShaderResources[i]); | |
1234 fShaderResources[i]->ref(); | |
1235 if (recursive) { | |
1236 fShaderResources[i]->getResources(knownResourceObjects, | |
1237 newResourceObjects); | |
1238 } | |
1239 } | |
1240 } | |
1241 } | 1168 } |
1242 | 1169 |
1243 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const { | 1170 const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const { |
1244 return fFontResources; | 1171 return fFontResources; |
1245 } | 1172 } |
1246 | 1173 |
1247 SkPDFArray* SkPDFDevice::copyMediaBox() const { | 1174 SkPDFArray* SkPDFDevice::copyMediaBox() const { |
1248 // should this be a singleton? | 1175 // should this be a singleton? |
1249 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0))); | 1176 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0))); |
1250 | 1177 |
(...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1764 return result; | 1691 return result; |
1765 } | 1692 } |
1766 | 1693 |
1767 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, | 1694 void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, |
1768 ContentEntry* contentEntry) { | 1695 ContentEntry* contentEntry) { |
1769 SkTypeface* typeface = paint.getTypeface(); | 1696 SkTypeface* typeface = paint.getTypeface(); |
1770 if (contentEntry->fState.fFont == NULL || | 1697 if (contentEntry->fState.fFont == NULL || |
1771 contentEntry->fState.fTextSize != paint.getTextSize() || | 1698 contentEntry->fState.fTextSize != paint.getTextSize() || |
1772 !contentEntry->fState.fFont->hasGlyph(glyphID)) { | 1699 !contentEntry->fState.fFont->hasGlyph(glyphID)) { |
1773 int fontIndex = getFontResourceIndex(typeface, glyphID); | 1700 int fontIndex = getFontResourceIndex(typeface, glyphID); |
1774 contentEntry->fContent.writeText("/F"); | 1701 contentEntry->fContent.writeText("/"); |
1775 contentEntry->fContent.writeDecAsText(fontIndex); | 1702 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName( |
| 1703 SkPDFResourceDict::kFont_ResourceType, |
| 1704 fontIndex).c_str()); |
1776 contentEntry->fContent.writeText(" "); | 1705 contentEntry->fContent.writeText(" "); |
1777 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent); | 1706 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent); |
1778 contentEntry->fContent.writeText(" Tf\n"); | 1707 contentEntry->fContent.writeText(" Tf\n"); |
1779 contentEntry->fState.fFont = fFontResources[fontIndex]; | 1708 contentEntry->fState.fFont = fFontResources[fontIndex]; |
1780 } | 1709 } |
1781 } | 1710 } |
1782 | 1711 |
1783 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { | 1712 int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
1784 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID
)); | 1713 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID
)); |
1785 int resourceIndex = fFontResources.find(newFont.get()); | 1714 int resourceIndex = fFontResources.find(newFont.get()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1826 } | 1755 } |
1827 | 1756 |
1828 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, | 1757 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, |
1829 SkCanvas::Config8888) { | 1758 SkCanvas::Config8888) { |
1830 return false; | 1759 return false; |
1831 } | 1760 } |
1832 | 1761 |
1833 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { | 1762 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { |
1834 return false; | 1763 return false; |
1835 } | 1764 } |
OLD | NEW |