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

Implement functions and a class to treat texts and fonts #32

Merged
merged 6 commits into from
Jun 8, 2022
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
17 changes: 17 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use pyo3::prelude::*;

pub type Align = u32;
pub type MouseButton = u32;
pub type Key = u32;


pub const LEFT: Align = 0 as Align;
pub const RIGHT: Align = 1 as Align;
pub const TOP: Align = 2 as Align;
pub const BOTTOM: Align = 3 as Align;
pub const CENTER: Align = 4 as Align;
pub const MIDDLE: Align = 4 as Align;


pub const MOUSE_LEFT: MouseButton = 0 as MouseButton;
pub const MOUSE_RIGHT: MouseButton = 1 as MouseButton;
pub const MOUSE_MIDDLE: MouseButton = 2 as MouseButton;
Expand Down Expand Up @@ -178,6 +188,13 @@ pub fn add_module_constants(m: &PyModule) -> PyResult<()> {
};
}

add_constant!(LEFT)?;
add_constant!(RIGHT)?;
add_constant!(TOP)?;
add_constant!(BOTTOM)?;
add_constant!(MIDDLE)?;
add_constant!(CENTER)?;

add_constant!(MOUSE_LEFT)?;
add_constant!(MOUSE_RIGHT)?;
add_constant!(MOUSE_MIDDLE)?;
Expand Down
56 changes: 56 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ mod numpy_lib;
mod math_utils;
mod constant;

use crate::system::add_system_class;
use crate::system::*;
use crate::event::add_event_class;
use crate::numpy_lib::add_numpy_functions;
use crate::math_utils::add_math_functions;
use crate::constant::add_module_constants;
use crate::constant::*;

struct Model {
_window: window::Id,
Expand Down Expand Up @@ -193,6 +195,32 @@ fn stroke_weight(w: f32) {
instance().stroke_weight(w);
}

#[pyfunction]
fn text_font(font: QFont) {
instance().text_font(font);
}

#[pyfunction]
fn font_size(font_size: u32) {
instance().font_size(font_size);
}

#[pyfunction]
fn text_leading(text_leading: f32) {
instance().text_leading(text_leading);
}

#[pyfunction]
fn text_padding(padding: f32) {
instance().text_padding(padding);
}

#[pyfunction]
fn text_align(h_align: Align, v_align: Option<Align>) {
let v_align = v_align.unwrap_or(LEFT);
instance().text_align(h_align, v_align);
}

#[pyfunction]
fn background(r: u8, g: Option<u8>, b: Option<u8>, a: Option<u8>) {
let draw = get_draw();
Expand Down Expand Up @@ -286,6 +314,27 @@ fn polyline_list(points: &PyList) {
}));
}

#[pyfunction]
fn text(text: &str, x: f32, y: f32, w: Option<f32>, h: Option<f32>) {
let draw = get_draw();

match (w, h) {
(None, None) =>
draw.text(text)
.text_style()
.x_y(x, y),
(Some(w), Some(h)) => {
let rect = Rect::from_w_h(w, h)
.pad(instance().font_style.padding);
draw.text(text)
.text_style()
.x_y(x, y)
.wh(rect.wh())
}
_ => panic!("Invalid arguments")
};
}

