SkRefPtr<SkPDFArray> SkPDFDevice::getMediaBox() const { SkRefPtr<SkPDFInt> zero = new SkPDFInt(0); zero->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFArray> mediaBox = new SkPDFArray(); mediaBox->unref(); // SkRefPtr and new both took a reference. mediaBox->reserve(4); mediaBox->append(zero.get()); mediaBox->append(zero.get()); mediaBox->append(new SkPDFInt(fWidth))->unref(); mediaBox->append(new SkPDFInt(fHeight))->unref(); return mediaBox; }
// populateDict and operator== have to stay in sync with each other. void SkPDFGraphicState::populateDict() { if (!fPopulated) { fPopulated = true; insertName("Type", "ExtGState"); SkRefPtr<SkPDFScalar> alpha = new SkPDFScalar(fPaint.getAlpha() * SkScalarInvert(0xFF)); alpha->unref(); // SkRefPtr and new both took a reference. insert("CA", alpha.get()); insert("ca", alpha.get()); SK_COMPILE_ASSERT(SkPaint::kButt_Cap == 0, paint_cap_mismatch); SK_COMPILE_ASSERT(SkPaint::kRound_Cap == 1, paint_cap_mismatch); SK_COMPILE_ASSERT(SkPaint::kSquare_Cap == 2, paint_cap_mismatch); SK_COMPILE_ASSERT(SkPaint::kCapCount == 3, paint_cap_mismatch); SkASSERT(fPaint.getStrokeCap() >= 0 && fPaint.getStrokeCap() <= 2); insertInt("LC", fPaint.getStrokeCap()); SK_COMPILE_ASSERT(SkPaint::kMiter_Join == 0, paint_join_mismatch); SK_COMPILE_ASSERT(SkPaint::kRound_Join == 1, paint_join_mismatch); SK_COMPILE_ASSERT(SkPaint::kBevel_Join == 2, paint_join_mismatch); SK_COMPILE_ASSERT(SkPaint::kJoinCount == 3, paint_join_mismatch); SkASSERT(fPaint.getStrokeJoin() >= 0 && fPaint.getStrokeJoin() <= 2); insertInt("LJ", fPaint.getStrokeJoin()); insertScalar("LW", fPaint.getStrokeWidth()); insertScalar("ML", fPaint.getStrokeMiter()); insert("SA", new SkPDFBool(true))->unref(); // Auto stroke adjustment. SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode; // If asMode fails, default to kSrcOver_Mode. if (fPaint.getXfermode()) fPaint.getXfermode()->asMode(&xfermode); // If we don't support the mode, just use kSrcOver_Mode. if (xfermode < 0 || xfermode > SkXfermode::kLastMode || blend_mode_from_xfermode(xfermode) == NULL) { xfermode = SkXfermode::kSrcOver_Mode; NOT_IMPLEMENTED("unsupported xfermode", false); } insertName("BM", blend_mode_from_xfermode(xfermode)); } }
// static SkPDFGraphicState* SkPDFGraphicState::GetSMaskGraphicState( SkPDFFormXObject* sMask, bool invert) { // The practical chances of using the same mask more than once are unlikely // enough that it's not worth canonicalizing. SkAutoMutexAcquire lock(CanonicalPaintsMutex()); SkRefPtr<SkPDFDict> sMaskDict = new SkPDFDict("Mask"); sMaskDict->unref(); // SkRefPtr and new both took a reference. sMaskDict->insertName("S", "Alpha"); sMaskDict->insert("G", new SkPDFObjRef(sMask))->unref(); SkPDFGraphicState* result = new SkPDFGraphicState; result->fPopulated = true; result->fSMask = true; result->insertName("Type", "ExtGState"); result->insert("SMask", sMaskDict.get()); result->fResources.push(sMask); sMask->ref(); if (invert) { SkPDFObject* invertFunction = GetInvertFunction(); result->fResources.push(invertFunction); invertFunction->ref(); sMaskDict->insert("TR", new SkPDFObjRef(invertFunction))->unref(); } return result; }
// static SkPDFObject* SkPDFGraphicState::GetInvertFunction() { // This assumes that canonicalPaintsMutex is held. static SkPDFStream* invertFunction = NULL; if (!invertFunction) { // Acrobat crashes if we use a type 0 function, kpdf crashes if we use // a type 2 function, so we use a type 4 function. SkRefPtr<SkPDFArray> domainAndRange = new SkPDFArray; domainAndRange->unref(); // SkRefPtr and new both took a reference. domainAndRange->reserve(2); domainAndRange->appendInt(0); domainAndRange->appendInt(1); static const char psInvert[] = "{1 exch sub}"; SkRefPtr<SkMemoryStream> psInvertStream = new SkMemoryStream(&psInvert, strlen(psInvert), true); psInvertStream->unref(); // SkRefPtr and new both took a reference. invertFunction = new SkPDFStream(psInvertStream.get()); invertFunction->insertInt("FunctionType", 4); invertFunction->insert("Domain", domainAndRange.get()); invertFunction->insert("Range", domainAndRange.get()); } return invertFunction; }
SkPDFFormXObject::SkPDFFormXObject(SkPDFDevice* device) { // We don't want to keep around device because we'd have two copies // of content, so reference or copy everything we need (content and // resources). device->getResources(&fResources, false); SkRefPtr<SkStream> content = device->content(); content->unref(); // SkRefPtr and content() both took a reference. setData(content.get()); insertName("Type", "XObject"); insertName("Subtype", "Form"); insert("BBox", device->getMediaBox().get()); insert("Resources", device->getResourceDict()); // We invert the initial transform and apply that to the xobject so that // it doesn't get applied twice. We can't just undo it because it's // embedded in things like shaders and images. if (!device->initialTransform().isIdentity()) { SkMatrix inverse; if (!device->initialTransform().invert(&inverse)) { // The initial transform should be invertible. SkASSERT(false); inverse.reset(); } insert("Matrix", SkPDFUtils::MatrixToArray(inverse))->unref(); } // Right now SkPDFFormXObject is only used for saveLayer, which implies // isolated blending. Do this conditionally if that changes. SkRefPtr<SkPDFDict> group = new SkPDFDict("Group"); group->unref(); // SkRefPtr and new both took a reference. group->insertName("S", "Transparency"); group->insert("I", new SkPDFBool(true))->unref(); // Isolated. insert("Group", group.get()); }
void SkPDFPage::finalizePage(SkPDFCatalog* catalog, bool firstPage, SkTDArray<SkPDFObject*>* resourceObjects) { if (fContentStream.get() == NULL) { insert("Resources", fDevice->getResourceDict()); insert("MediaBox", fDevice->getMediaBox().get()); if (!SkToBool(catalog->getDocumentFlags() & SkPDFDocument::kNoLinks_Flags)) { SkRefPtr<SkPDFArray> annots = fDevice->getAnnotations(); if (annots.get() && annots->size() > 0) { insert("Annots", annots.get()); } } SkRefPtr<SkStream> content = fDevice->content(); content->unref(); // SkRefPtr and content() both took a reference. fContentStream = new SkPDFStream(content.get()); fContentStream->unref(); // SkRefPtr and new both took a reference. insert("Contents", new SkPDFObjRef(fContentStream.get()))->unref(); } catalog->addObject(fContentStream.get(), firstPage); fDevice->getResources(resourceObjects, true); }
SkPDFImageShader::SkPDFImageShader(SkPDFShader::State* state) : fState(state) { fState.get()->fImage.lockPixels(); SkMatrix finalMatrix = fState.get()->fCanvasTransform; finalMatrix.preConcat(fState.get()->fShaderTransform); SkRect surfaceBBox; surfaceBBox.set(fState.get()->fBBox); transformBBox(finalMatrix, &surfaceBBox); SkMatrix unflip; unflip.setTranslate(0, SkScalarRound(surfaceBBox.height())); unflip.preScale(SK_Scalar1, -SK_Scalar1); SkISize size = SkISize::Make(SkScalarRound(surfaceBBox.width()), SkScalarRound(surfaceBBox.height())); SkPDFDevice pattern(size, size, unflip); SkCanvas canvas(&pattern); canvas.translate(-surfaceBBox.fLeft, -surfaceBBox.fTop); finalMatrix.preTranslate(surfaceBBox.fLeft, surfaceBBox.fTop); const SkBitmap* image = &fState.get()->fImage; int width = image->width(); int height = image->height(); SkShader::TileMode tileModes[2]; tileModes[0] = fState.get()->fImageTileModes[0]; tileModes[1] = fState.get()->fImageTileModes[1]; canvas.drawBitmap(*image, 0, 0); SkRect patternBBox = SkRect::MakeXYWH(-surfaceBBox.fLeft, -surfaceBBox.fTop, width, height); // Tiling is implied. First we handle mirroring. if (tileModes[0] == SkShader::kMirror_TileMode) { SkMatrix xMirror; xMirror.setScale(-1, 1); xMirror.postTranslate(2 * width, 0); canvas.drawBitmapMatrix(*image, xMirror); patternBBox.fRight += width; } if (tileModes[1] == SkShader::kMirror_TileMode) { SkMatrix yMirror; yMirror.setScale(SK_Scalar1, -SK_Scalar1); yMirror.postTranslate(0, 2 * height); canvas.drawBitmapMatrix(*image, yMirror); patternBBox.fBottom += height; } if (tileModes[0] == SkShader::kMirror_TileMode && tileModes[1] == SkShader::kMirror_TileMode) { SkMatrix mirror; mirror.setScale(-1, -1); mirror.postTranslate(2 * width, 2 * height); canvas.drawBitmapMatrix(*image, mirror); } // Then handle Clamping, which requires expanding the pattern canvas to // cover the entire surfaceBBox. // If both x and y are in clamp mode, we start by filling in the corners. // (Which are just a rectangles of the corner colors.) if (tileModes[0] == SkShader::kClamp_TileMode && tileModes[1] == SkShader::kClamp_TileMode) { SkPaint paint; SkRect rect; rect = SkRect::MakeLTRB(surfaceBBox.fLeft, surfaceBBox.fTop, 0, 0); if (!rect.isEmpty()) { paint.setColor(image->getColor(0, 0)); canvas.drawRect(rect, paint); } rect = SkRect::MakeLTRB(width, surfaceBBox.fTop, surfaceBBox.fRight, 0); if (!rect.isEmpty()) { paint.setColor(image->getColor(width - 1, 0)); canvas.drawRect(rect, paint); } rect = SkRect::MakeLTRB(width, height, surfaceBBox.fRight, surfaceBBox.fBottom); if (!rect.isEmpty()) { paint.setColor(image->getColor(width - 1, height - 1)); canvas.drawRect(rect, paint); } rect = SkRect::MakeLTRB(surfaceBBox.fLeft, height, 0, surfaceBBox.fBottom); if (!rect.isEmpty()) { paint.setColor(image->getColor(0, height - 1)); canvas.drawRect(rect, paint); } } // Then expand the left, right, top, then bottom. if (tileModes[0] == SkShader::kClamp_TileMode) { SkIRect subset = SkIRect::MakeXYWH(0, 0, 1, height); if (surfaceBBox.fLeft < 0) { SkBitmap left; SkAssertResult(image->extractSubset(&left, subset)); SkMatrix leftMatrix; leftMatrix.setScale(-surfaceBBox.fLeft, 1); leftMatrix.postTranslate(surfaceBBox.fLeft, 0); canvas.drawBitmapMatrix(left, leftMatrix); if (tileModes[1] == SkShader::kMirror_TileMode) { leftMatrix.postScale(SK_Scalar1, -SK_Scalar1); leftMatrix.postTranslate(0, 2 * height); canvas.drawBitmapMatrix(left, leftMatrix); } patternBBox.fLeft = 0; } if (surfaceBBox.fRight > width) { SkBitmap right; subset.offset(width - 1, 0); SkAssertResult(image->extractSubset(&right, subset)); SkMatrix rightMatrix; rightMatrix.setScale(surfaceBBox.fRight - width, 1); rightMatrix.postTranslate(width, 0); canvas.drawBitmapMatrix(right, rightMatrix); if (tileModes[1] == SkShader::kMirror_TileMode) { rightMatrix.postScale(SK_Scalar1, -SK_Scalar1); rightMatrix.postTranslate(0, 2 * height); canvas.drawBitmapMatrix(right, rightMatrix); } patternBBox.fRight = surfaceBBox.width(); } } if (tileModes[1] == SkShader::kClamp_TileMode) { SkIRect subset = SkIRect::MakeXYWH(0, 0, width, 1); if (surfaceBBox.fTop < 0) { SkBitmap top; SkAssertResult(image->extractSubset(&top, subset)); SkMatrix topMatrix; topMatrix.setScale(SK_Scalar1, -surfaceBBox.fTop); topMatrix.postTranslate(0, surfaceBBox.fTop); canvas.drawBitmapMatrix(top, topMatrix); if (tileModes[0] == SkShader::kMirror_TileMode) { topMatrix.postScale(-1, 1); topMatrix.postTranslate(2 * width, 0); canvas.drawBitmapMatrix(top, topMatrix); } patternBBox.fTop = 0; } if (surfaceBBox.fBottom > height) { SkBitmap bottom; subset.offset(0, height - 1); SkAssertResult(image->extractSubset(&bottom, subset)); SkMatrix bottomMatrix; bottomMatrix.setScale(SK_Scalar1, surfaceBBox.fBottom - height); bottomMatrix.postTranslate(0, height); canvas.drawBitmapMatrix(bottom, bottomMatrix); if (tileModes[0] == SkShader::kMirror_TileMode) { bottomMatrix.postScale(-1, 1); bottomMatrix.postTranslate(2 * width, 0); canvas.drawBitmapMatrix(bottom, bottomMatrix); } patternBBox.fBottom = surfaceBBox.height(); } } SkRefPtr<SkPDFArray> patternBBoxArray = new SkPDFArray; patternBBoxArray->unref(); // SkRefPtr and new both took a reference. patternBBoxArray->reserve(4); patternBBoxArray->appendScalar(patternBBox.fLeft); patternBBoxArray->appendScalar(patternBBox.fTop); patternBBoxArray->appendScalar(patternBBox.fRight); patternBBoxArray->appendScalar(patternBBox.fBottom); // Put the canvas into the pattern stream (fContent). SkRefPtr<SkStream> content = pattern.content(); content->unref(); // SkRefPtr and content() both took a reference. pattern.getResources(&fResources); setData(content.get()); insertName("Type", "Pattern"); insertInt("PatternType", 1); insertInt("PaintType", 1); insertInt("TilingType", 1); insert("BBox", patternBBoxArray.get()); insertScalar("XStep", patternBBox.width()); insertScalar("YStep", patternBBox.height()); insert("Resources", pattern.getResourceDict()); insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); fState.get()->fImage.unlockPixels(); }
SkPDFFunctionShader::SkPDFFunctionShader(SkPDFShader::State* state) : SkPDFDict("Pattern"), fState(state) { SkString (*codeFunction)(const SkShader::GradientInfo& info) = NULL; SkPoint transformPoints[2]; // Depending on the type of the gradient, we want to transform the // coordinate space in different ways. const SkShader::GradientInfo* info = &fState.get()->fInfo; transformPoints[0] = info->fPoint[0]; transformPoints[1] = info->fPoint[1]; switch (fState.get()->fType) { case SkShader::kLinear_GradientType: codeFunction = &linearCode; break; case SkShader::kRadial_GradientType: transformPoints[1] = transformPoints[0]; transformPoints[1].fX += info->fRadius[0]; codeFunction = &radialCode; break; case SkShader::kRadial2_GradientType: { // Bail out if the radii are the same. Empty fResources signals // an error and isValid will return false. if (info->fRadius[0] == info->fRadius[1]) { return; } transformPoints[1] = transformPoints[0]; SkScalar dr = info->fRadius[1] - info->fRadius[0]; transformPoints[1].fX += dr; codeFunction = &twoPointRadialCode; break; } case SkShader::kSweep_GradientType: transformPoints[1] = transformPoints[0]; transformPoints[1].fX += 1; codeFunction = &sweepCode; break; case SkShader::kColor_GradientType: case SkShader::kNone_GradientType: default: return; } // Move any scaling (assuming a unit gradient) or translation // (and rotation for linear gradient), of the final gradient from // info->fPoints to the matrix (updating bbox appropriately). Now // the gradient can be drawn on on the unit segment. SkMatrix mapperMatrix; unitToPointsMatrix(transformPoints, &mapperMatrix); SkMatrix finalMatrix = fState.get()->fCanvasTransform; finalMatrix.preConcat(mapperMatrix); finalMatrix.preConcat(fState.get()->fShaderTransform); SkRect bbox; bbox.set(fState.get()->fBBox); transformBBox(finalMatrix, &bbox); SkRefPtr<SkPDFArray> domain = new SkPDFArray; domain->unref(); // SkRefPtr and new both took a reference. domain->reserve(4); domain->appendScalar(bbox.fLeft); domain->appendScalar(bbox.fRight); domain->appendScalar(bbox.fTop); domain->appendScalar(bbox.fBottom); SkString functionCode; // The two point radial gradient further references fState.get()->fInfo // in translating from x, y coordinates to the t parameter. So, we have // to transform the points and radii according to the calculated matrix. if (fState.get()->fType == SkShader::kRadial2_GradientType) { SkShader::GradientInfo twoPointRadialInfo = *info; SkMatrix inverseMapperMatrix; mapperMatrix.invert(&inverseMapperMatrix); inverseMapperMatrix.mapPoints(twoPointRadialInfo.fPoint, 2); twoPointRadialInfo.fRadius[0] = inverseMapperMatrix.mapRadius(info->fRadius[0]); twoPointRadialInfo.fRadius[1] = inverseMapperMatrix.mapRadius(info->fRadius[1]); functionCode = codeFunction(twoPointRadialInfo); } else { functionCode = codeFunction(*info); } SkRefPtr<SkPDFStream> function = makePSFunction(functionCode, domain.get()); // Pass one reference to fResources, SkRefPtr and new both took a reference. fResources.push(function.get()); SkRefPtr<SkPDFDict> pdfShader = new SkPDFDict; pdfShader->unref(); // SkRefPtr and new both took a reference. pdfShader->insertInt("ShadingType", 1); pdfShader->insertName("ColorSpace", "DeviceRGB"); pdfShader->insert("Domain", domain.get()); pdfShader->insert("Function", new SkPDFObjRef(function.get()))->unref(); insertInt("PatternType", 2); insert("Matrix", SkPDFUtils::MatrixToArray(finalMatrix))->unref(); insert("Shading", pdfShader.get()); }
// static void SkPDFPage::GeneratePageTree(const SkTDArray<SkPDFPage*>& pages, SkPDFCatalog* catalog, SkTDArray<SkPDFDict*>* pageTree, SkPDFDict** rootNode) { // PDF wants a tree describing all the pages in the document. We arbitrary // choose 8 (kNodeSize) as the number of allowed children. The internal // nodes have type "Pages" with an array of children, a parent pointer, and // the number of leaves below the node as "Count." The leaves are passed // into the method, have type "Page" and need a parent pointer. This method // builds the tree bottom up, skipping internal nodes that would have only // one child. static const int kNodeSize = 8; SkRefPtr<SkPDFName> kidsName = new SkPDFName("Kids"); kidsName->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFName> countName = new SkPDFName("Count"); countName->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFName> parentName = new SkPDFName("Parent"); parentName->unref(); // SkRefPtr and new both took a reference. // curNodes takes a reference to its items, which it passes to pageTree. SkTDArray<SkPDFDict*> curNodes; curNodes.setReserve(pages.count()); for (int i = 0; i < pages.count(); i++) { SkSafeRef(pages[i]); curNodes.push(pages[i]); } // nextRoundNodes passes its references to nodes on to curNodes. SkTDArray<SkPDFDict*> nextRoundNodes; nextRoundNodes.setReserve((pages.count() + kNodeSize - 1)/kNodeSize); int treeCapacity = kNodeSize; do { for (int i = 0; i < curNodes.count(); ) { if (i > 0 && i + 1 == curNodes.count()) { nextRoundNodes.push(curNodes[i]); break; } SkPDFDict* newNode = new SkPDFDict("Pages"); SkRefPtr<SkPDFObjRef> newNodeRef = new SkPDFObjRef(newNode); newNodeRef->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFArray> kids = new SkPDFArray; kids->unref(); // SkRefPtr and new both took a reference. kids->reserve(kNodeSize); int count = 0; for (; i < curNodes.count() && count < kNodeSize; i++, count++) { curNodes[i]->insert(parentName.get(), newNodeRef.get()); kids->append(new SkPDFObjRef(curNodes[i]))->unref(); // TODO(vandebo): put the objects in strict access order. // Probably doesn't matter because they are so small. if (curNodes[i] != pages[0]) { pageTree->push(curNodes[i]); // Transfer reference. catalog->addObject(curNodes[i], false); } else { SkSafeUnref(curNodes[i]); catalog->addObject(curNodes[i], true); } } newNode->insert(kidsName.get(), kids.get()); int pageCount = treeCapacity; if (count < kNodeSize) { pageCount = pages.count() % treeCapacity; } newNode->insert(countName.get(), new SkPDFInt(pageCount))->unref(); nextRoundNodes.push(newNode); // Transfer reference. } curNodes = nextRoundNodes; nextRoundNodes.rewind(); treeCapacity *= kNodeSize; } while (curNodes.count() > 1); pageTree->push(curNodes[0]); // Transfer reference. catalog->addObject(curNodes[0], false); if (rootNode) { *rootNode = curNodes[0]; } }
void SkPDFDevice::updateGSFromPaint(const SkPaint& paint, bool forText) { SkASSERT(paint.getPathEffect() == NULL); NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false); NOT_IMPLEMENTED(paint.getColorFilter() != NULL, false); SkPaint newPaint = paint; // PDF treats a shader as a color, so we only set one or the other. SkRefPtr<SkPDFShader> pdfShader; const SkShader* shader = newPaint.getShader(); if (shader) { // PDF positions patterns relative to the initial transform, so // we need to apply the current transform to the shader parameters. SkMatrix transform = fGraphicStack[fGraphicStackIndex].fTransform; if (fFlipOrigin == kFlip_OriginTransform) { transform.postScale(1, -1); transform.postTranslate(0, fHeight); } // PDF doesn't support kClamp_TileMode, so we simulate it by making // a pattern the size of the drawing service. SkIRect bounds = fGraphicStack[fGraphicStackIndex].fClip.getBounds(); pdfShader = SkPDFShader::getPDFShader(*shader, transform, bounds); SkSafeUnref(pdfShader.get()); // getShader and SkRefPtr both took a ref // A color shader is treated as an invalid shader so we don't have // to set a shader just for a color. if (pdfShader.get() == NULL) { newPaint.setColor(0); // Check for a color shader. SkShader::GradientInfo gradientInfo; SkColor gradientColor; gradientInfo.fColors = &gradientColor; gradientInfo.fColorOffsets = NULL; gradientInfo.fColorCount = 1; if (shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType) { newPaint.setColor(gradientColor); } } } if (pdfShader) { // pdfShader has been canonicalized so we can directly compare // pointers. if (fGraphicStack[fGraphicStackIndex].fShader != pdfShader.get()) { int resourceIndex = fShaderResources.find(pdfShader.get()); if (resourceIndex < 0) { resourceIndex = fShaderResources.count(); fShaderResources.push(pdfShader.get()); pdfShader->ref(); } fContent.writeText("/Pattern CS /Pattern cs /P"); fContent.writeDecAsText(resourceIndex); fContent.writeText(" SCN /P"); fContent.writeDecAsText(resourceIndex); fContent.writeText(" scn\n"); fGraphicStack[fGraphicStackIndex].fShader = pdfShader.get(); } } else { SkColor newColor = newPaint.getColor(); newColor = SkColorSetA(newColor, 0xFF); if (fGraphicStack[fGraphicStackIndex].fShader || fGraphicStack[fGraphicStackIndex].fColor != newColor) { emitPDFColor(newColor, &fContent); fContent.writeText("RG "); emitPDFColor(newColor, &fContent); fContent.writeText("rg\n"); fGraphicStack[fGraphicStackIndex].fColor = newColor; fGraphicStack[fGraphicStackIndex].fShader = NULL; } } SkRefPtr<SkPDFGraphicState> newGraphicState = SkPDFGraphicState::getGraphicStateForPaint(newPaint); newGraphicState->unref(); // getGraphicState and SkRefPtr both took a ref. // newGraphicState has been canonicalized so we can directly compare // pointers. if (fGraphicStack[fGraphicStackIndex].fGraphicState != newGraphicState.get()) { int resourceIndex = fGraphicStateResources.find(newGraphicState.get()); if (resourceIndex < 0) { resourceIndex = fGraphicStateResources.count(); fGraphicStateResources.push(newGraphicState.get()); newGraphicState->ref(); } fContent.writeText("/G"); fContent.writeDecAsText(resourceIndex); fContent.writeText(" gs\n"); fGraphicStack[fGraphicStackIndex].fGraphicState = newGraphicState.get(); } if (forText) { if (fGraphicStack[fGraphicStackIndex].fTextScaleX != newPaint.getTextScaleX()) { SkScalar scale = newPaint.getTextScaleX(); SkScalar pdfScale = SkScalarMul(scale, SkIntToScalar(100)); SkPDFScalar::Append(pdfScale, &fContent); fContent.writeText(" Tz\n"); fGraphicStack[fGraphicStackIndex].fTextScaleX = scale; } if (fGraphicStack[fGraphicStackIndex].fTextFill != newPaint.getStyle()) { SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value); SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1, enum_must_match_value); SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2, enum_must_match_value); fContent.writeDecAsText(newPaint.getStyle()); fContent.writeText(" Tr\n"); fGraphicStack[fGraphicStackIndex].fTextFill = newPaint.getStyle(); } } }
const SkRefPtr<SkPDFDict>& SkPDFDevice::getResourceDict() { if (fResourceDict.get() == NULL) { fResourceDict = new SkPDFDict; fResourceDict->unref(); // SkRefPtr and new both took a reference. if (fGraphicStateResources.count()) { SkRefPtr<SkPDFDict> extGState = new SkPDFDict(); extGState->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fGraphicStateResources.count(); i++) { SkString nameString("G"); nameString.appendS32(i); extGState->insert( nameString.c_str(), new SkPDFObjRef(fGraphicStateResources[i]))->unref(); } fResourceDict->insert("ExtGState", extGState.get()); } if (fXObjectResources.count()) { SkRefPtr<SkPDFDict> xObjects = new SkPDFDict(); xObjects->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fXObjectResources.count(); i++) { SkString nameString("X"); nameString.appendS32(i); xObjects->insert( nameString.c_str(), new SkPDFObjRef(fXObjectResources[i]))->unref(); } fResourceDict->insert("XObject", xObjects.get()); } if (fFontResources.count()) { SkRefPtr<SkPDFDict> fonts = new SkPDFDict(); fonts->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fFontResources.count(); i++) { SkString nameString("F"); nameString.appendS32(i); fonts->insert(nameString.c_str(), new SkPDFObjRef(fFontResources[i]))->unref(); } fResourceDict->insert("Font", fonts.get()); } if (fShaderResources.count()) { SkRefPtr<SkPDFDict> patterns = new SkPDFDict(); patterns->unref(); // SkRefPtr and new both took a reference. for (int i = 0; i < fShaderResources.count(); i++) { SkString nameString("P"); nameString.appendS32(i); patterns->insert(nameString.c_str(), new SkPDFObjRef(fShaderResources[i]))->unref(); } fResourceDict->insert("Pattern", patterns.get()); } // For compatibility, add all proc sets (only used for output to PS // devices). const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; SkRefPtr<SkPDFArray> procSets = new SkPDFArray(); procSets->unref(); // SkRefPtr and new both took a reference. procSets->reserve(SK_ARRAY_COUNT(procs)); for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) procSets->append(new SkPDFName(procs[i]))->unref(); fResourceDict->insert("ProcSet", procSets.get()); } return fResourceDict; }
static void TestPDFStream(skiatest::Reporter* reporter) { char streamBytes[] = "Test\nFoo\tBar"; SkRefPtr<SkMemoryStream> streamData = new SkMemoryStream( streamBytes, strlen(streamBytes), true); streamData->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFStream> stream = new SkPDFStream(streamData.get()); stream->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput( reporter, stream.get(), "<</Length 12\n>> stream\nTest\nFoo\tBar\nendstream"); stream->insert("Attribute", new SkPDFInt(42))->unref(); SimpleCheckObjectOutput(reporter, stream.get(), "<</Length 12\n/Attribute 42\n>> stream\n" "Test\nFoo\tBar\nendstream"); if (SkFlate::HaveFlate()) { char streamBytes2[] = "This is a longer string, so that compression " "can do something with it. With shorter strings, " "the short circuit logic cuts in and we end up " "with an uncompressed string."; SkAutoDataUnref streamData2(SkData::NewWithCopy(streamBytes2, strlen(streamBytes2))); SkRefPtr<SkPDFStream> stream = new SkPDFStream(streamData2.get()); stream->unref(); // SkRefPtr and new both took a reference. SkDynamicMemoryWStream compressedByteStream; SkFlate::Deflate(streamData2.get(), &compressedByteStream); SkAutoDataUnref compressedData(compressedByteStream.copyToData()); // Check first without compression. SkDynamicMemoryWStream expectedResult1; expectedResult1.writeText("<</Length 167\n>> stream\n"); expectedResult1.writeText(streamBytes2); expectedResult1.writeText("\nendstream"); SkAutoDataUnref expectedResultData1(expectedResult1.copyToData()); CheckObjectOutput(reporter, stream.get(), (const char*) expectedResultData1->data(), expectedResultData1->size(), true, false); // Then again with compression. SkDynamicMemoryWStream expectedResult2; expectedResult2.writeText("<</Filter /FlateDecode\n/Length 116\n" ">> stream\n"); expectedResult2.write(compressedData->data(), compressedData->size()); expectedResult2.writeText("\nendstream"); SkAutoDataUnref expectedResultData2(expectedResult2.copyToData()); CheckObjectOutput(reporter, stream.get(), (const char*) expectedResultData2->data(), expectedResultData2->size(), true, true); } }
static void TestPDFPrimitives(skiatest::Reporter* reporter) { SkRefPtr<SkPDFInt> int42 = new SkPDFInt(42); int42->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, int42.get(), "42"); SkRefPtr<SkPDFScalar> realHalf = new SkPDFScalar(SK_ScalarHalf); realHalf->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, realHalf.get(), "0.5"); #if defined(SK_SCALAR_IS_FLOAT) SkRefPtr<SkPDFScalar> bigScalar = new SkPDFScalar(110999.75f); bigScalar->unref(); // SkRefPtr and new both took a reference. #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) SimpleCheckObjectOutput(reporter, bigScalar.get(), "111000"); #else SimpleCheckObjectOutput(reporter, bigScalar.get(), "110999.75"); SkRefPtr<SkPDFScalar> biggerScalar = new SkPDFScalar(50000000.1); biggerScalar->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, biggerScalar.get(), "50000000"); SkRefPtr<SkPDFScalar> smallestScalar = new SkPDFScalar(1.0/65536); smallestScalar->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, smallestScalar.get(), "0.00001526"); #endif #endif SkRefPtr<SkPDFString> stringSimple = new SkPDFString("test ) string ( foo"); stringSimple->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, stringSimple.get(), "(test \\) string \\( foo)"); SkRefPtr<SkPDFString> stringComplex = new SkPDFString("\ttest ) string ( foo"); stringComplex->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, stringComplex.get(), "<0974657374202920737472696E67202820666F6F>"); SkRefPtr<SkPDFName> name = new SkPDFName("Test name\twith#tab"); name->unref(); // SkRefPtr and new both took a reference. const char expectedResult[] = "/Test#20name#09with#23tab"; CheckObjectOutput(reporter, name.get(), expectedResult, strlen(expectedResult), false, false); SkRefPtr<SkPDFName> escapedName = new SkPDFName("A#/%()<>[]{}B"); escapedName->unref(); // SkRefPtr and new both took a reference. const char escapedNameExpected[] = "/A#23#2F#25#28#29#3C#3E#5B#5D#7B#7DB"; CheckObjectOutput(reporter, escapedName.get(), escapedNameExpected, strlen(escapedNameExpected), false, false); // Test that we correctly handle characters with the high-bit set. const unsigned char highBitCString[] = {0xDE, 0xAD, 'b', 'e', 0xEF, 0}; SkRefPtr<SkPDFName> highBitName = new SkPDFName((const char*)highBitCString); highBitName->unref(); // SkRefPtr and new both took a reference. const char highBitExpectedResult[] = "/#DE#ADbe#EF"; CheckObjectOutput(reporter, highBitName.get(), highBitExpectedResult, strlen(highBitExpectedResult), false, false); SkRefPtr<SkPDFArray> array = new SkPDFArray; array->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, array.get(), "[]"); array->append(int42.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42]"); array->append(realHalf.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5]"); SkRefPtr<SkPDFInt> int0 = new SkPDFInt(0); int0->unref(); // SkRefPtr and new both took a reference. array->append(int0.get()); SimpleCheckObjectOutput(reporter, array.get(), "[42 0.5 0]"); SkRefPtr<SkPDFInt> int1 = new SkPDFInt(1); int1->unref(); // SkRefPtr and new both took a reference. array->setAt(0, int1.get()); SimpleCheckObjectOutput(reporter, array.get(), "[1 0.5 0]"); SkRefPtr<SkPDFDict> dict = new SkPDFDict; dict->unref(); // SkRefPtr and new both took a reference. SimpleCheckObjectOutput(reporter, dict.get(), "<<>>"); SkRefPtr<SkPDFName> n1 = new SkPDFName("n1"); n1->unref(); // SkRefPtr and new both took a reference. dict->insert(n1.get(), int42.get()); SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n>>"); SkRefPtr<SkPDFName> n2 = new SkPDFName("n2"); n2->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFName> n3 = new SkPDFName("n3"); n3->unref(); // SkRefPtr and new both took a reference. dict->insert(n2.get(), realHalf.get()); dict->insert(n3.get(), array.get()); SimpleCheckObjectOutput(reporter, dict.get(), "<</n1 42\n/n2 0.5\n/n3 [1 0.5 0]\n>>"); TestPDFStream(reporter); TestCatalog(reporter); TestObjectRef(reporter); TestSubstitute(reporter); }
static void TestSubstitute(skiatest::Reporter* reporter) { SkRefPtr<SkPDFTestDict> proxy = new SkPDFTestDict(); proxy->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFTestDict> stub = new SkPDFTestDict(); stub->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFInt> int33 = new SkPDFInt(33); int33->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFDict> stubResource = new SkPDFDict(); stubResource->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFInt> int44 = new SkPDFInt(44); int44->unref(); // SkRefPtr and new both took a reference. stub->insert("Value", int33.get()); stubResource->insert("InnerValue", int44.get()); stub->addResource(stubResource.get()); SkPDFCatalog catalog((SkPDFDocument::Flags)0); catalog.addObject(proxy.get(), false); catalog.setSubstitute(proxy.get(), stub.get()); SkDynamicMemoryWStream buffer; proxy->emit(&buffer, &catalog, false); catalog.emitSubstituteResources(&buffer, false); char objectResult[] = "2 0 obj\n<</Value 33\n>>\nendobj\n"; REPORTER_ASSERT( reporter, catalog.setFileOffset(proxy.get(), 0) == strlen(objectResult)); char expectedResult[] = "<</Value 33\n>>1 0 obj\n<</InnerValue 44\n>>\nendobj\n"; REPORTER_ASSERT(reporter, buffer.getOffset() == strlen(expectedResult)); REPORTER_ASSERT(reporter, stream_equals(buffer, 0, expectedResult, buffer.getOffset())); }
SkPDFImage::SkPDFImage(const SkBitmap& bitmap, const SkPaint& paint) { SkBitmap::Config config = bitmap.getConfig(); // TODO(vandebo) Handle alpha and alpha only images correctly. SkASSERT(config == SkBitmap::kRGB_565_Config || config == SkBitmap::kARGB_4444_Config || config == SkBitmap::kARGB_8888_Config || config == SkBitmap::kIndex8_Config || config == SkBitmap::kRLE_Index8_Config); SkMemoryStream* image_data = extractImageData(bitmap); SkAutoUnref image_data_unref(image_data); fStream = new SkPDFStream(image_data); fStream->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFName> typeValue = new SkPDFName("XObject"); typeValue->unref(); // SkRefPtr and new both took a reference. insert("Type", typeValue.get()); SkRefPtr<SkPDFName> subTypeValue = new SkPDFName("Image"); subTypeValue->unref(); // SkRefPtr and new both took a reference. insert("Subtype", subTypeValue.get()); SkRefPtr<SkPDFInt> widthValue = new SkPDFInt(bitmap.width()); widthValue->unref(); // SkRefPtr and new both took a reference. insert("Width", widthValue.get()); SkRefPtr<SkPDFInt> heightValue = new SkPDFInt(bitmap.height()); heightValue->unref(); // SkRefPtr and new both took a reference. insert("Height", heightValue.get()); // if (!image mask) { SkRefPtr<SkPDFObject> colorSpaceValue; if (config == SkBitmap::kIndex8_Config || config == SkBitmap::kRLE_Index8_Config) { colorSpaceValue = makeIndexedColorSpace(bitmap.getColorTable()); } else { colorSpaceValue = new SkPDFName("DeviceRGB"); } colorSpaceValue->unref(); // SkRefPtr and new both took a reference. insert("ColorSpace", colorSpaceValue.get()); // } int bitsPerComp = bitmap.bytesPerPixel() * 2; if (bitsPerComp == 0) { SkASSERT(config == SkBitmap::kA1_Config); bitsPerComp = 1; } else if (bitsPerComp == 2 || (bitsPerComp == 4 && config == SkBitmap::kRGB_565_Config)) { bitsPerComp = 8; } SkRefPtr<SkPDFInt> bitsPerCompValue = new SkPDFInt(bitsPerComp); bitsPerCompValue->unref(); // SkRefPtr and new both took a reference. insert("BitsPerComponent", bitsPerCompValue.get()); if (config == SkBitmap::kRGB_565_Config) { SkRefPtr<SkPDFInt> zeroVal = new SkPDFInt(0); zeroVal->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFScalar> scale5Val = new SkPDFScalar(8.2258); // 255/2^5-1 scale5Val->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFScalar> scale6Val = new SkPDFScalar(4.0476); // 255/2^6-1 scale6Val->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFArray> decodeValue = new SkPDFArray(); decodeValue->unref(); // SkRefPtr and new both took a reference. decodeValue->reserve(6); decodeValue->append(zeroVal.get()); decodeValue->append(scale5Val.get()); decodeValue->append(zeroVal.get()); decodeValue->append(scale6Val.get()); decodeValue->append(zeroVal.get()); decodeValue->append(scale5Val.get()); insert("Decode", decodeValue.get()); } }
// populateDict and operator== have to stay in sync with each other. void SkPDFGraphicState::populateDict() { if (!fPopulated) { fPopulated = true; SkRefPtr<SkPDFName> typeName = new SkPDFName("ExtGState"); typeName->unref(); // SkRefPtr and new both took a reference. insert("Type", typeName.get()); SkScalar maxAlpha = SkIntToScalar(0xFF); SkRefPtr<SkPDFScalar> alpha = new SkPDFScalar(SkColorGetA(fPaint.getColor())/maxAlpha); alpha->unref(); // SkRefPtr and new both took a reference. insert("CA", alpha.get()); insert("ca", alpha.get()); SkASSERT(SkPaint::kButt_Cap == 0); SkASSERT(SkPaint::kRound_Cap == 1); SkASSERT(SkPaint::kSquare_Cap == 2); SkASSERT(fPaint.getStrokeCap() >= 0 && fPaint.getStrokeCap() <= 2); SkRefPtr<SkPDFInt> strokeCap = new SkPDFInt(fPaint.getStrokeCap()); strokeCap->unref(); // SkRefPtr and new both took a reference. insert("LC", strokeCap.get()); SkASSERT(SkPaint::kMiter_Join == 0); SkASSERT(SkPaint::kRound_Join == 1); SkASSERT(SkPaint::kBevel_Join == 2); SkASSERT(fPaint.getStrokeJoin() >= 0 && fPaint.getStrokeJoin() <= 2); SkRefPtr<SkPDFInt> strokeJoin = new SkPDFInt(fPaint.getStrokeJoin()); strokeJoin->unref(); // SkRefPtr and new both took a reference. insert("LJ", strokeJoin.get()); /* TODO(vandebo) Font. if (fPaint.getTypeFace() != NULL) { SkRefPtr<SkPDFTypeFace> typeFace = SkPDFTypeFace::getFontForTypeFace(fPaint.getTypeFace); SkRefPtr<SkPDFObjRef> typeFaceRef = new SkPDFObjRef(typeFace.get()); fontRef->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFScalar> fontSize = new SkPDFScalar(fPaint.getTetSize()); fontSize->unref(); // SkRefPtr and new both took a reference. SkRefPtr<SkPDFArray> font = new SkPDFArray(); font->unref(); // SkRefPtr and new both took a reference. font->reserve(2); font->append(typeFaceRef.get()); font->append(fontSize.get()); insert("LJ", font.get()); } */ SkRefPtr<SkPDFScalar> strokeWidth = new SkPDFScalar(fPaint.getStrokeWidth()); strokeWidth->unref(); // SkRefPtr and new both took a reference. insert("LW", strokeWidth.get()); SkRefPtr<SkPDFScalar> strokeMiterLimit = new SkPDFScalar( fPaint.getStrokeMiter()); strokeMiterLimit->unref(); // SkRefPtr and new both took a reference. insert("ML", strokeWidth.get()); // Turn on automatic stroke adjustment. SkRefPtr<SkPDFBool> trueVal = new SkPDFBool(true); trueVal->unref(); // SkRefPtr and new both took a reference. insert("SA", trueVal.get()); } }