Skip to content

Commit 6726864

Browse files
committed
Add outlined digits
1 parent fb3ecc1 commit 6726864

File tree

5 files changed

+140
-4
lines changed

5 files changed

+140
-4
lines changed

readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ assert_eq!("¼", format!("{}", VulgarFraction::new(1, 4)));
2020
```
2121

2222
### Sub- and superscript
23-
Formats integers as sub- or superscript.
23+
Formats integers as sub- or superscript.
2424

2525
```rust
2626
use fmtastic::{Subscript, Superscript};
@@ -51,6 +51,16 @@ use fmtastic::Segmented;
5151
assert_eq!("🯶🯲🯸", format!("{}", Segmented(628_u32)));
5252
```
5353

54+
### Outlined
55+
Formats an unsigned integer using outlined digits
56+
from the [Legacy Computing Supplement] block.
57+
58+
```rust
59+
use fmtastic::Outlined;
60+
61+
assert_eq!("𜳶𜳲𜳸", format!("{}", Outlined(628_u32)));
62+
```
63+
5464
### Tally Marks
5565
Formats an unsigned integer as tally marks.
5666
```rust
@@ -71,8 +81,6 @@ assert_eq!("☐ Do the dishes", format!("{} Do the dishes", BallotBox(false)));
7181
assert_eq!("☒ Laundry", format!("{:#} Laundry", BallotBox(true)));
7282
```
7383

74-
[Legacy Computing]: https://www.unicode.org/charts/PDF/U1FB00.pdf
75-
7684
## [Docs](https://docs.rs/fmtastic)
7785

7886
## License
@@ -93,5 +101,7 @@ for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
93101
multi-licensed as above, without any additional terms or conditions.
94102

95103

104+
[Legacy Computing]: https://www.unicode.org/charts/PDF/U1FB00.pdf
105+
[Legacy Computing Supplement]: https://www.unicode.org/charts/PDF/U1CC00.pdf
96106
[Vulgar Fractions]: https://en.wikipedia.org/wiki/Fraction_(mathematics)#Simple,_common,_or_vulgar_fractions
97107
[`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html

src/integer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ where
2020
type Public: crate::Integer;
2121
type BaseTwo: Base<Self>;
2222
type BaseTen: Base<Self>;
23+
type BaseSixteen: Base<Self>;
2324

2425
fn range(from: Self, to: Self) -> impl DoubleEndedIterator<Item = Self>;
2526

@@ -68,6 +69,9 @@ pub(crate) struct Ten;
6869
#[derive(Debug)]
6970
pub(crate) struct Two;
7071

72+
#[derive(Debug)]
73+
pub(crate) struct Sixteen;
74+
7175
pub(crate) trait Base<I: IntegerImpl>: fmt::Debug {
7276
const VALUE: I;
7377

@@ -88,6 +92,7 @@ macro_rules! common_integer_items {
8892
type Public = $ty;
8993
type BaseTwo = Two;
9094
type BaseTen = Ten;
95+
type BaseSixteen = Sixteen;
9196

9297
fn range(from: Self, to: Self) -> impl DoubleEndedIterator<Item = Self> {
9398
from..to
@@ -124,6 +129,14 @@ macro_rules! impl_bases {
124129
x.ilog10()
125130
}
126131
}
132+
133+
impl Base<$ty> for Sixteen {
134+
const VALUE: $ty = 16;
135+
136+
fn ilog(x: $ty) -> u32 {
137+
x.ilog(Self::VALUE)
138+
}
139+
}
127140
};
128141
}
129142

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
//!
4242
//! [Legacy Computing]: https://www.unicode.org/charts/PDF/U1FB00.pdf
4343
//!
44+
//! # Outlined
45+
//! Formats an unsigned integer using outlined digits
46+
//! from the [Legacy Computing Supplement] block.
47+
//!
48+
//! ```
49+
//! # use fmtastic::Outlined;
50+
//! assert_eq!("𜳶𜳲𜳸", format!("{}", Outlined(628_u32)));
51+
//! ```
52+
//!
53+
//! [Legacy Computing Supplement]: https://www.unicode.org/charts/PDF/U1CC00.pdf
54+
//!
4455
//! # Tally Marks
4556
//! Formats an unsigned integer as tally marks.
4657
//!
@@ -114,6 +125,8 @@ mod ballot_box;
114125
pub use ballot_box::*;
115126
mod roman;
116127
pub use roman::*;
128+
mod outlined;
129+
pub use outlined::*;
117130

118131
mod digits;
119132

