1 /* 2 * Copyright 2017 Google Inc. 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 LIB_TXT_SRC_STYLED_RUNS_H_ 18 #define LIB_TXT_SRC_STYLED_RUNS_H_ 19 20 #include <list> 21 #include <vector> 22 23 #include "text_style.h" 24 #include "third_party/googletest/googletest/include/gtest/gtest_prod.h" // nogncheck 25 #include "utils/WindowsUtils.h" 26 27 namespace txt { 28 29 // This holds and handles the start/end positions of discrete chunks of text 30 // that use different styles (a 'run'). 31 class StyledRuns { 32 public: 33 struct Run { 34 const TextStyle& style; 35 size_t start; 36 size_t end; 37 }; 38 39 StyledRuns(); 40 41 ~StyledRuns(); 42 43 StyledRuns(const StyledRuns& other) = delete; 44 45 StyledRuns(StyledRuns&& other); 46 47 const StyledRuns& operator=(StyledRuns&& other); 48 49 void swap(StyledRuns& other); 50 51 size_t AddStyle(const TextStyle& style); 52 53 const TextStyle& GetStyle(size_t style_index) const; 54 55 void StartRun(size_t style_index, size_t start); 56 57 void EndRunIfNeeded(size_t end); 58 size()59 size_t size() const { return runs_.size(); } 60 61 Run GetRun(size_t index) const; 62 63 private: 64 FRIEND_TEST(ParagraphTest, SimpleParagraph); 65 FRIEND_TEST(ParagraphTest, SimpleRedParagraph); 66 FRIEND_TEST(ParagraphTest, RainbowParagraph); 67 FRIEND_TEST(ParagraphTest, DefaultStyleParagraph); 68 FRIEND_TEST(ParagraphTest, BoldParagraph); 69 FRIEND_TEST_WINDOWS_DISABLED(ParagraphTest, LeftAlignParagraph); 70 FRIEND_TEST_WINDOWS_DISABLED(ParagraphTest, RightAlignParagraph); 71 FRIEND_TEST_WINDOWS_DISABLED(ParagraphTest, CenterAlignParagraph); 72 FRIEND_TEST_WINDOWS_DISABLED(ParagraphTest, JustifyAlignParagraph); 73 FRIEND_TEST(ParagraphTest, DecorationsParagraph); 74 FRIEND_TEST(ParagraphTest, ItalicsParagraph); 75 FRIEND_TEST(ParagraphTest, ChineseParagraph); 76 FRIEND_TEST(ParagraphTest, DISABLED_ArabicParagraph); 77 FRIEND_TEST(ParagraphTest, LongWordParagraph); 78 FRIEND_TEST(ParagraphTest, KernParagraph); 79 FRIEND_TEST(ParagraphTest, HyphenBreakParagraph); 80 FRIEND_TEST(ParagraphTest, RepeatLayoutParagraph); 81 FRIEND_TEST(ParagraphTest, Ellipsize); 82 FRIEND_TEST(ParagraphTest, SimpleShadow); 83 FRIEND_TEST(ParagraphTest, ComplexShadow); 84 FRIEND_TEST(ParagraphTest, FontFallbackParagraph); 85 86 struct IndexedRun { 87 size_t style_index = 0; 88 size_t start = 0; 89 size_t end = 0; 90 IndexedRunIndexedRun91 explicit IndexedRun(size_t style_index, size_t start, size_t end) 92 : style_index(style_index), start(start), end(end) {} 93 }; 94 95 std::vector<TextStyle> styles_; 96 std::vector<IndexedRun> runs_; 97 }; 98 99 } // namespace txt 100 101 #endif // LIB_TXT_SRC_STYLED_RUNS_H_ 102