
/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_RS_BUILD_FOR_HOST
#include "rsContext.h"
#else
#include "rsContextHostStub.h"
#endif

#include "rsFont.h"
#include "rsProgramFragment.h"
#include FT_BITMAP_H

#include <GLES/gl.h>
#include <GLES/glext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

using namespace android;
using namespace android::renderscript;

Font::Font(Context *rsc) : ObjectBase(rsc), mCachedGlyphs(NULL)
{
    mAllocFile = __FILE__;
    mAllocLine = __LINE__;
    mInitialized = false;
    mHasKerning = false;
    mFace = NULL;
}

bool Font::init(const char *name, uint32_t fontSize, uint32_t dpi)
{
    if(mInitialized) {
        LOGE("Reinitialization of fonts not supported");
        return false;
    }

    String8 fontsDir("/fonts/");
    String8 fullPath(getenv("ANDROID_ROOT"));
    fullPath += fontsDir;
    fullPath += name;

    FT_Error error = FT_New_Face(mRSC->mStateFont.getLib(), fullPath.string(), 0, &mFace);
    if(error) {
        LOGE("Unable to initialize font %s", fullPath.string());
        return false;
    }

    mFontName = name;
    mFontSize = fontSize;
    mDpi = dpi;

    error = FT_Set_Char_Size(mFace, fontSize * 64, 0, dpi, 0);
    if(error) {
        LOGE("Unable to set font size on %s", fullPath.string());
        return false;
    }

    mHasKerning = FT_HAS_KERNING(mFace);

    mInitialized = true;
    return true;
}

void Font::invalidateTextureCache()
{
    for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) {
        mCachedGlyphs.valueAt(i)->mIsValid = false;
    }
}

void Font::drawCachedGlyph(CachedGlyphInfo *glyph, int x, int y)
{
    FontState *state = &mRSC->mStateFont;

    int nPenX = x + glyph->mBitmapLeft;
    int nPenY = y - glyph->mBitmapTop + glyph->mBitmapHeight;

    state->appendMeshQuad(nPenX, nPenY, 0,
                            glyph->mBitmapMinU, glyph->mBitmapMaxV,

                            nPenX + (int)glyph->mBitmapWidth, nPenY, 0,
                            glyph->mBitmapMaxU, glyph->mBitmapMaxV,

                            nPenX + (int)glyph->mBitmapWidth, nPenY - (int)glyph->mBitmapHeight, 0,
                            glyph->mBitmapMaxU, glyph->mBitmapMinV,

                            nPenX, nPenY - (int)glyph->mBitmapHeight, 0,
                            glyph->mBitmapMinU, glyph->mBitmapMinV);
}

void Font::renderUTF(const char *text, uint32_t len, uint32_t start, int numGlyphs, int x, int y)
{
    if(!mInitialized || numGlyphs == 0 || text == NULL || len == 0) {
        return;
    }

    int penX = x, penY = y;
    int glyphsLeft = 1;
    if(numGlyphs > 0) {
        glyphsLeft = numGlyphs;
    }

    size_t index = start;
    size_t nextIndex = 0;

    while (glyphsLeft > 0) {

        int32_t utfChar = utf32_at(text, len, index, &nextIndex);

        // Reached the end of the string or encountered
        if(utfChar < 0) {
            break;
        }

        // Move to the next character in the array
        index = nextIndex;

        CachedGlyphInfo *cachedGlyph = getCachedUTFChar(utfChar);

        // If it's still not valid, we couldn't cache it, so we shouldn't draw garbage
        if(cachedGlyph->mIsValid) {
            drawCachedGlyph(cachedGlyph, penX, penY);
        }

        penX += (cachedGlyph->mAdvance.x >> 6);

        // If we were given a specific number of glyphs, decrement
        if(numGlyphs > 0) {
            glyphsLeft --;
        }
    }
}

Font::CachedGlyphInfo* Font::getCachedUTFChar(int32_t utfChar) {

    CachedGlyphInfo *cachedGlyph = mCachedGlyphs.valueFor((uint32_t)utfChar);
    if(cachedGlyph == NULL) {
        cachedGlyph = cacheGlyph((uint32_t)utfChar);
    }
    // Is the glyph still in texture cache?
    if(!cachedGlyph->mIsValid) {
        updateGlyphCache(cachedGlyph);
    }

    return cachedGlyph;
}

