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

Commit 3660789f authored by Raph Levien's avatar Raph Levien
Browse files

Switch all text layout to Minikin

This patch switches all text layout operations to use Minikin, removes
the USE_MINIKIN #ifdef, and deletes some of the code that was only used
in the old TextLayout path (although some more refactoring remains).

Change-Id: I51b5c4e2bb46cfd9d204c12b9f16f135c769f5b5
parent d194262f
Loading
Loading
Loading
Loading
+8 −14
Original line number Diff line number Diff line
@@ -105,6 +105,8 @@ LOCAL_SRC_FILES:= \
	android/graphics/Interpolator.cpp \
	android/graphics/MaskFilter.cpp \
	android/graphics/Matrix.cpp \
	android/graphics/MinikinSkia.cpp \
	android/graphics/MinikinUtils.cpp \
	android/graphics/Movie.cpp \
	android/graphics/NinePatch.cpp \
	android/graphics/NinePatchImpl.cpp \
@@ -120,8 +122,6 @@ LOCAL_SRC_FILES:= \
	android/graphics/Region.cpp \
	android/graphics/Shader.cpp \
	android/graphics/SurfaceTexture.cpp \
	android/graphics/TextLayout.cpp \
	android/graphics/TextLayoutCache.cpp \
	android/graphics/Typeface.cpp \
	android/graphics/TypefaceImpl.cpp \
	android/graphics/Utils.cpp \
@@ -197,6 +197,9 @@ LOCAL_C_INCLUDES += \
	frameworks/opt/emoji \
	libcore/include \
	$(call include-path-for, audio-utils) \
	frameworks/minikin/include \
	external/freetype/include
# TODO: clean up Minikin so it doesn't need the freetype include

LOCAL_SHARED_LIBRARIES := \
	libmemtrack \
@@ -237,23 +240,14 @@ LOCAL_SHARED_LIBRARIES := \
	libpdfium \
	libimg_utils \
	libnetd_client \
	libsoundtrigger
	libsoundtrigger \
	libminikin \
	libstlport

ifeq ($(USE_OPENGL_RENDERER),true)
	LOCAL_SHARED_LIBRARIES += libhwui
endif

ifeq ($(USE_MINIKIN), true)
	LOCAL_CFLAGS += -DUSE_MINIKIN
	LOCAL_C_INCLUDES += frameworks/minikin/include \
		external/freetype/include
	LOCAL_SRC_FILES += 	android/graphics/MinikinSkia.cpp \
		android/graphics/MinikinUtils.cpp
# note: the freetype include is spurious; minikin itself probably
# shouldn't depend on it
	LOCAL_SHARED_LIBRARIES += libminikin libstlport
endif

LOCAL_SHARED_LIBRARIES += \
	libdl
# we need to access the private Bionic header
+0 −35
Original line number Diff line number Diff line
@@ -29,14 +29,10 @@
#include "SkTArray.h"
#include "SkTemplates.h"

#ifdef USE_MINIKIN
#include <minikin/Layout.h>
#include "MinikinSkia.h"
#include "MinikinUtils.h"
#endif

#include "TextLayout.h"
#include "TextLayoutCache.h"
#include "TypefaceImpl.h"

#include "unicode/ubidi.h"
@@ -301,11 +297,7 @@ public:
    }

    static void freeTextLayoutCaches(JNIEnv* env, jobject) {
#ifdef USE_MINIKIN
        Layout::purgeCaches();
#else
        TextLayoutEngine::getInstance().purgeCaches();
#endif
    }

    static jboolean isOpaque(JNIEnv*, jobject, jlong canvasHandle) {
@@ -908,7 +900,6 @@ public:
        env->ReleaseStringChars(text, textArray);
    }

