Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package android.text;
     18 
     19 import android.content.res.ColorStateList;
     20 import android.graphics.Typeface;
     21 import android.icu.text.UnicodeSet;
     22 import android.icu.text.UnicodeSetIterator;
     23 import android.text.style.TextAppearanceSpan;
     24 
     25 import java.nio.CharBuffer;
     26 import java.util.ArrayList;
     27 import java.util.Random;
     28 
     29 public class TextPerfUtils {
     30 
     31     private static final int PARA_LENGTH = 500;  // Number of characters in a paragraph.
     32 
     33     private Random mRandom = new Random(0);
     34 
     35     private static final String[] ALPHABET;
     36     private static final int ALPHABET_LENGTH;
     37     static {
     38         String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
     39         ALPHABET_LENGTH = alphabets.length();
     40         ALPHABET = new String[ALPHABET_LENGTH];
     41         for (int i = 0; i < ALPHABET_LENGTH; ++i) {
     42             ALPHABET[i] = Character.toString(alphabets.charAt(i));
     43         }
     44     }
     45 
     46 
     47     private static final ColorStateList TEXT_COLOR = ColorStateList.valueOf(0x00000000);
     48     private static final String[] FAMILIES = { "sans-serif", "serif", "monospace" };
     49     private static final int[] STYLES = {
     50             Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC, Typeface.BOLD_ITALIC
     51     };
     52 
     53     public void resetRandom(long seed) {
     54         mRandom = new Random(seed);
     55     }
     56 
     57     private static String[] UnicodeSetToArray(String setStr) {
     58         final UnicodeSet set = new UnicodeSet(setStr);
     59         final UnicodeSetIterator iterator = new UnicodeSetIterator(set);
     60         final ArrayList<String> out = new ArrayList<>(set.size());
     61         while (iterator.next()) {
     62           out.add(iterator.getString());
     63         }
     64         return out.toArray(new String[out.size()]);
     65     }
     66 
     67     public CharSequence nextRandomParagraph(int wordLen, boolean applyRandomStyle, String setStr) {
     68         return nextRandomParagraph(wordLen, PARA_LENGTH, applyRandomStyle,
     69                 UnicodeSetToArray(setStr));
     70     }
     71 
     72     public CharSequence nextRandomParagraph(int wordLen, boolean applyRandomStyle) {
     73         return nextRandomParagraph(wordLen, PARA_LENGTH, applyRandomStyle, ALPHABET);
     74     }
     75 
     76     public CharSequence nextRandomParagraph(int wordLen, int paraLength) {
     77         return nextRandomParagraph(wordLen, paraLength, false /* no style */, ALPHABET);
     78     }
     79 
     80     public CharSequence nextRandomParagraph(int wordLen, int paraLength, boolean applyRandomStyle,
     81             String[] charSet) {
     82         ArrayList<Character> chars = new ArrayList<>();
     83         ArrayList<Integer> wordOffsets = new ArrayList<>();
     84         for (int i = 0; i < paraLength; i++) {
     85             if (i % (wordLen + 1) == wordLen) {
     86                 chars.add(' ');
     87                 wordOffsets.add(chars.size());
     88             } else {
     89                 final String str = charSet[mRandom.nextInt(charSet.length)];
     90                 for (int j = 0; j < str.length(); ++j) {
     91                     chars.add(str.charAt(j));
     92                 }
     93             }
     94         }
     95         wordOffsets.add(chars.size());
     96 
     97         char[] buffer = new char[chars.size()];
     98         for (int i = 0; i < buffer.length; ++i) {
     99             buffer[i] = chars.get(i);
    100         }
    101         CharSequence cs = CharBuffer.wrap(buffer);
    102         if (!applyRandomStyle) {
    103             return cs;
    104         }
    105 
    106         SpannableStringBuilder ssb = new SpannableStringBuilder(cs);
    107         int prevWordStart = 0;
    108         for (int i = 0; i < wordOffsets.size(); i++) {
    109             final int spanStart = prevWordStart;
    110             final int spanEnd = wordOffsets.get(i);
    111 
    112             final TextAppearanceSpan span = new TextAppearanceSpan(
    113                   FAMILIES[mRandom.nextInt(FAMILIES.length)],
    114                   STYLES[mRandom.nextInt(STYLES.length)],
    115                   24 + mRandom.nextInt(32),  // text size. min 24 max 56
    116                   TEXT_COLOR, TEXT_COLOR);
    117 
    118             ssb.setSpan(span, spanStart, spanEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    119             prevWordStart = spanEnd;
    120         }
    121         return ssb;
    122     }
    123 }
    124