void Font::updateGlyphCache(CachedGlyphInfo *glyph)
{
    FT_Error error = FT_Load_Glyph( mFace, glyph->mGlyphIndex, FT_LOAD_RENDER );
    if(error) {
        LOGE("Couldn't load glyph.");
        return;
    }

    glyph->mAdvance = mFace->glyph->advance;
    glyph->mBitmapLeft = mFace->glyph->bitmap_left;
    glyph->mBitmapTop = mFace->glyph->bitmap_top;

    FT_Bitmap *bitmap = &mFace->glyph->bitmap;

    // Now copy the bitmap into the cache texture
    uint32_t startX = 0;
    uint32_t startY = 0;

    // Let the font state figure out where to put the bitmap
    FontState *state = &mRSC->mStateFont;
    glyph->mIsValid = state->cacheBitmap(bitmap, &startX, &startY);

    if(!glyph->mIsValid) {
        return;
    }

    uint32_t endX = startX + bitmap->width;
    uint32_t endY = startY + bitmap->rows;

    glyph->mBitmapMinX = startX;
    glyph->mBitmapMinY = startY;
    glyph->mBitmapWidth = bitmap->width;
    glyph->mBitmapHeight = bitmap->rows;

    uint32_t cacheWidth = state->getCacheTextureType()->getDimX();
    uint32_t cacheHeight = state->getCacheTextureType()->getDimY();

    glyph->mBitmapMinU = (float)startX / (float)cacheWidth;
    glyph->mBitmapMinV = (float)startY / (float)cacheHeight;
    glyph->mBitmapMaxU = (float)endX / (float)cacheWidth;
    glyph->mBitmapMaxV = (float)endY / (float)cacheHeight;
}

Font::CachedGlyphInfo *Font::cacheGlyph(uint32_t glyph)
{
    CachedGlyphInfo *newGlyph = new CachedGlyphInfo();
    mCachedGlyphs.add(glyph, newGlyph);

    newGlyph->mGlyphIndex = FT_Get_Char_Index(mFace, glyph);
    newGlyph->mIsValid = false;

    updateGlyphCache(newGlyph);

    return newGlyph;
}

Font * Font::create(Context *rsc, const char *name, uint32_t fontSize, uint32_t dpi)
{
    rsc->mStateFont.checkInit();
    Vector<Font*> &activeFonts = rsc->mStateFont.mActiveFonts;

    for(uint32_t i = 0; i < activeFonts.size(); i ++) {
        Font *ithFont = activeFonts[i];
        if(ithFont->mFontName == name && ithFont->mFontSize == fontSize && ithFont->mDpi == dpi) {
            return ithFont;
        }
    }

    Font *newFont = new Font(rsc);
    bool isInitialized = newFont->init(name, fontSize, dpi);
    if(isInitialized) {
        activeFonts.push(newFont);
        rsc->mStateFont.precacheLatin(newFont);
        return newFont;
    }

    delete newFont;
    return NULL;

}

Font::~Font()
{
    if(mFace) {
        FT_Done_Face(mFace);
    }

    for (uint32_t ct = 0; ct < mRSC->mStateFont.mActiveFonts.size(); ct++) {
        if (mRSC->mStateFont.mActiveFonts[ct] == this) {
            mRSC->mStateFont.mActiveFonts.removeAt(ct);
            break;
        }
    }

    for(uint32_t i = 0; i < mCachedGlyphs.size(); i ++) {
        CachedGlyphInfo *glyph = mCachedGlyphs.valueAt(i);
        delete glyph;
    }
}

FontState::FontState()
{
    mInitialized = false;
    mMaxNumberOfQuads = 1024;
    mCurrentQuadIndex = 0;
    mRSC = NULL;
    mLibrary = NULL;
    setFontColor(0.1f, 0.1f, 0.1f, 1.0f);
}

FontState::~FontState()
{
    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
        delete mCacheLines[i];
    }

    rsAssert(!mActiveFonts.size());
}

FT_Library FontState::getLib()
{
    if(!mLibrary) {
        FT_Error error = FT_Init_FreeType(&mLibrary);
        if(error) {
            LOGE("Unable to initialize freetype");
            return NULL;
        }
    }

    return mLibrary;
}

void FontState::init(Context *rsc)
{
    mRSC = rsc;
}