#ifdef USE_MINIKIN
    class DrawTextFunctor {
    public:
        DrawTextFunctor(const Layout& layout, SkCanvas* canvas, jfloat x, jfloat y, SkPaint* paint,
@@ -950,7 +941,6 @@ public:
        delete[] glyphs;
        delete[] pos;
    }
#endif

    static void drawTextWithGlyphs(SkCanvas* canvas, const jchar* textArray,
            int start, int end,
@@ -965,29 +955,10 @@ public:
            int start, int count, int contextCount,
            jfloat x, jfloat y, int bidiFlags, SkPaint* paint, TypefaceImpl* typeface) {

#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(textArray, start, count, contextCount, css);
        drawGlyphsToSkia(canvas, paint, layout, x, y);
#else
        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(paint,
                textArray, start, count, contextCount, bidiFlags);
        if (value == NULL) {
            return;
        }
        SkPaint::Align align = paint->getTextAlign();
        if (align == SkPaint::kCenter_Align) {
            x -= 0.5 * value->getTotalAdvance();
        } else if (align == SkPaint::kRight_Align) {
            x -= value->getTotalAdvance();
        }
        paint->setTextAlign(SkPaint::kLeft_Align);
        doDrawGlyphsPos(canvas, value->getGlyphs(), value->getPos(), 0, value->getGlyphsCount(),
                x, y, paint);
        doDrawTextDecorations(canvas, x, y, value->getTotalAdvance(), paint);
        paint->setTextAlign(align);
#endif
    }

// Same values used by Skia
@@ -1128,7 +1099,6 @@ public:
        delete[] posPtr;
    }

#ifdef USE_MINIKIN
    class DrawTextOnPathFunctor {
    public:
        DrawTextOnPathFunctor(const Layout& layout, SkCanvas* canvas, float hOffset,
@@ -1153,11 +1123,9 @@ public:
        SkPaint* paint;
        SkPath* path;
    };
#endif

    static void doDrawTextOnPath(SkPaint* paint, const jchar* text, int count, int bidiFlags,
            float hOffset, float vOffset, SkPath* path, SkCanvas* canvas, TypefaceImpl* typeface) {
#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(text, 0, count, count, css);
@@ -1171,9 +1139,6 @@ public:
        DrawTextOnPathFunctor f(layout, canvas, hOffset, vOffset, paint, path);
        MinikinUtils::forFontRun(layout, paint, f);
        paint->setTextAlign(align);
#else
        TextLayout::drawTextOnPath(paint, text, count, bidiFlags, hOffset, vOffset, path, canvas);
#endif
    }

    static void drawTextOnPath___CIIPathFFPaint(JNIEnv* env, jobject,
+0 −18
Original line number Diff line number Diff line
@@ -27,44 +27,33 @@
#include <androidfw/AssetManager.h>
#include "Utils.h"

#ifdef USE_MINIKIN
#include <minikin/FontFamily.h>
#include "MinikinSkia.h"
#endif

namespace android {

static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) {
#ifdef USE_MINIKIN
    FontLanguage fontLanguage;
    if (lang != NULL) {
        ScopedUtfChars str(env, lang);
        fontLanguage = FontLanguage(str.c_str(), str.size());
    }
    return (jlong)new FontFamily(fontLanguage, variant);
#else
    return 0;
#endif
}

static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
#ifdef USE_MINIKIN
    FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
    fontFamily->Unref();
#endif
}

#ifdef USE_MINIKIN
static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) {
    MinikinFont* minikinFont = new MinikinFontSkia(face);
    bool result = family->addFont(minikinFont);
    minikinFont->Unref();
    return result;
}
#endif

static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) {
#ifdef USE_MINIKIN
    NPE_CHECK_RETURN_ZERO(env, path);
    ScopedUtfChars str(env, path);
    SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
@@ -74,14 +63,10 @@ static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr,
    }
    FontFamily* fontFamily = (FontFamily*)familyPtr;
    return addSkTypeface(fontFamily, face);
#else
    return false;
#endif
}

static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr,
        jobject jassetMgr, jstring jpath) {
#ifdef USE_MINIKIN
    NPE_CHECK_RETURN_ZERO(env, jassetMgr);
    NPE_CHECK_RETURN_ZERO(env, jpath);

@@ -108,9 +93,6 @@ static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPt
    }
    FontFamily* fontFamily = (FontFamily*)familyPtr;
    return addSkTypeface(fontFamily, face);
#else
    return false;
#endif
}

///////////////////////////////////////////////////////////////////////////////
+12 −0
Original line number Diff line number Diff line
@@ -26,6 +26,18 @@

namespace android {

// TODO: these should be defined in Minikin's Layout.h
enum {
    kBidi_LTR = 0,
    kBidi_RTL = 1,
    kBidi_Default_LTR = 2,
    kBidi_Default_RTL = 3,
    kBidi_Force_LTR = 4,
    kBidi_Force_RTL = 5,

