OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "DMSrcSink.h" | 8 #include "DMSrcSink.h" |
9 #include "Resources.h" | 9 #include "Resources.h" |
10 #include "SkAndroidCodec.h" | 10 #include "SkAndroidCodec.h" |
(...skipping 1080 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } | 1091 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } |
1092 | 1092 |
1093 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ | 1093 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~*/ |
1094 #if defined(SK_XML) | 1094 #if defined(SK_XML) |
1095 // Used when the image doesn't have an intrinsic size. | 1095 // Used when the image doesn't have an intrinsic size. |
1096 static const SkSize kDefaultSVGSize = SkSize::Make(1000, 1000); | 1096 static const SkSize kDefaultSVGSize = SkSize::Make(1000, 1000); |
1097 | 1097 |
1098 // Used to force-scale tiny fixed-size images. | 1098 // Used to force-scale tiny fixed-size images. |
1099 static const SkSize kMinimumSVGSize = SkSize::Make(128, 128); | 1099 static const SkSize kMinimumSVGSize = SkSize::Make(128, 128); |
1100 | 1100 |
1101 SVGSrc::SVGSrc(Path path) : fPath(path), fScale(1) {} | 1101 SVGSrc::SVGSrc(Path path) |
| 1102 : fName(SkOSPath::Basename(path.c_str())) |
| 1103 , fScale(1) { |
1102 | 1104 |
1103 Error SVGSrc::ensureDom() const { | 1105 SkFILEStream stream(path.c_str()); |
| 1106 if (!stream.isValid()) { |
| 1107 return; |
| 1108 } |
| 1109 fDom = SkSVGDOM::MakeFromStream(stream); |
| 1110 if (!fDom) { |
| 1111 return; |
| 1112 } |
| 1113 |
| 1114 const SkSize& sz = fDom->containerSize(); |
| 1115 if (sz.isEmpty()) { |
| 1116 // no intrinsic size |
| 1117 fDom->setContainerSize(kDefaultSVGSize); |
| 1118 } else { |
| 1119 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), |
| 1120 kMinimumSVGSize.height() / sz.height())); |
| 1121 } |
| 1122 } |
| 1123 |
| 1124 Error SVGSrc::draw(SkCanvas* canvas) const { |
1104 if (!fDom) { | 1125 if (!fDom) { |
1105 SkFILEStream stream(fPath.c_str()); | 1126 return SkStringPrintf("Unable to parse file: %s", fName.c_str()); |
1106 if (!stream.isValid()) { | 1127 } |
1107 return SkStringPrintf("Unable to open file: %s", fPath.c_str()); | |
1108 } | |
1109 fDom = SkSVGDOM::MakeFromStream(stream); | |
1110 if (!fDom) { | |
1111 return SkStringPrintf("Unable to parse file: %s", fPath.c_str()); | |
1112 } | |
1113 | 1128 |
1114 const SkSize& sz = fDom->containerSize(); | 1129 SkAutoCanvasRestore acr(canvas, true); |
1115 if (sz.isEmpty()) { | 1130 canvas->scale(fScale, fScale); |
1116 // no intrinsic size | 1131 fDom->render(canvas); |
1117 fDom->setContainerSize(kDefaultSVGSize); | |
1118 } else { | |
1119 fScale = SkTMax(1.f, SkTMax(kMinimumSVGSize.width() / sz.width(), | |
1120 kMinimumSVGSize.height() / sz.height()))
; | |
1121 } | |
1122 } | |
1123 | 1132 |
1124 return ""; | 1133 return ""; |
1125 } | 1134 } |
1126 | 1135 |
1127 Error SVGSrc::draw(SkCanvas* canvas) const { | |
1128 Error err = this->ensureDom(); | |
1129 if (err.isEmpty()) { | |
1130 SkAutoCanvasRestore acr(canvas, true); | |
1131 canvas->scale(fScale, fScale); | |
1132 fDom->render(canvas); | |
1133 } | |
1134 | |
1135 return err; | |
1136 } | |
1137 | |
1138 SkISize SVGSrc::size() const { | 1136 SkISize SVGSrc::size() const { |
1139 Error err = this->ensureDom(); | 1137 if (!fDom) { |
1140 if (!err.isEmpty()) { | |
1141 return SkISize::Make(0, 0); | 1138 return SkISize::Make(0, 0); |
1142 } | 1139 } |
1143 | 1140 |
1144 return SkSize::Make(fDom->containerSize().width() * fScale, | 1141 return SkSize::Make(fDom->containerSize().width() * fScale, |
1145 fDom->containerSize().height() * fScale).toRound(); | 1142 fDom->containerSize().height() * fScale).toRound(); |
1146 } | 1143 } |
1147 | 1144 |
1148 Name SVGSrc::name() const { return SkOSPath::Basename(fPath.c_str()); } | 1145 Name SVGSrc::name() const { return fName; } |
1149 | 1146 |
1150 bool SVGSrc::veto(SinkFlags flags) const { | 1147 bool SVGSrc::veto(SinkFlags flags) const { |
1151 // No need to test to non-(raster||gpu) or indirect backends. | 1148 // No need to test to non-(raster||gpu) or indirect backends. |
1152 bool type_ok = flags.type == SinkFlags::kRaster | 1149 bool type_ok = flags.type == SinkFlags::kRaster |
1153 || flags.type == SinkFlags::kGPU; | 1150 || flags.type == SinkFlags::kGPU; |
1154 | 1151 |
1155 return !type_ok || flags.approach != SinkFlags::kDirect; | 1152 return !type_ok || flags.approach != SinkFlags::kDirect; |
1156 } | 1153 } |
1157 | 1154 |
1158 #endif // defined(SK_XML) | 1155 #endif // defined(SK_XML) |
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1757 Error err = src.draw(&rec); | 1754 Error err = src.draw(&rec); |
1758 if (!err.isEmpty()) { | 1755 if (!err.isEmpty()) { |
1759 return err; | 1756 return err; |
1760 } | 1757 } |
1761 dl->draw(canvas); | 1758 dl->draw(canvas); |
1762 return check_against_reference(bitmap, src, fSink.get()); | 1759 return check_against_reference(bitmap, src, fSink.get()); |
1763 }); | 1760 }); |
1764 } | 1761 } |
1765 | 1762 |
1766 } // namespace DM | 1763 } // namespace DM |
OLD | NEW |