metafile_skia_wrapper.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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"
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkCanvas.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkDevice.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkMetaData.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace printing {
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kMetafileKey = "CrMetafile";
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char* kCustomScaleKey = "CrCustomScale";
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void MetafileSkiaWrapper::SetMetafileOnCanvas(const SkCanvas& canvas,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                              Metafile* metafile) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MetafileSkiaWrapper* wrapper = NULL;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (metafile)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    wrapper = new MetafileSkiaWrapper(metafile);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkMetaData& meta = skia::getMetaData(canvas);
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  meta.setRefCnt(kMetafileKey, wrapper);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SkSafeUnref(wrapper);
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