    kBidi_Mask = 0x7
};

class Layout;
class TypefaceImpl;

+1 −112
Original line number Diff line number Diff line
@@ -34,14 +34,11 @@
#include "unicode/uloc.h"
#include "unicode/ushape.h"
#include "utils/Blur.h"
#include "TextLayout.h"

#ifdef USE_MINIKIN
#include <minikin/GraphemeBreak.h>
#include <minikin/Layout.h>
#include "MinikinSkia.h"
#include "MinikinUtils.h"
#endif

// temporary for debugging
#include <Caches.h>
@@ -304,14 +301,8 @@ public:
    }

    static jlong setTypeface(JNIEnv* env, jobject clazz, jlong objHandle, jlong typefaceHandle) {
#ifndef USE_MINIKIN
        SkPaint* obj = reinterpret_cast<SkPaint*>(objHandle);
        SkTypeface* typeface = reinterpret_cast<SkTypeface*>(typefaceHandle);
        return reinterpret_cast<jlong>(obj->setTypeface(typeface));
#else
        // TODO(raph): not yet implemented
        // TODO: in Paint refactoring, set typeface on android Paint, not SkPaint
        return NULL;
#endif
    }

    static jlong setRasterizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong rasterizerHandle) {
@@ -437,22 +428,18 @@ public:
        const int kElegantDescent = -500;
        const int kElegantLeading = 0;
        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
#ifdef USE_MINIKIN
        TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
        typeface = TypefaceImpl_resolveDefault(typeface);
        FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
        float saveSkewX = paint->getTextSkewX();
        bool savefakeBold = paint->isFakeBoldText();
        MinikinFontSkia::populateSkPaint(paint, baseFont.font, baseFont.fakery);
#endif
        SkScalar spacing = paint->getFontMetrics(metrics);
#ifdef USE_MINIKIN
        // The populateSkPaint call may have changed fake bold / text skew
        // because we want to measure with those effects applied, so now
        // restore the original settings.
        paint->setTextSkewX(saveSkewX);
        paint->setFakeBoldText(savefakeBold);
#endif
        SkPaintOptionsAndroid paintOpts = paint->getPaintOptionsAndroid();
        if (paintOpts.getFontVariant() == SkPaintOptionsAndroid::kElegant_Variant) {
            SkScalar size = paint->getTextSize();
@@ -534,17 +521,11 @@ public:
        const jchar* textArray = env->GetCharArrayElements(text, NULL);
        jfloat result = 0;

#ifdef USE_MINIKIN
        Layout layout;
        TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(textArray, index, count, textLength, css);
        result = layout.getAdvance();
#else
        TextLayout::getTextRunAdvances(paint, textArray, index, count, textLength,
                bidiFlags, NULL /* dont need all advances */, &result);
#endif

        env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
        return result;
    }
@@ -568,16 +549,11 @@ public:
        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
        jfloat width = 0;

#ifdef USE_MINIKIN
        Layout layout;
        TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(textArray, start, count, textLength, css);
        width = layout.getAdvance();
#else
        TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
                bidiFlags, NULL /* dont need all advances */, &width);
#endif

        env->ReleaseStringChars(text, textArray);
        return width;
@@ -596,16 +572,11 @@ public:
        SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
        jfloat width = 0;

#ifdef USE_MINIKIN
        Layout layout;
        TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(textArray, 0, textLength, textLength, css);
        width = layout.getAdvance();
#else
        TextLayout::getTextRunAdvances(paint, textArray, 0, textLength, textLength,
                bidiFlags, NULL /* dont need all advances */, &width);
#endif

        env->ReleaseStringChars(text, textArray);
        return width;
@@ -632,15 +603,10 @@ public:
        AutoJavaFloatArray autoWidths(env, widths, count);
        jfloat* widthsArray = autoWidths.ptr();

#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(text, 0, count, count, css);
        layout.getAdvances(widthsArray);
#else
        TextLayout::getTextRunAdvances(paint, text, 0, count, count,
                bidiFlags, widthsArray, NULL /* dont need totalAdvance */);
#endif

        return count;
    }
@@ -691,16 +657,11 @@ public:

        int bidiFlags = isRtl ? kBidi_Force_RTL : kBidi_Force_LTR;

#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(text, start, count, contextCount, css);
        layout.getAdvances(advancesArray);
        totalAdvance = layout.getAdvance();
#else
        TextLayout::getTextRunAdvances(paint, text, start, count, contextCount, bidiFlags,
                                       advancesArray, &totalAdvance);
