1// LAF OS Library
2// Copyright (c) 2022 Igara Studio S.A.
3// Copyright (C) 2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef OS_DRAW_TEXT_H_INCLUDED
9#define OS_DRAW_TEXT_H_INCLUDED
10#pragma once
11
12#include "base/string.h"
13#include "gfx/color.h"
14#include "gfx/fwd.h"
15
16namespace os {
17
18 class Font;
19 class Surface;
20 class Paint;
21
22 enum class TextAlign { Left, Center, Right };
23
24 class DrawTextDelegate {
25 public:
26 virtual ~DrawTextDelegate() { }
27
28 // This is called before drawing the character.
29 virtual void preProcessChar(const int index,
30 const int codepoint,
31 gfx::Color& fg,
32 gfx::Color& bg,
33 const gfx::Rect& charBounds) {
34 // Do nothing
35 }
36
37 virtual bool preDrawChar(const gfx::Rect& charBounds) {
38 // Returns false if the process should stop here.
39 return true;
40 }
41
42 virtual void postDrawChar(const gfx::Rect& charBounds) {
43 // Do nothing
44 }
45 };
46
47 // The surface can be nullptr just to process the string
48 // (e.g. measure how much space will use the text without drawing
49 // it). It uses FreeType2 library and harfbuzz. Doesn't support RTL
50 // (right-to-left) languages.
51 gfx::Rect draw_text(
52 Surface* surface, Font* font,
53 const std::string& text,
54 gfx::Color fg, gfx::Color bg,
55 int x, int y,
56 DrawTextDelegate* delegate);
57
58 // Uses SkTextUtils::Draw() to draw text (doesn't depend on harfbuzz
59 // or big dependencies, useful to print English text only).
60 void draw_text(
61 Surface* surface, Font* font,
62 const std::string& text,
63 const gfx::Point& pos,
64 const Paint* paint = nullptr,
65 const TextAlign textAlign = TextAlign::Left,
66 DrawTextDelegate* delegate = nullptr);
67
68 // Uses SkShaper::Make() to draw text (harfbuzz if available),
69 // useful for RTL (right-to-left) languages. Avoid this function if
70 // you are not going to translate your app to non-English languages
71 // (prefer os::draw_text() when possible).
72 void draw_text_with_shaper(
73 Surface* surface, Font* font,
74 const std::string& text,
75 const gfx::Point& pos,
76 const Paint* paint = nullptr,
77 const TextAlign textAlign = TextAlign::Left,
78 DrawTextDelegate* delegate = nullptr);
79
80} // namespace os
81
82#endif
83