| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-17 16:55:47 +08:00
										 |  |  | #include <kiwano/render/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | #include <kiwano/render/TextLayout.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							|  |  |  | #include <kiwano/render/DirectX/NativePtr.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | TextLayoutPtr TextLayout::Create() | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     TextLayoutPtr ptr = new (std::nothrow) TextLayout; | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | TextLayoutPtr TextLayout::Create(const String& content, const TextStyle& style) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     TextLayoutPtr ptr = new (std::nothrow) TextLayout; | 
					
						
							|  |  |  |     if (ptr) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ptr->Reset(content, style); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ptr; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | TextLayout::TextLayout() | 
					
						
							|  |  |  |     : dirty_flag_(DirtyFlag::Clean) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::Reset(const String& text, const TextStyle& style) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     if (!text.empty()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Renderer::GetInstance().CreateTextLayout(*this, text, style); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         SetAlignment(style.alignment); | 
					
						
							|  |  |  |         SetWrapWidth(style.wrap_width); | 
					
						
							|  |  |  |         SetLineSpacing(style.line_spacing); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         SetDefaultFillBrush(style.fill_brush); | 
					
						
							|  |  |  |         SetDefaultOutlineBrush(style.outline_brush); | 
					
						
							|  |  |  |         SetDefaultOutlineStrokeStyle(style.outline_stroke); | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (style.show_underline) | 
					
						
							|  |  |  |             SetUnderline(style.show_underline, { 0, text.length() }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (style.show_strikethrough) | 
					
						
							|  |  |  |             SetStrikethrough(style.show_strikethrough, { 0, text.length() }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         Clear(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | uint32_t TextLayout::GetLineCount() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         DWRITE_TEXT_METRICS metrics; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         if (SUCCEEDED(native->GetMetrics(&metrics))) | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |             return metrics.lineCount; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | Size TextLayout::GetLayoutSize() const | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         DWRITE_TEXT_METRICS metrics; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         if (SUCCEEDED(native->GetMetrics(&metrics))) | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |             return (metrics.layoutWidth > 0) ? Size(metrics.layoutWidth, metrics.height) | 
					
						
							|  |  |  |                                              : Size(metrics.width, metrics.height); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     return Size(); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetFont(FontPtr font, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         ComPtr<IDWriteFontCollection> collection = NativePtr::Get<IDWriteFontCollection>(font); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetFontCollection(collection.Get(), { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontCollection failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetFontFamily(String const& family, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         WideString font_family = family.empty() ? L"" : string::ToWide(family); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetFontFamilyName(font_family.c_str(), { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontFamilyName failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetFontSize(float size, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetFontSize(size, { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontSize failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetFontWeight(uint32_t weight, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         DWRITE_FONT_WEIGHT font_weight = DWRITE_FONT_WEIGHT(weight); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetFontWeight(font_weight, { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontWeight failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetItalic(bool italic, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         DWRITE_FONT_STYLE font_style = italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetFontStyle(font_style, { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontStyle failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetUnderline(bool enable, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetUnderline(enable, { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetUnderline failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetStrikethrough(bool enable, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetStrikethrough(enable, { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetStrikethrough failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetFillBrush(BrushPtr brush, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = | 
					
						
							|  |  |  |             native->SetDrawingEffect(NativePtr::Get<ID2D1Brush>(brush).Get(), { range.start, range.length }); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetDrawingEffect failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetOutlineBrush(BrushPtr brush, TextRange range) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							|  |  |  |     // TODO
 | 
					
						
							|  |  |  |     KGE_NOT_USED(range); | 
					
						
							|  |  |  |     SetDefaultOutlineBrush(brush); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetOutlineStrokeStyle(StrokeStylePtr stroke, TextRange range) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							|  |  |  |     // TODO
 | 
					
						
							|  |  |  |     KGE_NOT_USED(range); | 
					
						
							|  |  |  |     SetDefaultOutlineStrokeStyle(stroke); | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | void TextLayout::SetAlignment(TextAlign align) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         DWRITE_TEXT_ALIGNMENT alignment = DWRITE_TEXT_ALIGNMENT(); | 
					
						
							|  |  |  |         switch (align) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |         case TextAlign::Left: | 
					
						
							|  |  |  |             alignment = DWRITE_TEXT_ALIGNMENT_LEADING; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextAlign::Right: | 
					
						
							|  |  |  |             alignment = DWRITE_TEXT_ALIGNMENT_TRAILING; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextAlign::Center: | 
					
						
							|  |  |  |             alignment = DWRITE_TEXT_ALIGNMENT_CENTER; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case TextAlign::Justified: | 
					
						
							|  |  |  |             alignment = DWRITE_TEXT_ALIGNMENT_JUSTIFIED; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetTextAlignment(alignment); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetTextAlignment failed"); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextLayout::SetWrapWidth(float wrap_width) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         DWRITE_WORD_WRAPPING wrapping = (wrap_width > 0) ? DWRITE_WORD_WRAPPING_WRAP : DWRITE_WORD_WRAPPING_NO_WRAP; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |         HRESULT hr = native->SetWordWrapping(wrapping); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (wrap_width > 0) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 hr = native->SetMaxWidth(wrap_width); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 // Fix the layout width when the text does not wrap
 | 
					
						
							|  |  |  |                 DWRITE_TEXT_METRICS metrics; | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                 hr = native->GetMetrics(&metrics); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |                     hr = native->SetMaxWidth(metrics.width); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetWordWrapping failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TextLayout::SetLineSpacing(float line_spacing) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if KGE_RENDER_ENGINE == KGE_RENDER_ENGINE_DIRECTX
 | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |     auto native = NativePtr::Get<IDWriteTextLayout>(this); | 
					
						
							|  |  |  |     KGE_ASSERT(native); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (native) | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         HRESULT hr = S_OK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         float spacing = line_spacing; | 
					
						
							|  |  |  |         if (spacing == 0.f) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             hr = native->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-02-16 20:14:01 +08:00
										 |  |  |             hr = native->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, spacing, spacing * 0.8f); | 
					
						
							| 
									
										
										
										
											2020-02-16 12:53:18 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetLineSpacing failed"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-14 22:59:29 +08:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     return;  // not supported
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace kiwano
 |