SampleMeasure.cpp revision 99ae881a7f55a03c2b16fb5704e6e90ea86d965d
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc. 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SampleCode.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkView.h" 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h" 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGradientShader.h" 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h" 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h" 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h" 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "Sk1DPathEffect.h" 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCornerPathEffect.h" 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h" 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRandom.h" 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h" 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorFilter.h" 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDither.h" 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// exercise scale/linear/devkern 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct Setting { 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool fLinearText; 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool fDevKernText; 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const Setting gSettings[] = { 3199ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { false, false }, 3299ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { false, true }, 3399ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { true, false }, 3499ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { true, true }, 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3781e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.comstatic void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) { 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar dy = paint.getFontMetrics(NULL); 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t len = strlen(text); 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoTMalloc<SkScalar> autoWidths(len); 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar* widths = autoWidths.get(); 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoTMalloc<SkRect> autoRects(len); 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect* rects = autoRects.get(); 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect bounds; 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint p(paint); 4864cc579efa7e416c7298ed159d76b074b283c0f9senorblanco@chromium.org for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) { 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setLinearText(gSettings[i].fLinearText); 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setDevKernText(gSettings[i].fDevKernText); 51ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int n = p.getTextWidths(text, len, widths, rects); 5399ae881a7f55a03c2b16fb5704e6e90ea86d965dreed SkScalar w = p.measureText(text, len, &bounds); 54ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStyle(SkPaint::kFill_Style); 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0x8888FF88); 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(bounds, p); 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFF000000); 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawText(text, len, 0, 0, p); 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStyle(SkPaint::kStroke_Style); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStrokeWidth(0); 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFFFF0000); 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = 0; 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int j = 0; j < n; j++) { 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect r = rects[j]; 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r.offset(x, 0); 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(r, p); 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x += widths[j]; 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFF0000FF); 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawLine(0, 0, w, 0, p); 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStrokeWidth(SkIntToScalar(4)); 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawPoint(x, 0, p); 76ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->translate(0, dy); 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8181e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.comclass MeasureView : public SampleView { 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint fPaint; 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 85ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com MeasureView() { 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPaint.setAntiAlias(true); 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPaint.setTextSize(SkIntToScalar(64)); 8881e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com this->setBGColor(0xFFDDDDDD); 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected: 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // overrides from SkEventSink 9381e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com virtual bool onQuery(SkEvent* evt) { 9481e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com if (SampleCode::TitleQ(*evt)) { 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SampleCode::TitleR(evt, "Measure"); 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return true; 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->INHERITED::onQuery(evt); 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 100ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 10181e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com virtual void onDrawContent(SkCanvas* canvas) { 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->translate(fPaint.getTextSize(), fPaint.getTextSize()); 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com doMeasure(canvas, fPaint, "Hamburgefons"); 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 105ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 10781e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com typedef SampleView INHERITED; 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new MeasureView; } 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory); 114