#endif

        if (advances != NULL) {
            env->SetFloatArrayRegion(advances, advancesIndex, count, advancesArray);
@@ -738,52 +699,9 @@ public:

    static jint doTextRunCursor(JNIEnv *env, SkPaint* paint, const jchar *text, jint start,
            jint count, jint flags, jint offset, jint opt) {
#ifdef USE_MINIKIN
        GraphemeBreak::MoveOpt moveOpt = GraphemeBreak::MoveOpt(opt);
        size_t result = GraphemeBreak::getTextRunCursor(text, start, count, offset, moveOpt);
        return static_cast<jint>(result);
#else
        jfloat scalarArray[count];

        TextLayout::getTextRunAdvances(paint, text, start, count, start + count, flags,
                scalarArray, NULL /* dont need totalAdvance */);

        jint pos = offset - start;
        switch (opt) {
        case AFTER:
          if (pos < count) {
            pos += 1;
          }
          // fall through
        case AT_OR_AFTER:
          while (pos < count && scalarArray[pos] == 0) {
            ++pos;
          }
          break;
        case BEFORE:
          if (pos > 0) {
            --pos;
          }
          // fall through
        case AT_OR_BEFORE:
          while (pos > 0 && scalarArray[pos] == 0) {
            --pos;
          }
          break;
        case AT:
        default:
          if (scalarArray[pos] == 0) {
            pos = -1;
          }
          break;
        }

        if (pos != -1) {
          pos += start;
        }

        return pos;
#endif
    }

    static jint getTextRunCursor___C(JNIEnv* env, jobject clazz, jlong paintHandle, jcharArray text,
@@ -806,7 +724,6 @@ public:
        return result;
    }

#ifdef USE_MINIKIN
    class GetTextFunctor {
    public:
        GetTextFunctor(const Layout& layout, SkPath* path, jfloat x, jfloat y, SkPaint* paint,
@@ -837,11 +754,9 @@ public:
        SkPoint* pos;
        SkPath tmpPath;
    };
#endif

    static void getTextPath(JNIEnv* env, SkPaint* paint, TypefaceImpl* typeface, const jchar* text,
            jint count, jint bidiFlags, jfloat x, jfloat y, SkPath* path) {
#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, paint, bidiFlags, typeface);
        layout.doLayout(text, 0, count, count, css);
@@ -858,9 +773,6 @@ public:
        paint->setTextAlign(align);
        delete[] glyphs;
        delete[] pos;
#else
        TextLayout::getTextPath(paint, text, count, bidiFlags, x, y, path);
#endif
    }

    static void getTextPath___C(JNIEnv* env, jobject clazz, jlong paintHandle,
@@ -908,7 +820,6 @@ public:
        size_t measuredCount = 0;
        float measured = 0;

#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, &paint, bidiFlags, typeface);
        layout.doLayout(text, 0, count, count, css);
@@ -929,19 +840,6 @@ public:
            measured += width;
        }
        delete[] advances;
#else
        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
                text, 0, count, count, bidiFlags);
        if (value == NULL) {
            return 0;
        }
        SkScalar m;
        size_t bytes = paint.breakText(value->getGlyphs(), value->getGlyphsCount() << 1,
                maxWidth, &m, textBufferDirection);
        SkASSERT((bytes & 1) == 0);
        measuredCount = bytes >> 1;
        measured = SkScalarToFloat(m);
#endif

        if (jmeasured && env->GetArrayLength(jmeasured) > 0) {
            AutoJavaFloatArray autoMeasured(env, jmeasured, 1);
@@ -1003,7 +901,6 @@ public:
        SkRect  r;
        SkIRect ir;

#ifdef USE_MINIKIN
        Layout layout;
        std::string css = MinikinUtils::setLayoutProperties(&layout, &paint, bidiFlags, typeface);
        layout.doLayout(text, 0, count, count, css);
@@ -1013,14 +910,6 @@ public:
        r.fTop = rect.mTop;
        r.fRight = rect.mRight;
        r.fBottom = rect.mBottom;
#else
        sp<TextLayoutValue> value = TextLayoutEngine::getInstance().getValue(&paint,
                text, 0, count, count, bidiFlags);
        if (value == NULL) {
            return;
        }
        paint.measureText(value->getGlyphs(), value->getGlyphsCount() << 1, &r);
#endif
        r.roundOut(&ir);
        GraphicsJNI::irect_to_jrect(ir, env, bounds);
    }
Loading