2014-06-18 21:32:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-10-26 12:23:53 +00:00
|
|
|
#include "SkBitmap.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
2014-10-26 12:23:53 +00:00
|
|
|
#include "SkData.h"
|
2015-01-08 02:04:45 +00:00
|
|
|
#include "SkImageGenerator.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkOSFile.h"
|
2015-04-30 21:12:58 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypeface.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
|
|
|
|
|
|
|
|
SkString GetResourcePath(const char* resource) {
|
2014-07-29 02:26:58 +00:00
|
|
|
return SkOSPath::Join(FLAGS_resourcePath[0], resource);
|
2014-06-18 21:32:48 +00:00
|
|
|
}
|
2014-07-11 19:14:51 +00:00
|
|
|
|
|
|
|
void SetResourcePath(const char* resource) {
|
|
|
|
FLAGS_resourcePath.set(0, resource);
|
|
|
|
}
|
2014-10-26 12:23:53 +00:00
|
|
|
|
|
|
|
bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) {
|
|
|
|
SkString resourcePath = GetResourcePath(resource);
|
2015-04-30 21:12:58 +00:00
|
|
|
SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str()));
|
2015-01-08 02:04:45 +00:00
|
|
|
return resourceData && SkInstallDiscardablePixelRef(resourceData, dst);
|
2014-10-26 12:23:53 +00:00
|
|
|
}
|
2015-04-30 21:12:58 +00:00
|
|
|
|
|
|
|
SkStreamAsset* GetResourceAsStream(const char* resource) {
|
|
|
|
SkString resourcePath = GetResourcePath(resource);
|
|
|
|
SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
|
|
|
|
if (stream->isValid()) {
|
|
|
|
return stream.detach();
|
|
|
|
} else {
|
|
|
|
SkDebugf("Resource %s not found.\n", resource);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface* GetResourceAsTypeface(const char* resource) {
|
|
|
|
SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource));
|
|
|
|
if (!stream) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-01 19:51:23 +00:00
|
|
|
return SkTypeface::CreateFromStream(stream.detach());
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|