• 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; }
extent()56     const MinikinExtent& extent() const { return mExtent; }
fonts()57     const std::vector<FakedFont>& fonts() const { return mFonts; }
58 
59     // Helper accessors
glyphCount()60     uint32_t glyphCount() const { return mGlyphIds.size(); }
fontAt(int glyphPos)61     const FakedFont& fontAt(int glyphPos) const { return mFonts[mFontIndices[glyphPos]]; }
glyphIdAt(int glyphPos)62     uint32_t glyphIdAt(int glyphPos) const { return mGlyphIds[glyphPos]; }
pointAt(int glyphPos)63     const Point& pointAt(int glyphPos) const { return mPoints[glyphPos]; }
64 
getMemoryUsage()65     uint32_t getMemoryUsage() const {
66         return sizeof(uint8_t) * mFontIndices.size() + sizeof(uint32_t) * mGlyphIds.size() +
67                sizeof(Point) * mPoints.size() + sizeof(float) * mAdvances.size() + sizeof(float) +
68                sizeof(MinikinRect) + sizeof(MinikinExtent);
69     }
70 
71 private:
72     FRIEND_TEST(LayoutTest, doLayoutWithPrecomputedPiecesTest);
73 
74     std::vector<uint8_t> mFontIndices;  // per glyph
75     std::vector<uint32_t> mGlyphIds;    // per glyph
76     std::vector<Point> mPoints;         // per glyph
77 
78     std::vector<float> mAdvances;  // per code units
79 
80     float mAdvance;
81     MinikinExtent mExtent;
82 
83     std::vector<FakedFont> mFonts;
84 };
85 
86 // For gtest output
87 inline std::ostream& operator<<(std::ostream& os, const Point& p) {
88     return os << "(" << p.x << ", " << p.y << ")";
89 }
90 }  // namespace minikin
91 
92 #endif  // MINIKIN_LAYOUT_CORE_H
93