summaryrefslogtreecommitdiff
path: root/src/frame.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-10-18 00:02:38 +0200
committerLaurenz <laurmaedje@gmail.com>2022-10-18 00:04:12 +0200
commitc0e972b91a7bf8d22cd24a38fc92a9c6214c8a0c (patch)
tree19362eb993f15ef1b9bceeac821852bb6edfe955 /src/frame.rs
parente21822665591dc19766275da1e185215a6b945ef (diff)
Reduce dependencies from compiler on library
Diffstat (limited to 'src/frame.rs')
-rw-r--r--src/frame.rs61
1 files changed, 59 insertions, 2 deletions
diff --git a/src/frame.rs b/src/frame.rs
index c7827a9c..a367935c 100644
--- a/src/frame.rs
+++ b/src/frame.rs
@@ -6,10 +6,9 @@ use std::sync::Arc;
use crate::font::Font;
use crate::geom::{
- Align, Em, Length, Numeric, Paint, Point, Shape, Size, Spec, Transform,
+ Align, Dir, Em, Length, Numeric, Paint, Point, Shape, Size, Spec, Transform,
};
use crate::image::Image;
-use crate::library::text::Lang;
use crate::model::{Dict, Value};
use crate::util::EcoString;
@@ -397,6 +396,64 @@ pub struct Glyph {
pub c: char,
}
+/// A code for a natural language.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Lang([u8; 3], u8);
+
+impl Lang {
+ /// The code for the english language.
+ pub const ENGLISH: Self = Self(*b"en ", 2);
+
+ /// Construct a language from a two- or three-byte ISO 639-1/2/3 code.
+ pub fn from_str(iso: &str) -> Option<Self> {
+ let len = iso.len();
+ if matches!(len, 2 ..= 3) && iso.is_ascii() {
+ let mut bytes = [b' '; 3];
+ bytes[.. len].copy_from_slice(iso.as_bytes());
+ bytes.make_ascii_lowercase();
+ Some(Self(bytes, len as u8))
+ } else {
+ None
+ }
+ }
+
+ /// Return the language code as an all lowercase string slice.
+ pub fn as_str(&self) -> &str {
+ std::str::from_utf8(&self.0[.. usize::from(self.1)]).unwrap_or_default()
+ }
+
+ /// The default direction for the language.
+ pub fn dir(self) -> Dir {
+ match self.as_str() {
+ "ar" | "dv" | "fa" | "he" | "ks" | "pa" | "ps" | "sd" | "ug" | "ur"
+ | "yi" => Dir::RTL,
+ _ => Dir::LTR,
+ }
+ }
+}
+
+/// A code for a region somewhere in the world.
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
+pub struct Region([u8; 2]);
+
+impl Region {
+ /// Construct a region from its two-byte ISO 3166-1 alpha-2 code.
+ pub fn from_str(iso: &str) -> Option<Self> {
+ if iso.is_ascii() {
+ let mut bytes: [u8; 2] = iso.as_bytes().try_into().ok()?;
+ bytes.make_ascii_uppercase();
+ Some(Self(bytes))
+ } else {
+ None
+ }
+ }
+
+ /// Return the region code as an all uppercase string slice.
+ pub fn as_str(&self) -> &str {
+ std::str::from_utf8(&self.0).unwrap_or_default()
+ }
+}
+
/// A link destination.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum Destination {