Skip to content

Commit

Permalink
Fix DPI scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlcctrlv committed Sep 1, 2020
1 parent a5ffb08 commit b740027
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
7 changes: 7 additions & 0 deletions AUTHORS
@@ -1,2 +1,9 @@
Source code:
Fredrick Brennan (@ctrlcctrlv)
@jazzfool

Design, icons:
Eli Heuer (@eliheuer)

Font:
Ubuntu (Canonical Ltd., Dalton Maag)
11 changes: 10 additions & 1 deletion Cargo.toml
Expand Up @@ -6,14 +6,23 @@ authors = ["Fredrick Brennan <copypaste@kittens.ph>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# For display
reclutch = { git = "https://github.com/jazzfool/reclutch", features = ["skia"] }
imgui-glium-renderer = "0.4.0"
imgui-winit-support = "0.4.0"
imgui = "0.4.0"
gl = "0.14.0"
glium = "0.27.0"

# For icons in toolbox
nsvg = "0.5.1"

# For glifparser
xmltree = "0.10.1"

# For global state
lazy_static = "1.4.0"

# For argument parsing
clap = "2.33.3"
git-version = "0.3.4"
thiserror = "1.0.20"
Binary file added resources/fonts/Ubuntu-Regular.ttf
Binary file not shown.
32 changes: 26 additions & 6 deletions src/main.rs
@@ -1,15 +1,19 @@
#![feature(assoc_char_funcs, panic_info_message)]
//! Qglif - A cross-platform .glif renderer and editor.
//! Main author is Fredrick Brennan (@ctrlcctrlv); see AUTHORS.
//! (c) 2020. Apache 2.0 licensed.

// Cargo.toml comments say what crates are used for what.
#[macro_use]
extern crate lazy_static;
extern crate thiserror;
#[macro_use]
extern crate glium;
extern crate clap;
extern crate gl;
extern crate xmltree;
#[macro_use]
extern crate git_version; // for util::parse_args
extern crate nsvg;
extern crate reclutch;

use glium::glutin;
Expand All @@ -29,7 +33,7 @@ extern crate imgui_glium_renderer;
extern crate imgui_winit_support;
use imgui::Context as ImguiContext;
use imgui_glium_renderer::Renderer as ImguiRenderer;
use imgui_winit_support::WinitPlatform;
use imgui_winit_support::{HiDpiMode, WinitPlatform};

#[macro_use]
mod util;
Expand Down Expand Up @@ -119,8 +123,22 @@ fn main() {

let mut imgui = ImguiContext::create();
let mut platform = WinitPlatform::init(&mut imgui);
platform.attach_window(
imgui.io_mut(),
&gl_display.gl_window().window(),
HiDpiMode::Default,
);
debug!("DPI is {}", gl_display.gl_window().window().scale_factor());

imgui.set_ini_filename(None);
imgui.io_mut().display_size = [window_size.0 as f32, window_size.1 as f32];

state.with(|v| {
v.borrow_mut().dpi = gl_display.gl_window().window().scale_factor();
});

opengl::imgui::set_imgui_fonts(&mut imgui);
opengl::imgui::set_imgui_dpi(&mut imgui, window_size);

let mut renderer =
ImguiRenderer::init(&mut imgui, &gl_display).expect("Failed to initialize renderer");

Expand Down Expand Up @@ -150,8 +168,10 @@ fn main() {
.unwrap();

gl_display.gl_window().resize(physical_size);
imgui.io_mut().display_size =
[physical_size.width as f32, physical_size.height as f32];
opengl::imgui::set_imgui_dpi(
&mut imgui,
(physical_size.width, physical_size.height),
);

skia_context =
Some(unsafe { skia_context.take().unwrap().make_current().unwrap() });
Expand Down Expand Up @@ -282,7 +302,6 @@ fn main() {
)
.unwrap();

let mut frame_target = gl_display.draw();
let target = &mut out_texture_fb;

//target.clear_color_and_depth((1.0, 1.0, 1.0, 1.0), 1.0);
Expand All @@ -309,6 +328,7 @@ fn main() {
&mut last_frame,
&mut renderer,
);
let mut frame_target = gl_display.draw();
frame_target
.draw(
&quad_vertex_buffer,
Expand Down
31 changes: 31 additions & 0 deletions src/opengl/imgui.rs
Expand Up @@ -6,6 +6,8 @@ use std::time::Instant;

use crate::state::state;

mod icons;

pub fn build_imgui_ui(ui: &mut imgui::Ui) {
imgui::Window::new(im_str!("Hello world"))
.bg_alpha(1.) // See comment on fn redraw_skia
Expand All @@ -22,6 +24,35 @@ pub fn build_imgui_ui(ui: &mut imgui::Ui) {
});
}

pub fn set_imgui_fonts(imgui: &mut imgui::Context) {
let dpi = state.with(|v| v.borrow().dpi as f32);
let mut fontconfig = imgui::FontConfig::default();
fontconfig.oversample_h = f32::ceil(dpi) as i32 + 1;
fontconfig.oversample_v = fontconfig.oversample_h;
imgui.fonts().add_font(&[imgui::FontSource::TtfData {
data: include_bytes!(concat!(
env!("PWD"),
"/",
"resources/fonts/Ubuntu-Regular.ttf"
)),
size_pixels: 14.,
config: Some(fontconfig),
}]);
}

pub fn set_imgui_dpi(imgui: &mut imgui::Context, window_size: (u32, u32)) {
state.with(|v| {
let dpi = v.borrow().dpi as f32;
imgui.style_mut().scale_all_sizes(dpi);
imgui.io_mut().display_size = [
window_size.0 as f32 * (1. / dpi),
window_size.1 as f32 * (1. / dpi),
];
imgui.io_mut().font_global_scale = 1.;
imgui.style_mut().use_light_colors();
});
}

pub fn render_imgui_frame(
target: &mut glium::framebuffer::SimpleFrameBuffer,
imgui: &mut imgui::Context,
Expand Down
3 changes: 3 additions & 0 deletions src/state.rs
Expand Up @@ -14,6 +14,7 @@ pub enum Mode {
Move,
Select,
}

// Thread local state.
pub struct State {
pub mode: Mode,
Expand All @@ -28,6 +29,7 @@ pub struct State {
pub winsize: PhysicalSize<u32>, // for Skia
pub factor: f32,
pub offset: (f32, f32),
pub dpi: f64, // from glutin scale_factor()
}

impl State {
Expand All @@ -48,6 +50,7 @@ impl State {
},
factor: 1.,
offset: (0., 0.),
dpi: 1.,
}
}
}
Expand Down

0 comments on commit b740027

Please sign in to comment.