void FontState::flushAllAndInvalidate()
{
    if(mCurrentQuadIndex != 0) {
        issueDrawCommand();
        mCurrentQuadIndex = 0;
    }
    for(uint32_t i = 0; i < mActiveFonts.size(); i ++) {
        mActiveFonts[i]->invalidateTextureCache();
    }
    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
        mCacheLines[i]->mCurrentCol = 0;
    }
}

bool FontState::cacheBitmap(FT_Bitmap *bitmap, uint32_t *retOriginX, uint32_t *retOriginY)
{
    // If the glyph is too tall, don't cache it
    if((uint32_t)bitmap->rows > mCacheLines[mCacheLines.size()-1]->mMaxHeight) {
        LOGE("Font size to large to fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows);
        return false;
    }

    // Now copy the bitmap into the cache texture
    uint32_t startX = 0;
    uint32_t startY = 0;

    bool bitmapFit = false;
    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
        bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY);
        if(bitmapFit) {
            break;
        }
    }

    // If the new glyph didn't fit, flush the state so far and invalidate everything
    if(!bitmapFit) {
        flushAllAndInvalidate();

        // Try to fit it again
        for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
            bitmapFit = mCacheLines[i]->fitBitmap(bitmap, &startX, &startY);
            if(bitmapFit) {
                break;
            }
        }

        // if we still don't fit, something is wrong and we shouldn't draw
        if(!bitmapFit) {
            LOGE("Bitmap doesn't fit in cache. width, height = %i, %i", (int)bitmap->width, (int)bitmap->rows);
            return false;
        }
    }

    *retOriginX = startX;
    *retOriginY = startY;

    uint32_t endX = startX + bitmap->width;
    uint32_t endY = startY + bitmap->rows;

    uint32_t cacheWidth = getCacheTextureType()->getDimX();

    unsigned char *cacheBuffer = (unsigned char*)mTextTexture->getPtr();
    unsigned char *bitmapBuffer = bitmap->buffer;

    uint32_t cacheX = 0, bX = 0, cacheY = 0, bY = 0;
    for(cacheX = startX, bX = 0; cacheX < endX; cacheX ++, bX ++) {
        for(cacheY = startY, bY = 0; cacheY < endY; cacheY ++, bY ++) {
            unsigned char tempCol = bitmapBuffer[bY * bitmap->width + bX];
            cacheBuffer[cacheY*cacheWidth + cacheX] = tempCol;
        }
    }

    // This will dirty the texture and the shader so next time
    // we draw it will upload the data
    mTextTexture->deferedUploadToTexture(mRSC, false, 0);
    mFontShaderF->bindTexture(0, mTextTexture.get());

    // Some debug code
    /*for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
        LOGE("Cache Line: H: %u Empty Space: %f",
             mCacheLines[i]->mMaxHeight,
              (1.0f - (float)mCacheLines[i]->mCurrentCol/(float)mCacheLines[i]->mMaxWidth)*100.0f);

    }*/

    return true;
}

void FontState::initRenderState()
{
    String8 shaderString("varying vec4 varTex0;\n");
    shaderString.append("void main() {\n");
    shaderString.append("  lowp vec4 col = UNI_Color;\n");
    shaderString.append("  col.a = texture2D(UNI_Tex0, varTex0.xy).a;\n");
    shaderString.append("  gl_FragColor = col;\n");
    shaderString.append("}\n");

    const Element *colorElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 4);
    mRSC->mStateElement.elementBuilderBegin();
    mRSC->mStateElement.elementBuilderAdd(colorElem, "Color", 1);
    const Element *constInput = mRSC->mStateElement.elementBuilderCreate(mRSC);

    Type *inputType = new Type(mRSC);
    inputType->setElement(constInput);
    inputType->setDimX(1);
    inputType->compute();

    uint32_t tmp[4];
    tmp[0] = RS_PROGRAM_PARAM_CONSTANT;
    tmp[1] = (uint32_t)inputType;
    tmp[2] = RS_PROGRAM_PARAM_TEXTURE_COUNT;
    tmp[3] = 1;

    mFontShaderFConstant.set(new Allocation(mRSC, inputType));
    ProgramFragment *pf = new ProgramFragment(mRSC, shaderString.string(),
                                              shaderString.length(), tmp, 4);
    mFontShaderF.set(pf);
    mFontShaderF->bindAllocation(mFontShaderFConstant.get(), 0);

    Sampler *sampler = new Sampler(mRSC, RS_SAMPLER_NEAREST, RS_SAMPLER_NEAREST,
                                      RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP, RS_SAMPLER_CLAMP);
    mFontSampler.set(sampler);
    mFontShaderF->bindSampler(0, sampler);

    ProgramStore *fontStore = new ProgramStore(mRSC);
    mFontProgramStore.set(fontStore);
    mFontProgramStore->setDepthFunc(RS_DEPTH_FUNC_ALWAYS);
    mFontProgramStore->setBlendFunc(RS_BLEND_SRC_SRC_ALPHA, RS_BLEND_DST_ONE_MINUS_SRC_ALPHA);
    mFontProgramStore->setDitherEnable(false);
    mFontProgramStore->setDepthMask(false);
}

