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 2010 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2021 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID )); | 2021 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID )); |
2022 int resourceIndex = fFontResources.find(newFont.get()); | 2022 int resourceIndex = fFontResources.find(newFont.get()); |
2023 if (resourceIndex < 0) { | 2023 if (resourceIndex < 0) { |
2024 resourceIndex = fFontResources.count(); | 2024 resourceIndex = fFontResources.count(); |
2025 fFontResources.push(newFont.get()); | 2025 fFontResources.push(newFont.get()); |
2026 newFont.get()->ref(); | 2026 newFont.get()->ref(); |
2027 } | 2027 } |
2028 return resourceIndex; | 2028 return resourceIndex; |
2029 } | 2029 } |
2030 | 2030 |
2031 void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix, | 2031 static void setup_transparent_bitmap(SkBitmap* bitmap, int width, int height) { |
2032 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
2033 bitmap->allocPixels(); | |
2034 bitmap->eraseColor(SK_ColorTRANSPARENT); | |
2035 } | |
2036 | |
2037 void SkPDFDevice::internalDrawBitmap(const SkMatrix& origMatrix, | |
2032 const SkClipStack* clipStack, | 2038 const SkClipStack* clipStack, |
2033 const SkRegion& clipRegion, | 2039 const SkRegion& origClipRegion, |
2034 const SkBitmap& bitmap, | 2040 const SkBitmap& origBitmap2, |
2035 const SkIRect* srcRect, | 2041 const SkIRect* origSrcRect, |
2036 const SkPaint& paint) { | 2042 const SkPaint& paint) { |
2037 // TODO(edisonn): Perspective matrix support implemented here | 2043 SkMatrix matrix = origMatrix; |
2044 SkRegion all; | |
2045 const SkRegion* clipRegion = &origClipRegion; | |
2046 SkBitmap origBitmap; | |
2047 SkBitmap bitmap = origBitmap; | |
2048 const SkIRect* srcRect = origSrcRect; | |
2049 | |
2050 // Raster the bitmap using perspective in a new bitmap. | |
2051 if (matrix.hasPerspective()) { | |
2052 if (origSrcRect) { | |
2053 if (!origBitmap2.extractSubset(&origBitmap, *origSrcRect)) { | |
2054 return; | |
2055 } | |
2056 } else { | |
2057 origBitmap = origBitmap2; | |
2058 } | |
2059 srcRect = NULL; | |
2060 | |
2061 // Transform the bitmap in the new space. | |
2062 SkPath path; | |
2063 path.addRect(SkRect::MakeWH(SkIntToScalar(origBitmap.width()), | |
2064 SkIntToScalar(origBitmap.height()))); | |
2065 path.transform(matrix); | |
2066 | |
2067 // Retrieve the bounds of the new shape. | |
2068 SkRect bounds = path.getBounds(); | |
vandebo (ex-Chrome)
2013/10/15 21:41:32
Should this be further constrained by the current
edisonn
2013/10/16 15:08:03
This is an optimization. I will add a todo. in a n
| |
2069 | |
2070 // TODO(edisonn): add DPI settings. Currently 1 pixel/point, which does not look great, | |
vandebo (ex-Chrome)
2013/10/15 21:41:32
nit: 80 cols
edisonn
2013/10/16 15:08:03
this file is already 100c
vandebo (ex-Chrome)
2013/10/16 16:46:41
Hardly, 31/2073 lines and most of those are over b
edisonn
2013/10/16 18:28:51
Done.
| |
2071 // but it is not producing large PDFs. | |
2072 | |
2073 // Create transparent bitmap. | |
2074 setup_transparent_bitmap(&bitmap, bounds.width(), bounds.height()); | |
2075 | |
2076 SkAutoTUnref<SkBitmapDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)) ); | |
2077 SkCanvas canvas(device); | |
2078 | |
2079 // Find where (0,0) gets mapped in the new space. | |
2080 SkPoint zeroZero = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0)); | |
vandebo (ex-Chrome)
2013/10/15 21:41:32
zeroZero isn't used.
edisonn
2013/10/16 15:08:03
Done.
| |
2081 matrix.mapPoints(&zeroZero, 1); | |
2082 | |
2083 SkScalar deltaX = bounds.left(); | |
2084 SkScalar deltaY = bounds.top(); | |
2085 | |
2086 SkMatrix matrix2 = matrix; | |
2087 matrix2.postTranslate(-deltaX, -deltaY); | |
2088 | |
2089 // Translate the draw in the new canvas, so we perfectly fit the shape i n the bitmap. | |
2090 canvas.setMatrix(matrix2); | |
2091 | |
2092 canvas.drawBitmap(origBitmap, SkIntToScalar(0), SkIntToScalar(0)); | |
2093 | |
2094 // Make sure the final bits ar in the bitmap. | |
vandebo (ex-Chrome)
2013/10/15 21:41:32
ar -> are
edisonn
2013/10/16 15:08:03
Done.
| |
2095 canvas.flush(); | |
2096 | |
2097 // In the new space, we use the identity matrix translated. | |
2098 matrix.setTranslate(deltaX, deltaY); | |
2099 all.setRect(SkIRect::MakeXYWH(SkScalarToFixed(bounds.x()), | |
2100 SkScalarToFixed(bounds.y()), | |
2101 SkScalarToFixed(bounds.width()), | |
2102 SkScalarToFixed(bounds.height()))); | |
2103 clipRegion = &all; | |
2104 srcRect = NULL; | |
2105 } | |
2106 | |
2038 SkMatrix scaled; | 2107 SkMatrix scaled; |
2039 // Adjust for origin flip. | 2108 // Adjust for origin flip. |
2040 scaled.setScale(SK_Scalar1, -SK_Scalar1); | 2109 scaled.setScale(SK_Scalar1, -SK_Scalar1); |
2041 scaled.postTranslate(0, SK_Scalar1); | 2110 scaled.postTranslate(0, SK_Scalar1); |
2042 // Scale the image up from 1x1 to WxH. | 2111 // Scale the image up from 1x1 to WxH. |
2043 SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height()); | 2112 SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
2044 scaled.postScale(SkIntToScalar(subset.width()), | 2113 scaled.postScale(SkIntToScalar(subset.width()), |
2045 SkIntToScalar(subset.height())); | 2114 SkIntToScalar(subset.height())); |
2046 scaled.postConcat(matrix); | 2115 scaled.postConcat(matrix); |
2047 ScopedContentEntry content(this, clipStack, clipRegion, scaled, paint); | 2116 |
2117 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint); | |
2048 if (!content.entry()) { | 2118 if (!content.entry()) { |
2049 return; | 2119 return; |
2050 } | 2120 } |
2051 | 2121 |
2052 if (srcRect && !subset.intersect(*srcRect)) { | 2122 if (srcRect && !subset.intersect(*srcRect)) { |
2053 return; | 2123 return; |
2054 } | 2124 } |
2055 | 2125 |
2056 SkPDFImage* image = SkPDFImage::CreateImage(bitmap, subset, fEncoder); | 2126 SkPDFImage* image = SkPDFImage::CreateImage(bitmap, subset, fEncoder); |
2057 if (!image) { | 2127 if (!image) { |
2058 return; | 2128 return; |
2059 } | 2129 } |
2060 | 2130 |
2061 fXObjectResources.push(image); // Transfer reference. | 2131 fXObjectResources.push(image); // Transfer reference. |
2062 SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1, | 2132 SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1, |
2063 &content.entry()->fContent); | 2133 &content.entry()->fContent); |
2064 } | 2134 } |
2065 | 2135 |
2066 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, | 2136 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, |
2067 SkCanvas::Config8888) { | 2137 SkCanvas::Config8888) { |
2068 return false; | 2138 return false; |
2069 } | 2139 } |
2070 | 2140 |
2071 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { | 2141 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { |
2072 return false; | 2142 return false; |
2073 } | 2143 } |
OLD | NEW |