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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "SkPDFGraphicState.h" | 22 #include "SkPDFGraphicState.h" |
23 #include "SkPDFImage.h" | 23 #include "SkPDFImage.h" |
24 #include "SkPDFResourceDict.h" | 24 #include "SkPDFResourceDict.h" |
25 #include "SkPDFShader.h" | 25 #include "SkPDFShader.h" |
26 #include "SkPDFStream.h" | 26 #include "SkPDFStream.h" |
27 #include "SkPDFTypes.h" | 27 #include "SkPDFTypes.h" |
28 #include "SkPDFUtils.h" | 28 #include "SkPDFUtils.h" |
29 #include "SkRect.h" | 29 #include "SkRect.h" |
30 #include "SkRRect.h" | 30 #include "SkRRect.h" |
31 #include "SkString.h" | 31 #include "SkString.h" |
| 32 #include "SkSurface.h" |
32 #include "SkTextFormatParams.h" | 33 #include "SkTextFormatParams.h" |
33 #include "SkTemplates.h" | 34 #include "SkTemplates.h" |
34 #include "SkTypefacePriv.h" | 35 #include "SkTypefacePriv.h" |
35 #include "SkTSet.h" | 36 #include "SkTSet.h" |
36 | 37 |
37 #ifdef SK_BUILD_FOR_ANDROID | 38 #ifdef SK_BUILD_FOR_ANDROID |
38 #include "SkTypeface_android.h" | 39 #include "SkTypeface_android.h" |
39 | 40 |
40 struct TypefaceFallbackData { | 41 struct TypefaceFallbackData { |
41 SkTypeface* typeface; | 42 SkTypeface* typeface; |
(...skipping 653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 paint.getXfermode()->asMode(&fXfermode); | 696 paint.getXfermode()->asMode(&fXfermode); |
696 } | 697 } |
697 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion, | 698 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion, |
698 matrix, paint, hasText, | 699 matrix, paint, hasText, |
699 &fDstFormXObject); | 700 &fDstFormXObject); |
700 } | 701 } |
701 }; | 702 }; |
702 | 703 |
703 //////////////////////////////////////////////////////////////////////////////// | 704 //////////////////////////////////////////////////////////////////////////////// |
704 | 705 |
705 static inline SkBitmap makeContentBitmap(const SkISize& contentSize, | 706 static inline SkImageInfo make_content_info(const SkISize& contentSize, |
706 const SkMatrix* initialTransform) { | 707 const SkMatrix* initialTransform) { |
707 SkImageInfo info; | 708 SkImageInfo info; |
708 if (initialTransform) { | 709 if (initialTransform) { |
709 // Compute the size of the drawing area. | 710 // Compute the size of the drawing area. |
710 SkVector drawingSize; | 711 SkVector drawingSize; |
711 SkMatrix inverse; | 712 SkMatrix inverse; |
712 drawingSize.set(SkIntToScalar(contentSize.fWidth), | 713 drawingSize.set(SkIntToScalar(contentSize.fWidth), |
713 SkIntToScalar(contentSize.fHeight)); | 714 SkIntToScalar(contentSize.fHeight)); |
714 if (!initialTransform->invert(&inverse)) { | 715 if (!initialTransform->invert(&inverse)) { |
715 // This shouldn't happen, initial transform should be invertible. | 716 // This shouldn't happen, initial transform should be invertible. |
716 SkASSERT(false); | 717 SkASSERT(false); |
717 inverse.reset(); | 718 inverse.reset(); |
718 } | 719 } |
719 inverse.mapVectors(&drawingSize, 1); | 720 inverse.mapVectors(&drawingSize, 1); |
720 SkISize size = SkSize::Make(drawingSize.fX, drawingSize.fY).toRound(); | 721 SkISize size = SkSize::Make(drawingSize.fX, drawingSize.fY).toRound(); |
721 info = SkImageInfo::MakeUnknown(abs(size.fWidth), abs(size.fHeight)); | 722 info = SkImageInfo::MakeUnknown(abs(size.fWidth), abs(size.fHeight)); |
722 } else { | 723 } else { |
723 info = SkImageInfo::MakeUnknown(abs(contentSize.fWidth), | 724 info = SkImageInfo::MakeUnknown(abs(contentSize.fWidth), |
724 abs(contentSize.fHeight)); | 725 abs(contentSize.fHeight)); |
725 } | 726 } |
726 | 727 return info; |
727 SkBitmap bitmap; | |
728 bitmap.setInfo(info); | |
729 return bitmap; | |
730 } | 728 } |
731 | 729 |
732 // TODO(vandebo) change pageSize to SkSize. | 730 // TODO(vandebo) change pageSize to SkSize. |
733 // TODO: inherit from SkBaseDevice instead of SkBitmapDevice | |
734 SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, | 731 SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, |
735 const SkMatrix& initialTransform) | 732 const SkMatrix& initialTransform) |
736 : SkBitmapDevice(makeContentBitmap(contentSize, &initialTransform)), | 733 : fPageSize(pageSize) |
737 fPageSize(pageSize), | 734 , fContentSize(contentSize) |
738 fContentSize(contentSize), | 735 , fLastContentEntry(NULL) |
739 fLastContentEntry(NULL), | 736 , fLastMarginContentEntry(NULL) |
740 fLastMarginContentEntry(NULL), | 737 , fClipStack(NULL) |
741 fClipStack(NULL), | 738 , fEncoder(NULL) |
742 fEncoder(NULL), | 739 , fRasterDpi(72.0f) |
743 fRasterDpi(72.0f) { | 740 { |
| 741 const SkImageInfo info = make_content_info(contentSize, &initialTransform); |
| 742 |
744 // Just report that PDF does not supports perspective in the | 743 // Just report that PDF does not supports perspective in the |
745 // initial transform. | 744 // initial transform. |
746 NOT_IMPLEMENTED(initialTransform.hasPerspective(), true); | 745 NOT_IMPLEMENTED(initialTransform.hasPerspective(), true); |
747 | 746 |
748 // Skia generally uses the top left as the origin but PDF natively has the | 747 // Skia generally uses the top left as the origin but PDF natively has the |
749 // origin at the bottom left. This matrix corrects for that. But that only | 748 // origin at the bottom left. This matrix corrects for that. But that only |
750 // needs to be done once, we don't do it when layering. | 749 // needs to be done once, we don't do it when layering. |
751 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight)); | 750 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight)); |
752 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); | 751 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); |
753 fInitialTransform.preConcat(initialTransform); | 752 fInitialTransform.preConcat(initialTransform); |
| 753 fLegacyBitmap.setInfo(info); |
754 | 754 |
755 SkIRect existingClip = SkIRect::MakeWH(this->width(), this->height()); | 755 SkIRect existingClip = SkIRect::MakeWH(info.width(), info.height()); |
756 fExistingClipRegion.setRect(existingClip); | 756 fExistingClipRegion.setRect(existingClip); |
757 | |
758 this->init(); | 757 this->init(); |
759 } | 758 } |
760 | 759 |
761 // TODO(vandebo) change layerSize to SkSize. | 760 // TODO(vandebo) change layerSize to SkSize. |
762 SkPDFDevice::SkPDFDevice(const SkISize& layerSize, | 761 SkPDFDevice::SkPDFDevice(const SkISize& layerSize, |
763 const SkClipStack& existingClipStack, | 762 const SkClipStack& existingClipStack, |
764 const SkRegion& existingClipRegion) | 763 const SkRegion& existingClipRegion) |
765 : SkBitmapDevice(makeContentBitmap(layerSize, NULL)), | 764 : fPageSize(layerSize) |
766 fPageSize(layerSize), | 765 , fContentSize(layerSize) |
767 fContentSize(layerSize), | 766 , fExistingClipStack(existingClipStack) |
768 fExistingClipStack(existingClipStack), | 767 , fExistingClipRegion(existingClipRegion) |
769 fExistingClipRegion(existingClipRegion), | 768 , fLastContentEntry(NULL) |
770 fLastContentEntry(NULL), | 769 , fLastMarginContentEntry(NULL) |
771 fLastMarginContentEntry(NULL), | 770 , fClipStack(NULL) |
772 fClipStack(NULL), | 771 , fEncoder(NULL) |
773 fEncoder(NULL), | 772 , fRasterDpi(72.0f) |
774 fRasterDpi(72.0f) { | 773 { |
775 fInitialTransform.reset(); | 774 fInitialTransform.reset(); |
| 775 fLegacyBitmap.setInfo(make_content_info(layerSize, NULL)); |
| 776 |
776 this->init(); | 777 this->init(); |
777 } | 778 } |
778 | 779 |
779 SkPDFDevice::~SkPDFDevice() { | 780 SkPDFDevice::~SkPDFDevice() { |
780 this->cleanUp(true); | 781 this->cleanUp(true); |
781 } | 782 } |
782 | 783 |
783 void SkPDFDevice::init() { | 784 void SkPDFDevice::init() { |
784 fAnnotations = NULL; | 785 fAnnotations = NULL; |
785 fResourceDict = NULL; | 786 fResourceDict = NULL; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
954 | 955 |
955 ScopedContentEntry content(this, d, paint); | 956 ScopedContentEntry content(this, d, paint); |
956 if (!content.entry()) { | 957 if (!content.entry()) { |
957 return; | 958 return; |
958 } | 959 } |
959 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent); | 960 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent); |
960 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, | 961 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
961 &content.entry()->fContent); | 962 &content.entry()->fContent); |
962 } | 963 } |
963 | 964 |
964 void SkPDFDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, | 965 void SkPDFDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect, const SkPa
int& paint) { |
965 const SkPaint& paint) { | |
966 SkPath path; | 966 SkPath path; |
967 path.addRRect(rrect); | 967 path.addRRect(rrect); |
968 this->drawPath(draw, path, paint, NULL, true); | 968 this->drawPath(draw, path, paint, NULL, true); |
969 } | 969 } |
970 | 970 |
| 971 void SkPDFDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint
& paint) { |
| 972 SkPath path; |
| 973 path.addOval(oval); |
| 974 this->drawPath(draw, path, paint, NULL, true); |
| 975 } |
| 976 |
971 void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& origPath, | 977 void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& origPath, |
972 const SkPaint& paint, const SkMatrix* prePathMatrix, | 978 const SkPaint& paint, const SkMatrix* prePathMatrix, |
973 bool pathIsMutable) { | 979 bool pathIsMutable) { |
974 SkPath modifiedPath; | 980 SkPath modifiedPath; |
975 SkPath* pathPtr = const_cast<SkPath*>(&origPath); | 981 SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
976 | 982 |
977 SkMatrix matrix = *d.fMatrix; | 983 SkMatrix matrix = *d.fMatrix; |
978 if (prePathMatrix) { | 984 if (prePathMatrix) { |
979 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { | 985 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
980 if (!pathIsMutable) { | 986 if (!pathIsMutable) { |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1357 } | 1363 } |
1358 | 1364 |
1359 SkAutoTUnref<SkPDFFormXObject> xObject(new SkPDFFormXObject(pdfDevice)); | 1365 SkAutoTUnref<SkPDFFormXObject> xObject(new SkPDFFormXObject(pdfDevice)); |
1360 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), | 1366 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), |
1361 &content.entry()->fContent); | 1367 &content.entry()->fContent); |
1362 | 1368 |
1363 // Merge glyph sets from the drawn device. | 1369 // Merge glyph sets from the drawn device. |
1364 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage()); | 1370 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage()); |
1365 } | 1371 } |
1366 | 1372 |
| 1373 SkImageInfo SkPDFDevice::imageInfo() const { |
| 1374 return fLegacyBitmap.info(); |
| 1375 } |
| 1376 |
1367 void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) { | 1377 void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) { |
1368 INHERITED::onAttachToCanvas(canvas); | 1378 INHERITED::onAttachToCanvas(canvas); |
1369 | 1379 |
1370 // Canvas promises that this ptr is valid until onDetachFromCanvas is called | 1380 // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
1371 fClipStack = canvas->getClipStack(); | 1381 fClipStack = canvas->getClipStack(); |
1372 } | 1382 } |
1373 | 1383 |
1374 void SkPDFDevice::onDetachFromCanvas() { | 1384 void SkPDFDevice::onDetachFromCanvas() { |
1375 INHERITED::onDetachFromCanvas(); | 1385 INHERITED::onDetachFromCanvas(); |
1376 | 1386 |
1377 fClipStack = NULL; | 1387 fClipStack = NULL; |
1378 } | 1388 } |
1379 | 1389 |
| 1390 SkSurface* SkPDFDevice::newSurface(const SkImageInfo& info) { |
| 1391 return SkSurface::NewRaster(info); |
| 1392 } |
| 1393 |
1380 ContentEntry* SkPDFDevice::getLastContentEntry() { | 1394 ContentEntry* SkPDFDevice::getLastContentEntry() { |
1381 if (fDrawingArea == kContent_DrawingArea) { | 1395 if (fDrawingArea == kContent_DrawingArea) { |
1382 return fLastContentEntry; | 1396 return fLastContentEntry; |
1383 } else { | 1397 } else { |
1384 return fLastMarginContentEntry; | 1398 return fLastMarginContentEntry; |
1385 } | 1399 } |
1386 } | 1400 } |
1387 | 1401 |
1388 SkAutoTDelete<ContentEntry>* SkPDFDevice::getContentEntries() { | 1402 SkAutoTDelete<ContentEntry>* SkPDFDevice::getContentEntries() { |
1389 if (fDrawingArea == kContent_DrawingArea) { | 1403 if (fDrawingArea == kContent_DrawingArea) { |
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2230 // the image. Avoiding alpha will reduce the pdf size and generation | 2244 // the image. Avoiding alpha will reduce the pdf size and generation |
2231 // CPU time some. | 2245 // CPU time some. |
2232 | 2246 |
2233 const int w = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().w
idth()); | 2247 const int w = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().w
idth()); |
2234 const int h = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().h
eight()); | 2248 const int h = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().h
eight()); |
2235 if (!perspectiveBitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) { | 2249 if (!perspectiveBitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) { |
2236 return; | 2250 return; |
2237 } | 2251 } |
2238 perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT); | 2252 perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT); |
2239 | 2253 |
2240 SkBitmapDevice device(perspectiveBitmap); | 2254 SkCanvas canvas(perspectiveBitmap); |
2241 SkCanvas canvas(&device); | |
2242 | 2255 |
2243 SkScalar deltaX = bounds.left(); | 2256 SkScalar deltaX = bounds.left(); |
2244 SkScalar deltaY = bounds.top(); | 2257 SkScalar deltaY = bounds.top(); |
2245 | 2258 |
2246 SkMatrix offsetMatrix = origMatrix; | 2259 SkMatrix offsetMatrix = origMatrix; |
2247 offsetMatrix.postTranslate(-deltaX, -deltaY); | 2260 offsetMatrix.postTranslate(-deltaX, -deltaY); |
2248 offsetMatrix.postScale(scaleX, scaleY); | 2261 offsetMatrix.postScale(scaleX, scaleY); |
2249 | 2262 |
2250 // Translate the draw in the new canvas, so we perfectly fit the | 2263 // Translate the draw in the new canvas, so we perfectly fit the |
2251 // shape in the bitmap. | 2264 // shape in the bitmap. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2298 SkAutoTUnref<SkPDFImage> image( | 2311 SkAutoTUnref<SkPDFImage> image( |
2299 SkPDFImage::CreateImage(*bitmap, subset, fEncoder)); | 2312 SkPDFImage::CreateImage(*bitmap, subset, fEncoder)); |
2300 if (!image) { | 2313 if (!image) { |
2301 return; | 2314 return; |
2302 } | 2315 } |
2303 | 2316 |
2304 SkPDFUtils::DrawFormXObject(this->addXObjectResource(image.get()), | 2317 SkPDFUtils::DrawFormXObject(this->addXObjectResource(image.get()), |
2305 &content.entry()->fContent); | 2318 &content.entry()->fContent); |
2306 } | 2319 } |
2307 | 2320 |
2308 bool SkPDFDevice::allowImageFilter(const SkImageFilter*) { | |
2309 return false; | |
2310 } | |
OLD | NEW |