From 0012f09eebfda65625ff0f755f61444a8b8e72a1 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 20 Jun 2019 21:43:57 -0700 Subject: [PATCH] Fix examples --- Cargo.lock | 41 ++++------- c/include/pathfinder/pathfinder.h | 2 +- c/src/lib.rs | 4 +- canvas/Cargo.toml | 4 +- canvas/src/lib.rs | 72 ++++++++++++++++---- examples/c_canvas_minimal/c_canvas_minimal.c | 2 +- examples/canvas_glutin_minimal/src/main.rs | 3 +- examples/canvas_metal_minimal/src/main.rs | 3 +- examples/canvas_minimal/src/main.rs | 3 +- examples/canvas_moire/src/main.rs | 2 +- examples/canvas_text/Cargo.toml | 1 + examples/canvas_text/src/main.rs | 18 +++-- text/Cargo.toml | 4 +- 13 files changed, 102 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb9935bd..3ce77aac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,6 +210,7 @@ dependencies = [ name = "canvas_text" version = "0.1.0" dependencies = [ + "font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "gl 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "pathfinder_canvas 0.1.0", "pathfinder_geometry 0.3.0", @@ -495,13 +496,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "dwrote" -version = "0.5.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -605,23 +604,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "font-kit" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "cocoa 0.18.4 (registry+https://github.com/rust-lang/crates.io-index)", "core-foundation 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "core-graphics 0.17.3 (registry+https://github.com/rust-lang/crates.io-index)", "core-text 13.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", - "dwrote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "dwrote 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "euclid 0.19.8 (registry+https://github.com/rust-lang/crates.io-index)", - "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "float-ord 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "freetype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -907,14 +901,6 @@ dependencies = [ "adler32 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "itertools" -version = "0.7.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "itoa" version = "0.4.4" @@ -1357,11 +1343,11 @@ dependencies = [ name = "pathfinder_canvas" version = "0.1.0" dependencies = [ - "font-kit 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pathfinder_geometry 0.3.0", "pathfinder_renderer 0.1.0", "pathfinder_text 0.1.0", - "skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a89e9ca99e0d6736ea1b7754517f4df14fd96a2b)", + "skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a2d683856ba1f2d0095b12dd7823d1602a87614e)", ] [[package]] @@ -1509,11 +1495,11 @@ name = "pathfinder_text" version = "0.1.0" dependencies = [ "euclid 0.19.8 (registry+https://github.com/rust-lang/crates.io-index)", - "font-kit 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lyon_path 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "pathfinder_geometry 0.3.0", "pathfinder_renderer 0.1.0", - "skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a89e9ca99e0d6736ea1b7754517f4df14fd96a2b)", + "skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a2d683856ba1f2d0095b12dd7823d1602a87614e)", ] [[package]] @@ -1961,10 +1947,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "skribo" version = "0.0.1" -source = "git+https://github.com/linebender/skribo.git?rev=a89e9ca99e0d6736ea1b7754517f4df14fd96a2b#a89e9ca99e0d6736ea1b7754517f4df14fd96a2b" +source = "git+https://github.com/linebender/skribo.git?rev=a2d683856ba1f2d0095b12dd7823d1602a87614e#a2d683856ba1f2d0095b12dd7823d1602a87614e" dependencies = [ "euclid 0.19.8 (registry+https://github.com/rust-lang/crates.io-index)", - "font-kit 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "harfbuzz 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "harfbuzz-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2439,7 +2425,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901" "checksum dlib 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77e51249a9d823a4cb79e3eca6dcd756153e8ed0157b6c04775d04bf1b13b76a" "checksum downcast-rs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f2b92dfd5c2f75260cbf750572f95d387e7ca0ba5e3fbe9e1a33f23025be020f" -"checksum dwrote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "30a998e9ff70cd208ccdc4f864e998688bf61d7b897dccec8e17a884d17358bf" +"checksum dwrote 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0bd1369e02db5e9b842a9b67bce8a2fcc043beafb2ae8a799dd482d46ea1ff0d" "checksum egl 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a373bc9844200b1ff15bd1b245931d1c20d09d06e4ec09f361171f29a4b0752d" "checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b" "checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a" @@ -2452,7 +2438,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33" "checksum float-cmp 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "17ae3a6394183450225f33d9419cbd627b3cfc831e14f3f1146d5bcaf984e00c" "checksum float-ord 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7bad48618fdb549078c333a7a8528acb57af271d0433bdecd523eb620628364e" -"checksum font-kit 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5be0133b580e2920076afd5bd4b81dc172d10e5653bd1c516476718d52347859" +"checksum font-kit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c5b784f7a48d718df8d9ee28f81f8afa0a7ffdd61038957cba1ec6837a836d46" "checksum foreign-types 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" "checksum foreign-types-shared 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" "checksum freetype 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "11926b2b410b469d0e9399eca4cbbe237a9ef02176c485803b29216307e8e028" @@ -2480,7 +2466,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum image 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ebdff791af04e30089bde8ad2a632b86af433b40c04db8d70ad4b21487db7a6a" "checksum image 0.21.1 (registry+https://github.com/rust-lang/crates.io-index)" = "293e54ce142a936a39da748ba8178ae6aa1914b82d846a4278f11590c89bf116" "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" -"checksum itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0d47946d458e94a1b7bcabbf6521ea7c037062c81f534615abcad76e84d4970d" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum jemalloc-sys 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "bfc62c8e50e381768ce8ee0428ee53741929f7ebd73e4d83f669bcf7693e00ae" "checksum jemallocator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9f0cd42ac65f758063fea55126b0148b1ce0a6354ff78e07a4d6806bc65c4ab3" @@ -2582,7 +2567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" "checksum simplecss 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "135685097a85a64067df36e28a243e94a94f76d829087ce0be34eeb014260c0e" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" -"checksum skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a89e9ca99e0d6736ea1b7754517f4df14fd96a2b)" = "" +"checksum skribo 0.0.1 (git+https://github.com/linebender/skribo.git?rev=a2d683856ba1f2d0095b12dd7823d1602a87614e)" = "" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum smithay-client-toolkit 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aa4899558362a65589b53313935099835acf999740915e134dff20cca7c6a28b" diff --git a/c/include/pathfinder/pathfinder.h b/c/include/pathfinder/pathfinder.h index 3ab80705..7ded8d08 100644 --- a/c/include/pathfinder/pathfinder.h +++ b/c/include/pathfinder/pathfinder.h @@ -129,7 +129,7 @@ typedef struct PFSceneProxy *PFSceneProxyRef; PFCanvasRef PFCanvasCreate(PFCanvasFontContextRef font_context, const PFVector2F *size); void PFCanvasDestroy(PFCanvasRef canvas); -PFCanvasFontContextRef PFCanvasFontContextCreate(); +PFCanvasFontContextRef PFCanvasFontContextCreateWithSystemSource(); void PFCanvasFontContextDestroy(PFCanvasFontContextRef font_context); PFCanvasFontContextRef PFCanvasFontContextClone(PFCanvasFontContextRef font_context); PFSceneRef PFCanvasCreateScene(PFCanvasRef canvas); diff --git a/c/src/lib.rs b/c/src/lib.rs index b70f62c9..219e43bd 100644 --- a/c/src/lib.rs +++ b/c/src/lib.rs @@ -148,8 +148,8 @@ pub unsafe extern "C" fn PFCanvasDestroy(canvas: PFCanvasRef) { } #[no_mangle] -pub unsafe extern "C" fn PFCanvasFontContextCreate() -> PFCanvasFontContextRef { - Box::into_raw(Box::new(CanvasFontContext::new())) +pub unsafe extern "C" fn PFCanvasFontContextCreateWithSystemSource() -> PFCanvasFontContextRef { + Box::into_raw(Box::new(CanvasFontContext::from_system_source())) } #[no_mangle] diff --git a/canvas/Cargo.toml b/canvas/Cargo.toml index d93ac0cf..1c620859 100644 --- a/canvas/Cargo.toml +++ b/canvas/Cargo.toml @@ -8,7 +8,7 @@ edition = "2018" crate-type = ["rlib", "staticlib"] [dependencies] -font-kit = "0.1" +font-kit = "0.2" [dependencies.pathfinder_geometry] path = "../geometry" @@ -21,4 +21,4 @@ path = "../text" [dependencies.skribo] git = "https://github.com/linebender/skribo.git" -rev = "a89e9ca99e0d6736ea1b7754517f4df14fd96a2b" +rev = "a2d683856ba1f2d0095b12dd7823d1602a87614e" diff --git a/canvas/src/lib.rs b/canvas/src/lib.rs index b31fa1c0..9f710dd0 100644 --- a/canvas/src/lib.rs +++ b/canvas/src/lib.rs @@ -11,9 +11,12 @@ //! A simple API for Pathfinder that mirrors a subset of HTML canvas. use font_kit::family_name::FamilyName; +use font_kit::handle::Handle; use font_kit::hinting::HintingOptions; +use font_kit::loaders::default::Font; use font_kit::properties::Properties; -use font_kit::source::SystemSource; +use font_kit::source::{Source, SystemSource}; +use font_kit::sources::mem::MemSource; use pathfinder_geometry::basic::line_segment::LineSegment2F; use pathfinder_geometry::basic::vector::Vector2F; use pathfinder_geometry::basic::rect::RectF; @@ -29,6 +32,7 @@ use pathfinder_text::{SceneExt, TextRenderMode}; use skribo::{FontCollection, FontFamily, Layout, TextStyle}; use std::default::Default; use std::f32::consts::PI; +use std::iter; use std::mem; use std::sync::Arc; @@ -170,6 +174,40 @@ impl CanvasRenderingContext2D { // Text styles + #[inline] + pub fn set_font_collection(&mut self, font_collection: Arc) { + self.current_state.font_collection = font_collection; + } + + #[inline] + pub fn set_font_families(&mut self, font_families: I) where I: Iterator { + let mut font_collection = FontCollection::new(); + for font_family in font_families { + font_collection.add_family(font_family); + } + self.current_state.font_collection = Arc::new(font_collection); + } + + /// A convenience method to set a single font family. + #[inline] + pub fn set_font_family(&mut self, font_family: FontFamily) { + self.set_font_families(iter::once(font_family)) + } + + /// A convenience method to set a single font family consisting of a single font. + #[inline] + pub fn set_font(&mut self, font: Font) { + self.set_font_family(FontFamily::new_from_font(font)) + } + + /// A convenience method to set a single font family consisting of a font + /// described by a PostScript name. + #[inline] + pub fn set_font_by_postscript_name(&mut self, postscript_name: &str) { + let font = self.font_context.font_source.select_by_postscript_name(postscript_name); + self.set_font(font.expect("Didn't find the font!").load().unwrap()); + } + #[inline] pub fn set_font_size(&mut self, new_font_size: f32) { self.current_state.font_size = new_font_size; @@ -509,29 +547,37 @@ pub struct TextMetrics { #[derive(Clone)] pub struct CanvasFontContext { #[allow(dead_code)] - font_source: Arc, + font_source: Arc, #[allow(dead_code)] default_font_collection: Arc, } impl CanvasFontContext { - pub fn new() -> CanvasFontContext { - let font_source = Arc::new(SystemSource::new()); - + pub fn new(font_source: Arc) -> CanvasFontContext { let mut default_font_collection = FontCollection::new(); - let default_font = - font_source.select_best_match(&[FamilyName::SansSerif], &Properties::new()) - .expect("Failed to select the default font!") - .load() - .expect("Failed to load the default font!"); - default_font_collection.add_family(FontFamily::new_from_font(default_font)); - let default_font_collection = Arc::new(default_font_collection); + if let Ok(default_font) = font_source.select_best_match(&[FamilyName::SansSerif], + &Properties::new()) { + if let Ok(default_font) = default_font.load() { + default_font_collection.add_family(FontFamily::new_from_font(default_font)); + } + } CanvasFontContext { font_source, - default_font_collection, + default_font_collection: Arc::new(default_font_collection), } } + + /// A convenience method to create a font context with the system source. + /// This allows usage of fonts installed on the system. + pub fn from_system_source() -> CanvasFontContext { + CanvasFontContext::new(Arc::new(SystemSource::new())) + } + + /// A convenience method to create a font context with a set of in-memory fonts. + pub fn from_fonts(fonts: I) -> CanvasFontContext where I: Iterator { + CanvasFontContext::new(Arc::new(MemSource::from_fonts(fonts).unwrap())) + } } // Text layout utilities diff --git a/examples/c_canvas_minimal/c_canvas_minimal.c b/examples/c_canvas_minimal/c_canvas_minimal.c index 9721ee3f..bcfa09b9 100644 --- a/examples/c_canvas_minimal/c_canvas_minimal.c +++ b/examples/c_canvas_minimal/c_canvas_minimal.c @@ -64,7 +64,7 @@ int main(int argc, const char **argv) { }); // Make a canvas. We're going to draw a house. - PFCanvasRef canvas = PFCanvasCreate(PFCanvasFontContextCreate(), + PFCanvasRef canvas = PFCanvasCreate(PFCanvasFontContextCreateWithSystemSource(), &(PFVector2F){640.0f, 480.0f}); // Set line width. diff --git a/examples/canvas_glutin_minimal/src/main.rs b/examples/canvas_glutin_minimal/src/main.rs index b73a0fdb..9b8662c0 100644 --- a/examples/canvas_glutin_minimal/src/main.rs +++ b/examples/canvas_glutin_minimal/src/main.rs @@ -54,7 +54,8 @@ fn main() { RendererOptions { background_color: Some(ColorF::white()) }); // Make a canvas. We're going to draw a house. - let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::new(), window_size.to_f32()); + let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::from_system_source(), + window_size.to_f32()); // Set line width. canvas.set_line_width(10.0); diff --git a/examples/canvas_metal_minimal/src/main.rs b/examples/canvas_metal_minimal/src/main.rs index 043b9d5d..8d8417ad 100644 --- a/examples/canvas_metal_minimal/src/main.rs +++ b/examples/canvas_metal_minimal/src/main.rs @@ -52,7 +52,8 @@ fn main() { RendererOptions { background_color: Some(ColorF::white()) }); // Make a canvas. We're going to draw a house. - let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::new(), window_size.to_f32()); + let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::from_system_source(), + window_size.to_f32()); // Set line width. canvas.set_line_width(10.0); diff --git a/examples/canvas_minimal/src/main.rs b/examples/canvas_minimal/src/main.rs index 06fc1a7e..230640c3 100644 --- a/examples/canvas_minimal/src/main.rs +++ b/examples/canvas_minimal/src/main.rs @@ -52,7 +52,8 @@ fn main() { RendererOptions { background_color: Some(ColorF::white()) }); // Make a canvas. We're going to draw a house. - let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::new(), window_size.to_f32()); + let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::from_system_source(), + window_size.to_f32()); // Set line width. canvas.set_line_width(10.0); diff --git a/examples/canvas_moire/src/main.rs b/examples/canvas_moire/src/main.rs index 0ac8fffd..1f579b1e 100644 --- a/examples/canvas_moire/src/main.rs +++ b/examples/canvas_moire/src/main.rs @@ -100,7 +100,7 @@ impl MoireRenderer { -> MoireRenderer { MoireRenderer { renderer, - font_context: CanvasFontContext::new(), + font_context: CanvasFontContext::from_system_source(), scene: SceneProxy::new(RayonExecutor), frame: 0, window_size, diff --git a/examples/canvas_text/Cargo.toml b/examples/canvas_text/Cargo.toml index 174d4c96..720a0863 100644 --- a/examples/canvas_text/Cargo.toml +++ b/examples/canvas_text/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Patrick Walton "] edition = "2018" [dependencies] +font-kit = "0.2" gl = "0.6" sdl2 = "0.32" sdl2-sys = "0.32" diff --git a/examples/canvas_text/src/main.rs b/examples/canvas_text/src/main.rs index de69fbe4..7d4b8385 100644 --- a/examples/canvas_text/src/main.rs +++ b/examples/canvas_text/src/main.rs @@ -8,11 +8,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use font_kit::handle::Handle; use pathfinder_canvas::{CanvasFontContext, CanvasRenderingContext2D, TextAlign}; use pathfinder_geometry::basic::vector::{Vector2F, Vector2I}; use pathfinder_geometry::color::ColorF; use pathfinder_gl::{GLDevice, GLVersion}; -use pathfinder_gpu::resources::FilesystemResourceLoader; +use pathfinder_gpu::resources::{FilesystemResourceLoader, ResourceLoader}; use pathfinder_renderer::concurrent::rayon::RayonExecutor; use pathfinder_renderer::concurrent::scene_proxy::SceneProxy; use pathfinder_renderer::gpu::options::{DestFramebuffer, RendererOptions}; @@ -21,6 +22,8 @@ use pathfinder_renderer::options::BuildOptions; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::video::GLProfile; +use std::iter; +use std::sync::Arc; fn main() { // Set up SDL2. @@ -45,15 +48,22 @@ fn main() { window.gl_make_current(&gl_context).unwrap(); // Create a Pathfinder renderer. + let resource_loader = FilesystemResourceLoader::locate(); let mut renderer = Renderer::new(GLDevice::new(GLVersion::GL3, 0), - &FilesystemResourceLoader::locate(), + &resource_loader, DestFramebuffer::full_window(window_size), RendererOptions { background_color: Some(ColorF::white()) }); - // Make a canvas. We're going to draw some text. - let mut canvas = CanvasRenderingContext2D::new(CanvasFontContext::new(), window_size.to_f32()); + // Load a font. + let font_data = Arc::new(resource_loader.slurp("fonts/overpass-regular.otf").unwrap()); + let font = Handle::from_memory(font_data, 0); + let font_context = CanvasFontContext::from_fonts(iter::once(font)); + + // Make a canvas. + let mut canvas = CanvasRenderingContext2D::new(font_context, window_size.to_f32()); // Draw the text. + canvas.set_font_by_postscript_name("Overpass-Regular"); canvas.set_font_size(32.0); canvas.fill_text("Hello Pathfinder!", Vector2F::new(32.0, 48.0)); canvas.set_text_align(TextAlign::Right); diff --git a/text/Cargo.toml b/text/Cargo.toml index 352e801f..76985371 100644 --- a/text/Cargo.toml +++ b/text/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] euclid = "0.19" -font-kit = "0.1" +font-kit = "0.2" lyon_path = "0.12" [dependencies.pathfinder_geometry] @@ -17,4 +17,4 @@ path = "../renderer" [dependencies.skribo] git = "https://github.com/linebender/skribo.git" -rev = "a89e9ca99e0d6736ea1b7754517f4df14fd96a2b" +rev = "a2d683856ba1f2d0095b12dd7823d1602a87614e"