Skip to content

Commit ceb179b

Browse files
committed
Fix vertical alignment
1 parent caca096 commit ceb179b

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

plotters/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ all_series = ["area_series", "line_series", "point_series", "surface_series"]
7474
all_elements = ["errorbar", "candlestick", "boxplot", "histogram"]
7575

7676
# Tier 1 Backends
77-
bitmap_backend = ["plotters-bitmap", "ttf"]
77+
bitmap_backend = ["plotters-bitmap"]
7878
bitmap_encoder = ["plotters-bitmap/image_encoder"]
7979
bitmap_gif = ["plotters-bitmap/gif_backend"]
8080
svg_backend = ["plotters-svg"]

plotters/src/style/font/ab_glyph.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use super::{FontData, FontFamily, FontStyle, LayoutBox};
2+
use ::ab_glyph::{Font, FontRef, ScaleFont};
23
use ::core::fmt::{self, Display};
3-
use ::std::error::Error;
4+
use ::once_cell::sync::Lazy;
45
use ::std::collections::HashMap;
6+
use ::std::error::Error;
57
use ::std::sync::RwLock;
6-
use ::once_cell::sync::Lazy;
7-
use ::ab_glyph::{FontRef, Font, ScaleFont};
88

9-
static FONTS: Lazy<RwLock<HashMap<String, FontRef<'static>>>> = Lazy::new(|| RwLock::new(HashMap::new()));
9+
static FONTS: Lazy<RwLock<HashMap<String, FontRef<'static>>>> =
10+
Lazy::new(|| RwLock::new(HashMap::new()));
1011
pub struct InvalidFont {
1112
_priv: (),
1213
}
@@ -21,7 +22,7 @@ pub fn register_font(name: &str, bytes: &'static [u8]) -> Result<(), InvalidFont
2122

2223
#[derive(Clone)]
2324
pub struct FontDataInternal {
24-
font_ref: FontRef<'static>
25+
font_ref: FontRef<'static>,
2526
}
2627

2728
#[derive(Debug, Clone)]
@@ -45,7 +46,12 @@ impl FontData for FontDataInternal {
4546
type ErrorType = FontError;
4647
fn new(family: FontFamily<'_>, style: FontStyle) -> Result<Self, Self::ErrorType> {
4748
Ok(Self {
48-
font_ref: FONTS.read().unwrap().get(family.as_str()).ok_or(FontError::FontUnavailable)?.clone()
49+
font_ref: FONTS
50+
.read()
51+
.unwrap()
52+
.get(family.as_str())
53+
.ok_or(FontError::FontUnavailable)?
54+
.clone(),
4955
})
5056
}
5157
// TODO: ngl, it makes no sense that this uses the same error type as `new`
@@ -77,8 +83,7 @@ impl FontData for FontDataInternal {
7783
mut draw: DrawFunc,
7884
) -> Result<Result<(), E>, Self::ErrorType> {
7985
let font = self.font_ref.as_scaled(size as f32);
80-
let mut draw = |x: u32, y: u32, c| {
81-
let (x, y) = (x as i32, y as i32);
86+
let mut draw = |x: i32, y: i32, c| {
8287
let (base_x, base_y) = pos;
8388
draw(base_x + x, base_y + y, c)
8489
};
@@ -93,23 +98,20 @@ impl FontData for FontDataInternal {
9398
if let Some(q) = font.outline_glyph(glyph) {
9499
use ::std::panic::{self, AssertUnwindSafe};
95100
let rect = q.px_bounds();
96-
// Vertically center the things.
97-
let y_shift = (size as f32 - rect.height()) / 2.0;
98-
let y_shift = y_shift as u32;
101+
let y_shift = ((size as f32) / 2.0 + rect.min.y) as i32;
102+
let x_shift = x_shift as i32;
99103
let res = panic::catch_unwind(AssertUnwindSafe(|| {
100104
q.draw(|x, y, c| {
101-
if let Err(_) = draw(x + (x_shift as u32), y + y_shift, c) {
105+
if let Err(_) = draw(x as i32 + x_shift, y as i32 + y_shift, c) {
102106
panic!("fail")
103107
}
104108
});
105109
}));
106110
if let Err(_) = res {
107-
return Err(FontError::Unknown)
111+
return Err(FontError::Unknown);
108112
}
109-
x_shift += font.h_advance(font.glyph_id(c));
110-
} else {
111-
x_shift += font.h_advance(font.glyph_id(c));
112113
}
114+
x_shift += font.h_advance(font.glyph_id(c));
113115
}
114116
Ok(Ok(()))
115117
}

0 commit comments

Comments
 (0)