metafile_skia_wrapper.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "printing/metafile_skia_wrapper.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "skia/ext/platform_device.h"
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "skia/ext/refptr.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkCanvas.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkDevice.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkMetaData.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace printing {
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kMetafileKey = "CrMetafile";
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kCustomScaleKey = "CrCustomScale";
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetafileSkiaWrapper::SetMetafileOnCanvas(const SkCanvas& canvas,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              Metafile* metafile) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  skia::RefPtr<MetafileSkiaWrapper> wrapper;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (metafile)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    wrapper = skia::AdoptRef(new MetafileSkiaWrapper(metafile));
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  meta.setRefCnt(kMetafileKey, wrapper.get());
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Metafile* MetafileSkiaWrapper::GetMetafileFromCanvas(const SkCanvas& canvas) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkRefCnt* value;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!meta.findRefCnt(kMetafileKey, &value) || !value)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<MetafileSkiaWrapper*>(value)->metafile_;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetafileSkiaWrapper::SetCustomScaleOnCanvas(const SkCanvas& canvas,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 double scale) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  meta.setScalar(kCustomScaleKey, SkFloatToScalar(scale));
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MetafileSkiaWrapper::GetCustomScaleOnCanvas(const SkCanvas& canvas,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 double* scale) {
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkScalar value;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!meta.findScalar(kCustomScaleKey, &value))
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *scale = SkScalarToFloat(value);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)MetafileSkiaWrapper::MetafileSkiaWrapper(Metafile* metafile)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : metafile_(metafile) {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace printing
66