• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_CORE_H
18 #define MINIKIN_LAYOUT_CORE_H
19 
20 #include <vector>
21 
22 #include <gtest/gtest_prod.h>
23 
24 #include "minikin/FontFamily.h"
25 #include "minikin/Hyphenator.h"
26 #include "minikin/MinikinExtent.h"
27 #include "minikin/MinikinFont.h"
28 #include "minikin/MinikinRect.h"
29 #include "minikin/Range.h"
30 #include "minikin/U16StringPiece.h"
31 
32 namespace minikin {
33 
34 struct MinikinPaint;
35 
36 struct Point {
PointPoint37     Point() : x(0), y(0) {}
PointPoint38     Point(float x, float y) : x(x), y(y) {}
39     bool operator==(const Point& o) const { return x == o.x && y == o.y; }
40     float x;
41     float y;
42 };
43 
44 // Immutable, recycle-able layout result.
45 class LayoutPiece {
46 public:
47     LayoutPiece(const U16StringPiece& textBuf, const Range& range, bool isRtl,
48                 const MinikinPaint& paint, StartHyphenEdit startHyphen, EndHyphenEdit endHyphen);
49 
50     // Low level accessors.
fontIndices()51     const std::vector<uint8_t>& fontIndices() const { return mFontIndices; }
glyphIds()52     const std::vector<uint32_t> glyphIds() const { return mGlyphIds; }
points()53     const std::vector<Point> points() const { return mPoints; }
advances()54     const std::vector<float> advances() const { return mAdvances; }
advance()55     float advance() const { return mAdvance; }
bounds()56     const MinikinRect& bounds() const { return mBounds; }
extent()57     const MinikinExtent& extent() const { return mExtent; }
fonts()58     const std::vector<FakedFont>& fonts() const { return mFonts; }
59 
60     // Helper accessors
glyphCount()61     uint32_t glyphCount() const { return mGlyphIds.size(); }
fontAt(int glyphPos)62     const FakedFont& fontAt(int glyphPos) const { return mFonts[mFontIndices[glyphPos]]; }
glyphIdAt(int glyphPos)63     uint32_t glyphIdAt(int glyphPos) const { return mGlyphIds[glyphPos]; }
pointAt(int glyphPos)64     const Point& pointAt(int glyphPos) const { return mPoints[glyphPos]; }
65 
getMemoryUsage()66     uint32_t getMemoryUsage() const {
67         return sizeof(uint8_t) * mFontIndices.size() + sizeof(uint32_t) * mGlyphIds.size() +
68                sizeof(Point) * mPoints.size() + sizeof(float) * mAdvances.size() + sizeof(float) +
69                sizeof(MinikinRect) + sizeof(MinikinExtent);
70     }
71 
72 private:
73     FRIEND_TEST(LayoutTest, doLayoutWithPrecomputedPiecesTest);
74 
75     std::vector<uint8_t> mFontIndices;  // per glyph
76     std::vector<uint32_t> mGlyphIds;    // per glyph
77     std::vector<Point> mPoints;         // per glyph
78 
79     std::vector<float> mAdvances;  // per code units
80 
81     float mAdvance;
82     MinikinRect mBounds;
83     MinikinExtent mExtent;
84 
85     std::vector<FakedFont> mFonts;
86 };
87 
88 // For gtest output
89 inline std::ostream& operator<<(std::ostream& os, const Point& p) {
90     return os << "(" << p.x << ", " << p.y << ")";
91 }
92 }  // namespace minikin
93 
94 #endif  // MINIKIN_LAYOUT_CORE_H
95