Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 208d4592 authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Fix potential crash when shaping Hebrew with bold

- make code more resilient
- make correct initialization of gHebrewRegularTypeface

Change-Id: I97e98d36b830ad35979184c1459e8c8503eb3d28
parent 4873dc8f
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define LOG_TAG "HarfbuzzSkia"

#include "HarfbuzzSkia.h"

#include "SkFontHost.h"
@@ -214,6 +216,10 @@ HB_Error harfbuzzSkiaGetTable(void* voidface, const HB_Tag tag, HB_Byte* buffer,
    FontData* data = reinterpret_cast<FontData*>(voidface);
    SkTypeface* typeface = data->typeFace;

    if (!typeface) {
        LOGD("Typeface cannot be null");
        return HB_Err_Invalid_Argument;
    }
    const size_t tableSize = SkFontHost::GetTableSize(typeface->uniqueID(), tag);
    if (!tableSize)
        return HB_Err_Invalid_Argument;
+11 −15
Original line number Diff line number Diff line
@@ -415,10 +415,7 @@ unsigned TextLayoutCacheValue::shapeFontRun(HB_ShaperItem& shaperItem, SkPaint*
    FontData* data = reinterpret_cast<FontData*>(shaperItem.font->userData);
    switch(shaperItem.item.script) {
        case HB_Script_Arabic:
            if (!gArabicTypeface) {
                gArabicTypeface = SkTypeface::CreateFromFile(TYPEFACE_ARABIC);
            }
            data->typeFace = gArabicTypeface;
            data->typeFace = getCachedTypeface(gArabicTypeface, TYPEFACE_ARABIC);
#if DEBUG_GLYPHS
            LOGD("Using Arabic Typeface");
#endif
@@ -430,29 +427,21 @@ unsigned TextLayoutCacheValue::shapeFontRun(HB_ShaperItem& shaperItem, SkPaint*
                    case SkTypeface::kNormal:
                    case SkTypeface::kItalic:
                    default:
                        if (!gHebrewRegularTypeface) {
                            gHebrewRegularTypeface = SkTypeface::CreateFromFile(
                                    TYPE_FACE_HEBREW_REGULAR);
                        }
                        data->typeFace = gHebrewRegularTypeface;
                        data->typeFace = getCachedTypeface(gHebrewRegularTypeface, TYPE_FACE_HEBREW_REGULAR);
#if DEBUG_GLYPHS
                        LOGD("Using Hebrew Regular/Italic Typeface");
#endif
                        break;
                    case SkTypeface::kBold:
                    case SkTypeface::kBoldItalic:
                        if (!gHebrewBoldTypeface) {
                            gHebrewBoldTypeface = SkTypeface::CreateFromFile(
                                    TYPE_FACE_HEBREW_BOLD);
                        }
                        data->typeFace = gHebrewBoldTypeface;
                        data->typeFace = getCachedTypeface(gHebrewBoldTypeface, TYPE_FACE_HEBREW_BOLD);
#if DEBUG_GLYPHS
                        LOGD("Using Hebrew Bold/BoldItalic Typeface");
#endif
                        break;
                }
            } else {
                data->typeFace = gHebrewRegularTypeface;
                data->typeFace = getCachedTypeface(gHebrewRegularTypeface, TYPE_FACE_HEBREW_REGULAR);
#if DEBUG_GLYPHS
                        LOGD("Using Hebrew Regular Typeface");
#endif
@@ -492,6 +481,13 @@ unsigned TextLayoutCacheValue::shapeFontRun(HB_ShaperItem& shaperItem, SkPaint*
    return result;
}

SkTypeface* TextLayoutCacheValue::getCachedTypeface(SkTypeface* typeface, const char path[]) {
    if (!typeface) {
        typeface = SkTypeface::CreateFromFile(path);
    }
    return typeface;
}

void TextLayoutCacheValue::computeValuesWithHarfbuzz(SkPaint* paint, const UChar* chars,
        size_t start, size_t count, size_t contextCount, int dirFlags,
        Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
+2 −0
Original line number Diff line number Diff line
@@ -174,6 +174,8 @@ private:
    static unsigned shapeFontRun(HB_ShaperItem& shaperItem, SkPaint* paint,
            size_t count, bool isRTL);

    static SkTypeface* getCachedTypeface(SkTypeface* typeface, const char path[]);

    static void deleteGlyphArrays(HB_ShaperItem& shaperItem);

    static void createGlyphArrays(HB_ShaperItem& shaperItem, int size);