diff --git a/include/minikin/MinikinFontFreeType.h b/include/minikin/MinikinFontFreeType.h index 70518319acf..13a513982b3 100644 --- a/include/minikin/MinikinFontFreeType.h +++ b/include/minikin/MinikinFontFreeType.h @@ -47,6 +47,9 @@ public: float GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint &paint) const; + void GetBounds(MinikinRect* bounds, uint32_t glyph_id, + const MinikinPaint& paint) const; + // If buf is NULL, just update size bool GetTable(uint32_t tag, uint8_t *buf, size_t *size); diff --git a/libs/minikin/MinikinFontFreeType.cpp b/libs/minikin/MinikinFontFreeType.cpp index be61345a9cb..a251ddadbf1 100644 --- a/libs/minikin/MinikinFontFreeType.cpp +++ b/libs/minikin/MinikinFontFreeType.cpp @@ -53,6 +53,11 @@ float MinikinFontFreeType::GetHorizontalAdvance(uint32_t glyph_id, return advance * (1.0 / 65536); } +void MinikinFontFreeType::GetBounds(MinikinRect* bounds, uint32_t glyph_id, + const MinikinPaint& paint) const { + // TODO: NYI +} + bool MinikinFontFreeType::GetTable(uint32_t tag, uint8_t *buf, size_t *size) { FT_ULong ftsize = *size; FT_Error error = FT_Load_Sfnt_Table(mTypeface, tag, 0, buf, &ftsize); diff --git a/sample/MinikinSkia.cpp b/sample/MinikinSkia.cpp index d67e59fbbcc..8b499d81312 100644 --- a/sample/MinikinSkia.cpp +++ b/sample/MinikinSkia.cpp @@ -25,22 +25,39 @@ bool MinikinFontSkia::GetGlyph(uint32_t codepoint, uint32_t *glyph) const { return !!glyph; } +static void MinikinFontSkia_SetSkiaPaint(SkTypeface* typeface, SkPaint* skPaint, const MinikinPaint& paint) { + skPaint->setTypeface(typeface); + skPaint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); + // TODO: set more paint parameters from Minikin + skPaint->setTextSize(paint.size); +} + float MinikinFontSkia::GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint &paint) const { - SkPaint skpaint; - skpaint.setTypeface(mTypeface); - skpaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); - // TODO: set paint from Minikin - skpaint.setTextSize(100); + SkPaint skPaint; uint16_t glyph16 = glyph_id; SkScalar skWidth; - SkRect skBounds; - skpaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, &skBounds); - // bounds? - //printf("advance for glyph %d = %f\n", glyph_id, SkScalarToFP(skWidth)); + MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint); + skPaint.getTextWidths(&glyph16, sizeof(glyph16), &skWidth, NULL); +#ifdef VERBOSE + ALOGD("width for typeface %d glyph %d = %f", mTypeface->uniqueID(), glyph_id +#endif return skWidth; } +void MinikinFontSkia::GetBounds(MinikinRect* bounds, uint32_t glyph_id, + const MinikinPaint& paint) const { + SkPaint skPaint; + uint16_t glyph16 = glyph_id; + SkRect skBounds; + MinikinFontSkia_SetSkiaPaint(mTypeface, &skPaint, paint); + skPaint.getTextWidths(&glyph16, sizeof(glyph16), NULL, &skBounds); + bounds->mLeft = skBounds.fLeft; + bounds->mTop = skBounds.fTop; + bounds->mRight = skBounds.fRight; + bounds->mBottom = skBounds.fBottom; +} + bool MinikinFontSkia::GetTable(uint32_t tag, uint8_t *buf, size_t *size) { if (buf == NULL) { const size_t tableSize = mTypeface->getTableSize(tag); diff --git a/sample/MinikinSkia.h b/sample/MinikinSkia.h index 8286a4c5c6b..fca6ca23228 100644 --- a/sample/MinikinSkia.h +++ b/sample/MinikinSkia.h @@ -11,6 +11,9 @@ public: float GetHorizontalAdvance(uint32_t glyph_id, const MinikinPaint &paint) const; + void GetBounds(MinikinRect* bounds, uint32_t glyph_id, + const MinikinPaint& paint) const; + // If buf is NULL, just update size bool GetTable(uint32_t tag, uint8_t *buf, size_t *size);