| 1 | // LAF OS Library |
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // |
| 4 | // This file is released under the terms of the MIT license. |
| 5 | // Read LICENSE.txt for more information. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "os/draw_text.h" |
| 12 | #include "os/paint.h" |
| 13 | #include "os/skia/skia_helpers.h" |
| 14 | #include "os/skia/skia_surface.h" |
| 15 | |
| 16 | #include "include/core/SkTextBlob.h" |
| 17 | #include "include/utils/SkTextUtils.h" |
| 18 | #include "modules/skshaper/include/SkShaper.h" |
| 19 | |
| 20 | #include "include/core/SkCanvas.h" |
| 21 | |
| 22 | #include <cfloat> |
| 23 | |
| 24 | namespace os { |
| 25 | |
| 26 | void draw_text_with_shaper( |
| 27 | Surface* surface, Font* font, |
| 28 | const std::string& text, |
| 29 | const gfx::Point& pos, |
| 30 | const Paint* paint, |
| 31 | const TextAlign textAlign, |
| 32 | DrawTextDelegate* delegate) |
| 33 | { |
| 34 | // SkFont skFont(SkTypeface::MakeFromFile("/Library/Fonts/Arial Unicode.ttf"), SkIntToScalar(24)); |
| 35 | SkFont skFont(SkTypeface::MakeDefault(), SkIntToScalar(24)); |
| 36 | sk_sp<SkTextBlob> textBlob; |
| 37 | auto shaper = SkShaper::Make(); |
| 38 | if (shaper) { |
| 39 | SkTextBlobBuilderRunHandler builder(text.c_str(), { 0, 0 }); |
| 40 | shaper->shape(text.c_str(), text.size(), skFont, true, FLT_MAX, &builder); |
| 41 | textBlob = builder.makeBlob(); |
| 42 | } |
| 43 | else { |
| 44 | textBlob = SkTextBlob::MakeFromText(text.c_str(), text.size(), skFont, SkTextEncoding::kUTF8); |
| 45 | } |
| 46 | |
| 47 | if (textBlob) |
| 48 | static_cast<SkiaSurface*>(surface)->canvas() |
| 49 | .drawTextBlob( |
| 50 | textBlob, |
| 51 | SkIntToScalar(pos.x), |
| 52 | SkIntToScalar(pos.y), |
| 53 | (paint ? paint->skPaint(): SkPaint())); |
| 54 | } |
| 55 | |
| 56 | } // namespace os |
| 57 | |