void FontState::initTextTexture()
{
    const Element *alphaElem = Element::create(mRSC, RS_TYPE_UNSIGNED_8, RS_KIND_PIXEL_A, true, 1);

    // We will allocate a texture to initially hold 32 character bitmaps
    Type *texType = new Type(mRSC);
    texType->setElement(alphaElem);
    texType->setDimX(1024);
    texType->setDimY(256);
    texType->compute();

    Allocation *cacheAlloc = new Allocation(mRSC, texType);
    mTextTexture.set(cacheAlloc);
    mTextTexture->deferedUploadToTexture(mRSC, false, 0);

    // Split up our cache texture into lines of certain widths
    int nextLine = 0;
    mCacheLines.push(new CacheTextureLine(16, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(24, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(32, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(40, texType->getDimX(), nextLine, 0));
    nextLine += mCacheLines.top()->mMaxHeight;
    mCacheLines.push(new CacheTextureLine(texType->getDimY() - nextLine, texType->getDimX(), nextLine, 0));
}

// Avoid having to reallocate memory and render quad by quad
void FontState::initVertexArrayBuffers()
{
    // Now lets write index data
    const Element *indexElem = Element::create(mRSC, RS_TYPE_UNSIGNED_16, RS_KIND_USER, false, 1);
    Type *indexType = new Type(mRSC);
    uint32_t numIndicies = mMaxNumberOfQuads * 6;
    indexType->setDimX(numIndicies);
    indexType->setElement(indexElem);
    indexType->compute();

    Allocation *indexAlloc = new Allocation(mRSC, indexType);
    uint16_t *indexPtr = (uint16_t*)indexAlloc->getPtr();

    // Four verts, two triangles , six indices per quad
    for(uint32_t i = 0; i < mMaxNumberOfQuads; i ++) {
        int i6 = i * 6;
        int i4 = i * 4;

        indexPtr[i6 + 0] = i4 + 0;
        indexPtr[i6 + 1] = i4 + 1;
        indexPtr[i6 + 2] = i4 + 2;

        indexPtr[i6 + 3] = i4 + 0;
        indexPtr[i6 + 4] = i4 + 2;
        indexPtr[i6 + 5] = i4 + 3;
    }

    indexAlloc->deferedUploadToBufferObject(mRSC);
    mIndexBuffer.set(indexAlloc);

    const Element *posElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 3);
    const Element *texElem = Element::create(mRSC, RS_TYPE_FLOAT_32, RS_KIND_USER, false, 2);

    const Element *elemArray[2];
    elemArray[0] = posElem;
    elemArray[1] = texElem;

    String8 posName("position");
    String8 texName("texture0");

    const char *nameArray[2];
    nameArray[0] = posName.string();
    nameArray[1] = texName.string();
    size_t lengths[2];
    lengths[0] = posName.size();
    lengths[1] = texName.size();
    uint32_t arraySizes[2] = {1, 1};

    const Element *vertexDataElem = Element::create(mRSC, 2, elemArray, nameArray, lengths, arraySizes);

    Type *vertexDataType = new Type(mRSC);
    vertexDataType->setDimX(mMaxNumberOfQuads * 4);
    vertexDataType->setElement(vertexDataElem);
    vertexDataType->compute();

    Allocation *vertexAlloc = new Allocation(mRSC, vertexDataType);
    mTextMeshPtr = (float*)vertexAlloc->getPtr();

    mVertexArray.set(vertexAlloc);
}

// We don't want to allocate anything unless we actually draw text
void FontState::checkInit()
{
    if(mInitialized) {
        return;
    }

    initTextTexture();
    initRenderState();

    initVertexArrayBuffers();

    // We store a string with letters in a rough frequency of occurrence
    mLatinPrecache = String8(" eisarntolcdugpmhbyfvkwzxjq");
    mLatinPrecache += String8("EISARNTOLCDUGPMHBYFVKWZXJQ");
    mLatinPrecache += String8(",.?!()-+@;:`'");
    mLatinPrecache += String8("0123456789");

    mInitialized = true;
}

void FontState::issueDrawCommand() {

    ObjectBaseRef<const ProgramVertex> tmpV(mRSC->getVertex());
    mRSC->setVertex(mRSC->getDefaultProgramVertex());

    ObjectBaseRef<const ProgramRaster> tmpR(mRSC->getRaster());
    mRSC->setRaster(mRSC->getDefaultProgramRaster());

    ObjectBaseRef<const ProgramFragment> tmpF(mRSC->getFragment());
    mRSC->setFragment(mFontShaderF.get());

    ObjectBaseRef<const ProgramStore> tmpPS(mRSC->getFragmentStore());
    mRSC->setFragmentStore(mFontProgramStore.get());

    if(mFontColorDirty) {
        mFontShaderFConstant->data(mRSC, &mFontColor, 4*sizeof(float));
        mFontColorDirty = false;
    }

    if (!mRSC->setupCheck()) {
        mRSC->setVertex((ProgramVertex *)tmpV.get());
        mRSC->setRaster((ProgramRaster *)tmpR.get());
        mRSC->setFragment((ProgramFragment *)tmpF.get());
        mRSC->setFragmentStore((ProgramStore *)tmpPS.get());
        return;
    }

    float *vtx = (float*)mVertexArray->getPtr();
    float *tex = vtx + 3;

    VertexArray va;
    va.add(GL_FLOAT, 3, 20, false, (uint32_t)vtx, "position");
    va.add(GL_FLOAT, 2, 20, false, (uint32_t)tex, "texture0");
    va.setupGL2(mRSC, &mRSC->mStateVertexArray, &mRSC->mShaderCache);

    mIndexBuffer->uploadCheck(mRSC);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer->getBufferObjectID());
    glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, (uint16_t *)(0));

    // Reset the state
    mRSC->setVertex((ProgramVertex *)tmpV.get());
    mRSC->setRaster((ProgramRaster *)tmpR.get());
    mRSC->setFragment((ProgramFragment *)tmpF.get());
    mRSC->setFragmentStore((ProgramStore *)tmpPS.get());
}

