virtual void onDraw(SkCanvas* canvas) { SkPaint paint; SkScalar horizMargin(SkIntToScalar(10)); SkScalar vertMargin(SkIntToScalar(10)); SkBitmapDevice devTmp(SkBitmap::kARGB_8888_Config, 40, 40, false); SkCanvas canvasTmp(&devTmp); draw_checks(&canvasTmp, 40, 40); SkBitmap src = canvasTmp.getTopDevice()->accessBitmap(false); for (unsigned i = 0; i < NUM_CONFIGS; ++i) { if (!src.deepCopyTo(&fDst[i], gConfigs[i])) { src.copyTo(&fDst[i], gConfigs[i]); } } canvas->clear(0xFFDDDDDD); paint.setAntiAlias(true); SkScalar width = SkIntToScalar(40); SkScalar height = SkIntToScalar(40); if (paint.getFontSpacing() > height) { height = paint.getFontSpacing(); } for (unsigned i = 0; i < NUM_CONFIGS; i++) { const char* name = gConfigNames[src.config()]; SkScalar textWidth = paint.measureText(name, strlen(name)); if (textWidth > width) { width = textWidth; } } SkScalar horizOffset = width + horizMargin; SkScalar vertOffset = height + vertMargin; canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); for (unsigned i = 0; i < NUM_CONFIGS; i++) { canvas->save(); // Draw destination config name const char* name = gConfigNames[fDst[i].config()]; SkScalar textWidth = paint.measureText(name, strlen(name)); SkScalar x = (width - textWidth) / SkScalar(2); SkScalar y = paint.getFontSpacing() / SkScalar(2); canvas->drawText(name, strlen(name), x, y, paint); // Draw destination bitmap canvas->translate(0, vertOffset); x = (width - 40) / SkScalar(2); canvas->drawBitmap(fDst[i], x, 0, &paint); canvas->restore(); canvas->translate(horizOffset, 0); } }
virtual void onDraw(SkCanvas* canvas) { SkPaint paint; SkScalar horizMargin = 10; SkScalar vertMargin = 10; SkBitmap src; src.allocN32Pixels(40, 40); SkCanvas canvasTmp(src); draw_checks(&canvasTmp, 40, 40); for (unsigned i = 0; i < NUM_CONFIGS; ++i) { src.copyTo(&fDst[i], gColorTypes[i]); } canvas->clear(0xFFDDDDDD); paint.setAntiAlias(true); sk_tool_utils::set_portable_typeface(&paint); SkScalar width = SkIntToScalar(40); SkScalar height = SkIntToScalar(40); if (paint.getFontSpacing() > height) { height = paint.getFontSpacing(); } for (unsigned i = 0; i < NUM_CONFIGS; i++) { const char* name = gColorTypeNames[src.colorType()]; SkScalar textWidth = paint.measureText(name, strlen(name)); if (textWidth > width) { width = textWidth; } } SkScalar horizOffset = width + horizMargin; SkScalar vertOffset = height + vertMargin; canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); for (unsigned i = 0; i < NUM_CONFIGS; i++) { canvas->save(); // Draw destination config name const char* name = gColorTypeNames[fDst[i].colorType()]; SkScalar textWidth = paint.measureText(name, strlen(name)); SkScalar x = (width - textWidth) / SkScalar(2); SkScalar y = paint.getFontSpacing() / SkScalar(2); canvas->drawText(name, strlen(name), x, y, paint); // Draw destination bitmap canvas->translate(0, vertOffset); x = (width - 40) / SkScalar(2); canvas->drawBitmap(fDst[i], x, 0, &paint); canvas->restore(); canvas->translate(horizOffset, 0); } }
void onDrawContent(SkCanvas* canvas) override { SkPaint paint; SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf"))); paint.setAntiAlias(true); paint.setFilterQuality(kMedium_SkFilterQuality); SkString outString("fps: "); fTimer.end(); // TODO: generalize this timing code in utils fTimes[fCurrentTime] = (float)(fTimer.fWall); fCurrentTime = (fCurrentTime + 1) & 0x1f; float meanTime = 0.0f; for (int i = 0; i < 32; ++i) { meanTime += fTimes[i]; } meanTime /= 32.f; SkScalar fps = 1000.f / meanTime; outString.appendScalar(fps); outString.append(" ms: "); outString.appendScalar(meanTime); SkString modeString("Text scale: "); modeString.appendU32(fSizeScale); modeString.append("x"); fTimer.start(); canvas->save(); #if SK_SUPPORT_GPU SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibility_testing(); GrContext* grContext = canvas->getGrContext(); if (grContext) { GrTexture* tex = grContext->getFontAtlasTexture(GrMaskFormat::kA8_GrMaskFormat); reinterpret_cast<SkGpuDevice*>(device)->drawTexture(tex, SkRect::MakeXYWH(512, 10, 512, 512), paint); } #endif canvas->translate(180, 180); canvas->rotate(fRotation); canvas->scale(fScale, fScale); canvas->translate(-180, -180); const char* text = "Hamburgefons"; size_t length = strlen(text); SkScalar y = SkIntToScalar(0); for (int i = 12; i <= 26; i++) { paint.setTextSize(SkIntToScalar(i*fSizeScale)); y += paint.getFontSpacing(); DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint); } canvas->restore(); paint.setTextSize(16); // canvas->drawText(outString.c_str(), outString.size(), 512.f, 540.f, paint); canvas->drawText(modeString.c_str(), modeString.size(), 768.f, 540.f, paint); }
void onDraw(SkCanvas* canvas) override { canvas->save(); SkPaint paint; paint.setAntiAlias(true); sk_tool_utils::set_portable_typeface(&paint); paint.setTextSize(SkIntToScalar(56)); SkScalar x = SkIntToScalar(20); SkScalar y = paint.getTextSize(); SkString str("Hamburgefons"); for (int i = 0; i < static_cast<int>(SK_ARRAY_COUNT(gRastProcs)); i++) { apply_shader(&paint, i); // paint.setMaskFilter(NULL); // paint.setColor(SK_ColorBLACK); canvas->drawText(str.c_str(), str.size(), x, y, paint); y += paint.getFontSpacing(); } canvas->restore(); }
int main (int argc, char * const argv[]) { SkAutoGraphics ag; SkString path("skhello.png"); SkString text("Hello"); for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--help")) { show_help(); return 0; } if (!strcmp(argv[i], "-o")) { if (i == argc-1) { SkDebugf("ERROR: -o needs a following filename\n"); return -1; } path.set(argv[i+1]); i += 1; // skip the out dir name } else if (!strcmp(argv[i], "-t")) { if (i == argc-1) { SkDebugf("ERROR: -t needs a following string\n"); return -1; } text.set(argv[i+1]); i += 1; // skip the text string } } SkPaint paint; paint.setAntiAlias(true); paint.setTextSize(SkIntToScalar(30)); SkScalar width = paint.measureText(text.c_str(), text.size()); SkScalar spacing = paint.getFontSpacing(); int w = SkScalarRound(width) + 30; int h = SkScalarRound(spacing) + 30; SkBitmap bitmap; bitmap.setConfig(SkBitmap::kARGB_8888_Config, w, h); bitmap.allocPixels(); SkCanvas canvas(bitmap); canvas.drawColor(SK_ColorWHITE); paint.setTextAlign(SkPaint::kCenter_Align); canvas.drawText(text.c_str(), text.size(), SkIntToScalar(w)/2, SkIntToScalar(h)*2/3, paint); bool success = SkImageEncoder::EncodeFile(path.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); if (!success) { SkDebugf("--- failed to write %s\n", path.c_str()); } return !success; }
static void test_text(SkCanvas* canvas, SkScalar size, SkColor color, SkScalar Y) { SkPaint type; type.setTextSize(24); sk_tool_utils::set_portable_typeface(&type); type.setColor(color); const char text[] = "HELLO WORLD"; canvas->drawText(text, strlen(text), 32, size / 2 + Y, type); SkScalar lineSpacing = type.getFontSpacing(); exercise_draw_pos_text(canvas, text, 32, size / 2 + Y + lineSpacing, type); exercise_draw_pos_text_h(canvas, text, 32, size / 2 + Y + 2 * lineSpacing, type); }
JNIEXPORT void JNICALL Java_com_example_HelloSkiaActivity_drawIntoBitmap(JNIEnv* env, jobject thiz, jobject dstBitmap, jlong elapsedTime) { // Grab the dst bitmap info and pixels AndroidBitmapInfo dstInfo; void* dstPixels; AndroidBitmap_getInfo(env, dstBitmap, &dstInfo); AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels); SkImageInfo info = SkImageInfo::MakeN32Premul(dstInfo.width, dstInfo.height); // Create a surface from the given bitmap SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterDirect(info, dstPixels, dstInfo.stride)); SkCanvas* canvas = surface->getCanvas(); // Draw something "interesting" // Clear the canvas with a white color canvas->drawColor(SK_ColorWHITE); // Setup a SkPaint for drawing our text SkPaint paint; paint.setColor(SK_ColorBLACK); // This is a solid black color for our text paint.setTextSize(SkIntToScalar(30)); // Sets the text size to 30 pixels paint.setAntiAlias(true); // We turn on anti-aliasing so that the text to looks good. // Draw some text SkString text("Skia is Best!"); SkScalar fontHeight = paint.getFontSpacing(); canvas->drawText(text.c_str(), text.size(), // text's data and length 10, fontHeight, // X and Y coordinates to place the text paint); // SkPaint to tell how to draw the text // Adapt the SkPaint for drawing blue lines paint.setAntiAlias(false); // Turning off anti-aliasing speeds up the line drawing paint.setColor(0xFF0000FF); // This is a solid blue color for our lines paint.setStrokeWidth(SkIntToScalar(2)); // This makes the lines have a thickness of 2 pixels // Draw some interesting lines using trig functions for (int i = 0; i < 100; i++) { float x = (float)i / 99.0f; float offset = elapsedTime / 1000.0f; canvas->drawLine(sin(x * M_PI + offset) * 800.0f, 0, // first endpoint cos(x * M_PI + offset) * 800.0f, 800, // second endpoint paint); // SkPapint to tell how to draw the line } // Unlock the dst's pixels AndroidBitmap_unlockPixels(env, dstBitmap); }
virtual void onDrawContent(SkCanvas* canvas) { SkAutoCanvasRestore restore(canvas, false); { SkRect r; r.set(0, 0, SkIntToScalar(1000), SkIntToScalar(20)); // canvas->saveLayer(&r, NULL, SkCanvas::kHasAlphaLayer_SaveFlag); } SkPaint paint; // const uint16_t glyphs[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 }; int index = fHints % SK_ARRAY_COUNT(gHints); index = 1; // const char* style = gHints[index].fName; // canvas->translate(0, SkIntToScalar(50)); // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint); SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf"))); paint.setAntiAlias(true); paint.setFlags(paint.getFlags() | gHints[index].fFlags); SkRect clip; clip.set(SkIntToScalar(25), SkIntToScalar(34), SkIntToScalar(88), SkIntToScalar(155)); const char* text = "Hamburgefons"; size_t length = strlen(text); SkScalar y = SkIntToScalar(0); for (int i = 9; i <= 24; i++) { paint.setTextSize(SkIntToScalar(i) /*+ (gRand.nextU() & 0xFFFF)*/); for (SkScalar dx = 0; dx <= SkIntToScalar(3)/4; dx += SkIntToScalar(1) /* /4 */) { y += paint.getFontSpacing(); DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, paint, fClickX, fMF); } } if (gHints[index].fFlushCache) { // SkGraphics::SetFontCacheUsed(0); } }
void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) { SkAutoCanvasRestore acr(canvas, true); canvas->clipRect(SkRect::MakeWH(w, h)); canvas->drawColor(bg); SkScalar margin = 20; SkTextBox tbox; tbox.setMode(SkTextBox::kLineBreak_Mode); tbox.setBox(margin, margin, w - margin, h - margin); tbox.setSpacing(SkIntToScalar(3)/3, 0); SkPaint paint; paint.setAntiAlias(true); paint.setLCDRenderText(true); paint.setColor(fg); tbox.setText(gText, strlen(gText), paint); for (int i = 9; i < 24; i += 2) { paint.setTextSize(SkIntToScalar(i)); tbox.draw(canvas); canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing()); } }