1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.graphics.spritetext;
18 
19 import javax.microedition.khronos.opengles.GL10;
20 
21 import android.graphics.Paint;
22 
23 public class NumericSprite {
NumericSprite()24     public NumericSprite() {
25         mText = "";
26         mLabelMaker = null;
27     }
28 
initialize(GL10 gl, Paint paint)29     public void initialize(GL10 gl, Paint paint) {
30         int height = roundUpPower2((int) paint.getFontSpacing());
31         final float interDigitGaps = 9 * 1.0f;
32         int width = roundUpPower2((int) (interDigitGaps + paint.measureText(sStrike)));
33         mLabelMaker = new LabelMaker(true, width, height);
34         mLabelMaker.initialize(gl);
35         mLabelMaker.beginAdding(gl);
36         for (int i = 0; i < 10; i++) {
37             String digit = sStrike.substring(i, i+1);
38             mLabelId[i] = mLabelMaker.add(gl, digit, paint);
39             mWidth[i] = (int) Math.ceil(mLabelMaker.getWidth(i));
40         }
41         mLabelMaker.endAdding(gl);
42     }
43 
shutdown(GL10 gl)44     public void shutdown(GL10 gl) {
45         mLabelMaker.shutdown(gl);
46         mLabelMaker = null;
47     }
48 
49     /**
50      * Find the smallest power of two >= the input value.
51      * (Doesn't work for negative numbers.)
52      */
roundUpPower2(int x)53     private int roundUpPower2(int x) {
54         x = x - 1;
55         x = x | (x >> 1);
56         x = x | (x >> 2);
57         x = x | (x >> 4);
58         x = x | (x >> 8);
59         x = x | (x >>16);
60         return x + 1;
61     }
62 
setValue(int value)63     public void setValue(int value) {
64         mText = format(value);
65     }
66 
draw(GL10 gl, float x, float y, float viewWidth, float viewHeight)67     public void draw(GL10 gl, float x, float y,
68             float viewWidth, float viewHeight) {
69         int length = mText.length();
70         mLabelMaker.beginDrawing(gl, viewWidth, viewHeight);
71         for(int i = 0; i < length; i++) {
72             char c = mText.charAt(i);
73             int digit = c - '0';
74             mLabelMaker.draw(gl, x, y, mLabelId[digit]);
75             x += mWidth[digit];
76         }
77         mLabelMaker.endDrawing(gl);
78     }
79 
width()80     public float width() {
81         float width = 0.0f;
82         int length = mText.length();
83         for(int i = 0; i < length; i++) {
84             char c = mText.charAt(i);
85             width += mWidth[c - '0'];
86         }
87         return width;
88     }
89 
format(int value)90     private String format(int value) {
91         return Integer.toString(value);
92     }
93 
94     private LabelMaker mLabelMaker;
95     private String mText;
96     private int[] mWidth = new int[10];
97     private int[] mLabelId = new int[10];
98     private final static String sStrike = "0123456789";
99 }
100