iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
ISkParagraphControl.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
13#ifndef IGRAPHICS_SKIA
14#error This IControl only works with the Skia graphics backend
15#endif
16
22#include "IControl.h"
23#include "IGraphicsSkia.h"
24#include "include/core/SkCanvas.h"
25#include "include/core/SkColorFilter.h"
26#include "include/core/SkColorPriv.h"
27#include "include/core/SkFontMgr.h"
28#include "include/core/SkGraphics.h"
29#include "include/core/SkPath.h"
30#include "include/core/SkRegion.h"
31#include "include/core/SkShader.h"
32#include "include/core/SkStream.h"
33#include "include/core/SkTextBlob.h"
34#include "include/core/SkTypeface.h"
35#include "include/effects/SkGradientShader.h"
36#include "modules/skparagraph/include/DartTypes.h"
37#include "modules/skparagraph/include/FontCollection.h"
38#include "modules/skparagraph/include/Paragraph.h"
39#include "modules/skparagraph/include/ParagraphBuilder.h"
40#include "modules/skparagraph/include/ParagraphStyle.h"
41#include "modules/skunicode/include/SkUnicode_icu.h"
42
43#if defined OS_MAC || defined OS_IOS
44 #include "include/ports/SkFontMgr_mac_ct.h"
45#endif
46
47#ifdef OS_WIN
48 #include "include/ports/SkTypeface_win.h"
49 #pragma comment(lib, "skparagraph.lib")
50#endif
51
52BEGIN_IPLUG_NAMESPACE
53BEGIN_IGRAPHICS_NAMESPACE
54
55using namespace skia::textlayout;
56
57static sk_sp<SkFontMgr> fontmgr_factory() {
58#if defined OS_MAC || OS_IOS
59 return SkFontMgr_New_CoreText(nullptr);
60#elif defined OS_WIN
61 return SkFontMgr_New_DirectWrite();
62#else
63 #error "Not supported"
64#endif
65}
66
67bool g_factory_called = false;
68
69sk_sp<SkFontMgr> SkFontMgr_RefDefault() {
70 static std::once_flag flag;
71 static sk_sp<SkFontMgr> mgr;
72 std::call_once(flag, [] {
73 mgr = fontmgr_factory();
74 g_factory_called = true;
75 });
76 return mgr;
77}
78
82{
83public:
84 ISkParagraphControl(const IRECT& bounds)
85 : IControl(bounds)
86 {
87 }
88
89 void Draw(IGraphics& g) override
90 {
91 g.FillRect(COLOR_GREEN, mRECT);
92 SkCanvas* canvas = (SkCanvas*) g.GetDrawContext();
93 DoDrawContent(canvas);
94 }
95
96// void DoDrawContent(SkCanvas* canvas)
97// {
98// float w = mRECT.W();
99//
100// const std::vector<
101// std::tuple<std::string, bool, bool, int, SkColor, SkColor, bool, TextDecorationStyle>>
102// gParagraph = { {"monospace", true, false, 14, SK_ColorWHITE, SK_ColorRED, true,
103// TextDecorationStyle::kDashed},
104// {"Assyrian", false, false, 20, SK_ColorWHITE, SK_ColorBLUE, false,
105// TextDecorationStyle::kDotted},
106// {"serif", true, true, 10, SK_ColorWHITE, SK_ColorRED, true,
107// TextDecorationStyle::kDouble},
108// {"Arial", false, true, 16, SK_ColorGRAY, SK_ColorGREEN, true,
109// TextDecorationStyle::kSolid},
110// {"sans-serif", false, false, 8, SK_ColorWHITE, SK_ColorRED, false,
111// TextDecorationStyle::kWavy} };
112// SkAutoCanvasRestore acr(canvas, true);
113//
114// canvas->drawColor(SK_ColorWHITE);
115//
116// SkScalar margin = 20;
117//
118// SkPaint paint;
119// paint.setAntiAlias(true);
120// paint.setColor(SK_ColorWHITE);
121//
122// SkPaint blue;
123// blue.setColor(SK_ColorBLUE);
124//
125// TextStyle defaultStyle;
126// defaultStyle.setBackgroundColor(blue);
127// defaultStyle.setForegroundColor(paint);
128// ParagraphStyle paraStyle;
129// sk_sp<SkUnicode> unicode = SkUnicodes::ICU::Make();
130//
131// #ifdef OS_WIN
132// if (unicode == nullptr)
133// {
134// const char* errorText = "Failed to create SkUnicode - could not find icudtl.dat next to DLL/EXE";
135// DBGMSG("%s\n", errorText);
136// GetUI()->DrawText(mText, errorText, mRECT);
137// return;
138// }
139// #endif
140//
141// auto fontCollection = sk_make_sp<FontCollection>();
142// fontCollection->enableFontFallback();
143// fontCollection->setDefaultFontManager(SkFontMgr_RefDefault());
144// for (auto i = 1; i < 5; ++i) {
145// defaultStyle.setFontSize(24 * i);
146// paraStyle.setTextStyle(defaultStyle);
147// std::unique_ptr<ParagraphBuilder> builder = ParagraphBuilder::make(paraStyle, fontCollection, unicode);
148// std::string name = "Paragraph: " + std::to_string(24 * i);
149// builder->addText(name.c_str(), name.length());
150// for (auto para : gParagraph) {
151// TextStyle style;
152// style.setFontFamilies({ SkString(std::get<0>(para).c_str()) });
153// SkFontStyle fontStyle(std::get<1>(para) ? SkFontStyle::Weight::kBold_Weight
154// : SkFontStyle::Weight::kNormal_Weight,
155// SkFontStyle::Width::kNormal_Width,
156// std::get<2>(para) ? SkFontStyle::Slant::kItalic_Slant
157// : SkFontStyle::Slant::kUpright_Slant);
158// style.setFontStyle(fontStyle);
159// style.setFontSize(std::get<3>(para) * i);
160// SkPaint background;
161// background.setColor(std::get<4>(para));
162// style.setBackgroundColor(background);
163// SkPaint foreground;
164// foreground.setColor(std::get<5>(para));
165// foreground.setAntiAlias(true);
166// style.setForegroundColor(foreground);
167// if (std::get<6>(para)) {
168// style.addShadow(TextShadow(SK_ColorBLACK, SkPoint::Make(5, 5), 2));
169// }
170//
171// auto decoration = (i % 4);
172// if (decoration == 3) {
173// decoration = 4;
174// }
175//
176// bool test = (TextDecoration)decoration != TextDecoration::kNoDecoration;
177// std::string deco = std::to_string((int)decoration);
178// if (test) {
179// style.setDecoration((TextDecoration)decoration);
180// style.setDecorationStyle(std::get<7>(para));
181// style.setDecorationColor(std::get<5>(para));
182// }
183// builder->pushStyle(style);
184// std::string name = " " + std::get<0>(para) + " " +
185// (std::get<1>(para) ? ", bold" : "") +
186// (std::get<2>(para) ? ", italic" : "") + " " +
187// std::to_string(std::get<3>(para) * i) +
188// (std::get<4>(para) != SK_ColorRED ? ", background" : "") +
189// (std::get<5>(para) != SK_ColorWHITE ? ", foreground" : "") +
190// (std::get<6>(para) ? ", shadow" : "") +
191// (test ? ", decorations " + deco : "") + ";";
192// builder->addText(name.c_str(), name.length());
193// builder->pop();
194// }
195//
196// auto paragraph = builder->Build();
197// paragraph->layout(w - margin * 2);
198// paragraph->paint(canvas, mRECT.L + margin, mRECT.T + margin);
199//
200// canvas->translate(0, paragraph->getHeight());
201// }
202// }
203
204 void DoDrawContent(SkCanvas* canvas)
205 {
206 auto fontCollection = sk_make_sp<skia::textlayout::FontCollection>();
207 fontCollection->setDefaultFontManager(SkFontMgr_RefDefault());
208
209 canvas->clear(SK_ColorWHITE);
210
211 SkPaint paint;
212 paint.setAntiAlias(true);
213 paint.setColor(SK_ColorBLACK);
214
215 skia::textlayout::TextStyle style;
216 style.setForegroundColor(paint);
217 style.setFontFamilies({SkString("sans-serif")});
218 style.setFontSize(30);
219 skia::textlayout::ParagraphStyle paraStyle;
220 paraStyle.setTextStyle(style);
221 paraStyle.setTextAlign(skia::textlayout::TextAlign::kRight);
222
223 sk_sp<SkUnicode> unicode = SkUnicodes::ICU::Make();
224
225#ifdef OS_WIN
226 if (unicode == nullptr)
227 {
228 const char* errorText = "Failed to create SkUnicode - could not find icudtl.dat next to DLL/EXE";
229 DBGMSG("%s\n", errorText);
230 GetUI()->DrawText(mText, errorText, mRECT);
231 return;
232 }
233#endif
234
235 using skia::textlayout::ParagraphBuilder;
236 std::unique_ptr<ParagraphBuilder> builder =
237 ParagraphBuilder::make(paraStyle, fontCollection, unicode);
238 builder->addText(" Furthermore, العربية نص جميل. द क्विक ब्राउन फ़ॉक्स jumps over the lazy 🐕.");
239 auto paragraph = builder->Build();
240 paragraph->layout(mRECT.W() - 20);
241 paragraph->paint(canvas, 10, 10);
242
243 }
244};
245
246END_IGRAPHICS_NAMESPACE
247END_IPLUG_NAMESPACE
This file contains the base IControl implementation, along with some base classes for specific types ...
The lowest level base class of an IGraphics control.
Definition: IControl.h:49
IGraphics * GetUI()
Definition: IControl.h:472
The lowest level base class of an IGraphics context.
Definition: IGraphics.h:86
void DrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend=0)
Draw some text to the graphics context in a specific rectangle.
Definition: IGraphics.cpp:678
virtual void * GetDrawContext()=0
Gets a void pointer to underlying drawing context, for the IGraphics backend See draw class implement...
virtual void FillRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend=0)
Fill a rectangular region of the graphics context with a color.
Definition: IGraphics.cpp:2580
A control that demonstrates how to draw rich text using SkParagraph.
void Draw(IGraphics &g) override
Draw the control to the graphics context.
Used to manage a rectangular area, independent of draw class/platform.
float W() const