本文整理汇总了C++中SkPaint::measureText方法的典型用法代码示例。如果您正苦于以下问题:C++ SkPaint::measureText方法的具体用法?C++ SkPaint::measureText怎么用?C++ SkPaint::measureText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SkPaint
的用法示例。
在下文中一共展示了SkPaint::measureText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: selectionRectForComplexText
//SAMSUNG_THAI_EDITING_FIX >>
FloatRect Font::selectionRectForComplexText(const TextRun& run,
const IntPoint& point, int h, int from, int to) const
{
SkPaint paint;
SkPaint::FontMetrics metrics;
float beforeWidth = 0.0f ;
float afterWidth = 0.0f ;
primaryFont()->platformData().setupPaint(&paint);
size_t length = from ;
if (length > 0) {
beforeWidth = paint.measureText(run.characters(), length << 1);
}
length = to ;
if (length > 0) {
afterWidth = paint.measureText(run.characters(), length << 1);
}
return FloatRect(point.x() + floorf(beforeWidth),
point.y(),
roundf(afterWidth) - floorf(beforeWidth),
roundf(SkScalarToFloat(h)));
}
示例2: selectionRectForComplexText
FloatRect Font::selectionRectForComplexText(const TextRun& run,
const IntPoint& point, int h, int from, int to) const
{
SkPaint paint;
SkScalar width, left;
SkPaint::FontMetrics metrics;
primaryFont()->platformData().setupPaint(&paint);
SkScalar spacing = paint.getFontMetrics(&metrics);
float beforeWidth = SkScalarToFloat(paint.measureText(run.characters(), from << 1));
float afterWidth = SkScalarToFloat(paint.measureText(run.characters(), to << 1));
if (run.rtl()) {
float totalWidth = SkScalarToFloat(paint.measureText(run.characters(), run.length() << 1));
return FloatRect(point.x() + floorf(totalWidth - afterWidth),
point.y(),
roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth),
//roundf(SkScalarToFloat(spacing))); // Don't compute rect height, but use h.
h);
} else {
return FloatRect(point.x() + floorf(beforeWidth),
point.y(),
roundf(afterWidth) - floorf(beforeWidth),
//roundf(SkScalarToFloat(spacing))); // Don't compute rect height, but use h.
h);
}
}
示例3: drawHelp
void CommandSet::drawHelp(SkCanvas* canvas) {
if (kNone_HelpMode == fHelpMode) {
return;
}
// Sort commands for current mode:
SkTQSort(fCommands.begin(), fCommands.end() - 1,
kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCommandGroup);
SkPaint bgPaint;
bgPaint.setColor(0xC0000000);
canvas->drawPaint(bgPaint);
SkPaint paint;
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setColor(0xFFFFFFFF);
SkPaint groupPaint;
groupPaint.setTextSize(18);
groupPaint.setUnderlineText(true);
groupPaint.setAntiAlias(true);
groupPaint.setColor(0xFFFFFFFF);
SkScalar x = SkIntToScalar(10);
SkScalar y = SkIntToScalar(10);
// Measure all key strings:
SkScalar keyWidth = 0;
for (Command& cmd : fCommands) {
keyWidth = SkMaxScalar(keyWidth,
paint.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size()));
}
keyWidth += paint.measureText(" ", 1);
// If we're grouping by category, we'll be adding text height on every new group (including the
// first), so no need to do that here. Otherwise, skip down so the first line is where we want.
if (kGrouped_HelpMode != fHelpMode) {
y += paint.getTextSize();
}
// Print everything:
SkString lastGroup;
for (Command& cmd : fCommands) {
if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) {
// Group change. Advance and print header:
y += paint.getTextSize();
canvas->drawText(cmd.fGroup.c_str(), cmd.fGroup.size(), x, y, groupPaint);
y += groupPaint.getTextSize() + 2;
lastGroup = cmd.fGroup;
}
canvas->drawText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), x, y, paint);
SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
canvas->drawText(text.c_str(), text.size(), x + keyWidth, y, paint);
y += paint.getTextSize() + 2;
}
}
示例4: onDraw
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);
}
}
示例5: onDraw
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);
}
}
示例6: test_text
static void test_text(SkCanvas* canvas) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setTextSize(20);
const char* str = "Hamburgefons";
size_t len = strlen(str);
SkScalar x = 20;
SkScalar y = 20;
canvas->drawText(str, len, x, y, paint);
y += 20;
const SkPoint pts[] = { { x, y }, { x + paint.measureText(str, len), y } };
const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
const SkScalar pos[] = { 0, 0.9f, 1 };
SkShader* s = SkGradientShader::CreateLinear(pts, colors, pos,
SK_ARRAY_COUNT(colors),
SkShader::kClamp_TileMode);
paint.setShader(s)->unref();
canvas->drawText(str, len, x, y, paint);
y += 20;
paint.setShader(NULL);
drawFadingText(canvas, str, len, x, y, paint);
}
示例7: recordCanvas
void recordCanvas(SkCanvas* canvas) override {
SkPaint paint;
paint.setTextSize(fTextSize);
paint.setColor(SK_ColorBLACK);
const char* text = "Hamburgefons";
size_t len = strlen(text);
const SkScalar textWidth = paint.measureText(text, len);
SkScalar* adv = new SkScalar[len];
paint.getTextWidths(text, len, adv);
for (SkScalar x = 0; x < fPictureWidth; x += textWidth) {
for (SkScalar y = 0; y < fPictureHeight; y += fTextSize) {
SkPoint* pos = new SkPoint[len];
SkScalar advX = 0;
for (size_t i = 0; i < len; i++) {
if (fDrawPosH)
pos[i].set(x + advX, y);
else
pos[i].set(x + advX, y + i);
advX += adv[i];
}
canvas->drawPosText(text, len, pos, paint);
delete[] pos;
}
}
delete[] adv;
}
示例8: onDraw
void onDraw(SkCanvas* canvas) override {
SkPaint paint;
paint.setAntiAlias(true);
paint.setLCDRenderText(true);
paint.setSubpixelText(true);
paint.setTypeface(fTypefaces[0]);
paint.setTextSize(192);
// Make sure the nul character does not cause problems.
paint.measureText("\0", 1);
SkScalar x = 20;
SkScalar y = 128;
SkString text("ABCDEFGHIJ");
draw_string(canvas, text, x, y, paint);
y += 100;
SkString text2("KLMNOPQRS");
draw_string(canvas, text2, x, y, paint);
y += 100;
SkString text3("TUVWXYZ012");
draw_string(canvas, text3, x, y, paint);
y += 100;
paint.setTypeface(fTypefaces[1]);
draw_string(canvas, text, x, y, paint);
y += 100;
draw_string(canvas, text2, x, y, paint);
y += 100;
draw_string(canvas, text3, x, y, paint);
y += 100;
}
示例9: drawBaseline
static void drawBaseline(SkCanvas* canvas, const SkPaint& paint,
SkScalar x, SkScalar y) {
SkScalar total = paint.measureText(gText, gLen);
SkPaint p;
p.setAntiAlias(true);
p.setColor(0x80FF0000);
canvas->drawLine(x, y,
paint.isVerticalText() ? x : x + total,
paint.isVerticalText() ? y + total : y,
p);
p.setColor(0xFF0000FF);
SkScalar adv[gLen];
int numChars = paint.getTextWidths(gText, gLen, adv, nullptr);
for (int i = 0; i < numChars; ++i) {
canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
if (paint.isVerticalText()) {
y += adv[i];
} else {
x += adv[i];
}
}
canvas->drawCircle(x, y, SK_Scalar1 * 3 / 2, p);
}
示例10: draw
bool draw(SkCanvas* canvas, const SkString& newText, SkScalar x, SkScalar y, SkPaint& paint)
{
SkScalar scale;
if (fInterp.timeToValues(SkTime::GetMSecs(), &scale) == SkInterpolator::kFreezeEnd_Result)
{
canvas->drawText(newText.c_str(), newText.size(), x, y, paint);
return false;
}
else
{
U8 alpha = paint.getAlpha();
SkScalar above, below;
(void)paint.measureText(nil, 0, &above, &below);
SkScalar height = below - above;
SkScalar dy = SkScalarMul(height, scale);
if (scale < 0)
height = -height;
// draw the old
paint.setAlpha((U8)SkScalarMul(alpha, SK_Scalar1 - SkScalarAbs(scale)));
canvas->drawText(fOldText.c_str(), fOldText.size(), x, y - dy, paint);
// draw the new
paint.setAlpha((U8)SkScalarMul(alpha, SkScalarAbs(scale)));
canvas->drawText(newText.c_str(), newText.size(), x, y + height - dy, paint);
// restore the paint
paint.setAlpha(alpha);
return true;
}
}
示例11: test_breakText
static void test_breakText() {
SkPaint paint;
const char* text = "sdfkljAKLDFJKEWkldfjlk#$%&sdfs.dsj";
size_t length = strlen(text);
SkScalar width = paint.measureText(text, length);
SkScalar mm = 0;
SkScalar nn = 0;
for (SkScalar w = 0; w <= width; w += SK_Scalar1) {
SkScalar m;
size_t n = paint.breakText(text, length, w, &m,
SkPaint::kBackward_TextBufferDirection);
SkASSERT(n <= length);
SkASSERT(m <= width);
if (n == 0) {
SkASSERT(m == 0);
} else {
// now assert that we're monotonic
if (n == nn) {
SkASSERT(m == mm);
} else {
SkASSERT(n > nn);
SkASSERT(m > mm);
}
}
nn = SkIntToScalar(n);
mm = m;
}
SkDEBUGCODE(size_t length2 =) paint.breakText(text, length, width, &mm);
SkASSERT(length2 == length);
SkASSERT(mm == width);
}
示例12: GetMetrics
const SkAdvancedTypefaceMetrics* SkPDFFont::GetMetrics(SkTypeface* typeface,
SkPDFCanon* canon) {
SkASSERT(typeface);
SkFontID id = typeface->uniqueID();
if (std::unique_ptr<SkAdvancedTypefaceMetrics>* ptr = canon->fTypefaceMetrics.find(id)) {
return ptr->get(); // canon retains ownership.
}
int count = typeface->countGlyphs();
if (count <= 0 || count > 1 + SK_MaxU16) {
// Cache nullptr to skip this check. Use SkSafeUnref().
canon->fTypefaceMetrics.set(id, nullptr);
return nullptr;
}
std::unique_ptr<SkAdvancedTypefaceMetrics> metrics = typeface->getAdvancedMetrics();
if (!metrics) {
metrics = skstd::make_unique<SkAdvancedTypefaceMetrics>();
}
if (0 == metrics->fStemV || 0 == metrics->fCapHeight) {
SkPaint tmpPaint;
tmpPaint.setHinting(SkPaint::kNo_Hinting);
tmpPaint.setTypeface(sk_ref_sp(typeface));
tmpPaint.setTextSize(1000); // glyph coordinate system
if (0 == metrics->fStemV) {
// Figure out a good guess for StemV - Min width of i, I, !, 1.
// This probably isn't very good with an italic font.
int16_t stemV = SHRT_MAX;
for (char c : {'i', 'I', '!', '1'}) {
SkRect bounds;
tmpPaint.measureText(&c, 1, &bounds);
stemV = SkTMin(stemV, SkToS16(SkScalarRoundToInt(bounds.width())));
}
metrics->fStemV = stemV;
}
if (0 == metrics->fCapHeight) {
// Figure out a good guess for CapHeight: average the height of M and X.
SkScalar capHeight = 0;
for (char c : {'M', 'X'}) {
SkRect bounds;
tmpPaint.measureText(&c, 1, &bounds);
capHeight += bounds.height();
}
metrics->fCapHeight = SkToS16(SkScalarRoundToInt(capHeight / 2));
}
}
return canon->fTypefaceMetrics.set(id, std::move(metrics))->get();
}
示例13: thread_main
static void thread_main(void*) {
SkGraphics::SetTLSFontCacheLimit(1 * 1024 * 1024);
const char text[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
size_t len = strlen(text);
SkPaint paint;
for (int j = 0; j < 10; ++j) {
for (int i = 9; i <= 48; ++i) {
paint.setTextSize(SkIntToScalar(i));
paint.setAntiAlias(false);
paint.measureText(text, len);
paint.setAntiAlias(true);
paint.measureText(text, len);
}
}
}
示例14: draw_label
static void draw_label(SkCanvas* canvas, const char* label,
const SkPoint& offset) {
SkPaint paint;
size_t len = strlen(label);
SkScalar width = paint.measureText(label, len);
canvas->drawText(label, len, offset.x() - width / 2, offset.y(),
paint);
}
示例15: drawTestCase
static void drawTestCase(SkCanvas* canvas,
SkScalar textScale,
SkScalar strokeWidth,
SkPaint::Style strokeStyle) {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setAntiAlias(true);
paint.setTextSize(kTextHeight * textScale);
sk_tool_utils::set_portable_typeface(&paint);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(strokeStyle);
// This demonstrates that we can not measure the text if
// there's a device transform. The canvas total matrix will
// end up being a device transform.
bool drawRef = !(canvas->getTotalMatrix().getType() &
~(SkMatrix::kIdentity_Mask | SkMatrix::kTranslate_Mask));
SkRect bounds;
if (drawRef) {
SkScalar advance = paint.measureText(kText, sizeof(kText) - 1, &bounds);
paint.setStrokeWidth(0.0f);
paint.setStyle(SkPaint::kStroke_Style);
// Green box is the measured text bounds.
paint.setColor(SK_ColorGREEN);
canvas->drawRect(bounds, paint);
// Red line is the measured advance from the 0,0 of the text position.
paint.setColor(SK_ColorRED);
canvas->drawLine(0.0f, 0.0f, advance, 0.0f, paint);
}
// Black text is the testcase, eg. the text.
paint.setColor(SK_ColorBLACK);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(strokeStyle);
canvas->drawText(kText, sizeof(kText) - 1, 0.0f, 0.0f, paint);
if (drawRef) {
SkScalar widths[sizeof(kText) - 1];
paint.getTextWidths(kText, sizeof(kText) - 1, widths, nullptr);
paint.setStrokeWidth(0.0f);
paint.setStyle(SkPaint::kStroke_Style);
// Magenta lines are the positions for the characters.
paint.setColor(SK_ColorMAGENTA);
SkScalar w = bounds.x();
for (size_t i = 0; i < sizeof(kText) - 1; ++i) {
canvas->drawLine(w, 0.0f, w, 5.0f, paint);
w += widths[i];
}
}
}