Skip to content

Commit

Permalink
Merge pull request #201 from kas-gui/work
Browse files Browse the repository at this point in the history
Fontdue rasterizer and other font adjustments
  • Loading branch information
dhardy authored Jun 11, 2021
2 parents e0c9277 + 1284112 commit 8fd412d
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 82 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
run: cargo test --manifest-path kas-theme/Cargo.toml --all-features
- name: test (kas-wgpu)
run: |
cargo test --manifest-path kas-wgpu/Cargo.toml --no-default-features
cargo test --manifest-path kas-wgpu/Cargo.toml --no-default-features --features ab_glyph
cargo test --manifest-path kas-wgpu/Cargo.toml --all-features
test:
Expand Down Expand Up @@ -74,4 +74,4 @@ jobs:
- name: test (kas-theme)
run: cargo test --manifest-path kas-theme/Cargo.toml
- name: test (kas-wgpu)
run: cargo test --manifest-path kas-wgpu/Cargo.toml --features clipboard
run: cargo test --manifest-path kas-wgpu/Cargo.toml
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ features = ["serde"]
members = ["kas-macros", "kas-theme", "kas-wgpu"]

[patch.crates-io]
kas-text = { git = "https://github.com/kas-gui/kas-text.git", rev = "05b50366a0a2fe7d1e611d93acbb2d9f83c52f0a" }
kas-text = { git = "https://github.com/kas-gui/kas-text.git", rev = "8e85e49df78f7908662d1356cacdf0b796274530" }
4 changes: 4 additions & 0 deletions example-config/theme.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ fonts:
families:
- sans-serif
weight: 600
raster:
mode: 0
subpixel_threshold: 0
subpixel_steps: 1
75 changes: 75 additions & 0 deletions kas-theme/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct Config {
/// Standard fonts
#[cfg_attr(feature = "config", serde(default))]
fonts: BTreeMap<TextClass, FontSelector<'static>>,

/// Text glyph rastering settings
#[cfg_attr(feature = "config", serde(default))]
raster: RasterConfig,
}

impl Default for Config {
Expand All @@ -50,6 +54,61 @@ impl Default for Config {
color_schemes: defaults::color_schemes(),
font_aliases: Default::default(),
fonts: defaults::fonts(),
raster: Default::default(),
}
}
}

/// Font raster settings
///
/// These are not used by the theme, but passed through to the rendering
/// backend.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "config", derive(serde::Serialize, serde::Deserialize))]
pub struct RasterConfig {
//// Raster mode/engine (backend dependent)
#[cfg_attr(feature = "config", serde(default))]
pub mode: u8,
/// Scale multiplier for fixed-precision
///
/// This should be an integer `n >= 1`, e.g. `n = 4` provides four sub-pixel
/// steps of precision. It is also required that `n * h < (1 << 24)` where
/// `h` is the text height in pixels.
#[cfg_attr(feature = "config", serde(default = "defaults::scale_steps"))]
pub scale_steps: u8,
/// Subpixel positioning threshold
///
/// Text with height `h` less than this threshold will use sub-pixel
/// positioning, which should make letter spacing more accurate for small
/// fonts (though exact behaviour depends on the font; it may be worse).
/// This may make rendering worse by breaking pixel alignment.
///
/// Note: this feature may not be available, depending on the backend and
/// the mode.
///
/// See also sub-pixel positioning steps.
#[cfg_attr(feature = "config", serde(default = "defaults::subpixel_threshold"))]
pub subpixel_threshold: u8,
/// Subpixel steps
///
/// The number of sub-pixel positioning steps to use. 1 is the minimum and
/// equivalent to no sub-pixel positioning. 16 is the maximum.
///
/// Note that since this applies to horizontal and vertical positioning, the
/// maximum number of rastered glyphs is multiplied by the square of this
/// value, though this maxmimum may not be reached in practice. Since this
/// feature is usually only used for small fonts this likely acceptable.
#[cfg_attr(feature = "config", serde(default = "defaults::subpixel_steps"))]
pub subpixel_steps: u8,
}

impl Default for RasterConfig {
fn default() -> Self {
RasterConfig {
mode: 0,
scale_steps: defaults::scale_steps(),
subpixel_threshold: defaults::subpixel_threshold(),
subpixel_steps: defaults::subpixel_steps(),
}
}
}
Expand Down Expand Up @@ -152,6 +211,12 @@ impl ThemeConfig for Config {
});
}
}

/// Get raster config
#[inline]
fn raster(&self) -> &RasterConfig {
&self.raster
}
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -185,4 +250,14 @@ mod defaults {
];
list.iter().cloned().collect()
}