#[pyfunction]
fn save_frame(file_path: &str) {
get_app().main_window().capture_frame(file_path);
Expand All @@ -312,6 +361,11 @@ fn engine(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(stroke, m)?)?;
m.add_function(wrap_pyfunction!(no_stroke, m)?)?;
m.add_function(wrap_pyfunction!(stroke_weight, m)?)?;
m.add_function(wrap_pyfunction!(text_font, m)?)?;
m.add_function(wrap_pyfunction!(font_size, m)?)?;
m.add_function(wrap_pyfunction!(text_leading, m)?)?;
m.add_function(wrap_pyfunction!(text_padding, m)?)?;
m.add_function(wrap_pyfunction!(text_align, m)?)?;
m.add_function(wrap_pyfunction!(background, m)?)?;
m.add_function(wrap_pyfunction!(ellipse, m)?)?;
m.add_function(wrap_pyfunction!(circle, m)?)?;
Expand All @@ -320,8 +374,10 @@ fn engine(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(arrow, m)?)?;
m.add_function(wrap_pyfunction!(polygon_list, m)?)?;
m.add_function(wrap_pyfunction!(polyline_list, m)?)?;
m.add_function(wrap_pyfunction!(text, m)?)?;
m.add_function(wrap_pyfunction!(save_frame, m)?)?;

add_system_class(&m)?;
add_event_class(&m)?;
add_numpy_functions(&m)?;
add_module_constants(&m)?;
Expand Down
120 changes: 120 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use nannou::draw::primitive::polygon::*;
use nannou::event::{Key, MouseButton};

use crate::event::*;
use crate::constant::*;

static mut INSTANCE: *mut AppState = 0 as *mut AppState;
static mut APP_INSTANCE: *mut App = 0 as *mut App;
Expand Down Expand Up @@ -104,6 +105,7 @@ pub struct AppState<'a> {
pub height: u32,
pub title: &'a str,
drawing_style: DrawingStyle,
pub font_style: FontStyle,
transform_matrix: Mat4,
matrix_stack: Vec<Mat4>,

Expand Down Expand Up @@ -139,6 +141,7 @@ impl<'a> AppState<'a> {
height: 800,
title: "q5",
drawing_style: DrawingStyle::new(),
font_style: FontStyle::new(),
transform_matrix: Mat4::IDENTITY,
matrix_stack,
mouse_event_state: MouseEventState::new(0.0, 0.0),
Expand Down Expand Up @@ -219,6 +222,38 @@ impl<'a> AppState<'a> {
pub fn get_stroke_weight(&self) -> f32 {
self.drawing_style.stroke_weight
}

pub fn text_font(&mut self, qfont: QFont) {
self.font_style.font = qfont.font;
}

pub fn font_size(&mut self, font_size: u32) {
self.font_style.font_size = font_size;
}

pub fn text_leading(&mut self, text_leading: f32) {
self.font_style.line_spacing = text_leading;
}

pub fn text_padding(&mut self, padding: f32) {
self.font_style.padding = padding;
}

pub fn text_align(&mut self, h_align: Align, v_align: Align) {
self.font_style.horizontal_align = match h_align {
LEFT => TextAlign::Start,
MIDDLE => TextAlign::Middle,
RIGHT => TextAlign::End,
_ => self.font_style.horizontal_align,
};

self.font_style.vertical_align = match v_align {
TOP => TextAlign::Start,
MIDDLE => TextAlign::Middle,
BOTTOM => TextAlign::End,
_ => self.font_style.vertical_align,
};
}
}

impl<'a> PythonCallback for AppState<'a> {
Expand Down Expand Up @@ -387,3 +422,88 @@ impl<'a, T> PathStyle for Drawing<'a, T>
}
}
}

pub struct FontStyle {
pub font: nannou::text::Font,
pub font_size: u32,
pub line_spacing: f32,
pub padding: f32,
pub horizontal_align: TextAlign,
pub vertical_align: TextAlign,
}

#[derive(Debug, Copy, Clone)]
pub enum TextAlign {
Start,
Middle,
End,
}

impl FontStyle {
pub fn new() -> FontStyle {
FontStyle {
font: nannou::text::font::default_notosans(),
font_size: 24,
line_spacing: 0.0,
padding: 0.0,
horizontal_align: TextAlign::Middle,
vertical_align: TextAlign::Middle,
}
}
}

pub trait TextStyle {
fn text_style(self) -> Self;
}

impl<'a> TextStyle for Drawing<'a, Text> {
fn text_style(self) -> Self {
let state = instance();
let ctx = match state.drawing_style.fill_color {
PColor::Gray8(lum) => self.color(rgb8(lum, lum, lum)),
PColor::Rgb8(r, g, b) => self.color(rgb8(r, g, b)),
PColor::Rgba8(r, g, b, a) => self.color(rgba8(r, g, b, a)),
_ => self,
};

let ctx = match state.font_style.vertical_align {
TextAlign::Start => ctx.align_text_top(),
TextAlign::Middle => ctx.align_text_middle_y(),
TextAlign::End => ctx.align_text_bottom(),
};

let ctx = match state.font_style.horizontal_align {
TextAlign::Start => ctx.left_justify(),
TextAlign::Middle => ctx.center_justify(),
TextAlign::End => ctx.right_justify(),
};

ctx.font(state.font_style.font.clone())
.font_size(state.font_style.font_size)
.line_spacing(state.font_style.line_spacing)
}
}

#[pyclass]
#[derive(Debug, Clone)]
pub struct QFont {
pub font: nannou::text::Font,
}

#[pymethods]
impl QFont {
#[new]
fn new(font_path: Option<&str>) -> Self {
let font = match font_path {
Some(path) => nannou::text::font::from_file(path)
.unwrap_or_else(|_| panic!("Failed to load font file.")),
None => nannou::text::font::default_notosans(),
};
QFont { font }
}
}

pub fn add_system_class(m: &PyModule) -> PyResult<()> {
m.add_class::<QFont>()?;
Ok(())
}