Skip to content

Commit 092331e

Browse files
authored
feat: add more optons to style text (#18)
1 parent 6b3c0cb commit 092331e

File tree

16 files changed

+645
-106
lines changed

16 files changed

+645
-106
lines changed

Cargo.lock

Lines changed: 106 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "protextinator"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2021"
55
description = "Text management, made simple"
66
keywords = ["text", "rendering", "gui", "graphics", "image"]
@@ -16,7 +16,7 @@ default = []
1616
serialization = ["dep:serde"]
1717

1818
[dependencies]
19-
cosmic-text = "0.14"
19+
cosmic-text = "0.16.0"
2020
ahash = "0.8.12"
2121
smol_str = "0.3"
2222
serde = { version = "1.0.219", features = ["derive"], optional = true }

README.md

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,99 @@
1-
# 🚀 Protextinator
1+
# Protextinator
22

3-
<div align="center">
4-
<a href="https://crates.io/crates/protextinator"><img src="https://img.shields.io/crates/v/protextinator.svg" alt="Crates.io"></a>
5-
<a href="https://docs.rs/protextinator/latest/protextinator/"><img src="https://img.shields.io/docsrs/protextinator" alt="Docs.rs"></a>
6-
<a href="https://github.com/antouhou/protextinator/blob/main/LICENSE_MIT"><img src="https://img.shields.io/crates/l/protextinator" alt="License"></a>
7-
</div>
3+
[![Crates.io](https://img.shields.io/crates/v/protextinator.svg)](https://crates.io/crates/protextinator)
4+
[![Docs.rs](https://img.shields.io/docsrs/protextinator)](https://docs.rs/protextinator/latest/protextinator/)
5+
[![License](https://img.shields.io/crates/l/protextinator)](https://github.com/antouhou/protextinator/blob/main/LICENSE_MIT)
86

9-
## ✨ Text Management, Made Simple!
7+
Protextinator is a text editing and rendering library for Rust, built on top of [cosmic_text](https://github.com/pop-os/cosmic-text). It provides a simpler API while adding features like vertical alignment, scroll position management, and text selection.
108

11-
**Protextinator** is a powerful text editing and rendering library built on top of [cosmic_text](https://github.com/pop-os/cosmic-text), providing a simpler API with advanced features for all your text handling needs!
9+
**Note:** This library is still a work in progress. APIs may change.
1210

13-
> 💡 Perfect for game UIs, text editors, and any application that needs sophisticated text rendering with minimal hassle.
11+
## Features
1412

15-
⚠️ **WARNING**: This library is work in progress, use at your own risk! ⚠️
13+
- Vertical text alignment
14+
- Text buffer size measurement
15+
- Scroll position management with absolute coordinates
16+
- Simple font loading from files or embedded bytes
17+
- Text state collection with optional usage tracking
18+
- Custom metadata for text states
19+
- Text selection and editing
20+
- Efficient text buffer caching
21+
- Word wrapping and text styling
22+
- Optional serialization support
1623

17-
## 🔥 Features
24+
## Installation
1825

19-
- **Vertical text alignment** - Position your text exactly where you want it
20-
- **Text buffer size measurement** - Know exactly how much space your text needs
21-
- **Scroll position management** with absolute coordinates
22-
- **Simple font loading interface** - Load fonts from files or embedded bytes
23-
- **Text state collection** with optional usage tracking for garbage collection
24-
- **Custom metadata** for text states
25-
- **Text selection and editing** capabilities
26-
- **Efficient text buffer caching**
27-
- **Word wrapping and text styling**
28-
- **Optional serialization** support via the `serialization` feature
29-
30-
## 📦 Installation
31-
32-
Add Protextinator to your `Cargo.toml`:
26+
Add this to your `Cargo.toml`:
3327

3428
```toml
3529
[dependencies]
36-
protextinator = "0.1.0"
30+
protextinator = "0.5.0"
3731
```
3832

39-
With serialization support:
33+
With serialization:
4034

4135
```toml
4236
[dependencies]
4337
protextinator = { version = "0.1.0", features = ["serialization"] }
4438
```
4539

46-
## 🚀 Quick Start
47-
48-
For code examples and detailed usage, check out:
49-
- [Documentation on docs.rs](https://docs.rs/protextinator/)
50-
- [Example code in the repository](https://github.com/antouhou/protextinator/tree/main/examples)
51-
52-
Protextinator makes it easy to:
53-
1. Create and manage text states
54-
2. Style text with various fonts, colors, and alignments
55-
3. Handle text selection and editing
56-
4. Efficiently render text in your application
57-
58-
## 🎮 Integration Example
59-
60-
Protextinator works great with rendering libraries like [Grafo](https://github.com/antouhou/grafo) and windowing libraries like [Winit](https://github.com/rust-windowing/winit). Check out the examples directory for a complete integration example.
61-
62-
## 📚 API Overview
63-
64-
- **TextManager**: The main entry point for managing text states and fonts
65-
- **TextState**: Represents a text buffer with styling and layout information
66-
- **Id**: A unique identifier for text states
67-
- **TextStyle**: Configure font size, color, alignment, and more
68-
- **Action**: Perform operations like copy, paste, and cursor movement
69-
70-
## 🔧 Contributing
40+
## Quick Start
41+
42+
```rust
43+
use protextinator::{TextManager, TextState, math::Size};
44+
use cosmic_text::{fontdb, Color};
45+
use protextinator::style::TextStyle;
46+
47+
// Create a text manager
48+
let mut text_manager = TextManager::new();
49+
50+
// Create a text state
51+
let id = protextinator::Id::new("my_text");
52+
let text = "Hello, world!";
53+
text_manager.create_state(id, text, ());
54+
// Add fonts
55+
let font_sources: Vec<fontdb::Source> = vec![];
56+
text_manager.load_fonts(font_sources.into_iter());
57+
// Alternatively, you can load fonts from bytes if you want to embed them into the binary
58+
// or download them at runtime as bytes
59+
let byte_sources: Vec<&'static [u8]> = vec![];
60+
text_manager.load_fonts_from_bytes(byte_sources.into_iter());
61+
62+
// Optional: Marks the beginning of a frame so that you can track which text states are accessed
63+
text_manager.start_frame();
64+
65+
// Configure the text area size and style
66+
if let Some(state) = text_manager.text_states.get_mut(&id) {
67+
state.set_outer_size(&Size::new(400.0, 200.0));
68+
69+
let style = TextStyle::new(16.0, Color::rgb(255, 255, 255))
70+
.with_line_height(1.5);
71+
state.set_style(&style);
72+
73+
// Enable editing
74+
state.is_editable = true;
75+
state.is_selectable = true;
76+
state.are_actions_enabled = true;
77+
78+
// Recalculate layout
79+
state.recalculate(&mut text_manager.text_context);
80+
81+
// Get the inner size of the buffer - i.e., how much space the text needs to occupy
82+
let inner_size = state.inner_size();
83+
}
84+
85+
let mut remove_ids = vec![];
86+
// Optional: going to remove all states that were not accessed during the current frame
87+
text_manager.end_frame(&mut remove_ids);
88+
```
7189

72-
Contributions are welcome! Feel free to open issues or submit pull requests.
90+
For a complete example, see the [examples directory](https://github.com/antouhou/protextinator/tree/main/examples).
7391

74-
## 📄 License
92+
## License
7593

7694
Licensed under either of:
7795

78-
- Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
96+
- Apache License, Version 2.0 ([LICENSE-APACHE](http://www.apache.org/licenses/LICENSE-2.0))
7997
- MIT license ([LICENSE-MIT](LICENSE_MIT) or http://opensource.org/licenses/MIT)
8098

8199
at your option.

examples/text.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ impl<'a> App<'a> {
133133
horizontal_alignment: HorizontalTextAlignment::Start,
134134
vertical_alignment: VerticalTextAlignment::Start,
135135
wrap: Some(TextWrap::Wrap),
136-
font_family: FontFamily::SansSerif,
136+
font_family: FontFamily::Name("Arial".into()),
137+
weight: protextinator::style::Weight::NORMAL,
138+
letter_spacing: None,
137139
};
138140

139141
// Create or update the text state
@@ -191,6 +193,8 @@ impl<'a> App<'a> {
191193
vertical_alignment: VerticalTextAlignment::Start,
192194
wrap: Some(TextWrap::Wrap),
193195
font_family: FontFamily::Serif,
196+
weight: protextinator::style::Weight::NORMAL,
197+
letter_spacing: None,
194198
};
195199

196200
// Create or update the stats text state

0 commit comments

Comments
 (0)