metafile_skia_wrapper.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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,
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                              PdfMetafileSkia* 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
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciPdfMetafileSkia* MetafileSkiaWrapper::GetMetafileFromCanvas(
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const SkCanvas& canvas) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkRefCnt* value;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!meta.findRefCnt(kMetafileKey, &value) || !value)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return static_cast<MetafileSkiaWrapper*>(value)->metafile_;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetafileSkiaWrapper::SetCustomScaleOnCanvas(const SkCanvas& canvas,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 double scale) {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  meta.setScalar(kCustomScaleKey, SkFloatToScalar(scale));
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool MetafileSkiaWrapper::GetCustomScaleOnCanvas(const SkCanvas& canvas,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                 double* scale) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkScalar value;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!meta.findScalar(kCustomScaleKey, &value))
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return false;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  *scale = SkScalarToFloat(value);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciMetafileSkiaWrapper::MetafileSkiaWrapper(PdfMetafileSkia* metafile)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : metafile_(metafile) {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace printing
67