Android Open Source - android_font_widgets Typeface Style






From Project

Back to project page android_font_widgets.

License

The source code is released under:

[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...

If you think the Android project android_font_widgets listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * =================================================================================================
 *                 Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License
 * you may obtain at/*from w ww  .jav  a2s . c  om*/
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * You can redistribute, modify or publish any part of the code written within this file but as it
 * is described in the License, the software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.wit.android.ui.widget.font;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.AttributeSet;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <h3>Enum Overview</h3>
 * Represents each type of type face from {@link android.graphics.Typeface} class.
 *
 * @author Martin Albedinsky
 */
public enum TypefaceStyle {

  /**
   * Types =======================================================================================
   */

  /**
   * Type representing the <b>normal</b> text style.
   * <p>
   * System constant: <b>{@link android.graphics.Typeface#NORMAL}</b>
   * <p>
   * Tag: <b>""</b>
   */
  NORMAL(Typeface.NORMAL, ""),
  /**
   * Type representing the <b>bold</b> text style.
   * <p>
   * System constant: <b>{@link android.graphics.Typeface#BOLD}</b>
   * <p>
   * Tag: <b>_bold</b>
   */
  BOLD(Typeface.BOLD, "_bold"),
  /**
   * Type representing the <b>italic</b> text style.
   * <p>
   * System constant: <b>{@link android.graphics.Typeface#ITALIC}</b>
   * <p>
   * Tag: <b>_italic</b>
   */
  ITALIC(Typeface.ITALIC, "_italic"),
  /**
   * Type representing the <b>bold|italic</b> text style.
   * <p>
   * System constant: <b>{@link android.graphics.Typeface#BOLD_ITALIC}</b>
   * <p>
   * Tag: <b>_bold_italic</b>
   */
  BOLD_ITALIC(Typeface.BOLD_ITALIC, "_bold_italic");

  /**
   * Interface ===================================================================================
   */

  /**
   * <h3>Annotation Overview</h3>
   * Defines an annotation for determining set of allowed text style flags.
   */
  @Retention(RetentionPolicy.SOURCE)
  @IntDef(flag = true, value = {Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC})
  public @interface TextStyle {}

  /**
   * Members =====================================================================================
   */

  /**
   * Matcher for all TypefaceStyle available tags.
   */
  private static final Matcher ALL_STYLE_TAGS_MATCHER = Pattern.compile("(.*)(_bold|_italic)\\.ttf").matcher("");

  /**
   * Builder used to correct font path.
   */
  private static final StringBuilder BUILDER = new StringBuilder();

  /**
   * The constant specific for this TypefaceStyle. See {@link android.graphics.Typeface} style constants for more info.
   */
  public final int systemConstant;

  /**
   * Tag of this TypefaceStyle instance.
   * <p>
   * This tag is used to correct path to font for font widget depending on the value of its text
   * style attribute obtained form style.
   * <p>
   * See {@link #obtainTextStyle(android.content.Context, int)}, {@link #obtainTextStyle(android.content.Context, android.util.AttributeSet)}
   * and {@link #correctFontPath(String)} for more info.
   */
  public final String styleTag;

  /**
   * Constructors ================================================================================
   */

  /**
   * Creates a new instance of TypefaceStyle with the given parameters.
   *
   * @param flag A system constant specific for newly creating TypefaceStyle.
   * @param tag  A tag used to correct path to font.
   */
  private TypefaceStyle(int flag, String tag) {
    this.systemConstant = flag;
    this.styleTag = tag;
  }

  /**
   * Methods =====================================================================================
   */

  /**
   * Resolves an instance of TypefaceStyle for the requested <var>systemConstant</var>.
   *
   * @param systemConstant One of {@link android.graphics.Typeface#NORMAL}, {@link android.graphics.Typeface#BOLD}, {@link android.graphics.Typeface#ITALIC}
   *                       or {@link android.graphics.Typeface#BOLD_ITALIC}.
   * @return Resolved instance of TypefaceStyle or {@code null} if there is no TypefaceStyle
   * with the requested <var>systemConstant</var> available.
   */
  @NonNull
  public static TypefaceStyle resolve(int systemConstant) {
    for (TypefaceStyle style : values()) {
      if (style.systemConstant == systemConstant) {
        return style;
      }
    }
    return NORMAL;
  }

  /**
   * Obtains an integer flag representing the text style from the given <var>attrs</var>.
   *
   * @param context Valid context used to obtain requested text style flag.
   * @param attrs   Set of attributes from which should be text style flag obtained.
   * @return A value of {@link android.R.attr#textStyle android:textStyle} attribute obtained from
   * the given <var>attrs</var> or {@link android.graphics.Typeface#NORMAL} if there is no such attribute within the
   * passed attributes.
   */
  public static int obtainTextStyle(@NonNull Context context, @NonNull AttributeSet attrs) {
    return processTypedArray(context.obtainStyledAttributes(attrs, new int[]{android.R.attr.textStyle}));
  }

  /**
   * Obtains an integer flag representing the text style from the given <var>style</var>.
   *
   * @param context Valid context used to obtain requested text style flag.
   * @param style   A resource id of the style from which should be text style flag obtained.
   * @return A value of {@link android.R.attr#textStyle android:textStyle} attribute obtained from
   * the given <var>style</var> or {@link android.graphics.Typeface#NORMAL} if there is no such attribute within the
   * passed style.
   */
  @TextStyle
  @SuppressWarnings("ResourceType")
  public static int obtainTextStyle(@NonNull Context context, @StyleRes int style) {
    return processTypedArray(context.obtainStyledAttributes(style, new int[]{android.R.attr.textStyle}));
  }

  /**
   * Processes the given typed array to obtain an integer flag representing the text style.
   *
   * @param typedArray Typed array to process.
   * @return A value of {@link android.R.attr#textStyle android:textStyle} attribute obtained from
   * the given <var>typedArray</var> or {@link android.graphics.Typeface#NORMAL} if there is no such attribute within
   * the passed TypedArray or the given array is {@code null}.
   */
  @TextStyle
  @SuppressWarnings("ResourceType")
  private static int processTypedArray(TypedArray typedArray) {
    int textStyle = Typeface.NORMAL;
    if (typedArray != null) {
      textStyle = typedArray.getInt(0, textStyle);
      typedArray.recycle();
    }
    return textStyle;
  }

  /**
   * Corrects the given <var>fontPath</var> depends on this TypefaceStyle type.
   * <p>
   * This will for example correct path <b>/textFonts/custom_font.ttf</b> to <b>/textFonts/custom_font_bold.ttf</b>
   * if this TypefaceStyle is type of {@link #BOLD}.
   *
   * @param fontPath A font path which should be corrected. A {@link #styleTag TAG} specific for
   *                 this TypefaceStyle type will be appended to it.
   * @return Corrected font path with a TAG specific for this TypefaceStyle type if such a TAG was
   * not presented in the given path yet.
   */
  public String correctFontPath(@NonNull String fontPath) {
    if (this != NORMAL && !ALL_STYLE_TAGS_MATCHER.reset(fontPath).matches()) {
      fontPath = appendFontStyleTag(fontPath);
    }
    return fontPath;
  }

  /**
   * Appends the given path with the TAG specific for this TypefaceStyle.
   *
   * @param path A path to append with tag.
   * @return Appended path.
   */
  private String appendFontStyleTag(String path) {
    // Remove from the end the '.ttf' suffix, append style tag and append '.ttf' suffix back.
    BUILDER.setLength(0);
    return BUILDER
        .append(path.replace(Font.TTF_SUFFIX, ""))
        .append(styleTag)
        .append(Font.TTF_SUFFIX)
        .toString();
  }
}




Java Source Code List

com.wit.android.ui.widget.font.FontApplier.java
com.wit.android.ui.widget.font.FontAutoCompleteTextView.java
com.wit.android.ui.widget.font.FontButton.java
com.wit.android.ui.widget.font.FontCheckBox.java
com.wit.android.ui.widget.font.FontEditText.java
com.wit.android.ui.widget.font.FontRadioButton.java
com.wit.android.ui.widget.font.FontTextView.java
com.wit.android.ui.widget.font.FontWidgetsConfig.java
com.wit.android.ui.widget.font.Font.java
com.wit.android.ui.widget.font.TypefaceStyle.java