void FontState::appendMeshQuad(float x1, float y1, float z1,
                                  float u1, float v1,
                                  float x2, float y2, float z2,
                                  float u2, float v2,
                                  float x3, float y3, float z3,
                                  float u3, float v3,
                                  float x4, float y4, float z4,
                                  float u4, float v4)
{
    const uint32_t vertsPerQuad = 4;
    const uint32_t floatsPerVert = 5;
    float *currentPos = mTextMeshPtr + mCurrentQuadIndex * vertsPerQuad * floatsPerVert;

    // Cull things that are off the screen
    float width = (float)mRSC->getWidth();
    float height = (float)mRSC->getHeight();

    if(x1 > width || y1 < 0.0f || x2 < 0 || y4 > height) {
        return;
    }

    /*LOGE("V0 x: %f y: %f z: %f", x1, y1, z1);
    LOGE("V1 x: %f y: %f z: %f", x2, y2, z2);
    LOGE("V2 x: %f y: %f z: %f", x3, y3, z3);
    LOGE("V3 x: %f y: %f z: %f", x4, y4, z4);*/

    (*currentPos++) = x1;
    (*currentPos++) = y1;
    (*currentPos++) = z1;
    (*currentPos++) = u1;
    (*currentPos++) = v1;

    (*currentPos++) = x2;
    (*currentPos++) = y2;
    (*currentPos++) = z2;
    (*currentPos++) = u2;
    (*currentPos++) = v2;

    (*currentPos++) = x3;
    (*currentPos++) = y3;
    (*currentPos++) = z3;
    (*currentPos++) = u3;
    (*currentPos++) = v3;

    (*currentPos++) = x4;
    (*currentPos++) = y4;
    (*currentPos++) = z4;
    (*currentPos++) = u4;
    (*currentPos++) = v4;

    mCurrentQuadIndex ++;

    if(mCurrentQuadIndex == mMaxNumberOfQuads) {
        issueDrawCommand();
        mCurrentQuadIndex = 0;
    }
}

