1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
/*! A high-level, safe, zero-allocation TrueType font parser. ## Features - A high-level API, for people who doesn't know how TrueType works internally. Basically, no direct access to font tables. - Zero allocations. - Zero unsafe. - Zero required dependencies. Logging is enabled by default. - `no_std` compatible. - Fast. - Stateless. - Simple and maintainable code (no magic numbers). ## Supported TrueType features - (`cmap`) Character to glyph index mapping using [glyph_index()] method. <br/>All subtable formats except Mixed Coverage (8) are supported. - (`cmap`) Character variation to glyph index mapping using [glyph_variation_index()] method. - (`glyf`) Glyph outlining using [outline_glyph()] method. - (`hmtx`) Retrieving glyph's horizontal metrics using [glyph_hor_advance()] and [glyph_hor_side_bearing()] methods. - (`vmtx`) Retrieving glyph's vertical metrics using [glyph_ver_advance()] and [glyph_ver_side_bearing()] methods. - (`kern`) Retrieving glyphs pair kerning using [glyphs_kerning()] method. - (`maxp`) Retrieving total number of glyphs using [number_of_glyphs()] method. - (`name`) Listing all name records using [names()] method. - (`name`) Retrieving font's family name using [family_name()] method. - (`name`) Retrieving font's PostScript name using [post_script_name()] method. - (`post`) Retrieving font's underline metrics using [underline_metrics()] method. - (`post`) Retrieving glyph's name using [glyph_name()] method. - (`head`) Retrieving font's units per EM value using [units_per_em()] method. - (`hhea`) Retrieving generic font info using: [ascender()], [descender()], [height()] and [line_gap()] methods. [glyph_index()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_index [glyph_variation_index()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_variation_index [outline_glyph()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.outline_glyph [glyph_hor_advance()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_hor_advance [glyph_hor_side_bearing()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_hor_side_bearing [glyph_ver_advance()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_ver_advance [glyph_ver_side_bearing()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_ver_side_bearing [glyphs_kerning()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyphs_kerning [number_of_glyphs()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.number_of_glyphs [names()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.names [family_name()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.family_name [post_script_name()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.post_script_name [underline_metrics()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.underline_metrics [glyph_name()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_name [units_per_em()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.units_per_em [ascender()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.ascender [descender()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.descender [height()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.height [line_gap()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.line_gap ## Supported OpenType features - (`CFF `) Glyph outlining using [outline_glyph()] method. - (`OS/2`) Retrieving font's kind using [is_regular()], [is_italic()], [is_bold()] and [is_oblique()] methods. - (`OS/2`) Retrieving font's weight using [weight()] method. - (`OS/2`) Retrieving font's width using [width()] method. - (`OS/2`) Retrieving font's X height using [x_height()] method. - (`OS/2`) Retrieving font's strikeout metrics using [strikeout_metrics()] method. - (`OS/2`) Retrieving font's subscript metrics using [subscript_metrics()] method. - (`OS/2`) Retrieving font's superscript metrics using [superscript_metrics()] method. - (`GDEF`) Retrieving glyph's class using [glyph_class()] method. - (`GDEF`) Retrieving glyph's mark attachment class using [glyph_mark_attachment_class()] method. - (`GDEF`) Checking that glyph is a mark using [is_mark_glyph()] method. - (`VORG`) Retrieving glyph's vertical origin using [glyph_y_origin()] method. [is_regular()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.is_regular [is_italic()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.is_italic [is_bold()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.is_bold [is_oblique()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.is_oblique [weight()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.weight [width()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.width [x_height()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.x_height [strikeout_metrics()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.strikeout_metrics [subscript_metrics()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.subscript_metrics [superscript_metrics()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.superscript_metrics [glyph_class()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_class [glyph_mark_attachment_class()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_mark_attachment_class [is_mark_glyph()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.is_mark_glyph [glyph_y_origin()]: https://docs.rs/ttf-parser/0.4.0/ttf_parser/struct.Font.html#method.glyph_y_origin ## Error handling `ttf-parser` is designed to parse well-formed fonts, so it does not have an `Error` enum. It doesn't mean that it will crash or panic on malformed fonts, only that the error handling will boil down to `Option::None`. So you will not get a detailed cause of an error. By doing so we can simplify an API quite a lot since otherwise, we will have to use `Result<Option<T>, Error>`. Some methods may print warnings, when the `logging` feature is enabled. ## Methods' computational complexity TrueType fonts designed for fast querying, so most of the methods are very fast. The main exception is glyph outlining. Glyphs can be stored using two different methods: using [Glyph Data](https://docs.microsoft.com/en-us/typography/opentype/spec/glyf) format and [Compact Font Format](http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf) (pdf). The first one is fairly simple which makes it faster to process. The second one is basically a tiny language with a stack-based VM, which makes it way harder to process. ```text test outline_cff ... bench: 1,298,871 ns/iter (+/- 11,846) test outline_glyf ... bench: 837,958 ns/iter (+/- 6,261) ``` Here is some methods benchmarks: ```text test outline_glyph_276_from_cff ... bench: 1,041 ns/iter (+/- 71) test outline_glyph_276_from_glyf ... bench: 674 ns/iter (+/- 15) test from_data_otf_cff ... bench: 403 ns/iter (+/- 3) test outline_glyph_8_from_cff ... bench: 339 ns/iter (+/- 44) test outline_glyph_8_from_glyf ... bench: 295 ns/iter (+/- 16) test glyph_name_276 ... bench: 214 ns/iter (+/- 1) test from_data_ttf ... bench: 169 ns/iter (+/- 3) test family_name ... bench: 155 ns/iter (+/- 5) test glyph_index_u41 ... bench: 16 ns/iter (+/- 0) test glyph_name_8 ... bench: 1 ns/iter (+/- 0) test underline_metrics ... bench: 0.5 ns/iter (+/- 0) test units_per_em ... bench: 0.5 ns/iter (+/- 0) test strikeout_metrics ... bench: 0.5 ns/iter (+/- 0) test x_height ... bench: 0.4 ns/iter (+/- 0) test ascender ... bench: 0.2 ns/iter (+/- 0) test hor_advance ... bench: 0.2 ns/iter (+/- 0) test hor_side_bearing ... bench: 0.2 ns/iter (+/- 0) test subscript_metrics ... bench: 0.2 ns/iter (+/- 0) test width ... bench: 0.2 ns/iter (+/- 0) ``` `family_name` is expensive, because it allocates a `String` and the original data is stored as UTF-16 BE. `glyph_name_8` is faster that `glyph_name_276`, because for glyph indexes lower than 258 we are using predefined names, so no parsing is involved. ## Safety - The library must not panic. Any panic considered as a critical bug and should be reported. - The library forbids the unsafe code. */ #![doc(html_root_url = "https://docs.rs/ttf-parser/0.4.0")] #![no_std] #![forbid(unsafe_code)] #![warn(missing_docs)] #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] #[cfg(feature = "std")] #[macro_use] extern crate std; use core::fmt; use core::num::NonZeroU16; macro_rules! try_opt_or { ($value:expr, $ret:expr) => { match $value { Some(v) => v, None => return $ret, } }; } #[cfg(feature = "logging")] macro_rules! warn { ($($arg:tt)+) => ( log::log!(log::Level::Warn, $($arg)+); ) } #[cfg(not(feature = "logging"))] macro_rules! warn { ($($arg:tt)+) => () // do nothing } mod cff; mod cmap; mod gdef; mod ggg; mod glyf; mod head; mod hhea; mod hmtx; mod kern; mod loca; mod maxp; mod name; mod os2; mod parser; mod post; mod raw; mod vhea; mod vmtx; mod vorg; #[cfg(feature = "std")] mod writer; use parser::{Stream, SafeStream, Offset, FromData}; pub use gdef::GlyphClass; pub use ggg::*; pub use name::*; pub use os2::*; /// A type-safe wrapper for glyph ID. #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Debug)] pub struct GlyphId(pub u16); impl FromData for GlyphId { #[inline] fn parse(data: &[u8]) -> Self { let mut s = SafeStream::new(data); GlyphId(s.read()) } } impl Default for GlyphId { fn default() -> Self { GlyphId(0) } } /// A line metrics. /// /// Used for underline and strikeout. #[derive(Clone, Copy, PartialEq, Debug)] pub struct LineMetrics { /// Line position. pub position: i16, /// Line thickness. pub thickness: i16, } /// A rectangle. #[derive(Clone, Copy, PartialEq, Debug)] #[allow(missing_docs)] pub struct Rect { pub x_min: i16, pub y_min: i16, pub x_max: i16, pub y_max: i16, } /// A trait for glyph outline construction. pub trait OutlineBuilder { /// Appends a MoveTo segment. /// /// Start of a contour. fn move_to(&mut self, x: f32, y: f32); /// Appends a LineTo segment. fn line_to(&mut self, x: f32, y: f32); /// Appends a QuadTo segment. fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32); /// Appends a CurveTo segment. fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32); /// Appends a ClosePath segment. /// /// End of a contour. fn close(&mut self); } /// A table name. #[derive(Clone, Copy, PartialEq, Debug)] #[allow(missing_docs)] pub enum TableName { CharacterToGlyphIndexMapping, CompactFontFormat, GlyphData, GlyphDefinition, Header, HorizontalHeader, HorizontalMetrics, IndexToLocation, Kerning, MaximumProfile, Naming, PostScript, VerticalHeader, VerticalMetrics, VerticalOrigin, WindowsMetrics, } /// A font data handle. #[derive(Clone)] pub struct Font<'a> { cff_: Option<cff::Metadata<'a>>, cmap: Option<cmap::Table<'a>>, gdef: Option<gdef::Table<'a>>, glyf: Option<&'a [u8]>, head: raw::head::Table<'a>, hhea: raw::hhea::Table<'a>, hmtx: Option<hmtx::Table<'a>>, kern: Option<&'a [u8]>, loca: Option<loca::Table<'a>>, name: Option<name::Names<'a>>, os_2: Option<os2::Table<'a>>, post: Option<post::Table<'a>>, vhea: Option<raw::vhea::Table<'a>>, vmtx: Option<hmtx::Table<'a>>, vorg: Option<vorg::Table<'a>>, number_of_glyphs: NonZeroU16, } impl<'a> Font<'a> { /// Creates a `Font` object from a raw data. /// /// You can set `index` for font collections. /// For simple `ttf` fonts set `index` to 0. /// /// This method will do some parsing and sanitization, so it's a bit expensive. /// /// Required tables: `head`, `hhea` and `maxp`. /// /// If an optional table has an invalid data it will be skipped. pub fn from_data(data: &'a [u8], index: u32) -> Option<Self> { let table_data = if let Some(n) = fonts_in_collection(data) { if index < n { // https://docs.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header const OFFSET_32_SIZE: usize = 4; let offset = raw::TTCHeader::SIZE + OFFSET_32_SIZE * index as usize; let font_offset: u32 = Stream::read_at(data, offset)?; data.get(font_offset as usize .. data.len())? } else { return None; } } else { data }; // https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font const OFFSET_TABLE_SIZE: usize = 12; if data.len() < OFFSET_TABLE_SIZE { return None; } // https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font const SFNT_VERSION_TRUE_TYPE: u32 = 0x00010000; const SFNT_VERSION_OPEN_TYPE: u32 = 0x4F54544F; let mut s = Stream::new(table_data); let sfnt_version: u32 = s.read()?; if sfnt_version != SFNT_VERSION_TRUE_TYPE && sfnt_version != SFNT_VERSION_OPEN_TYPE { return None; } let num_tables: u16 = s.read()?; s.advance(6u32); // searchRange (u16) + entrySelector (u16) + rangeShift (u16) let tables = s.read_array::<raw::TableRecord, u16>(num_tables)?; let mut cff_ = None; let mut gdef = None; let mut os_2 = None; let mut vorg = None; let mut cmap = None; let mut glyf = None; let mut head = None; let mut hhea = None; let mut hmtx = None; let mut kern = None; let mut loca = None; let mut maxp = None; let mut name = None; let mut post = None; let mut vhea = None; let mut vmtx = None; for table in tables { let offset = table.offset().to_usize(); let length = table.length() as usize; let range = offset..(offset + length); // It's way faster to compare `[u8; 4]` with `&[u8]` // rather than `&[u8]` with `&[u8]`. match &table.table_tag() { b"CFF " => cff_ = data.get(range).and_then(|data| cff::parse_metadata(data)), b"GDEF" => gdef = data.get(range).and_then(|data| gdef::Table::parse(data)), b"OS/2" => os_2 = data.get(range).and_then(|data| os2::Table::parse(data)), b"VORG" => vorg = data.get(range).and_then(|data| vorg::Table::parse(data)), b"cmap" => cmap = data.get(range).and_then(|data| cmap::Table::parse(data)), b"glyf" => glyf = data.get(range), b"head" => head = data.get(range).and_then(|data| raw::head::Table::parse(data)), b"hhea" => hhea = data.get(range).and_then(|data| raw::hhea::Table::parse(data)), b"hmtx" => hmtx = data.get(range), b"kern" => kern = data.get(range), b"loca" => loca = data.get(range), b"maxp" => maxp = data.get(range).and_then(|data| maxp::parse(data)), b"name" => name = data.get(range).and_then(|data| name::parse(data)), b"post" => post = data.get(range).and_then(|data| post::Table::parse(data)), b"vhea" => vhea = data.get(range).and_then(|data| raw::vhea::Table::parse(data)), b"vmtx" => vmtx = data.get(range), _ => {} } } // Check for mandatory tables. let head = head?; let hhea = hhea?; let maxp = maxp?; let number_of_glyphs = maxp.number_of_glyphs; let mut font = Font { cff_, cmap, gdef, glyf, head, hhea, hmtx: None, kern, loca: None, name, os_2, post, vhea, vmtx: None, vorg, number_of_glyphs, }; if let Some(data) = hmtx { if let Some(number_of_h_metrics) = font.hhea.number_of_h_metrics() { font.hmtx = hmtx::Table::parse(data, number_of_h_metrics, font.number_of_glyphs); } } if let (Some(vhea), Some(data)) = (font.vhea, vmtx) { if let Some(number_of_v_metrics) = vhea.num_of_long_ver_metrics() { font.vmtx = hmtx::Table::parse(data, number_of_v_metrics, font.number_of_glyphs); } } if let Some(data) = loca { if let Some(format) = font.index_to_location_format() { font.loca = loca::Table::parse(data, font.number_of_glyphs, format); } } Some(font) } /// Checks that font has a specified table. /// /// Will return `true` only for tables that were successfully parsed. #[inline] pub fn has_table(&self, name: TableName) -> bool { match name { TableName::Header => true, TableName::HorizontalHeader => true, TableName::MaximumProfile => true, TableName::CharacterToGlyphIndexMapping => self.cmap.is_some(), TableName::CompactFontFormat => self.cff_.is_some(), TableName::GlyphData => self.glyf.is_some(), TableName::GlyphDefinition => self.gdef.is_some(), TableName::HorizontalMetrics => self.hmtx.is_some(), TableName::IndexToLocation => self.loca.is_some(), TableName::Kerning => self.kern.is_some(), TableName::Naming => self.name.is_some(), TableName::PostScript => self.post.is_some(), TableName::VerticalHeader => self.vhea.is_some(), TableName::VerticalMetrics => self.vmtx.is_some(), TableName::VerticalOrigin => self.vorg.is_some(), TableName::WindowsMetrics => self.os_2.is_some(), } } /// Returns a total number of glyphs in the font. /// /// Never zero. /// /// The value was already parsed, so this function doesn't involve any parsing. #[inline] pub fn number_of_glyphs(&self) -> u16 { self.number_of_glyphs.get() } /// Outlines a glyph and returns its tight bounding box. /// /// **Warning**: since `ttf-parser` is a pull parser, /// `OutlineBuilder` will emit segments even when outline is partially malformed. /// You must check `outline_glyph()` result for error before using /// `OutlineBuilder`'s output. /// /// This method supports `glyf`, `CFF` and `CFF2` tables. /// /// Returns `None` when glyph has no outline. /// /// # Example /// /// ``` /// use std::fmt::Write; /// use ttf_parser; /// /// struct Builder(String); /// /// impl ttf_parser::OutlineBuilder for Builder { /// fn move_to(&mut self, x: f32, y: f32) { /// write!(&mut self.0, "M {} {} ", x, y).unwrap(); /// } /// /// fn line_to(&mut self, x: f32, y: f32) { /// write!(&mut self.0, "L {} {} ", x, y).unwrap(); /// } /// /// fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { /// write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap(); /// } /// /// fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { /// write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap(); /// } /// /// fn close(&mut self) { /// write!(&mut self.0, "Z ").unwrap(); /// } /// } /// /// let data = std::fs::read("tests/fonts/glyphs.ttf").unwrap(); /// let font = ttf_parser::Font::from_data(&data, 0).unwrap(); /// let mut builder = Builder(String::new()); /// let bbox = font.outline_glyph(ttf_parser::GlyphId(0), &mut builder).unwrap(); /// assert_eq!(builder.0, "M 50 0 L 50 750 L 450 750 L 450 0 L 50 0 Z "); /// assert_eq!(bbox, ttf_parser::Rect { x_min: 50, y_min: 0, x_max: 450, y_max: 750 }); /// ``` #[inline] pub fn outline_glyph( &self, glyph_id: GlyphId, builder: &mut dyn OutlineBuilder, ) -> Option<Rect> { if self.glyf.is_some() { return self.glyf_glyph_outline(glyph_id, builder); } if let Some(ref metadata) = self.cff_ { return self.cff_glyph_outline(metadata, glyph_id, builder); } None } /// Returns a tight glyph bounding box. /// /// Note that this method's performance depends on a table type the current font is using. /// In case of a `glyf` table, it's basically free, since this table stores /// bounding box separately. In case of `CFF` and `CFF2`, we should actually outline /// a glyph and then calculate its bounding box. So if you need an outline and /// a bounding box and you have an OpenType font (which uses CFF/CFF2) /// then prefer `outline_glyph()` method. #[inline] pub fn glyph_bounding_box(&self, glyph_id: GlyphId) -> Option<Rect> { struct DummyOutline; impl OutlineBuilder for DummyOutline { fn move_to(&mut self, _: f32, _: f32) {} fn line_to(&mut self, _: f32, _: f32) {} fn quad_to(&mut self, _: f32, _: f32, _: f32, _: f32) {} fn curve_to(&mut self, _: f32, _: f32, _: f32, _: f32, _: f32, _: f32) {} fn close(&mut self) {} } if self.glyf.is_some() { return self.glyf_glyph_bbox(glyph_id); } if let Some(ref metadata) = self.cff_ { return self.cff_glyph_outline(metadata, glyph_id, &mut DummyOutline); } None } } impl fmt::Debug for Font<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "Font()") } } /// Parses the number of fonts stored in a TrueType font collection. /// /// Returns `None` if a provided data is not a TrueType font collection. #[inline] pub fn fonts_in_collection(data: &[u8]) -> Option<u32> { let table = raw::TTCHeader::new(data.get(0..raw::TTCHeader::SIZE)?); if &table.ttc_tag() != b"ttcf" { return None; } Some(table.num_fonts()) } #[cfg(test)] mod tests { use super::*; use crate::writer; use writer::TtfType::*; #[test] fn empty_font() { assert!(Font::from_data(&[], 0).is_none()); } #[test] fn incomplete_header() { let data = writer::convert(&[ TrueTypeMagic, UInt16(0), // numTables UInt16(0), // searchRange UInt16(0), // entrySelector UInt16(0), // rangeShift ]); for i in 0..data.len() { assert!(Font::from_data(&data[0..i], 0).is_none()); } } #[test] fn zero_tables() { let data = writer::convert(&[ TrueTypeMagic, UInt16(0), // numTables UInt16(0), // searchRange UInt16(0), // entrySelector UInt16(0), // rangeShift ]); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn tables_count_overflow() { let data = writer::convert(&[ TrueTypeMagic, UInt16(std::u16::MAX), // numTables UInt16(0), // searchRange UInt16(0), // entrySelector UInt16(0), // rangeShift ]); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn open_type_magic() { let data = writer::convert(&[ OpenTypeMagic, UInt16(0), // numTables UInt16(0), // searchRange UInt16(0), // entrySelector UInt16(0), // rangeShift ]); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn unknown_magic() { let data = writer::convert(&[ Raw(&[0xFF, 0xFF, 0xFF, 0xFF]), UInt16(0), // numTables UInt16(0), // searchRange UInt16(0), // entrySelector UInt16(0), // rangeShift ]); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn empty_font_collection() { let data = writer::convert(&[ FontCollectionMagic, UInt16(1), // majorVersion UInt16(0), // minorVersion UInt32(0), // numFonts ]); assert_eq!(fonts_in_collection(&data), Some(0)); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn font_collection_num_fonts_overflow() { let data = writer::convert(&[ FontCollectionMagic, UInt16(1), // majorVersion UInt16(0), // minorVersion UInt32(std::u32::MAX), // numFonts ]); assert_eq!(fonts_in_collection(&data), Some(std::u32::MAX)); assert!(Font::from_data(&data, 0).is_none()); } #[test] fn font_index_overflow_1() { let data = writer::convert(&[ FontCollectionMagic, UInt16(1), // majorVersion UInt16(0), // minorVersion UInt32(1), // numFonts ]); assert_eq!(fonts_in_collection(&data), Some(1)); assert!(Font::from_data(&data, std::u32::MAX).is_none()); } #[test] fn font_index_overflow_2() { let data = writer::convert(&[ FontCollectionMagic, UInt16(1), // majorVersion UInt16(0), // minorVersion UInt32(std::u32::MAX), // numFonts ]); assert_eq!(fonts_in_collection(&data), Some(std::u32::MAX)); assert!(Font::from_data(&data, std::u32::MAX - 1).is_none()); } }