TextRenderer.h revision aaa3f358410701710e31f31de62f0b4521989661
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
28namespace android {
29
30/*
31 * Simple font representation.
32 *
33 * Not thread-safe.
34 */
35class TextRenderer {
36public:
37    TextRenderer() :
38        mTextureName(0),
39        mScale(1.0f),
40        mBorderWidth(10.0f),
41        mIndentMult(30.0f),
42        mScreenWidth(0),
43        mScreenHeight(0)
44        {}
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.
51    void setScreenSize(uint32_t width, uint32_t height) {
52        mScreenWidth = width;
53        mScreenHeight = height;
54    }
55
56    // Get/set the font scaling.
57    float getScale() const { return mScale; }
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.
82    GLuint getTextureName() const { return mTextureName; }
83
84private:
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.
110    float getScaledGlyphHeight() const { return getGlyphHeight() * mScale; }
111
112    GLuint mTextureName;
113    float mScale;
114
115    // Number of pixels preserved at the left/right edges of the screen by
116    // drawWrappedString().  Not scaled.
117    float mBorderWidth;
118
119    // Distance to indent a broken line.  Used by drawWrappedString().
120    // Value will be adjusted by the current scale factor.
121    float mIndentMult;
122
123    // Screen dimensions.
124    uint32_t mScreenWidth;
125    uint32_t mScreenHeight;
126
127    // Static font info.
128    static bool mInitialized;
129    static uint32_t mXOffset[];
130
131    static const char kWhitespace[];
132};
133
134}; // namespace android
135
136#endif /*SCREENRECORD_TEXT_RENDER_H*/
137