• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 #ifndef FontIterator_DEFINED
3 #define FontIterator_DEFINED
4 
5 #include "include/core/SkSpan.h"
6 #include "include/core/SkString.h"
7 #include "include/core/SkTypes.h"
8 #include "modules/skparagraph/include/TextStyle.h"
9 #include "modules/skshaper/include/SkShaper.h"
10 
11 namespace skia {
12 namespace textlayout {
13 
14 class LangIterator final : public SkShaper::LanguageRunIterator {
15 public:
LangIterator(SkSpan<const char> utf8,SkSpan<Block> styles,const TextStyle & defaultStyle)16     LangIterator(SkSpan<const char> utf8, SkSpan<Block> styles, const TextStyle& defaultStyle)
17             : fText(utf8)
18             , fTextStyles(styles)
19             , fCurrentChar(utf8.begin())
20             , fCurrentStyle(fTextStyles.begin())
21             , fCurrentLocale(defaultStyle.getLocale()) {}
22 
consume()23     void consume() override {
24         SkASSERT(fCurrentChar < fText.end());
25 
26         if (fCurrentStyle == fTextStyles.end()) {
27             fCurrentChar = fText.end();
28             return;
29         }
30 
31         fCurrentChar = fText.begin() + fCurrentStyle->fRange.end;
32         fCurrentLocale = fCurrentStyle->fStyle.getLocale();
33         while (++fCurrentStyle != fTextStyles.end() && !fCurrentStyle->fStyle.isPlaceholder()) {
34             if (fCurrentStyle->fStyle.getLocale() != fCurrentLocale) {
35                 break;
36             }
37             fCurrentChar = fText.begin() + fCurrentStyle->fRange.end;
38         }
39     }
40 
endOfCurrentRun()41     size_t endOfCurrentRun() const override { return fCurrentChar - fText.begin(); }
atEnd()42     bool atEnd() const override { return fCurrentChar >= fText.end(); }
currentLanguage()43     const char* currentLanguage() const override { return fCurrentLocale.c_str(); }
44 
45 private:
46     SkSpan<const char> fText;
47     SkSpan<Block> fTextStyles;
48     const char* fCurrentChar;
49     Block* fCurrentStyle;
50     SkString fCurrentLocale;
51 };
52 }  // namespace textlayout
53 }  // namespace skia
54 
55 #endif  // FontIterator_DEFINED
56