skia2/xcode/hostapp/test.cpp
reed@android.com 76aa34bf8e add text to hostapp sample
rename duplicate of SkSVGPath.cpp



git-svn-id: http://skia.googlecode.com/svn/trunk@45 2bbb7eff-a529-9590-31e7-b0007b416f81
2008-12-23 01:27:39 +00:00

66 lines
1.8 KiB
C++

#include <Carbon/Carbon.h>
#include "SkCGUtils.h"
#include "SkCanvas.h"
#include "SkPaint.h"
extern "C" void SkiaDraw(CGContextRef cg, CGRect bounds);
static void sampleDraw(SkCanvas* canvas) {
canvas->drawColor(0xFF0000FF);
SkPaint paint;
paint.setAntiAlias(true);
canvas->drawCircle(SkIntToScalar(100), SkIntToScalar(100),
SkIntToScalar(90), paint);
const char text[] = "fry42";
const size_t len = strlen(text);
paint.setColor(SK_ColorWHITE);
paint.setTextSize(SkIntToScalar(50));
canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(50), paint);
paint.setTextAlign(SkPaint::kCenter_Align);
canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(100), paint);
paint.setTextAlign(SkPaint::kRight_Align);
canvas->drawText(text, len, SkIntToScalar(100), SkIntToScalar(150), paint);
}
static CGImageRef gImage;
void SkiaDraw(CGContextRef cg, CGRect bounds) {
if (NULL == gImage) {
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 640, 480);
bitmap.allocPixels();
SkCanvas canvas(bitmap);
sampleDraw(&canvas);
gImage = SkCreateCGImageRef(bitmap);
}
float components[] = { 1, 1, 1, 1 };
CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetFillColorWithColor(cg, color);
CGColorRelease(color);
CGColorSpaceRelease(colorspace);
CGContextFillRect(cg, bounds);
CGRect r = CGRectMake(0, 0, 640, 480);
CGContextSaveGState(cg);
CGContextTranslateCTM(cg, 0, r.size.height);
CGContextScaleCTM(cg, 1, -1);
CGContextDrawImage(cg, r, gImage);
CGContextRestoreGState(cg);
}