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 <gtest/gtest_prod.h> 21 22 #include <vector> 23 24 #include "minikin/Constants.h" 25 #include "minikin/FontFamily.h" 26 #include "minikin/Hyphenator.h" 27 #include "minikin/MinikinExtent.h" 28 #include "minikin/MinikinFont.h" 29 #include "minikin/MinikinRect.h" 30 #include "minikin/PackedVector.h" 31 #include "minikin/Point.h" 32 #include "minikin/Range.h" 33 #include "minikin/U16StringPiece.h" 34 35 namespace minikin { 36 37 struct MinikinPaint; 38 39 using FontIndexVector = PackedVector<uint8_t, 12>; 40 using GlyphIdVector = PackedVector<uint16_t, 12>; 41 using PointVector = PackedVector<Point>; 42 using ClusterVector = PackedVector<uint8_t, 12>; 43 using AdvanceVector = PackedVector<float>; 44 45 // Immutable, recycle-able layout result. 46 class LayoutPiece { 47 public: 48 LayoutPiece(const U16StringPiece& textBuf, const Range& range, bool isRtl, 49 const MinikinPaint& paint, StartHyphenEdit startHyphen, EndHyphenEdit endHyphen); 50 ~LayoutPiece(); 51 52 // Low level accessors. points()53 const PointVector& points() const { return mPoints; } advances()54 const AdvanceVector& 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; } clusterCount()58 uint32_t clusterCount() const { return mClusterCount; } 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]; } clusterAt(int glyphPos)65 uint16_t clusterAt(int glyphPos) const { return mClusters[glyphPos]; } isVerticalText()66 bool isVerticalText() const { return mVerticalText; } 67 getMemoryUsage()68 uint32_t getMemoryUsage() const { 69 return sizeof(uint8_t) * mFontIndices.size() + sizeof(uint32_t) * mGlyphIds.size() + 70 sizeof(Point) * mPoints.size() + sizeof(float) * mAdvances.size() + sizeof(float) + 71 sizeof(MinikinRect) + sizeof(MinikinExtent); 72 } 73 74 static MinikinRect calculateBounds(const LayoutPiece& layout, const MinikinPaint& paint); 75 76 private: 77 FRIEND_TEST(LayoutTest, doLayoutWithPrecomputedPiecesTest); 78 79 FontIndexVector mFontIndices; // per glyph 80 GlyphIdVector mGlyphIds; // per glyph 81 PointVector mPoints; // per glyph 82 ClusterVector mClusters; // per glyph 83 84 AdvanceVector mAdvances; // per code units 85 86 float mAdvance; 87 MinikinExtent mExtent; 88 uint32_t mClusterCount; 89 bool mVerticalText; 90 91 std::vector<FakedFont> mFonts; 92 }; 93 94 } // namespace minikin 95 96 #endif // MINIKIN_LAYOUT_CORE_H 97