本文整理汇总了C++中SkFont类的典型用法代码示例。如果您正苦于以下问题:C++ SkFont类的具体用法?C++ SkFont怎么用?C++ SkFont使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SkFont类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: drawKernText
static void drawKernText(SkCanvas* canvas, const void* text, size_t len,
SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) {
SkTypeface* face = font.getTypefaceOrDefault();
if (!face) {
canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
return;
}
SkAutoSTMalloc<128, uint16_t> glyphStorage(len);
uint16_t* glyphs = glyphStorage.get();
int glyphCount = font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs, len);
if (glyphCount < 1) {
return;
}
SkAutoSTMalloc<128, int32_t> adjustmentStorage(glyphCount - 1);
int32_t* adjustments = adjustmentStorage.get();
if (!face->getKerningPairAdjustments(glyphs, glyphCount, adjustments)) {
canvas->drawSimpleText(text, len, SkTextEncoding::kUTF8, x, y, font, paint);
return;
}
SkTextBlobBuilder builder;
auto rec = builder.allocRunPos(font, glyphCount);
memcpy(rec.glyphs, glyphs, glyphCount * sizeof(SkGlyphID));
getGlyphPositions(font, glyphs, glyphCount, x, y, rec.points());
applyKerning(rec.points(), adjustments, glyphCount, font);
canvas->drawTextBlob(builder.make(), 0, 0, paint);
}
示例2: DEF_TEST
DEF_TEST(TextBlob_extended, reporter) {
SkTextBlobBuilder textBlobBuilder;
SkFont font;
const char text1[] = "Foo";
const char text2[] = "Bar";
int glyphCount = font.countText(text1, strlen(text1), SkTextEncoding::kUTF8);
SkAutoTMalloc<uint16_t> glyphs(glyphCount);
(void)font.textToGlyphs(text1, strlen(text1), SkTextEncoding::kUTF8, glyphs.get(), glyphCount);
auto run = SkTextBlobBuilderPriv::AllocRunText(&textBlobBuilder,
font, glyphCount, 0, 0, SkToInt(strlen(text2)), SkString(), nullptr);
memcpy(run.glyphs, glyphs.get(), sizeof(uint16_t) * glyphCount);
memcpy(run.utf8text, text2, strlen(text2));
for (int i = 0; i < glyphCount; ++i) {
run.clusters[i] = SkTMin(SkToU32(i), SkToU32(strlen(text2)));
}
sk_sp<SkTextBlob> blob(textBlobBuilder.make());
REPORTER_ASSERT(reporter, blob);
for (SkTextBlobRunIterator it(blob.get()); !it.done(); it.next()) {
REPORTER_ASSERT(reporter, it.glyphCount() == (uint32_t)glyphCount);
for (uint32_t i = 0; i < it.glyphCount(); ++i) {
REPORTER_ASSERT(reporter, it.glyphs()[i] == glyphs[i]);
}
REPORTER_ASSERT(reporter, SkTextBlobRunIterator::kDefault_Positioning == it.positioning());
REPORTER_ASSERT(reporter, (SkPoint{0.0f, 0.0f}) == it.offset());
REPORTER_ASSERT(reporter, it.textSize() > 0);
REPORTER_ASSERT(reporter, it.clusters());
for (uint32_t i = 0; i < it.glyphCount(); ++i) {
REPORTER_ASSERT(reporter, i == it.clusters()[i]);
}
REPORTER_ASSERT(reporter, 0 == strncmp(text2, it.text(), it.textSize()));
}
}
示例3: draw
void draw(SkCanvas* canvas) {
SkFont font;
font.setSize(24);
SkPaint canvasPaint;
canvasPaint.setColor(SK_ColorBLUE); // respected
canvasPaint.setTextSize(2); // ignored
sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromString("Hello World", font);
canvas->drawTextBlob(blob, 20, 20, canvasPaint);
}
示例4: applyKerning
static void applyKerning(SkPoint pos[], const int32_t adjustments[], int count,
const SkFont& font) {
SkScalar scale = font.getSize() / font.getTypefaceOrDefault()->getUnitsPerEm();
SkScalar globalAdj = 0;
for (int i = 0; i < count - 1; ++i) {
globalAdj += adjustments[i] * scale;
pos[i + 1].fX += globalAdj;
}
}
示例5: draw
void draw(SkCanvas* canvas) {
SkFont blobFont;
blobFont.setSize(24);
sk_sp<SkTextBlob> blob = SkTextBlob::MakeFromText("Hello World", 11, blobFont);
char storage[2048];
size_t used = blob->serialize(SkSerialProcs(), storage, sizeof(storage));
sk_sp<SkTextBlob> copy = SkTextBlob::Deserialize(storage, used, SkDeserialProcs());
canvas->drawTextBlob(copy, 20, 20, SkPaint());
std::string usage = "size=" + std::to_string(sizeof(storage)) + " used=" + std::to_string(used);
canvas->drawString(usage.c_str(), 20, 40, SkPaint());
}
示例6: make_picture
static sk_sp<SkPicture> make_picture(sk_sp<SkTypeface> tf0, sk_sp<SkTypeface> tf1) {
SkPictureRecorder rec;
SkCanvas* canvas = rec.beginRecording(100, 100);
SkPaint paint;
SkFont font;
font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
font.setTypeface(tf0); canvas->drawString("hello", 0, 0, font, paint);
font.setTypeface(tf1); canvas->drawString("hello", 0, 0, font, paint);
return rec.finishRecordingAsPicture();
}
示例7: draw
void draw(SkCanvas* canvas) {
SkTextBlobBuilder textBlobBuilder;
SkFont font;
font.setSize(50);
const SkTextBlobBuilder::RunBuffer& run =
textBlobBuilder.allocRun(font, 1, 20, 100);
run.glyphs[0] = 20;
sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
SkPaint paint;
paint.setColor(SK_ColorBLUE);
canvas->drawTextBlob(blob.get(), 0, 0, paint);
}
示例8: onDraw
void onDraw(int loops, SkCanvas* canvas) override {
SkFont font;
font.setEdging(SkFont::Edging::kAntiAlias);
const uint16_t* array = gUniqueGlyphIDs;
while (*array != gUniqueGlyphIDs_Sentinel) {
int count = count_glyphs(array);
for (int i = 0; i < loops; ++i) {
(void)font.measureText(array, count * sizeof(uint16_t), SkTextEncoding::kGlyphID);
}
array += count + 1; // skip the sentinel
}
}
示例9: scaled_text_size
SkFont GrTextContext::InitDistanceFieldFont(const SkFont& font,
const SkMatrix& viewMatrix,
const Options& options,
SkScalar* textRatio) {
SkScalar textSize = font.getSize();
SkScalar scaledTextSize = scaled_text_size(textSize, viewMatrix);
SkFont dfFont{font};
if (scaledTextSize <= kSmallDFFontLimit) {
*textRatio = textSize / kSmallDFFontSize;
dfFont.setSize(SkIntToScalar(kSmallDFFontSize));
} else if (scaledTextSize <= kMediumDFFontLimit) {
*textRatio = textSize / kMediumDFFontSize;
dfFont.setSize(SkIntToScalar(kMediumDFFontSize));
} else {
*textRatio = textSize / kLargeDFFontSize;
dfFont.setSize(SkIntToScalar(kLargeDFFontSize));
}
dfFont.setEdging(SkFont::Edging::kAntiAlias);
dfFont.setForceAutoHinting(false);
dfFont.setHinting(SkFontHinting::kNormal);
// The sub-pixel position will always happen when transforming to the screen.
dfFont.setSubpixel(false);
return dfFont;
}
示例10: drawText
void drawText(SkCanvas* canvas) {
static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
SkString("abcdefghijklmnopqrstuvwxyz"),
SkString("0123456789"),
SkString("[email protected]#$%^&*()<>[]{}")};
SkFont font;
font.setEdging(SkFont::Edging::kAntiAlias);
font.setSubpixel(true);
static const SkScalar kSubPixelInc = 1 / 2.f;
SkScalar x = 0;
SkScalar y = 10;
SkScalar subpixelX = 0;
SkScalar subpixelY = 0;
bool offsetX = true;
if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
canvas->scale(10, 10);
}
do {
for (auto s : kSizes) {
auto size = 2 * s;
font.setSize(size);
for (const auto& typeface : fTypefaces) {
font.setTypeface(typeface);
for (const auto& text : kTexts) {
x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
x = SkScalarCeilToScalar(x);
if (x + 100 > kSize) {
x = 0;
y += SkScalarCeilToScalar(size + 3);
if (y > kSize) {
return;
}
}
}
}
(offsetX ? subpixelX : subpixelY) += kSubPixelInc;
offsetX = !offsetX;
}
} while (true);
}
示例11: draw_scene
static void draw_scene(SkCanvas* canvas, const SkHighContrastConfig& config) {
SkRect bounds = SkRect::MakeLTRB(0.0f, 0.0f, 1.0f, 1.0f);
SkPaint xferPaint;
xferPaint.setColorFilter(SkHighContrastFilter::Make(config));
canvas->saveLayer(&bounds, &xferPaint);
SkPaint paint;
bounds = SkRect::MakeLTRB(0.1f, 0.2f, 0.9f, 0.4f);
paint.setARGB(0xff, 0x66, 0x11, 0x11);
canvas->drawRect(bounds, paint);
SkFont font;
font.setSize(0.15f);
font.setEdging(SkFont::Edging::kAlias);
paint.setARGB(0xff, 0xbb, 0x77, 0x77);
canvas->drawString("A", 0.15f, 0.35f, font, paint);
bounds = SkRect::MakeLTRB(0.1f, 0.8f, 0.9f, 1.0f);
paint.setARGB(0xff, 0xcc, 0xcc, 0xff);
canvas->drawRect(bounds, paint);
paint.setARGB(0xff, 0x88, 0x88, 0xbb);
canvas->drawString("Z", 0.75f, 0.95f, font, paint);
bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
SkColor colors[] = { SK_ColorWHITE, SK_ColorBLACK };
SkScalar pos[] = { 0.2f, 0.8f };
paint.setShader(SkGradientShader::MakeLinear(
pts, colors, pos,
SK_ARRAY_COUNT(colors), SkTileMode::kClamp));
canvas->drawRect(bounds, paint);
bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
SkColor colors2[] = { SK_ColorGREEN, SK_ColorWHITE };
paint.setShader(SkGradientShader::MakeLinear(
pts, colors2, pos,
SK_ARRAY_COUNT(colors2), SkTileMode::kClamp));
canvas->drawRect(bounds, paint);
canvas->restore();
}
示例12: getGlyphPositions
static void getGlyphPositions(const SkFont& font, const uint16_t glyphs[],
int count, SkScalar x, SkScalar y, SkPoint pos[]) {
SkAutoSTMalloc<128, SkScalar> widthStorage(count);
SkScalar* widths = widthStorage.get();
font.getWidths(glyphs, count, widths);
for (int i = 0; i < count; ++i) {
pos[i].set(x, y);
x += widths[i];
}
}
示例13: onDraw
void onDraw(SkCanvas* canvas) override {
SkFont font;
font.setSize(30);
const char* text = fApplyKerning ? "Type AWAY" : "Hamburgefons";
const size_t textLen = strlen(text);
SkScalar x = SkIntToScalar(10);
SkScalar dy = font.getMetrics(nullptr);
SkScalar y = dy;
if (fApplyKerning) {
font.setSubpixel(true);
} else {
font.setLinearMetrics(true);
}
SkPaint paint;
for (int i = 0; i < gStylesCount; i++) {
font.setTypeface(fFaces[i]);
canvas->drawSimpleText(text, textLen, SkTextEncoding::kUTF8, x, y, font, paint);
if (fApplyKerning) {
drawKernText(canvas, text, textLen, x + 240, y, font, paint);
}
y += dy;
}
}
示例14: make_big_bitmap
static void make_big_bitmap(SkBitmap* bm) {
static const char gText[] =
"We the people, in order to form a more perfect union, establish justice,"
" ensure domestic tranquility, provide for the common defense, promote the"
" general welfare and ensure the blessings of liberty to ourselves and our"
" posterity, do ordain and establish this constitution for the United"
" States of America.";
const int BIG_H = 120;
SkFont font;
font.setSize(SkIntToScalar(BIG_H));
const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8));
bm->allocN32Pixels(BIG_W, BIG_H);
bm->eraseColor(SK_ColorWHITE);
SkCanvas canvas(*bm);
canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint());
}
示例15: GetPath
void SkTextUtils::GetPath(const void* text, size_t length, SkTextEncoding encoding,
SkScalar x, SkScalar y, const SkFont& font, SkPath* path) {
SkAutoToGlyphs ag(font, text, length, encoding);
SkAutoTArray<SkPoint> pos(ag.count());
font.getPos(ag.glyphs(), ag.count(), pos.get(), {x, y});
struct Rec {
SkPath* fDst;
const SkPoint* fPos;
} rec = { path, pos.get() };
path->reset();
font.getPaths(ag.glyphs(), ag.count(), [](const SkPath* src, const SkMatrix& mx, void* ctx) {
Rec* rec = (Rec*)ctx;
if (src) {
SkMatrix m(mx);
m.postTranslate(rec->fPos->fX, rec->fPos->fY);
rec->fDst->addPath(*src, m);
}
rec->fPos += 1;
}, &rec);
}