uint32_t FontState::getRemainingCacheCapacity() {
    uint32_t remainingCapacity = 0;
    uint32_t totalPixels = 0;
    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
         remainingCapacity += (mCacheLines[i]->mMaxWidth - mCacheLines[i]->mCurrentCol);
         totalPixels += mCacheLines[i]->mMaxWidth;
    }
    remainingCapacity = (remainingCapacity * 100) / totalPixels;
    return remainingCapacity;
}

void FontState::precacheLatin(Font *font) {
    // Remaining capacity is measured in %
    uint32_t remainingCapacity = getRemainingCacheCapacity();
    uint32_t precacheIdx = 0;
    while(remainingCapacity > 25 && precacheIdx < mLatinPrecache.size()) {
        font->getCachedUTFChar((int32_t)mLatinPrecache[precacheIdx]);
        remainingCapacity = getRemainingCacheCapacity();
        precacheIdx ++;
    }
}


void FontState::renderText(const char *text, uint32_t len, uint32_t startIndex, int numGlyphs, int x, int y)
{
    checkInit();

    // Render code here
    Font *currentFont = mRSC->getFont();
    if(!currentFont) {
        if(!mDefault.get()) {
            mDefault.set(Font::create(mRSC, "DroidSans.ttf", 16, 96));
        }
        currentFont = mDefault.get();
    }
    if(!currentFont) {
        LOGE("Unable to initialize any fonts");
        return;
    }

    currentFont->renderUTF(text, len, startIndex, numGlyphs, x, y);

    if(mCurrentQuadIndex != 0) {
        issueDrawCommand();
        mCurrentQuadIndex = 0;
    }
}

void FontState::renderText(const char *text, int x, int y)
{
    size_t textLen = strlen(text);
    renderText(text, textLen, 0, -1, x, y);
}

void FontState::renderText(Allocation *alloc, int x, int y)
{
    if(!alloc) {
        return;
    }

    const char *text = (const char *)alloc->getPtr();
    size_t allocSize = alloc->getType()->getSizeBytes();
    renderText(text, allocSize, 0, -1, x, y);
}

void FontState::renderText(Allocation *alloc, uint32_t start, int len, int x, int y)
{
    if(!alloc) {
        return;
    }

    const char *text = (const char *)alloc->getPtr();
    size_t allocSize = alloc->getType()->getSizeBytes();
    renderText(text, allocSize, start, len, x, y);
}

void FontState::setFontColor(float r, float g, float b, float a) {
    mFontColor[0] = r;
    mFontColor[1] = g;
    mFontColor[2] = b;
    mFontColor[3] = a;
    mFontColorDirty = true;
}

void FontState::getFontColor(float *r, float *g, float *b, float *a) const {
    *r = mFontColor[0];
    *g = mFontColor[1];
    *b = mFontColor[2];
    *a = mFontColor[3];
}

void FontState::deinit(Context *rsc)
{
    mInitialized = false;

    mIndexBuffer.clear();
    mVertexArray.clear();

    mFontShaderF.clear();
    mFontSampler.clear();
    mFontProgramStore.clear();

    mTextTexture.clear();
    for(uint32_t i = 0; i < mCacheLines.size(); i ++) {
        delete mCacheLines[i];
    }
    mCacheLines.clear();

    mDefault.clear();

    Vector<Font*> fontsToDereference = mActiveFonts;
    for(uint32_t i = 0; i < fontsToDereference.size(); i ++) {
        fontsToDereference[i]->zeroUserRef();
    }

    if(mLibrary) {
        FT_Done_FreeType( mLibrary );
        mLibrary = NULL;
    }
}

namespace android {
namespace renderscript {

RsFont rsi_FontCreateFromFile(Context *rsc, char const *name, uint32_t fontSize, uint32_t dpi)
{
    Font *newFont = Font::create(rsc, name, fontSize, dpi);
    if(newFont) {
        newFont->incUserRef();
    }
    return newFont;
}

} // renderscript
} // android
