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 2016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2027 } | 2027 } |
2028 return resourceIndex; | 2028 return resourceIndex; |
2029 } | 2029 } |
2030 | 2030 |
2031 void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix, | 2031 void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix, |
2032 const SkClipStack* clipStack, | 2032 const SkClipStack* clipStack, |
2033 const SkRegion& clipRegion, | 2033 const SkRegion& clipRegion, |
2034 const SkBitmap& bitmap, | 2034 const SkBitmap& bitmap, |
2035 const SkIRect* srcRect, | 2035 const SkIRect* srcRect, |
2036 const SkPaint& paint) { | 2036 const SkPaint& paint) { |
| 2037 // TODO(edisonn): Perspective matrix support implemented here |
2037 SkMatrix scaled; | 2038 SkMatrix scaled; |
2038 // Adjust for origin flip. | 2039 // Adjust for origin flip. |
2039 scaled.setScale(SK_Scalar1, -SK_Scalar1); | 2040 scaled.setScale(SK_Scalar1, -SK_Scalar1); |
2040 scaled.postTranslate(0, SK_Scalar1); | 2041 scaled.postTranslate(0, SK_Scalar1); |
2041 // Scale the image up from 1x1 to WxH. | 2042 // Scale the image up from 1x1 to WxH. |
2042 SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height()); | 2043 SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
2043 scaled.postScale(SkIntToScalar(subset.width()), | 2044 scaled.postScale(SkIntToScalar(subset.width()), |
2044 SkIntToScalar(subset.height())); | 2045 SkIntToScalar(subset.height())); |
2045 scaled.postConcat(matrix); | 2046 scaled.postConcat(matrix); |
2046 ScopedContentEntry content(this, clipStack, clipRegion, scaled, paint); | 2047 ScopedContentEntry content(this, clipStack, clipRegion, scaled, paint); |
(...skipping 16 matching lines...) Expand all Loading... |
2063 } | 2064 } |
2064 | 2065 |
2065 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, | 2066 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, |
2066 SkCanvas::Config8888) { | 2067 SkCanvas::Config8888) { |
2067 return false; | 2068 return false; |
2068 } | 2069 } |
2069 | 2070 |
2070 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { | 2071 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { |
2071 return false; | 2072 return false; |
2072 } | 2073 } |
| 2074 |
| 2075 static SkISize SkSizeToISize(const SkSize& size) { |
| 2076 return SkISize::Make(SkScalarRoundToInt(size.width()), SkScalarRoundToInt(si
ze.height())); |
| 2077 } |
| 2078 |
| 2079 static SkMatrix buildMatrixToMapTrimBox00(const SkRect& content) { |
| 2080 SkMatrix matrix; |
| 2081 matrix.setTranslate(-content.left(), -content.top()); |
| 2082 return matrix; |
| 2083 } |
| 2084 |
| 2085 SkPDFDeviceFlattener::SkPDFDeviceFlattener(const SkSize& trimBox, const SkRect&
content) |
| 2086 : SkPDFDevice(SkSizeToISize(trimBox), |
| 2087 SkSizeToISize(SkSize::Make(content.width(), content.he
ight())), |
| 2088 buildMatrixToMapTrimBox00(content)) { |
| 2089 } |
| 2090 |
| 2091 SkPDFDeviceFlattener::~SkPDFDeviceFlattener() { |
| 2092 } |
| 2093 |
| 2094 void SkPDFDeviceFlattener::drawPaint(const SkDraw& d, const SkPaint& paint) { |
| 2095 INHERITED::drawPaint(d, paint); |
| 2096 } |
| 2097 |
| 2098 void SkPDFDeviceFlattener::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, |
| 2099 size_t count, const SkPoint points[], |
| 2100 const SkPaint& paint) { |
| 2101 if (mustFlatten(d)) { |
| 2102 SkPoint* flattenedPoints = SkNEW_ARRAY(SkPoint, count); |
| 2103 d.fMatrix->mapPoints(flattenedPoints, points, count); |
| 2104 SkDraw draw(d); |
| 2105 SkMatrix identity = SkMatrix::I(); |
| 2106 draw.fMatrix = &identity; |
| 2107 INHERITED::drawPoints(draw, mode, count, flattenedPoints, paint); |
| 2108 SkDELETE_ARRAY(flattenedPoints); |
| 2109 return; |
| 2110 } |
| 2111 |
| 2112 INHERITED::drawPoints(d, mode, count, points, paint); |
| 2113 } |
| 2114 |
| 2115 void SkPDFDeviceFlattener::drawRect(const SkDraw& d, const SkRect& r, const SkPa
int& paint) { |
| 2116 if (mustFlatten(d)) { |
| 2117 SkPath path; |
| 2118 path.addRect(r); |
| 2119 path.transform(*d.fMatrix); |
| 2120 SkDraw draw(d); |
| 2121 SkMatrix matrix = SkMatrix::I(); |
| 2122 draw.fMatrix = &matrix; |
| 2123 |
| 2124 SkPaint paintFlatten = paint; |
| 2125 if (paintFlatten.getShader()) { |
| 2126 SkMatrix local = paintFlatten.getShader()->getLocalMatrix(); |
| 2127 local.preConcat(*d.fMatrix); |
| 2128 paintFlatten.getShader()->setLocalMatrix(local); |
| 2129 } |
| 2130 |
| 2131 INHERITED::drawPath(draw, path, paintFlatten, NULL, true); |
| 2132 return; |
| 2133 } |
| 2134 |
| 2135 INHERITED::drawRect(d, r, paint); |
| 2136 } |
| 2137 |
| 2138 void SkPDFDeviceFlattener::drawPath(const SkDraw& d, const SkPath& origpath, |
| 2139 const SkPaint& paint, const SkMatrix* prePat
hMatrix, |
| 2140 bool pathIsMutable) { |
| 2141 if (mustFlatten(d) || (prePathMatrix && prePathMatrix->hasPerspective())) { |
| 2142 SkPath path; |
| 2143 path.addPath(origpath); |
| 2144 if (prePathMatrix) { |
| 2145 path.transform(*prePathMatrix); |
| 2146 } |
| 2147 path.transform(*d.fMatrix); |
| 2148 SkDraw draw(d); |
| 2149 SkMatrix matrix = SkMatrix::I(); |
| 2150 draw.fMatrix = &matrix; |
| 2151 |
| 2152 SkPaint paintFlatten = paint; |
| 2153 if (paintFlatten.getShader()) { |
| 2154 SkMatrix local = *d.fMatrix; |
| 2155 local.preConcat(paintFlatten.getShader()->getLocalMatrix()); |
| 2156 |
| 2157 paintFlatten.getShader()->setLocalMatrix(local); |
| 2158 } |
| 2159 |
| 2160 INHERITED::drawPath(draw, path, paintFlatten, NULL, true); |
| 2161 return; |
| 2162 } |
| 2163 |
| 2164 INHERITED::drawPath(d, origpath, paint, prePathMatrix, pathIsMutable); |
| 2165 } |
| 2166 |
| 2167 void SkPDFDeviceFlattener::drawText(const SkDraw& d, const void* text, size_t le
n, |
| 2168 SkScalar x, SkScalar y, const SkPaint& paint
) { |
| 2169 if (mustPathText(d, paint)) { |
| 2170 d.drawText_asPaths((const char*)text, len, x, y, paint); |
| 2171 return; |
| 2172 } |
| 2173 |
| 2174 INHERITED::drawText(d, text, len, x, y, paint); |
| 2175 } |
| 2176 |
| 2177 void SkPDFDeviceFlattener::drawPosText(const SkDraw& d, const void* text, size_t
len, |
| 2178 const SkScalar pos[], SkScalar constY, |
| 2179 int scalarsPerPos, const SkPaint& paint)
{ |
| 2180 if (mustPathText(d, paint)) { |
| 2181 d.drawPosText_asPaths((const char*)text, len, pos, constY, scalarsPerPos
, paint); |
| 2182 return; |
| 2183 } |
| 2184 INHERITED::drawPosText(d, text, len,pos, constY,scalarsPerPos, paint); |
| 2185 } |
| 2186 |
| 2187 void SkPDFDeviceFlattener::drawTextOnPath(const SkDraw& d, const void* text, siz
e_t len, |
| 2188 const SkPath& path, const SkMatrix* ma
trix, |
| 2189 const SkPaint& paint) { |
| 2190 if (mustPathText(d, paint) || (matrix && matrix->hasPerspective())) { |
| 2191 d.drawTextOnPath((const char*)text, len, path, matrix, paint); |
| 2192 return; |
| 2193 } |
| 2194 INHERITED::drawTextOnPath(d, text, len, path, matrix, paint); |
| 2195 } |
| 2196 |
| 2197 void SkPDFDeviceFlattener::drawVertices(const SkDraw& d, SkCanvas::VertexMode mo
de, |
| 2198 int vertexCount, const SkPoint verts[], |
| 2199 const SkPoint texs[], const SkColor colo
rs[], |
| 2200 SkXfermode* xmode, const uint16_t indice
s[], |
| 2201 int indexCount, const SkPaint& paint) { |
| 2202 INHERITED::drawVertices(d, mode, vertexCount, verts, texs, colors, xmode, in
dices, indexCount, |
| 2203 paint); |
| 2204 } |
| 2205 |
| 2206 void SkPDFDeviceFlattener::drawDevice(const SkDraw& d, SkBaseDevice* dev, int x,
int y, |
| 2207 const SkPaint& paint) { |
| 2208 INHERITED::drawDevice(d, dev, x, y, paint); |
| 2209 } |
| 2210 |
| 2211 bool SkPDFDeviceFlattener::mustFlatten(const SkDraw& d) const { |
| 2212 // TODO(edisonn): testability, add flag to force return true. |
| 2213 return d.fMatrix->hasPerspective(); |
| 2214 } |
| 2215 |
| 2216 bool SkPDFDeviceFlattener::mustPathText(const SkDraw& d, const SkPaint&) { |
| 2217 // TODO(edisonn): testability, add flag to force return true. |
| 2218 // TODO(edisonn): TBD: How to flatten MaskFilter. |
| 2219 return d.fMatrix->hasPerspective(); |
| 2220 } |
OLD | NEW |