Rust文本渲染库iced_glyphon的使用,iced_glyphon为Rust GUI框架iced提供高级文本排版和字体渲染功能
Rust文本渲染库iced_glyphon的使用
iced_glyphon是为Rust GUI框架iced提供高级文本排版和字体渲染功能的临时分支库,它是一个基于wgpu的快速、简单的2D文本渲染解决方案。
示例代码
以下是一个使用iced_glyphon的基本示例:
use iced::widget::text;
use iced_glyphon::Text;
use iced::{Application, Settings};
struct Demo;
impl Application for Demo {
type Message = ();
type Flags = ();
type Executor = iced::executor::Default;
type Theme = iced::Theme;
fn new(_flags: ()) -> (Self, iced::Command<Self::Message>) {
(Demo, iced::Command::none())
}
fn title(&self) -> String {
String::from("iced_glyphon示例")
}
fn view(&self) -> iced::Element<Self::Message> {
// 使用iced_glyphon的Text组件
Text::new("Hello, iced_glyphon!")
.size(30)
.into()
}
fn update(&mut self, _message: Self::Message) -> iced::Command<Self::Message> {
iced::Command::none()
}
}
fn main() -> iced::Result {
Demo::run(Settings::default())
}
完整示例Demo
use iced::{
widget::{column, container, scrollable, text},
Alignment, Application, Command, Element, Length, Settings, Theme,
};
use iced_glyphon::Text;
fn main() -> iced::Result {
TextRenderingDemo::run(Settings::default())
}
struct TextRenderingDemo {
font_size: u16,
}
#[derive(Debug, Clone)]
enum Message {
IncreaseFontSize,
DecreaseFontSize,
}
impl Application for TextRenderingDemo {
type Message = Message;
type Flags = ();
type Executor = iced::executor::Default;
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<Message>) {
(
TextRenderingDemo {
font_size: 16,
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("iced_glyphon高级示例")
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::IncreaseFontSize => {
self.font_size += 2;
}
Message::DecreaseFontSize => {
if self.font_size > 10 {
self.font_size -= 2;
}
}
}
Command::none()
}
fn view(&self) -> Element<Message> {
let controls = column![
text("调整字体大小:").size(20),
text(format!("当前大小: {}", self.font_size)),
column![
text("增加字体").size(14).on_press(Message::IncreaseFontSize),
text("减小字体").size(14).on_press(Message::DecreaseFontSize),
]
.spacing(10),
]
.spacing(10)
.align_items(Alignment::Center);
let demo_text = Text::new(
"这是一个使用iced_glyphon的示例文本,展示了高级文本渲染功能。\n\n\
iced_glyphon提供了高质量的文本排版和字体渲染能力。",
)
.size(self.font_size);
let content = column![controls, demo_text]
.spacing(20)
.padding(20)
.align_items(Alignment::Center);
container(scrollable(content))
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
安装
在您的项目目录中运行以下Cargo命令:
cargo add iced_glyphon
或者在您的Cargo.toml中添加以下行:
iced_glyphon = "0.6.0"
许可证
该项目采用以下任一许可证:
- Apache License, Version 2.0
- zlib License
- MIT License
贡献
除非您明确声明,否则任何有意提交用于此项目的贡献,如Apache 2.0许可证中所定义,都应按照上述三重许可证进行授权,不附加任何额外条款或条件。
1 回复
Rust文本渲染库iced_glyphon使用指南
完整示例demo
以下是一个结合了基础使用、自定义字体和富文本渲染的完整示例:
use iced::{
alignment::{Horizontal, Vertical},
widget::{container, text},
Application, Command, Element, Font, Length, Settings, Theme, Color,
};
use iced_glyphon::{Glyphon, Text, Style, LineHeight, Shaping};
fn main() -> iced::Result {
Glyphon::run(Settings::default())
}
struct MyApp;
// 定义自定义字体
const CUSTOM_FONT: Font = Font::with_name("Noto Sans CJK SC");
impl Application for MyApp {
type Executor = iced::executor::Default;
type Message = ();
type Theme = Theme;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
(MyApp, Command::none())
}
fn title(&self) -> String {
String::from("iced_glyphon 完整示例")
}
fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
Command::none()
}
fn view(&self) -> Element<Self::Message> {
// 创建富文本内容
let rich_text = Text {
content: "欢迎使用iced_glyphon\n这是一个多行文本示例",
bounds: iced::Size::new(400.0, 150.0),
size: 28.0,
line_height: LineHeight::Absolute(36.0),
font: iced_glyphon::Font::DEFAULT,
horizontal_alignment: Horizontal::Center,
vertical_alignment: Vertical::Center,
shaping: Shaping::Advanced,
styles: vec![
Style {
color: Color::from_rgb(0.9, 0.2, 0.2), // 红色
start: 0,
end: 6,
},
Style {
color: Color::from_rgb(0.1, 0.6, 0.1), // 绿色
start: 7,
end: 19,
},
],
};
// 创建UI布局
container(
iced::widget::column![
// 基础文本
text("基础文本示例")
.size(32)
.font(iced_glyphon::Font::DEFAULT),
// 自定义字体文本
text("自定义字体文本")
.size(24)
.font(CUSTOM_FONT),
// 富文本渲染
iced_glyphon::text(rich_text),
// 高级布局文本
text("高级文本布局")
.size(20)
.shaping(Shaping::Advanced)
.horizontal_alignment(Horizontal::Right)
]
.spacing(20)
.align_items(iced::Alignment::Center)
)
.width(Length::Fill)
.height(Length::Fill)
.padding(40)
.center_x()
.center_y()
.into()
}
}
示例说明
- 基础文本:展示了如何使用默认字体渲染简单文本
- 自定义字体:演示了如何加载和使用自定义字体
- 富文本渲染:
- 包含多行文本
- 不同部分使用不同颜色
- 设置了自定义行高
- 文本居中显示
- 高级布局:
- 使用高级文本布局(Shaping::Advanced)
- 文本右对齐
运行说明
- 将上述代码保存为
main.rs
- 确保
Cargo.toml
中包含正确的依赖 - 如需使用自定义字体,请确保字体文件可用
- 运行命令:
cargo run
这个示例展示了iced_glyphon
的主要功能,包括基础文本渲染、自定义字体支持、富文本样式和高级布局选项。开发者可以根据实际需求调整文本样式和布局参数。