use objc2::rc::Retained;
use objc2_app_kit::{NSFont, NSFontDescriptorSymbolicTraits};
use objc2_foundation::{NSDictionary, NSString};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Weight {
UltraLight,
Thin,
Light,
Regular,
Medium,
Semibold,
Bold,
Heavy,
Black,
}
impl Weight {
pub(crate) fn ns(self) -> f64 {
unsafe {
use objc2_app_kit::*;
match self {
Weight::UltraLight => NSFontWeightUltraLight,
Weight::Thin => NSFontWeightThin,
Weight::Light => NSFontWeightLight,
Weight::Regular => NSFontWeightRegular,
Weight::Medium => NSFontWeightMedium,
Weight::Semibold => NSFontWeightSemibold,
Weight::Bold => NSFontWeightBold,
Weight::Heavy => NSFontWeightHeavy,
Weight::Black => NSFontWeightBlack,
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
enum Base {
Style(Style),
System(f64),
Monospaced(f64),
Named(String, f64),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Style {
LargeTitle,
Title,
Title2,
Title3,
Headline,
Subheadline,
Body,
Callout,
Footnote,
Caption,
Caption2,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Font {
base: Base,
weight: Option<Weight>,
size: Option<f64>,
italic: bool,
digits: bool,
}
macro_rules! styles {
($($name:ident => $style:ident, $doc:literal;)*) => {
$(
#[doc = $doc]
pub fn $name() -> Font {
Font::new(Base::Style(Style::$style))
}
)*
};
}
impl Font {
fn new(base: Base) -> Font {
Font { base, weight: None, size: None, italic: false, digits: false }
}
styles! {
large_title => LargeTitle, "The largest title (26 pt).";
title => Title, "A window or section title (22 pt).";
title2 => Title2, "A smaller title (17 pt).";
title3 => Title3, "A smaller title still (15 pt).";
headline => Headline, "Emphasised body text, as in a list's headings (13 pt, bold).";
subheadline => Subheadline, "Secondary headings (11 pt).";
body => Body, "Body text, what labels use (13 pt).";
callout => Callout, "Text set apart from the body (12 pt).";
footnote => Footnote, "Footnotes (10 pt).";
caption => Caption, "Captions (10 pt).";
caption2 => Caption2, "Secondary captions (10 pt).";
}
pub fn system(size: f64) -> Font {
Font::new(Base::System(size))
}
pub fn monospaced(size: f64) -> Font {
Font::new(Base::Monospaced(size))
}
pub fn named(name: &str, size: f64) -> Font {
Font::new(Base::Named(name.into(), size))
}
pub fn weight(mut self, weight: Weight) -> Font {
self.weight = Some(weight);
self
}
pub fn bold(self) -> Font {
self.weight(Weight::Bold)
}
pub fn size(mut self, points: f64) -> Font {
self.size = Some(points);
self
}
pub fn italic(mut self) -> Font {
self.italic = true;
self
}
pub fn monospaced_digits(mut self) -> Font {
self.digits = true;
self
}
pub fn ns(&self) -> Retained<NSFont> {
let (size, weight) = match &self.base {
Base::Style(style) => {
let f = preferred(*style);
(f.pointSize(), if *style == Style::Headline { Weight::Bold } else { Weight::Regular })
}
Base::System(s) | Base::Monospaced(s) | Base::Named(_, s) => (*s, Weight::Regular),
};
let size = self.size.unwrap_or(size);
let weight = self.weight.unwrap_or(weight);
let font = match &self.base {
Base::Monospaced(_) => NSFont::monospacedSystemFontOfSize_weight(size, weight.ns()),
Base::Named(name, _) => NSFont::fontWithName_size(&NSString::from_str(name), size)
.map(|f| if self.weight.is_some() { with_weight(&f, weight) } else { f })
.unwrap_or_else(|| NSFont::systemFontOfSize_weight(size, weight.ns())),
_ if self.digits => NSFont::monospacedDigitSystemFontOfSize_weight(size, weight.ns()),
Base::Style(style) if self.weight.is_none() && self.size.is_none() => preferred(*style),
_ => NSFont::systemFontOfSize_weight(size, weight.ns()),
};
if self.italic { italic(&font) } else { font }
}
}
impl Default for Font {
fn default() -> Font {
Font::body()
}
}
fn preferred(style: Style) -> Retained<NSFont> {
unsafe {
use objc2_app_kit::*;
let name: &objc2_app_kit::NSFontTextStyle = match style {
Style::LargeTitle => NSFontTextStyleLargeTitle,
Style::Title => NSFontTextStyleTitle1,
Style::Title2 => NSFontTextStyleTitle2,
Style::Title3 => NSFontTextStyleTitle3,
Style::Headline => NSFontTextStyleHeadline,
Style::Subheadline => NSFontTextStyleSubheadline,
Style::Body => NSFontTextStyleBody,
Style::Callout => NSFontTextStyleCallout,
Style::Footnote => NSFontTextStyleFootnote,
Style::Caption => NSFontTextStyleCaption1,
Style::Caption2 => NSFontTextStyleCaption2,
};
NSFont::preferredFontForTextStyle_options(name, &NSDictionary::new())
}
}
fn with_weight(font: &NSFont, weight: Weight) -> Retained<NSFont> {
let bold = matches!(weight, Weight::Semibold | Weight::Bold | Weight::Heavy | Weight::Black);
if !bold {
return font.retain();
}
let d = font.fontDescriptor().fontDescriptorWithSymbolicTraits(font.fontDescriptor().symbolicTraits() | NSFontDescriptorSymbolicTraits::TraitBold);
NSFont::fontWithDescriptor_size(&d, font.pointSize()).unwrap_or_else(|| font.retain())
}
fn italic(font: &NSFont) -> Retained<NSFont> {
let d = font.fontDescriptor().fontDescriptorWithSymbolicTraits(font.fontDescriptor().symbolicTraits() | NSFontDescriptorSymbolicTraits::TraitItalic);
NSFont::fontWithDescriptor_size(&d, font.pointSize()).unwrap_or_else(|| font.retain())
}
use objc2::Message;