blob: 7cb3446c0f2613b275c967b48733383a2cda3158 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
2#include "modules/skparagraph/src/FontIterator.h"
3#include <unicode/brkiter.h>
4#include <unicode/ubidi.h>
5#include "include/core/SkBlurTypes.h"
6#include "include/core/SkCanvas.h"
7#include "include/core/SkFontMgr.h"
8#include "include/core/SkPictureRecorder.h"
9#include "modules/skparagraph/src/ParagraphImpl.h"
10#include "src/core/SkSpan.h"
11#include "src/utils/SkUTF.h"
12
Julia Lavrovaa3552c52019-05-30 16:12:56 -040013// TODO: FontCollection and FontIterator have common functionality
14namespace skia {
15namespace textlayout {
16
17FontIterator::FontIterator(SkSpan<const char> utf8,
Julia Lavrova6e6333f2019-06-17 10:34:10 -040018 SkSpan<TextBlock> styles,
19 sk_sp<FontCollection> fonts)
Julia Lavrovaa3552c52019-05-30 16:12:56 -040020 : fText(utf8)
21 , fStyles(styles)
22 , fCurrentChar(utf8.begin())
Julia Lavrova6e6333f2019-06-17 10:34:10 -040023 , fFontResolver(std::move(fonts)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040024 findAllFontsForAllStyledBlocks();
25}
26
27void FontIterator::consume() {
28 SkASSERT(fCurrentChar < fText.end());
Julia Lavrova6e6333f2019-06-17 10:34:10 -040029 auto found = fFontResolver.findFirst(fCurrentChar, &fFont, &fLineHeight);
30 SkASSERT(found);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040031
32 // Move until we find the first character that cannot be resolved with the current font
33 while (++fCurrentChar != fText.end()) {
Julia Lavrova6e6333f2019-06-17 10:34:10 -040034 SkFont font;
35 SkScalar height;
36 found = fFontResolver.findNext(fCurrentChar, &font, &height);
37 if (found) {
38 if (fFont == font && fLineHeight == height) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040039 continue;
40 }
41 break;
42 }
43 }
44}
45
46void FontIterator::findAllFontsForAllStyledBlocks() {
47 TextBlock combined;
48 for (auto& block : fStyles) {
49 SkASSERT(combined.text().begin() == nullptr ||
50 combined.text().end() == block.text().begin());
51
52 if (combined.text().begin() != nullptr &&
53 block.style().matchOneAttribute(StyleType::kFont, combined.style())) {
54 combined.add(block.text());
55 continue;
56 }
57
58 if (!combined.text().empty()) {
Julia Lavrova6e6333f2019-06-17 10:34:10 -040059 fFontResolver.findAllFontsForStyledBlock(combined.style(), combined.text());
Julia Lavrovaa3552c52019-05-30 16:12:56 -040060 }
61
62 combined = block;
63 }
Julia Lavrova6e6333f2019-06-17 10:34:10 -040064 fFontResolver.findAllFontsForStyledBlock(combined.style(), combined.text());
Julia Lavrovaa3552c52019-05-30 16:12:56 -040065}
66
Julia Lavrovaa3552c52019-05-30 16:12:56 -040067} // namespace textlayout
68} // namespace skia