SampleStrokeText.cpp revision 1cab2921ab279367f8206cdadc9259d12e603548
11cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger 21cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger/* 31cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * Copyright 2011 Google Inc. 41cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * 51cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * Use of this source code is governed by a BSD-style license that can be 61cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * found in the LICENSE file. 71cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger */ 80b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SampleCode.h" 90b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkView.h" 100b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkCanvas.h" 110b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "Sk64.h" 120b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkGradientShader.h" 130b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkGraphics.h" 140b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkImageDecoder.h" 150b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkKernel33MaskFilter.h" 160b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkPath.h" 170b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkRandom.h" 180b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkRegion.h" 190b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkShader.h" 200b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkUtils.h" 210b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkColorPriv.h" 220b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkColorFilter.h" 230b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkTime.h" 240b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkTypeface.h" 250b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkXfermode.h" 260b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 270b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic void lettersToBitmap(SkBitmap* dst, const char chars[], 280b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger const SkPaint& original, SkBitmap::Config config) { 290b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPath path; 300b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar x = 0; 310b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar width; 320b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPath p; 330b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger for (size_t i = 0; i < strlen(chars); i++) { 340b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger original.getTextPath(&chars[i], 1, x, 0, &p); 350b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger path.addPath(p); 360b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger original.getTextWidths(&chars[i], 1, &width); 370b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger x += width; 380b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 390b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkRect bounds = path.getBounds(); 400b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar sw = -original.getStrokeWidth(); 410b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger bounds.inset(sw, sw); 420b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger path.offset(-bounds.fLeft, -bounds.fTop); 430b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger bounds.offset(-bounds.fLeft, -bounds.fTop); 440b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 450b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger int w = SkScalarRound(bounds.width()); 460b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger int h = SkScalarRound(bounds.height()); 470b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPaint paint(original); 480b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkBitmap src; 490b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger src.setConfig(config, w, h); 500b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger src.allocPixels(); 510b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger src.eraseColor(0); 520b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger { 530b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkCanvas canvas(src); 540b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setAntiAlias(true); 550b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setColor(SK_ColorBLACK); 560b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setStyle(SkPaint::kFill_Style); 570b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger canvas.drawPath(path, paint); 580b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 590b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 600b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->setConfig(config, w, h); 610b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->allocPixels(); 620b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->eraseColor(SK_ColorWHITE); 630b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger { 640b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkCanvas canvas(*dst); 650b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setXfermodeMode(SkXfermode::kDstATop_Mode); 660b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger canvas.drawBitmap(src, 0, 0, &paint); 670b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setColor(original.getColor()); 680b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setStyle(SkPaint::kStroke_Style); 690b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger canvas.drawPath(path, paint); 700b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 710b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger} 720b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 730b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic void lettersToBitmap2(SkBitmap* dst, const char chars[], 740b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger const SkPaint& original, SkBitmap::Config config) { 750b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPath path; 760b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar x = 0; 770b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar width; 780b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPath p; 790b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger for (size_t i = 0; i < strlen(chars); i++) { 800b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger original.getTextPath(&chars[i], 1, x, 0, &p); 810b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger path.addPath(p); 820b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger original.getTextWidths(&chars[i], 1, &width); 830b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger x += width; 840b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 850b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkRect bounds = path.getBounds(); 860b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkScalar sw = -original.getStrokeWidth(); 870b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger bounds.inset(sw, sw); 880b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger path.offset(-bounds.fLeft, -bounds.fTop); 890b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger bounds.offset(-bounds.fLeft, -bounds.fTop); 900b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 910b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger int w = SkScalarRound(bounds.width()); 920b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger int h = SkScalarRound(bounds.height()); 930b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPaint paint(original); 940b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 950b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setAntiAlias(true); 960b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setXfermodeMode(SkXfermode::kDstATop_Mode); 970b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setColor(original.getColor()); 980b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setStyle(SkPaint::kStroke_Style); 990b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1000b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->setConfig(config, w, h); 1010b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->allocPixels(); 1020b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger dst->eraseColor(SK_ColorWHITE); 1030b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1040b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkCanvas canvas(*dst); 1050b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger canvas.drawPath(path, paint); 1060b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger} 1070b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1080b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerclass StrokeTextView : public SampleView { 1090b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger bool fAA; 1100b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerpublic: 1110b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger StrokeTextView() : fAA(false) { 1120b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger this->setBGColor(0xFFCC8844); 1130b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 1140b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1150b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerprotected: 1160b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger // overrides from SkEventSink 1170b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger virtual bool onQuery(SkEvent* evt) { 1180b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger if (SampleCode::TitleQ(*evt)) { 1190b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SampleCode::TitleR(evt, "StrokeText"); 1200b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger return true; 1210b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 1220b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger return this->INHERITED::onQuery(evt); 1230b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 1240b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1250b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger virtual void onDrawContent(SkCanvas* canvas) { 1260b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkBitmap bm; 1270b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger SkPaint paint; 1280b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1290b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setStrokeWidth(SkIntToScalar(6)); 1300b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger paint.setTextSize(SkIntToScalar(80)); 1310b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger// paint.setTypeface(Typeface.DEFAULT_BOLD); 1320b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1330b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger lettersToBitmap(&bm, "Test Case", paint, SkBitmap::kARGB_4444_Config); 1340b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1350b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger canvas->drawBitmap(bm, 0, 0); 1360b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger } 1370b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1380b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerprivate: 1390b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1400b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger typedef SampleView INHERITED; 1410b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger}; 1420b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1430b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger////////////////////////////////////////////////////////////////////////////// 1440b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 1450b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic SkView* MyFactory() { return new StrokeTextView; } 1460b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic SkViewRegister reg(MyFactory); 1470b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger 148