Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/metafile_skia_wrapper.h" |
| 6 | #include "skia/ext/platform_device.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 7 | #include "skia/ext/refptr.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include "third_party/skia/include/core/SkCanvas.h" |
| 9 | #include "third_party/skia/include/core/SkDevice.h" |
| 10 | #include "third_party/skia/include/core/SkMetaData.h" |
| 11 | |
| 12 | namespace printing { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | const char* kMetafileKey = "CrMetafile"; |
| 17 | const char* kCustomScaleKey = "CrCustomScale"; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | // static |
| 22 | void MetafileSkiaWrapper::SetMetafileOnCanvas(const SkCanvas& canvas, |
| 23 | Metafile* metafile) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 24 | skia::RefPtr<MetafileSkiaWrapper> wrapper; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | if (metafile) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 26 | wrapper = skia::AdoptRef(new MetafileSkiaWrapper(metafile)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | |
| 28 | SkMetaData& meta = skia::getMetaData(canvas); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 29 | meta.setRefCnt(kMetafileKey, wrapper.get()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // static |
| 33 | Metafile* MetafileSkiaWrapper::GetMetafileFromCanvas(const SkCanvas& canvas) { |
| 34 | SkMetaData& meta = skia::getMetaData(canvas); |
| 35 | SkRefCnt* value; |
| 36 | if (!meta.findRefCnt(kMetafileKey, &value) || !value) |
| 37 | return NULL; |
| 38 | |
| 39 | return static_cast<MetafileSkiaWrapper*>(value)->metafile_; |
| 40 | } |
| 41 | |
| 42 | // static |
| 43 | void MetafileSkiaWrapper::SetCustomScaleOnCanvas(const SkCanvas& canvas, |
| 44 | double scale) { |
| 45 | SkMetaData& meta = skia::getMetaData(canvas); |
| 46 | meta.setScalar(kCustomScaleKey, SkFloatToScalar(scale)); |
| 47 | } |
| 48 | |
| 49 | // static |
| 50 | bool MetafileSkiaWrapper::GetCustomScaleOnCanvas(const SkCanvas& canvas, |
| 51 | double* scale) { |
| 52 | SkMetaData& meta = skia::getMetaData(canvas); |
| 53 | SkScalar value; |
| 54 | if (!meta.findScalar(kCustomScaleKey, &value)) |
| 55 | return false; |
| 56 | |
| 57 | *scale = SkScalarToFloat(value); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | MetafileSkiaWrapper::MetafileSkiaWrapper(Metafile* metafile) |
| 62 | : metafile_(metafile) { |
| 63 | } |
| 64 | |
| 65 | } // namespace printing |