src/outlined.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use crate::digits::iter_digits;
2+
use crate::integer::{Base, IntegerImpl};
3+
use crate::UnsignedInteger;
4+
use core::fmt;
5+
6+
/// Formats an unsigned integer using outlined digits
7+
/// from the [Legacy Computing Supplement] block.
8+
///
9+
/// You may need to install an extra font such as [Kreative Square] to display these digits.
10+
///
11+
/// [Legacy Computing Supplement]: https://www.unicode.org/charts/PDF/U1CC00.pdf
12+
/// [Kreative Square]: http://www.kreativekorp.com/software/fonts/ksquare/
13+
///
14+
/// ```
15+
/// use fmtastic::Outlined;
16+
///
17+
/// assert_eq!("𜳶𜳲𜳸", Outlined(628_u32).to_string());
18+
///
19+
/// assert_eq!("𜳰", Outlined(0_u32).to_string());
20+
/// assert_eq!("𜳱", Outlined(1_u32).to_string());
21+
/// assert_eq!("𜳲", Outlined(2_u32).to_string());
22+
/// assert_eq!("𜳳", Outlined(3_u32).to_string());
23+
/// assert_eq!("𜳴", Outlined(4_u32).to_string());
24+
/// assert_eq!("𜳵", Outlined(5_u32).to_string());
25+
/// assert_eq!("𜳶", Outlined(6_u32).to_string());
26+
/// assert_eq!("𜳷", Outlined(7_u32).to_string());
27+
/// assert_eq!("𜳸", Outlined(8_u32).to_string());
28+
/// assert_eq!("𜳹", Outlined(9_u32).to_string());
29+
///
30+
/// // Binary
31+
/// assert_eq!("𜳰", format!("{:b}", Outlined(0_u8)));
32+
/// assert_eq!("𜳱𜳰𜳱𜳰𜳱𜳰", format!("{:+b}", Outlined(0b101010_u8)));
33+
///
34+
/// // Hexadecimal
35+
/// assert_eq!("𜳱𜳘𜳘𜳛𜳰", format!("{:X}", Outlined(0x1CCF0_u32)));
36+
/// ```
37+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
38+
pub struct Outlined<T>(pub T);
39+
40+
impl<T> From<T> for Outlined<T>
41+
where
42+
T: UnsignedInteger,
43+
{
44+
fn from(value: T) -> Self {
45+
Outlined(value)
46+
}
47+
}
48+
49+
impl<T> fmt::Binary for Outlined<T>
50+
where
51+
T: UnsignedInteger,
52+
{
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseTwo>(self.0.into_impl(), f)
55+
}
56+
}
57+
58+
impl<T> fmt::Display for Outlined<T>
59+
where
60+
T: UnsignedInteger,
61+
{
62+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseTen>(self.0.into_impl(), f)
64+
}
65+
}
66+
67+
impl<T> fmt::UpperHex for Outlined<T>
68+
where
69+
T: UnsignedInteger,
70+
{
71+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72+
fmt_outlined::<_, <T::Impl as IntegerImpl>::BaseSixteen>(self.0.into_impl(), f)
73+
}
74+
}
75+
76+
fn fmt_outlined<T: IntegerImpl, B: Base<T>>(n: T, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77+
iter_digits::<_, B>(n).try_for_each(|digit| write!(f, "{}", DIGITS[digit]))
78+
}
79+
80+
const DIGITS: [&str; 16] = [
81+
// Outlined digits 0-9
82+
"\u{1CCF0}",
83+
"\u{1CCF1}",
84+
"\u{1CCF2}",
85+
"\u{1CCF3}",
86+
"\u{1CCF4}",
87+
"\u{1CCF5}",
88+
"\u{1CCF6}",
89+
"\u{1CCF7}",
90+
"\u{1CCF8}",
91+
"\u{1CCF9}",
92+
// Outlined uppercase Latin alphabet A-F
93+
"\u{1CCD6}",
94+
"\u{1CCD7}",
95+
"\u{1CCD8}",
96+
"\u{1CCD9}",
97+
"\u{1CCDA}",
98+
"\u{1CCDB}",
99+
];

src/seven_segment.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ use core::fmt;
66
/// Formats an unsigned integer using seven-segment digits
77
/// from the [Legacy Computing] block.
88
///
9-
/// You may need to install an extra font such as [Sieben 7] or [Noto Sans Symbols 2]
9+
/// You may need to install an extra font such as [Sieben 7], [Cascadia Code], or [Noto Sans Symbols 2]
1010
/// since most other fonts do not support these digits.
1111
///
1212
/// [Legacy Computing]: https://www.unicode.org/charts/PDF/U1FB00.pdf
1313
/// [Sieben 7]: https://github.com/bash/sieben-7
1414
/// [Noto Sans Symbols 2]: https://fonts.google.com/noto/specimen/Noto+Sans+Symbols+2
15+
/// [Cascadia Code]: https://github.com/microsoft/cascadia-code
1516
///
1617
/// ```
1718
/// use fmtastic::Segmented;

0 commit comments

Comments
 (0)