| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // 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:
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // 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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 21:55:29 +08:00
										 |  |  | #include <kiwano/renderer/TextLayout.h>
 | 
					
						
							|  |  |  | #include <kiwano/renderer/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace kiwano | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	TextLayout::TextLayout() | 
					
						
							|  |  |  | 		: dirty_flag_(DirtyFlag::Clean) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::Update() | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		if (!IsDirty()) | 
					
						
							|  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		if (text_.empty()) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text_format_.reset(); | 
					
						
							|  |  |  | 			text_layout_.reset(); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		if (!text_format_ || (dirty_flag_ & DirtyFlag::DirtyFormat)) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-17 11:24:24 +08:00
										 |  |  | 			Renderer::Instance().CreateTextFormat(*this); | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		if (dirty_flag_ & DirtyFlag::DirtyLayout) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-01-17 11:24:24 +08:00
										 |  |  | 			Renderer::Instance().CreateTextLayout(*this); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			if (text_layout_) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				SetAlignment(style_.alignment); | 
					
						
							|  |  |  | 				SetWrapWidth(style_.wrap_width); | 
					
						
							|  |  |  | 				SetLineSpacing(style_.line_spacing); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-10 09:30:30 +08:00
										 |  |  | 		dirty_flag_ = DirtyFlag::Updated; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetText(const String& text) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		text_ = text; | 
					
						
							|  |  |  | 		dirty_flag_ |= DirtyFlag::DirtyLayout; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetStyle(const TextStyle& style) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		style_ = style; | 
					
						
							|  |  |  | 		dirty_flag_ |= DirtyFlag::DirtyLayout; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetFont(FontPtr font) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		if (style_.font != font) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			style_.font = font; | 
					
						
							|  |  |  | 			dirty_flag_ |= DirtyFlag::DirtyFormat; | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetFontFamily(String const& family) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (style_.font_family != family) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			style_.font_family = family; | 
					
						
							|  |  |  | 			dirty_flag_ |= DirtyFlag::DirtyFormat; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetFontSize(float size) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (style_.font_size != size) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			style_.font_size = size; | 
					
						
							|  |  |  | 			dirty_flag_ |= DirtyFlag::DirtyFormat; | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetFontWeight(uint32_t weight) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (style_.font_weight != weight) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			style_.font_weight = weight; | 
					
						
							|  |  |  | 			dirty_flag_ |= DirtyFlag::DirtyFormat; | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetItalic(bool italic) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (style_.italic != italic) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			style_.italic = italic; | 
					
						
							|  |  |  | 			dirty_flag_ |= DirtyFlag::DirtyFormat; | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	uint32_t TextLayout::GetLineCount() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Force to update layout
 | 
					
						
							|  |  |  | 		const_cast<TextLayout*>(this)->Update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (text_layout_) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			DWRITE_TEXT_METRICS metrics; | 
					
						
							|  |  |  | 			if (SUCCEEDED(GetTextLayout()->GetMetrics(&metrics))) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 				return metrics.lineCount; | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	Size TextLayout::GetLayoutSize() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Force to update layout
 | 
					
						
							|  |  |  | 		const_cast<TextLayout*>(this)->Update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (text_layout_) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			DWRITE_TEXT_METRICS metrics; | 
					
						
							|  |  |  | 			if (SUCCEEDED(GetTextLayout()->GetMetrics(&metrics))) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 				return (metrics.layoutWidth > 0) ? Size(metrics.layoutWidth, metrics.height) : Size(metrics.width, metrics.height); | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		return Size(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetWrapWidth(float wrap_width) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		style_.wrap_width = wrap_width; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (text_layout_) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			HRESULT hr = S_OK; | 
					
						
							|  |  |  | 			bool enable_wrapping = (wrap_width > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 				hr = text_layout_->SetWordWrapping(enable_wrapping ? DWRITE_WORD_WRAPPING_WRAP : DWRITE_WORD_WRAPPING_NO_WRAP); | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				if (enable_wrapping) | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 					hr = text_layout_->SetMaxWidth(wrap_width); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					// Fix the layout width when the text does not wrap
 | 
					
						
							|  |  |  | 					DWRITE_TEXT_METRICS metrics; | 
					
						
							|  |  |  | 					hr = text_layout_->GetMetrics(&metrics); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						hr = text_layout_->SetMaxWidth(metrics.width); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-08-23 17:26:41 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetLineSpacing(float line_spacing) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		style_.line_spacing = line_spacing; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		if (text_layout_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			HRESULT hr = S_OK; | 
					
						
							|  |  |  | 			if (line_spacing == 0.f) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 				hr = text_layout_->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_DEFAULT, 0, 0); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				hr = text_layout_->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, line_spacing, line_spacing * 0.8f); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	void TextLayout::SetAlignment(TextAlign align) | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 		style_.alignment = align; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		if (text_layout_) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 			HRESULT hr = text_layout_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT(align)); | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 			win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void TextLayout::SetUnderline(bool enable, uint32_t start, uint32_t length) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Force to update layout
 | 
					
						
							|  |  |  | 		Update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		HRESULT hr = text_layout_ ? S_OK : E_FAIL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			hr = text_layout_->SetUnderline(enable, { start, length }); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 		win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-12-26 19:25:43 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void TextLayout::SetStrikethrough(bool enable, uint32_t start, uint32_t length) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// Force to update layout
 | 
					
						
							|  |  |  | 		Update(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		HRESULT hr = text_layout_ ? S_OK : E_FAIL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			hr = text_layout_->SetStrikethrough(enable, { start, length }); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-30 14:24:29 +08:00
										 |  |  | 		win32::ThrowIfFailed(hr); | 
					
						
							| 
									
										
										
										
											2019-08-15 11:22:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |