diff --git a/src/kiwano/render/DirectX/RenderContextImpl.cpp b/src/kiwano/render/DirectX/RenderContextImpl.cpp index 653ca846..da037a87 100644 --- a/src/kiwano/render/DirectX/RenderContextImpl.cpp +++ b/src/kiwano/render/DirectX/RenderContextImpl.cpp @@ -143,7 +143,7 @@ void RenderContextImpl::DrawTextLayout(const TextLayout& layout, const Point& of if (layout.GetOutlineStrokeStyle()) { - outline_width = layout.GetOutlineStrokeStyle()->GetStrokeWidth(); + outline_width = layout.GetOutlineStrokeStyle()->GetWidth(); } HRESULT hr = text_renderer_->DrawTextLayout(native.Get(), offset.x, offset.y, fill_brush.Get(), @@ -170,7 +170,7 @@ void RenderContextImpl::DrawShape(const Shape& shape) auto geometry = NativePtr::Get(shape); auto brush = NativePtr::Get(current_brush_); auto stroke_style = NativePtr::Get(current_stroke_); - float stroke_width = current_stroke_ ? current_stroke_->GetStrokeWidth() : 1.0f; + float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; render_target_->DrawGeometry(geometry.Get(), brush.Get(), stroke_width, stroke_style.Get()); @@ -185,7 +185,7 @@ void RenderContextImpl::DrawLine(const Point& point1, const Point& point2) auto brush = NativePtr::Get(current_brush_); auto stroke_style = NativePtr::Get(current_stroke_); - float stroke_width = current_stroke_ ? current_stroke_->GetStrokeWidth() : 1.0f; + float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; render_target_->DrawLine(DX::ConvertToPoint2F(point1), DX::ConvertToPoint2F(point2), brush.Get(), stroke_width, stroke_style.Get()); @@ -200,7 +200,7 @@ void RenderContextImpl::DrawRectangle(const Rect& rect) auto brush = NativePtr::Get(current_brush_); auto stroke_style = NativePtr::Get(current_stroke_); - float stroke_width = current_stroke_ ? current_stroke_->GetStrokeWidth() : 1.0f; + float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; render_target_->DrawRectangle(DX::ConvertToRectF(rect), brush.Get(), stroke_width, stroke_style.Get()); @@ -214,7 +214,7 @@ void RenderContextImpl::DrawRoundedRectangle(const Rect& rect, const Vec2& radiu auto brush = NativePtr::Get(current_brush_); auto stroke_style = NativePtr::Get(current_stroke_); - float stroke_width = current_stroke_ ? current_stroke_->GetStrokeWidth() : 1.0f; + float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; render_target_->DrawRoundedRectangle(D2D1::RoundedRect(DX::ConvertToRectF(rect), radius.x, radius.y), brush.Get(), stroke_width, stroke_style.Get()); @@ -229,7 +229,7 @@ void RenderContextImpl::DrawEllipse(const Point& center, const Vec2& radius) auto brush = NativePtr::Get(current_brush_); auto stroke_style = NativePtr::Get(current_stroke_); - float stroke_width = current_stroke_ ? current_stroke_->GetStrokeWidth() : 1.0f; + float stroke_width = current_stroke_ ? current_stroke_->GetWidth() : 1.0f; render_target_->DrawEllipse(D2D1::Ellipse(DX::ConvertToPoint2F(center), radius.x, radius.y), brush.Get(), stroke_width, stroke_style.Get()); diff --git a/src/kiwano/render/DirectX/RendererImpl.cpp b/src/kiwano/render/DirectX/RendererImpl.cpp index 42692fb4..64c9601d 100644 --- a/src/kiwano/render/DirectX/RendererImpl.cpp +++ b/src/kiwano/render/DirectX/RendererImpl.cpp @@ -605,11 +605,11 @@ void RendererImpl::CreateTextLayout(TextLayout& layout, const String& content, c font = new Font; } - float font_size = font->GetSize(); - auto font_weight = DWRITE_FONT_WEIGHT(font->GetWeight()); - bool is_italic = (font->GetPosture() == FontPosture::Italic); - auto font_style = is_italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL; - auto collection = NativePtr::Get(font); + float font_size = font->GetSize(); + auto font_weight = DWRITE_FONT_WEIGHT(font->GetWeight()); + auto font_style = DWRITE_FONT_STYLE(font->GetPosture()); + auto font_stretch = DWRITE_FONT_STRETCH(font->GetStretch()); + auto collection = NativePtr::Get(font); WideString font_family; @@ -620,8 +620,8 @@ void RendererImpl::CreateTextLayout(TextLayout& layout, const String& content, c } ComPtr format; - hr = d2d_res_->CreateTextFormat(format, font_family.c_str(), collection, font_weight, font_style, - DWRITE_FONT_STRETCH_NORMAL, font_size); + hr = d2d_res_->CreateTextFormat(format, font_family.c_str(), collection, font_weight, font_style, font_stretch, + font_size); if (SUCCEEDED(hr)) { diff --git a/src/kiwano/render/Font.cpp b/src/kiwano/render/Font.cpp index 4ddb859b..164830ed 100644 --- a/src/kiwano/render/Font.cpp +++ b/src/kiwano/render/Font.cpp @@ -87,14 +87,16 @@ FontPtr Font::Preload(const Resource& resource) Font::Font() : size_(18.0f) , weight_(FontWeight::Normal) - , posture_(FontPosture::Regular) + , posture_(FontPosture::Normal) + , stretch_(FontStretch::Normal) { } -Font::Font(const String& family_name, float size, uint32_t weight, FontPosture posture) +Font::Font(const String& family_name, float size, uint32_t weight, FontPosture posture, FontStretch stretch) : size_(size) , weight_(weight) , posture_(posture) + , stretch_(stretch) { if (family_name.empty()) return; diff --git a/src/kiwano/render/Font.h b/src/kiwano/render/Font.h index c53722f5..2ab760bd 100644 --- a/src/kiwano/render/Font.h +++ b/src/kiwano/render/Font.h @@ -46,7 +46,7 @@ struct FontWeight Light = 300U, Normal = 400U, ///< 正常 Medium = 500U, - Bold = 700U, + Bold = 700U, ///< 加粗 ExtraBold = 800U, Black = 900U, ExtraBlack = 950U @@ -59,10 +59,29 @@ struct FontWeight */ enum class FontPosture { - Regular, ///< 正常 + Normal, ///< 正常 + Oblique, ///< 倾斜体 Italic, ///< 斜体 }; +/** + * \~chinese + * @brief 字体拉伸 + */ +enum class FontStretch +{ + Unknown, + UltraCondensed, + ExtraCondensed, + Condensed, ///< 压缩 + SemiCondensed, + Normal, ///< 正常 + SemiExpanded, + Expanded, ///< 扩大 + ExtraExpanded, + UltraExpanded, +}; + /** * \~chinese * @brief 字体 @@ -91,7 +110,7 @@ public: /// @param weight 字体粗细 /// @param posture 字体形态 Font(const String& family_name, float size, uint32_t weight = FontWeight::Normal, - FontPosture posture = FontPosture::Regular); + FontPosture posture = FontPosture::Normal, FontStretch stretch = FontStretch::Normal); /// \~chinese /// @brief 获取字体族 @@ -109,6 +128,10 @@ public: /// @brief 获取字体形态 FontPosture GetPosture() const; + /// \~chinese + /// @brief 获取字体拉伸 + FontStretch GetStretch() const; + protected: /// \~chinese /// @brief 获取字体族 @@ -118,6 +141,7 @@ protected: float size_; uint32_t weight_; FontPosture posture_; + FontStretch stretch_; String family_name_; }; @@ -195,6 +219,11 @@ inline FontPosture Font::GetPosture() const return posture_; } +inline FontStretch Font::GetStretch() const +{ + return stretch_; +} + inline void Font::SetFamilyName(const String& name) { family_name_ = name; diff --git a/src/kiwano/render/StrokeStyle.cpp b/src/kiwano/render/StrokeStyle.cpp index c4ae9a50..3b8625a0 100644 --- a/src/kiwano/render/StrokeStyle.cpp +++ b/src/kiwano/render/StrokeStyle.cpp @@ -32,7 +32,7 @@ StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join) StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, DashStyle dash, float dash_offset) : StrokeStyle() { - SetStrokeWidth(width); + SetWidth(width); SetCapStyle(cap); SetLineJoinStyle(line_join); SetDashStyle(dash); @@ -43,7 +43,7 @@ StrokeStyle::StrokeStyle(float width, CapStyle cap, LineJoinStyle line_join, con float dash_offset) : StrokeStyle() { - SetStrokeWidth(width); + SetWidth(width); SetCapStyle(cap); SetLineJoinStyle(line_join); SetDashStyle(dash_array, dash_size); diff --git a/src/kiwano/render/StrokeStyle.h b/src/kiwano/render/StrokeStyle.h index d5ef65ae..f5fb8800 100644 --- a/src/kiwano/render/StrokeStyle.h +++ b/src/kiwano/render/StrokeStyle.h @@ -115,8 +115,7 @@ public: /// \~chinese /// @brief 获取线条宽度 - /// @param width 线条宽度 - float GetStrokeWidth() const; + float GetWidth() const; /// \~chinese /// @brief 获取线条端点样式 @@ -137,7 +136,7 @@ public: /// \~chinese /// @brief 设置线条宽度 /// @param width 线条宽度 - void SetStrokeWidth(float width); + void SetWidth(float width); /// \~chinese /// @brief 设置线条端点样式 @@ -188,7 +187,7 @@ private: /** @} */ -inline float StrokeStyle::GetStrokeWidth() const +inline float StrokeStyle::GetWidth() const { return stroke_width_; } @@ -213,7 +212,7 @@ inline float StrokeStyle::GetDashOffset() const return dash_offset_; } -inline void StrokeStyle::SetStrokeWidth(float width) +inline void StrokeStyle::SetWidth(float width) { stroke_width_ = width; } diff --git a/src/kiwano/render/TextLayout.cpp b/src/kiwano/render/TextLayout.cpp index 2103052f..962d213d 100644 --- a/src/kiwano/render/TextLayout.cpp +++ b/src/kiwano/render/TextLayout.cpp @@ -123,16 +123,23 @@ void TextLayout::SetFont(FontPtr font) auto font_weight = DWRITE_FONT_WEIGHT(font->GetWeight()); hr = native->SetFontWeight(font_weight, { 0, content_length_ }); - KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontSize failed"); + KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontWeight failed"); } // reset font style { - bool is_italic = (font->GetPosture() == FontPosture::Italic); - auto font_style = is_italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL; + auto font_style = DWRITE_FONT_STYLE(font->GetPosture()); hr = native->SetFontStyle(font_style, { 0, content_length_ }); - KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontSize failed"); + KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontStyle failed"); + } + + // reset font stretch + { + auto font_stretch = DWRITE_FONT_STRETCH(font->GetStretch()); + + hr = native->SetFontStretch(font_stretch, { 0, content_length_ }); + KGE_THROW_IF_FAILED(hr, "IDWriteTextLayout::SetFontStretch failed"); } } #else