1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc. 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SampleCode.h" 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkView.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h" 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGradientShader.h" 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h" 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h" 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h" 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "Sk1DPathEffect.h" 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCornerPathEffect.h" 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h" 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRandom.h" 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h" 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorFilter.h" 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkDither.h" 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// exercise scale/linear/devkern 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstruct Setting { 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool fLinearText; 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com bool fDevKernText; 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const Setting gSettings[] = { 3099ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { false, false }, 3199ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { false, true }, 3299ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { true, false }, 3399ae881a7f55a03c2b16fb5704e6e90ea86d965dreed { true, true }, 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3681e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.comstatic void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) { 3796fcdcc219d2a0d3579719b84b28bede76efba64halcanary SkScalar dy = paint.getFontMetrics(nullptr); 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com size_t len = strlen(text); 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoTMalloc<SkScalar> autoWidths(len); 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar* widths = autoWidths.get(); 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoTMalloc<SkRect> autoRects(len); 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect* rects = autoRects.get(); 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect bounds; 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint p(paint); 4764cc579efa7e416c7298ed159d76b074b283c0f9senorblanco@chromium.org for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) { 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setLinearText(gSettings[i].fLinearText); 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setDevKernText(gSettings[i].fDevKernText); 50ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int n = p.getTextWidths(text, len, widths, rects); 5299ae881a7f55a03c2b16fb5704e6e90ea86d965dreed SkScalar w = p.measureText(text, len, &bounds); 53ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStyle(SkPaint::kFill_Style); 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0x8888FF88); 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(bounds, p); 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFF000000); 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawText(text, len, 0, 0, p); 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStyle(SkPaint::kStroke_Style); 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStrokeWidth(0); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFFFF0000); 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkScalar x = 0; 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com for (int j = 0; j < n; j++) { 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect r = rects[j]; 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com r.offset(x, 0); 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(r, p); 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com x += widths[j]; 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setColor(0xFF0000FF); 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawLine(0, 0, w, 0, p); 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com p.setStrokeWidth(SkIntToScalar(4)); 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawPoint(x, 0, p); 75ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->translate(0, dy); 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 8081e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.comclass MeasureView : public SampleView { 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint fPaint; 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 84ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com MeasureView() { 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPaint.setAntiAlias(true); 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPaint.setTextSize(SkIntToScalar(64)); 8781e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com this->setBGColor(0xFFDDDDDD); 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected: 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // overrides from SkEventSink 9281e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com virtual bool onQuery(SkEvent* evt) { 9381e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com if (SampleCode::TitleQ(*evt)) { 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SampleCode::TitleR(evt, "Measure"); 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return true; 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->INHERITED::onQuery(evt); 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 99ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 10081e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com virtual void onDrawContent(SkCanvas* canvas) { 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->translate(fPaint.getTextSize(), fPaint.getTextSize()); 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com doMeasure(canvas, fPaint, "Hamburgefons"); 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 104ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 10681e3d7f7943d5c257a07580b75218a5e256b0aadreed@google.com typedef SampleView INHERITED; 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new MeasureView; } 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory); 113