티스토리 뷰

728x90

GitHub - nightelf3/FirstSkiaApp 에 skia 예제 코드가 좀 있다.

Visual Studio에서 실행할 수 있고, 주요 skia 코드만 Skia Fiddle 에서 돌려보고 결과를 확인할 수 있다.

(Skia Fiddle에서 코드를 돌려보면 CPU와 GPU에서 실행한 결과를 모두 출력해준다)

Example1

void ExampleLayer1::Draw(SkCanvas* canvas)
{
	// clear canvas with black color
	canvas->clear(SkColors::kBlack);

	// inititalize paint structure to fill shape with red color
	SkPaint paint;
	paint.setStyle(SkPaint::Style::kFill_Style);
	paint.setColor(SkColors::kRed);

	// draw 150x100 rect at (100, 100)
	SkRect rect = SkRect::MakeXYWH(100, 100, 150, 100);
	canvas->drawRect(rect, paint);
}

스키아 피들에서 아래와 같이 코드를 입력하고 실행하면 결과를 확인할 수 있다.

Example2

void ExampleLayer2::Draw(SkCanvas* canvas)
{
	// clear canvas with black color
	canvas->clear(SkColors::kBlack);

	// inititalize paint structure to fill shape with red color
	SkPaint paint;
	paint.setStyle(SkPaint::Style::kFill_Style);
	paint.setColor(SkColors::kRed);

	// get canvas size and resuce it for 1/8
	SkRect rect;
	rect.set(canvas->getDeviceClipBounds());
	rect.inset(rect.width() / 8.f, rect.height() / 8.f);
	canvas->drawOval(rect, paint);
}

Example3

void ExampleLayer3::Draw(SkCanvas* canvas)
{
	// clear canvas with black color
	canvas->clear(SkColors::kBlack);

	SkPaint paint;
	paint.setStyle(SkPaint::kFill_Style);
	paint.setAntiAlias(true);
	paint.setStrokeWidth(4);
	paint.setColor(0xff4285F4);

	SkRect rect = SkRect::MakeXYWH(10, 10, 100, 160);
	canvas->drawRect(rect, paint);

	SkRRect oval;
	oval.setOval(rect);
	oval.offset(40, 80);
	paint.setColor(0xffDB4437);
	canvas->drawRRect(oval, paint);

	paint.setColor(0xff0F9D58);
	canvas->drawCircle(180, 50, 25, paint);

	rect.offset(80, 50);
	paint.setColor(0xffF4B400);
	paint.setStyle(SkPaint::kStroke_Style);
	canvas->drawRoundRect(rect, 10, 10, paint);
}

Example4

 

SkFontMgr::RefDefault()는 최신 버전에서 삭제되었기 때문에, 최신 버전을 사용하는 Skia Fiddle에서 컴파일되지 않는 것 같다. GitHub 프로젝트는 이전 버전의 lib이 포함되어있기 때문에 컴파일이 잘 된다.

void ExampleLayer4::Draw(SkCanvas* canvas)
{
	// clear canvas with black color
	canvas->clear(SkColors::kBlack);

	sk_sp<SkFontMgr> fontManager = SkFontMgr::RefDefault();
	sk_sp<SkTypeface> typeface(fontManager->matchFamilyStyle(nullptr, {}));

	SkFont font1(typeface, 64.0f, 1.0f, 0.0f);
	SkFont font2(typeface, 64.0f, 1.5f, 0.0f);
	font1.setEdging(SkFont::Edging::kAntiAlias);
	font2.setEdging(SkFont::Edging::kAntiAlias);

	SkPaint paint1;
	paint1.setAntiAlias(true);
	paint1.setColor(SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4));
	canvas->drawString("Skia", 200.0f, 240.0f, font1, paint1);

	SkPaint paint2;
	paint2.setAntiAlias(true);
	paint2.setColor(SkColorSetARGB(0xFF, 0xDB, 0x44, 0x37));
	paint2.setStyle(SkPaint::kStroke_Style);
	paint2.setStrokeWidth(3.0f);
	canvas->drawString("Skia", 200.0f, 344.0f, font1, paint2);

	SkPaint paint3;
	paint3.setAntiAlias(true);
	paint3.setColor(SkColorSetARGB(0xFF, 0x0F, 0x9D, 0x58));
	canvas->drawString("Skia", 200.0f, 424.0f, font2, paint3);
}

Example5

#include "include/Layers/ExampleLayer5.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontMgr.h"

namespace
{
	const char sDefaultFamily[] = "Default";
	const std::vector<std::wstring> sText = {
		L"一 (yī) – “one”",
		L"二 (èr) – “two”",
		L"三 (sān) – “three”",
		L"四 (sì) – “four”",
		L"五 (wǔ) – “five”",
		L"六 (liù) – “six”",
		L"七 (qī) – “seven",
		L"八 (bā) – “eight”",
		L"九 (jiǔ) – “nine”",
		L"十 (shí) – “ten”",
		L"文言文 - wényánwén",
		L"白话文 - Báihuà Wén",
		L"貞 zhēn became 贞 zhēn",
		L"贈 zèng became 赠 zèng"
	};

	template<typename TChar>
	SkScalar DrawAndMeatureString(SkCanvas* canvas, const TChar str[], const size_t lenght, const SkFont& font, const SkPaint& paint, SkScalar x, SkScalar y)
	{
		const auto encoding = sizeof(TChar) == 1 ? SkTextEncoding::kUTF8 : SkTextEncoding::kUTF16;
		SkRect measure;
		font.measureText(str, lenght * sizeof(TChar), encoding, &measure);
		canvas->drawSimpleText(str, lenght * sizeof(TChar), encoding, x, y, font, paint);
		return measure.height() + font.getSize() / 4.f;
	}

	void DrawSkText(SkCanvas* canvas, const std::vector<std::wstring>& sText, const char familyName[], SkScalar x, SkScalar y, const SkPaint& paint)
	{
		sk_sp<SkFontMgr> fontManager = SkFontMgr::RefDefault();
		sk_sp<SkTypeface> typeface(fontManager->matchFamilyStyle(familyName, {}));
		SkFont font(typeface, 20);

		if (!familyName)
			familyName = sDefaultFamily;

		y += DrawAndMeatureString(canvas, familyName, strlen(familyName), font, paint, x, y);
		for (auto& str : sText)
			y += DrawAndMeatureString(canvas, str.c_str(), str.length(), font, paint, x, y);
	}
}

void ExampleLayer5::Draw(SkCanvas* canvas)
{
	// clear canvas with black color
	canvas->clear(SkColors::kBlack);

	SkPaint paint;
	paint.setColor(SkColors::kWhite);
	DrawSkText(canvas, sText, nullptr, 100, 100, paint);

	paint.setColor(SkColors::kGreen);
	DrawSkText(canvas, sText, "Yu Gothic UI", 400, 100, paint);
}

 

728x90

'개발 > Skia' 카테고리의 다른 글

Skia 예제 빌드 (Skia 이미지 png로 저장)  (0) 2024.08.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함