| 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: |
| 16 |
LangIterator(SkSpan<const char> utf8, SkSpan<Block> styles, const TextStyle& defaultStyle) |
| 17 |
: fText(utf8) |
| 18 |
, fTextStyles(styles) |
| 19 |
, fCurrentChar(utf8.data()) |
| 20 |
, fCurrentStyle(fTextStyles.data()) |
| 21 |
, fCurrentLocale(defaultStyle.getLocale()) {} |
| 22 |
|
| 23 |
void consume() override { |
| 24 |
const char* textEnd = fText.data() + fText.size(); |
| 25 |
const Block* stylesEnd = fTextStyles.data() + fTextStyles.size(); |
| 26 |
|
| 27 |
SkASSERT(fCurrentChar < textEnd); |
| 28 |
|
| 29 |
if (fCurrentStyle == stylesEnd) { |
| 30 |
fCurrentChar = textEnd; |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
fCurrentChar = fText.data() + fCurrentStyle->fRange.end; |
| 35 |
fCurrentLocale = fCurrentStyle->fStyle.getLocale(); |
| 36 |
while (++fCurrentStyle != stylesEnd && !fCurrentStyle->fStyle.isPlaceholder()) { |
| 37 |
if (fCurrentStyle->fStyle.getLocale() != fCurrentLocale) { |
| 38 |
break; |
| 39 |
} |
| 40 |
fCurrentChar = fText.data() + fCurrentStyle->fRange.end; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
size_t endOfCurrentRun() const override { return fCurrentChar - fText.data(); } |
| 45 |
bool atEnd() const override { return fCurrentChar >= fText.data() + fText.size(); } |
| 46 |
const char* currentLanguage() const override { return fCurrentLocale.c_str(); } |
| 47 |
|
| 48 |
private: |
| 49 |
SkSpan<const char> fText; |
| 50 |
SkSpan<Block> fTextStyles; |
| 51 |
const char* fCurrentChar; |
| 52 |
Block* fCurrentStyle; |
| 53 |
SkString fCurrentLocale; |
| 54 |
}; |
| 55 |
} // namespace textlayout |
| 56 |
} // namespace skia |
| 57 |
|
| 58 |
#endif // FontIterator_DEFINED |