pub fn scale_steps() -> u8 {
4
}
pub fn subpixel_threshold() -> u8 {
0
}
pub fn subpixel_steps() -> u8 {
5
}
}
4 changes: 2 additions & 2 deletions kas-theme/src/flat_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ where
}

fn init(&mut self, _draw: &mut D) {
if let Err(e) = kas::text::fonts::fonts().select_default() {
let fonts = fonts::fonts();
if let Err(e) = fonts.select_default() {
panic!("Error loading font: {}", e);
}
let fonts = fonts::fonts();
self.fonts = Some(Rc::new(
self.config
.iter_fonts()
Expand Down
2 changes: 1 addition & 1 deletion kas-theme/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod traits;
pub use kas;

pub use colors::{Colors, ColorsLinear, ColorsSrgb};
pub use config::Config;
pub use config::{Config, RasterConfig};
pub use dim::{Dimensions, DimensionsParams, DimensionsWindow};
pub use flat_theme::FlatTheme;
#[cfg(feature = "stack_dst")]
Expand Down
6 changes: 6 additions & 0 deletions kas-theme/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use std::ops::{Deref, DerefMut};
pub trait ThemeConfig: Clone + std::fmt::Debug + 'static {
/// Apply startup effects
fn apply_startup(&self);

/// Get raster config
fn raster(&self) -> &crate::RasterConfig;
}

/// Requirements on theme config (with `config` feature)
Expand All @@ -27,6 +30,9 @@ pub trait ThemeConfig:

/// Apply startup effects
fn apply_startup(&self);

/// Get raster config
fn raster(&self) -> &crate::RasterConfig;
}

/// A *theme* provides widget sizing and drawing implementations.
Expand Down
9 changes: 7 additions & 2 deletions kas-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ no-default-features = true
features = ["stack_dst"]

[features]
default = ["clipboard", "stack_dst"]
default = ["ab_glyph", "clipboard", "stack_dst"]
nightly = ["unsize", "kas/nightly", "kas-theme/nightly"]

# Use Generic Associated Types (this is too unstable to include in nightly!)
Expand All @@ -33,6 +33,9 @@ stack_dst = ["kas-theme/stack_dst"]
# Use kas-theme's unsize feature (nightly-only)
unsize = ["kas-theme/unsize"]

# Also:
# ab_glyph, fontdue: enable at least one of these for text rasterisation

[dependencies]
kas = { path = "..", version = "0.7.0", features = ["config", "winit"] }
kas-theme = { path = "../kas-theme", features = ["config"], version = "0.7.0" }
Expand All @@ -41,12 +44,14 @@ futures = "0.3"
log = "0.4"
smallvec = "1.6.1"
wgpu = "0.8.0"
ab_glyph = "0.2.10"
ab_glyph = { version = "0.2.10", optional = true }
winit = "0.25"
thiserror = "1.0.23"
window_clipboard = { version = "0.2.0", optional = true }
image = "0.23.14"
guillotiere = "0.6.0"
fontdue = { version = "0.5.2", optional = true }
cfg-if = "1.0.0"

[dev-dependencies]
chrono = "0.4"
Expand Down
3 changes: 3 additions & 0 deletions kas-wgpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ Optional features
This crate has the following feature flags:

- `clipboard` (enabled by default): clipboard integration
- `fontdue`: use [fontdue] library for font rasterisation (otherwise, `ab_glyph` is used)
- `stack_dst` (enabled by default): enables `kas-theme::MultiTheme`
- `gat`: enables usage of the Generic Associated Types feature (nightly only
and currently unstable), allowing some usages of `unsafe` to be avoided.
(The plan is to enable this by default once the feature is mature.)
- `unsize`: forwards this feature flag to `kas-theme`

[fontdue]: https://github.com/mooman219/fontdue

Copyright and Licence
-------

Expand Down
3 changes: 2 additions & 1 deletion kas-wgpu/src/draw/draw_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl<C: CustomPipe> DrawPipe<C> {
mut custom: CB,
device: &wgpu::Device,
shaders: &ShaderManager,
raster_config: &kas_theme::RasterConfig,
) -> Self {
// Create staging belt and a local pool
let staging_belt = wgpu::util::StagingBelt::new(1024);
Expand Down Expand Up @@ -60,7 +61,7 @@ impl<C: CustomPipe> DrawPipe<C> {
let shaded_round = shaded_round::Pipeline::new(device, shaders, &bgl_common);
let flat_round = flat_round::Pipeline::new(device, shaders, &bgl_common);
let custom = custom.build(&device, &bgl_common, RENDER_TEX_FORMAT);
let text = text_pipe::Pipeline::new(device, shaders, &bgl_common);
let text = text_pipe::Pipeline::new(device, shaders, &bgl_common, raster_config);

DrawPipe {
local_pool,
Expand Down
Loading

0 comments on commit 8fd412d

Please sign in to comment.