/*
 * Copyright 2015 Hippo Seven
 *
 * 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.
 */

package com.hippo.ehviewer.gallery.glrenderer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;

public class XmlResourceTexture extends UploadedTexture {

    private Context mContext;
    private int mResId;
    private int mWidth;
    private int mHeight;

    public XmlResourceTexture(Context context, @DrawableRes int resId) {
        mContext = context;
        mResId = resId;
        setOpaque(false);
    }

    public void setBound(int width, int height) {
        mWidth = width;
        mHeight = height;
        invalidateContent();
    }

    @Override
    protected Bitmap onGetBitmap() {
        Drawable drawable = ContextCompat.getDrawable(mContext, mResId);

        int width = mWidth;
        int height = mHeight;
        if (mWidth <= 0) {
            width = drawable.getIntrinsicWidth();
        }
        if (mHeight <= 0) {
            height = drawable.getIntrinsicHeight();
        }
        if (mWidth <= 0) {
            width = 1;
        }
        if (mHeight <= 0) {
            height = 1;
        }

        drawable.setBounds(0, 0, width, height);

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);

        return bitmap;
    }

    @Override
    protected void onFreeBitmap(Bitmap bitmap) {
        if (!inFinalizer()) {
            bitmap.recycle();
        }
    }
}

@@ -1517,34 +1517,33 @@
 ht;%0A        if (
-mW
+w
 idth %3C= 0) %7B%0A   
@@ -1603,34 +1603,33 @@
   %7D%0A        if (
-mH
+h
 eight %3C= 0) %7B%0A  
@@ -1700,18 +1700,17 @@
     if (
-mW
+w
 idth %3C= 
@@ -1759,18 +1759,17 @@
     if (
-mH
+h
 eight %3C=

Fix XmlResourceTexture bounds error