• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 #ifndef SCREENRECORD_TEXT_RENDER_H
18 #define SCREENRECORD_TEXT_RENDER_H
19 
20 #include "Program.h"
21 
22 #include <utils/String8.h>
23 #include <utils/Errors.h>
24 
25 #include <GLES2/gl2.h>
26 
27 
28 namespace android {
29 
30 /*
31  * Simple font representation.
32  *
33  * Not thread-safe.
34  */
35 class TextRenderer {
36 public:
TextRenderer()37     TextRenderer() :
38         mTextureName(0),
39         mScale(1.0f),
40         mBorderWidth(10.0f),
41         mIndentMult(30.0f),
42         mScreenWidth(0),
43         mScreenHeight(0)
44         {}
~TextRenderer()45     ~TextRenderer() {}
46 
47     // Load the glyph bitmap into a 2D texture in the current context.
48     status_t loadIntoTexture();
49 
50     // Set the screen dimensions, used for scaling and line wrap.
setScreenSize(uint32_t width,uint32_t height)51     void setScreenSize(uint32_t width, uint32_t height) {
52         mScreenWidth = width;
53         mScreenHeight = height;
54     }
55 
56     // Get/set the font scaling.
getScale()57     float getScale() const { return mScale; }
setScale(float scale)58     void setScale(float scale) { mScale = scale; }
59 
60     // Set the font scaling based on the desired number of lines per screen.
61     // The display's tallest axis is used, so if the device is in landscape
62     // the screen will fit fewer lines.
63     void setProportionalScale(float linesPerScreen);
64 
65     // Render the text string at the specified coordinates.  Pass in the
66     // upper-left corner in non-GL-flipped coordinates, i.e. to print text
67     // at the top left of the screen use (0,0).
68     //
69     // Set blend func (1, 1-srcAlpha) before calling if drawing onto
70     // something other than black.
71     void drawString(const Program& program, const float* texMatrix,
72             float x, float y, const String8& str) const;
73 
74     // Draw a string, possibly wrapping it at the screen boundary.  Top-left
75     // is at (0,0).
76     //
77     // Returns the updated Y position.
78     float drawWrappedString(const Program& texRender, float xpos, float ypos,
79             const String8& str);
80 
81     // Returns the name of the texture the font was loaded into.
getTextureName()82     GLuint getTextureName() const { return mTextureName; }
83 
84 private:
85     TextRenderer(const TextRenderer&);
86     TextRenderer& operator=(const TextRenderer&);
87 
88     // Perform one-time initialization.
89     static void initOnce();
90 
91     // Populate the mXOffset array.
92     static void initXOffset();
93 
94     // Find a good place to break the string.  Returns NULL if the entire
95     // string will fit.
96     char* breakString(const char* str, float maxWidth) const;
97 
98     // Computes the width of the string, in pixels.
99     float computeScaledStringWidth(const String8& str8) const;
100 
101     // Computes the width of first N characters in the string.
102     float computeScaledStringWidth(const char* str, size_t len) const;
103 
104     // Returns the font's glyph height.  This is the full pixel height of the
105     // tallest glyph, both above and below the baseline, NOT adjusted by the
106     // current scale factor.
107     float getGlyphHeight() const;
108 
109     // Like getGlyphHeight(), but result is scaled.
getScaledGlyphHeight()110     float getScaledGlyphHeight() const { return getGlyphHeight() * mScale; }
111 
112     // Convert an ASCII character to a glyph index.  Returns the glyph for
113     // '?' if we have no glyph for the specified character.
114     size_t glyphIndex(char ch) const;
115 
116     GLuint mTextureName;
117     float mScale;
118 
119     // Number of pixels preserved at the left/right edges of the screen by
120     // drawWrappedString().  Not scaled.
121     float mBorderWidth;
122 
123     // Distance to indent a broken line.  Used by drawWrappedString().
124     // Value will be adjusted by the current scale factor.
125     float mIndentMult;
126 
127     // Screen dimensions.
128     uint32_t mScreenWidth;
129     uint32_t mScreenHeight;
130 
131     // Static font info.
132     static bool mInitialized;
133     static uint32_t mXOffset[];
134 
135     static const char kWhitespace[];
136 };
137 
138 }; // namespace android
139 
140 #endif /*SCREENRECORD_TEXT_RENDER_H*/
141