Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 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 | #ifndef SkTextUtils_DEFINED |
| 9 | #define SkTextUtils_DEFINED |
| 10 | |
| 11 | #include "SkCanvas.h" |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 12 | #include "SkFont.h" |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 13 | #include "SkPaint.h" |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 14 | #include "SkString.h" |
| 15 | |
| 16 | class SkTextUtils { |
| 17 | public: |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 18 | enum Align { |
| 19 | kLeft_Align, |
| 20 | kCenter_Align, |
| 21 | kRight_Align, |
| 22 | }; |
| 23 | |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 24 | static void Draw(SkCanvas*, const void* text, size_t size, SkTextEncoding, |
| 25 | SkScalar x, SkScalar y, const SkFont&, const SkPaint&, Align = kLeft_Align); |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 26 | |
| 27 | static void DrawString(SkCanvas* canvas, const char text[], SkScalar x, SkScalar y, |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 28 | const SkFont& font, const SkPaint& paint, Align align = kLeft_Align) { |
| 29 | SkASSERT(paint.getTextEncoding() == kUTF8_SkTextEncoding); |
| 30 | Draw(canvas, text, strlen(text), kUTF8_SkTextEncoding, x, y, font, paint, align); |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 31 | } |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 32 | |
| 33 | #if 1 |
| 34 | static void DrawString(SkCanvas* canvas, const char text[], SkScalar x, SkScalar y, |
| 35 | const SkPaint& paint, Align align = kLeft_Align) { |
| 36 | SkASSERT(paint.getTextEncoding() == kUTF8_SkTextEncoding); |
| 37 | Draw(canvas, text, strlen(text), kUTF8_SkTextEncoding, x, y, |
| 38 | SkFont::LEGACY_ExtractFromPaint(paint), paint, align); |
| 39 | } |
| 40 | |
| 41 | static void DrawText(SkCanvas* canvas, const void* text, size_t size, SkScalar x, SkScalar y, |
| 42 | const SkPaint& paint, Align align = kLeft_Align) { |
| 43 | Draw(canvas, text, size, paint.getTextEncoding(), x, y, |
| 44 | SkFont::LEGACY_ExtractFromPaint(paint), paint, align); |
| 45 | } |
| 46 | |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 47 | static void DrawString(SkCanvas* canvas, const SkString& str, SkScalar x, SkScalar y, |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 48 | const SkPaint& paint, Align align = kLeft_Align) { |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 49 | DrawText(canvas, str.c_str(), str.size(), x, y, paint, align); |
| 50 | } |
Mike Reed | dc5863c | 2018-12-23 23:19:14 -0500 | [diff] [blame] | 51 | #endif |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #endif |