• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 MINIKIN_LAYOUT_H
18 #define MINIKIN_LAYOUT_H
19 
20 #include <memory>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include <gtest/gtest_prod.h>
25 
26 #include "minikin/FontCollection.h"
27 #include "minikin/LayoutCore.h"
28 #include "minikin/Range.h"
29 #include "minikin/U16StringPiece.h"
30 
31 namespace minikin {
32 
33 class Layout;
34 struct LayoutPieces;
35 
36 struct LayoutGlyph {
LayoutGlyphLayoutGlyph37     LayoutGlyph(FakedFont font, uint32_t glyph_id, float x, float y)
38             : font(font), glyph_id(glyph_id), x(x), y(y) {}
39     FakedFont font;
40 
41     uint32_t glyph_id;
42     float x;
43     float y;
44 };
45 
46 // Must be the same value with Paint.java
47 enum class Bidi : uint8_t {
48     LTR = 0b0000,          // Must be same with Paint.BIDI_LTR
49     RTL = 0b0001,          // Must be same with Paint.BIDI_RTL
50     DEFAULT_LTR = 0b0010,  // Must be same with Paint.BIDI_DEFAULT_LTR
51     DEFAULT_RTL = 0b0011,  // Must be same with Paint.BIDI_DEFAULT_RTL
52     FORCE_LTR = 0b0100,    // Must be same with Paint.BIDI_FORCE_LTR
53     FORCE_RTL = 0b0101,    // Must be same with Paint.BIDI_FORCE_RTL
54 };
55 
isRtl(Bidi bidi)56 inline bool isRtl(Bidi bidi) {
57     return static_cast<uint8_t>(bidi) & 0b0001;
58 }
isOverride(Bidi bidi)59 inline bool isOverride(Bidi bidi) {
60     return static_cast<uint8_t>(bidi) & 0b0100;
61 }
62 
63 // Lifecycle and threading assumptions for Layout:
64 // The object is assumed to be owned by a single thread; multiple threads
65 // may not mutate it at the same time.
66 class Layout {
67 public:
Layout(const U16StringPiece & str,const Range & range,Bidi bidiFlags,const MinikinPaint & paint,StartHyphenEdit startHyphen,EndHyphenEdit endHyphen)68     Layout(const U16StringPiece& str, const Range& range, Bidi bidiFlags, const MinikinPaint& paint,
69            StartHyphenEdit startHyphen, EndHyphenEdit endHyphen)
70             : mAdvance(0) {
71         doLayout(str, range, bidiFlags, paint, startHyphen, endHyphen);
72     }
73 
Layout(uint32_t count)74     Layout(uint32_t count) : mAdvance(0) {
75         mAdvances.resize(count, 0);
76         mGlyphs.reserve(count);
77     }
78 
79     static float measureText(const U16StringPiece& str, const Range& range, Bidi bidiFlags,
80                              const MinikinPaint& paint, StartHyphenEdit startHyphen,
81                              EndHyphenEdit endHyphen, float* advances);
82 
advances()83     const std::vector<float>& advances() const { return mAdvances; }
84 
85     // public accessors
nGlyphs()86     size_t nGlyphs() const { return mGlyphs.size(); }
getFont(int i)87     const Font* getFont(int i) const { return mGlyphs[i].font.font.get(); }
getFontRef(int i)88     const std::shared_ptr<Font>& getFontRef(int i) const { return mGlyphs[i].font.font; }
getFakery(int i)89     FontFakery getFakery(int i) const { return mGlyphs[i].font.fakery; }
getGlyphId(int i)90     unsigned int getGlyphId(int i) const { return mGlyphs[i].glyph_id; }
getX(int i)91     float getX(int i) const { return mGlyphs[i].x; }
getY(int i)92     float getY(int i) const { return mGlyphs[i].y; }
getAdvance()93     float getAdvance() const { return mAdvance; }
getCharAdvance(size_t i)94     float getCharAdvance(size_t i) const { return mAdvances[i]; }
getAdvances()95     const std::vector<float>& getAdvances() const { return mAdvances; }
96 
97     // Purge all caches, useful in low memory conditions
98     static void purgeCaches();
99 
100     // Dump minikin internal statistics, cache usage, cache hit ratio, etc.
101     static void dumpMinikinStats(int fd);
102 
103     // Append another layout (for example, cached value) into this one
104     void appendLayout(const LayoutPiece& src, size_t start, float extraAdvance);
105 
106 private:
107     FRIEND_TEST(LayoutTest, doLayoutWithPrecomputedPiecesTest);
108 
109     void doLayout(const U16StringPiece& str, const Range& range, Bidi bidiFlags,
110                   const MinikinPaint& paint, StartHyphenEdit startHyphen, EndHyphenEdit endHyphen);
111 
112     // Lay out a single bidi run
113     // When layout is not null, layout info will be stored in the object.
114     // When advances is not null, measurement results will be stored in the array.
115     static float doLayoutRunCached(const U16StringPiece& textBuf, const Range& range, bool isRtl,
116                                    const MinikinPaint& paint, size_t dstStart,
117                                    StartHyphenEdit startHyphen, EndHyphenEdit endHyphen,
118                                    Layout* layout, float* advances);
119 
120     // Lay out a single word
121     static float doLayoutWord(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
122                               bool isRtl, const MinikinPaint& paint, size_t bufStart,
123                               StartHyphenEdit startHyphen, EndHyphenEdit endHyphen, Layout* layout,
124                               float* advances);
125 
126     // Lay out a single bidi run
127     void doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t bufSize, bool isRtl,
128                      const MinikinPaint& paint, StartHyphenEdit startHyphen,
129                      EndHyphenEdit endHyphen);
130 
131     std::vector<LayoutGlyph> mGlyphs;
132 
133     // This vector defined per code unit, so their length is identical to the input text.
134     std::vector<float> mAdvances;
135 
136     float mAdvance;
137 };
138 
139 }  // namespace minikin
140 
141 #endif  // MINIKIN_LAYOUT_H
142