Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configurable font for text annotations #68

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ glow = "0.13.1"
glib-macros = "0.19.2"
glib = "0.19.2"
resource = "0.5.0" # font emedding
fontconfig = "0.8.0" # font loading

[dependencies.relm4-icons]
version = "0.8.2"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
save-after-copy = false
# Hide toolbars by default
default-hide-toolbars = false
# Font to use for text annotations
[font]
family = "Roboto"
style = "Bold"

# custom colours for the colour palette
[color-palette]
Expand Down Expand Up @@ -118,6 +122,10 @@ Options:
After copying the screenshot, save it to a file as well
-d, --default-hide-toolbars
Hide toolbars by default
--font-family <FONT_FAMILY>
Font family to use for text annotations
--font-style <FONT_STYLE>
Font style to use for text annotations
-h, --help
Print help
-V, --version
Expand Down Expand Up @@ -157,6 +165,7 @@ Satty is based on GTK-4 and Adwaita.
- libgtk-4-1
- libadwaita-1-0
- libepoxy
- fontconfig

### Arch Linux & Gentoo

Expand All @@ -165,6 +174,7 @@ Satty is based on GTK-4 and Adwaita.
- gtk4
- gdk-pixbuf2
- libepoxy
- fontconfig

## License

Expand Down
6 changes: 5 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
# After copying the screenshot, save it to a file as well
save-after-copy = false
# Hide toolbars by default
dafault-hide-toolbars = false
default-hide-toolbars = false
# Font to use for text annotations
[font]
family = "Roboto"
style = "Regular"

# custom colours for the colour palette
[color-palette]
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
gtk4
wrapGAppsHook4 # this is needed for relm4-icons to properly load after gtk::init()
libadwaita
fontconfig

(rust-bin.stable.latest.default.override
{
Expand Down
8 changes: 8 additions & 0 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ pub struct CommandLine {
/// Hide toolbars by default
#[arg(short, long)]
pub default_hide_toolbars: bool,

/// Font family to use for text annotations
#[arg(long)]
pub font_family: Option<String>,

/// Font style to use for text annotations
#[arg(long)]
pub font_style: Option<String>,
}

#[derive(Debug, Clone, Copy, Default, ValueEnum)]
Expand Down
46 changes: 46 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ pub struct Configuration {
save_after_copy: bool,
color_palette: ColorPalette,
default_hide_toolbars: bool,
font: FontConfiguration,
}

#[derive(Default)]
pub struct FontConfiguration {
family: Option<String>,
style: Option<String>,
}

impl FontConfiguration {
pub fn family(&self) -> Option<&str> {
self.family.as_deref()
}
pub fn style(&self) -> Option<&str> {
self.style.as_deref()
}
fn merge(&mut self, file_font: FontFile) {
if let Some(v) = file_font.family {
self.family = Some(v);
}
if let Some(v) = file_font.style {
self.style = Some(v);
}
}
}

pub struct ColorPalette {
Expand Down Expand Up @@ -159,6 +183,9 @@ impl Configuration {
if let Some(v) = file.color_palette {
self.color_palette.merge(v);
}
if let Some(v) = file.font {
self.font.merge(v);
}
}

// overwrite with all specified values from command line
Expand Down Expand Up @@ -186,6 +213,12 @@ impl Configuration {
if command_line.save_after_copy {
self.save_after_copy = command_line.save_after_copy;
}
if let Some(v) = command_line.font_family {
self.font.family = Some(v);
}
if let Some(v) = command_line.font_style {
self.font.style = Some(v);
}
}

pub fn early_exit(&self) -> bool {
Expand Down Expand Up @@ -227,6 +260,10 @@ impl Configuration {
pub fn default_hide_toolbars(&self) -> bool {
self.default_hide_toolbars
}

pub fn font(&self) -> &FontConfiguration {
&self.font
}
}

impl Default for Configuration {
Expand All @@ -242,6 +279,7 @@ impl Default for Configuration {
save_after_copy: false,
color_palette: ColorPalette::default(),
default_hide_toolbars: false,
font: FontConfiguration::default(),
}
}
}
Expand All @@ -264,6 +302,14 @@ impl Default for ColorPalette {
struct ConfigurationFile {
general: Option<ConfiguationFileGeneral>,
color_palette: Option<ColorPaletteFile>,
font: Option<FontFile>,
}

#[derive(Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct FontFile {
family: Option<String>,
style: Option<String>,
}

#[derive(Deserialize)]
Expand Down
46 changes: 37 additions & 9 deletions src/femtovg_area/imp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{anyhow, Error, Result};
use glow::HasContext;
use std::{
cell::{RefCell, RefMut},
Expand All @@ -12,6 +12,7 @@ use femtovg::{
rgb::{RGB, RGBA, RGBA8},
Canvas, FontId, ImageFlags, ImageId, ImageSource, Paint, Path, PixelFormat, Transform2D,
};
use fontconfig::Fontconfig;
use gdk_pixbuf::Pixbuf;
use gtk::{glib, prelude::*, subclass::prelude::*};
use relm4::{gtk, Sender};
Expand All @@ -21,6 +22,7 @@ use crate::{
math::Vec2D,
sketch_board::{Action, SketchBoardInput},
tools::{CropTool, Drawable, Tool},
APP_CONFIG,
};

#[derive(Default)]
Expand Down Expand Up @@ -162,14 +164,40 @@ impl FemtoVGArea {
self.canvas.borrow_mut().replace(c);
}

self.font.borrow_mut().replace(
self.canvas
.borrow_mut()
.as_mut()
.unwrap() // this unwrap is safe because it gets placed above
.add_font_mem(&resource!("src/assets/Roboto-Regular.ttf"))
.expect("Cannot add font"),
);
let app_config = APP_CONFIG.read();
let font = app_config
.font()
.family()
.map(|font| {
let font = Fontconfig::new()
.ok_or_else(|| anyhow!("Error while initializing fontconfig"))?
.find(font, app_config.font().style())
.ok_or_else(|| anyhow!("Can not find font"))?;
let font_id = self
.canvas
.borrow_mut()
.as_mut()
.unwrap() // this unwrap is safe because it gets placed above
.add_font(font.path)?;
Ok(font_id)
})
.transpose()
.unwrap_or_else(|e: Error| {
println!("Error while loading font. Using default font: {e}");
None
});
if let Some(font) = font {
self.font.borrow_mut().replace(font);
} else {
self.font.borrow_mut().replace(
self.canvas
ri-char marked this conversation as resolved.
Show resolved Hide resolved
.borrow_mut()
.as_mut()
.unwrap() // this unwrap is safe because it gets placed above
.add_font_mem(&resource!("src/assets/Roboto-Regular.ttf"))
.expect("Cannot add font"),
);
}
}

fn setup_canvas(&self) -> Result<femtovg::Canvas<femtovg::renderer::OpenGl>> {
Expand Down
Loading