<commit_before>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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.mta.tehreer.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.mta.tehreer.R;
import com.mta.tehreer.graphics.Renderer;
import com.mta.tehreer.graphics.Typeface;
import com.mta.tehreer.graphics.TypefaceManager;
import com.mta.tehreer.layout.ComposedLine;
import com.mta.tehreer.layout.WrapMode;
import com.mta.tehreer.layout.TruncationType;
import com.mta.tehreer.layout.Typesetter;

import java.util.ArrayList;

/**
 * Displays read-only text to the user.
 */
public class TLabel extends View {

    private static final String TRUNCATION_STRING = "...";

    private int mGravity;
    private String mText;
    private int mMaxLines;

    private Renderer mRenderer;
    private Typesetter mTypesetter;
    private WrapMode mWrapMode;
    private TruncationType mTruncationType;
    private ComposedLine mTruncationToken;

    private boolean mRtlText;
    private int mTextWidth;
    private int mTextHeight;

    private ArrayList<ComposedLine> mComposedLines = new ArrayList<>();

    private static float getLineHeight(ComposedLine composedLine) {
        return composedLine.getAscent() + composedLine.getDescent();
    }

    public TLabel(Context context) {
        super(context);
        setup(context, null, 0);
    }

    public TLabel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TLabel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup(context, attrs, defStyleAttr);
    }

    private void setup(Context context, AttributeSet attrs, int defStyleAttr) {
        mRenderer = new Renderer();

        if (attrs != null) {
            setupAttributes(context, attrs, defStyleAttr);
        }
    }

    private void setupAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray values = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TLabel, defStyleAttr, 0);

        try {
            WrapMode wrapMode = null;
            switch (values.getInt(R.styleable.TLabel_wrapMode, 0)) {
            case 0:
                wrapMode = WrapMode.WORD;
                break;

            case 1:
                wrapMode = WrapMode.CHARACTER;
                break;
            }

            TruncationType truncationType = null;
            switch (values.getInt(R.styleable.TLabel_truncationType, 0)) {
            case 1:
                truncationType = TruncationType.START;
                break;

            case 2:
                truncationType = TruncationType.MIDDLE;
                break;

            case 3:
                truncationType = TruncationType.END;
                break;
            }

            setGravity(values.getInt(R.styleable.TLabel_gravity, Gravity.TOP | Gravity.LEFT));
            setMaxLines(values.getInteger(R.styleable.TLabel_maxLines, 0));
            setShadowRadius(values.getDimension(R.styleable.TLabel_shadowRadius, 0.0f));
            setShadowDx(values.getDimension(R.styleable.TLabel_shadowDx, 0.0f));
            setShadowDy(values.getDimension(R.styleable.TLabel_shadowDy, 0.0f));
            setShadowColor(values.getInteger(R.styleable.TLabel_shadowColor, Color.TRANSPARENT));
            setWrapMode(wrapMode);
            setTruncationType(truncationType);
            setTextColor(values.getInteger(R.styleable.TLabel_textColor, Color.BLACK));
            setTextSize(values.getDimensionPixelSize(R.styleable.TLabel_textSize, 16));
            setText(values.getString(R.styleable.TLabel_text));
            if (values.hasValue(R.styleable.TLabel_typeface)) {
                setTypeface(values.getResourceId(R.styleable.TLabel_typeface, 0));
            }
        } finally {
            values.recycle();<commit_msg>[lib] Set default gravity to top start...<commit_after>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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.mta.tehreer.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.mta.tehreer.R;
import com.mta.tehreer.graphics.Renderer;
import com.mta.tehreer.graphics.Typeface;
import com.mta.tehreer.graphics.TypefaceManager;
import com.mta.tehreer.layout.ComposedLine;
import com.mta.tehreer.layout.WrapMode;
import com.mta.tehreer.layout.TruncationType;
import com.mta.tehreer.layout.Typesetter;

import java.util.ArrayList;

/**
 * Displays read-only text to the user.
 */
public class TLabel extends View {

    private static final String TRUNCATION_STRING = "...";

    private int mGravity = Gravity.TOP | Gravity.START;
    private int mMaxLines = 0;

    private Renderer mRenderer = new Renderer();
    private WrapMode mWrapMode = WrapMode.WORD;
    private TruncationType mTruncationType = null;
    private ComposedLine mTruncationToken = null;

    private String mText = "";
    private Typesetter mTypesetter = null;

    private boolean mRtlText = false;
    private int mTextWidth = 0;
    private int mTextHeight = 0;

    private ArrayList<ComposedLine> mComposedLines = new ArrayList<>();

    private static float getLineHeight(ComposedLine composedLine) {
        return composedLine.getAscent() + composedLine.getDescent();
    }

    public TLabel(Context context) {
        super(context);
        setup(context, null, 0);
    }

    public TLabel(Context context, AttributeSet attrs) {
        super(context, attrs);
        setup(context, attrs, 0);
    }

    public TLabel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setup(context, attrs, defStyleAttr);
    }

    private void setup(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            setupAttributes(context, attrs, defStyleAttr);
        }
    }

    private void setupAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray values = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TLabel, defStyleAttr, 0);

        try {
            WrapMode wrapMode = null;
            switch (values.getInt(R.styleable.TLabel_wrapMode, 0)) {
            case 0:
                wrapMode = WrapMode.WORD;
                break;

            case 1:
                wrapMode = WrapMode.CHARACTER;
                break;
            }

            TruncationType truncationType = null;
            switch (values.getInt(R.styleable.TLabel_truncationType, 0)) {
            case 1:
                truncationType = TruncationType.START;
                break;

            case 2:
                truncationType = TruncationType.MIDDLE;
                break;

            case 3:
                truncationType = TruncationType.END;
                break;
            }

            setGravity(values.getInt(R.styleable.TLabel_gravity, Gravity.TOP | Gravity.START));
            setMaxLines(values.getInteger(R.styleable.TLabel_maxLines, 0));
            setShadowRadius(values.getDimension(R.styleable.TLabel_shadowRadius, 0.0f));
            setShadowDx(values.getDimension(R.styleable.TLabel_shadowDx, 0.0f));
            setShadowDy(values.getDimension(R.styleable.TLabel_shadowDy, 0.0f));
            setShadowColor(values.getInteger(R.styleable.TLabel_shadowColor, Color.TRANSPARENT));
            setWrapMode(wrapMode);
            setTruncationType(truncationType);
            setTextColor(values.getInteger(R.styleable.TLabel_textColor, Color.BLACK));
            setTextSize(values.getDimensionPixelSize(R.styleable.TLabel_textSize, 16));
            setText(values.getString(R.styleable.TLabel_text));
            if (values.hasValue(R.styleable.TLabel_typeface)) {
                setTypeface(values.getResourceId(R.styleable.TLabel_typeface, 0));
            }
        } finally {
            values.recycle();
{'fraction_non_alphanumeric': 0.09102646868947709, 'fraction_numerical': 0.006348181622552184, 'mean_word_length': 2.7723214285714284, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2276044', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2663, 'n_words': 624}