1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkShaper.h"
9 #include "SkSpan.h"
10 #include "SkTextBlobPriv.h"
11 
Make()12 std::unique_ptr<SkShaper> SkShaper::Make() {
13 #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE
14     std::unique_ptr<SkShaper> shaper = SkShaper::MakeHarfBuzz();
15     if (shaper) {
16         return shaper;
17     }
18 #endif
19     return SkShaper::MakePrimitive();
20 }
21 
SkShaper()22 SkShaper::SkShaper() {}
~SkShaper()23 SkShaper::~SkShaper() {}
24 
newRunBuffer(const RunInfo &,const SkFont & font,int glyphCount,SkSpan<const char> utf8)25 SkShaper::RunHandler::Buffer SkTextBlobBuilderRunHandler::newRunBuffer(const RunInfo&,
26                                                                        const SkFont& font,
27                                                                        int glyphCount,
28                                                                        SkSpan<const char> utf8) {
29     const auto& runBuffer = SkTextBlobBuilderPriv::AllocRunTextPos(&fBuilder, font, glyphCount,
30                                                                    utf8.size(), SkString());
31     if (runBuffer.utf8text && fUtf8Text) {
32         memcpy(runBuffer.utf8text, utf8.data(), utf8.size());
33     }
34     fClusters = runBuffer.clusters;
35     fGlyphCount = glyphCount;
36     fClusterOffset = utf8.data() - fUtf8Text;
37 
38     return { runBuffer.glyphs,
39              runBuffer.points(),
40              runBuffer.clusters };
41 }
42 
commitRun()43 void SkTextBlobBuilderRunHandler::commitRun() {
44     SkASSERT(0 <= fClusterOffset);
45     for (int i = 0; i < fGlyphCount; ++i) {
46         SkASSERT(fClusters[i] >= (unsigned)fClusterOffset);
47         fClusters[i] -= fClusterOffset;
48     }
49 }
50 
makeBlob()51 sk_sp<SkTextBlob> SkTextBlobBuilderRunHandler::makeBlob() {
52     return fBuilder.make();
53 }
54