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 2 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ 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"
ri-char marked this conversation as resolved.
Show resolved Hide resolved
font-style = "Bold"

# custom colours for the colour palette
[color-palette]
Expand Down Expand Up @@ -118,6 +121,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 +164,7 @@ Satty is based on GTK-4 and Adwaita.
- libgtk-4-1
- libadwaita-1-0
- libepoxy
- fontconfig

### Arch Linux & Gentoo

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

## License

Expand Down
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ output-filename = "/tmp/test-%Y-%m-%d_%H:%M:%S.png"
save-after-copy = false
# Hide toolbars by default
dafault-hide-toolbars = false
# Font to use for text annotations
font-family = "Roboto"
font-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
26 changes: 26 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct Configuration {
save_after_copy: bool,
color_palette: ColorPalette,
default_hide_toolbars: bool,
font_family: Option<String>,
font_style: Option<String>,
}

pub struct ColorPalette {
Expand Down Expand Up @@ -146,6 +148,12 @@ impl Configuration {
if let Some(v) = general.default_hide_toolbars {
self.default_hide_toolbars = v;
}
if let Some(v) = general.font_family {
self.font_family = Some(v);
}
if let Some(v) = general.font_style {
self.font_style = Some(v);
}
}
fn merge(&mut self, file: Option<ConfigurationFile>, command_line: CommandLine) {
// input_filename is required and needs to be overwritten
Expand Down Expand Up @@ -186,6 +194,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 +241,14 @@ impl Configuration {
pub fn default_hide_toolbars(&self) -> bool {
self.default_hide_toolbars
}

pub fn font_family(&self) -> Option<&String> {
self.font_family.as_ref()
}

pub fn font_style(&self) -> Option<&String> {
self.font_style.as_ref()
}
}

impl Default for Configuration {
Expand All @@ -242,6 +264,8 @@ impl Default for Configuration {
save_after_copy: false,
color_palette: ColorPalette::default(),
default_hide_toolbars: false,
font_family: None,
font_style: None,
}
}
}
Expand Down Expand Up @@ -277,6 +301,8 @@ struct ConfiguationFileGeneral {
output_filename: Option<String>,
save_after_copy: Option<bool>,
default_hide_toolbars: Option<bool>,
font_family: Option<String>,
font_style: Option<String>,
}

#[derive(Deserialize)]
Expand Down
38 changes: 30 additions & 8 deletions src/femtovg_area/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,34 @@ 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()
.and_then(|font_family| {
Fontconfig::new().and_then(|fc| {
fc.find(font_family, app_config.font_style().map(String::as_str))
})
})
.and_then(|f| {
self.canvas
.borrow_mut()
.as_mut()
.unwrap() // this unwrap is safe because it gets placed above
.add_font(f.path)
.ok()
});
if let Some(font) = font {
self.font.borrow_mut().replace(font);
} else {
self.font.borrow_mut().replace(
ri-char marked this conversation as resolved.
Show resolved Hide resolved
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"),
);
}
}

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