Struct cosmic_text::SwashCache
source · pub struct SwashCache<'a> {
pub image_cache: Map<CacheKey, Option<SwashImage>>,
pub outline_command_cache: Map<CacheKey, Option<Vec<Command>>>,
/* private fields */
}Expand description
Cache for rasterizing with the swash scaler
Fields§
§image_cache: Map<CacheKey, Option<SwashImage>>§outline_command_cache: Map<CacheKey, Option<Vec<Command>>>Implementations§
source§impl<'a> SwashCache<'a>
impl<'a> SwashCache<'a>
sourcepub fn new(font_system: &'a FontSystem) -> Self
pub fn new(font_system: &'a FontSystem) -> Self
Create a new swash cache
sourcepub fn get_image_uncached(&mut self, cache_key: CacheKey) -> Option<SwashImage>
pub fn get_image_uncached(&mut self, cache_key: CacheKey) -> Option<SwashImage>
Create a swash Image from a cache key, without caching results
sourcepub fn get_image(&mut self, cache_key: CacheKey) -> &Option<SwashImage>
pub fn get_image(&mut self, cache_key: CacheKey) -> &Option<SwashImage>
Create a swash Image from a cache key, caching results
Examples found in repository?
src/swash.rs (line 129)
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
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
cache_key: CacheKey,
base: Color,
mut f: F
) {
if let Some(image) = self.get_image(cache_key) {
let x = image.placement.left;
let y = -image.placement.top;
match image.content {
Content::Mask => {
let mut i = 0;
for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha?
f(
x + off_x,
y + off_y,
Color(
((image.data[i] as u32) << 24) |
base.0 & 0xFF_FF_FF
)
);
i += 1;
}
}
}
Content::Color => {
let mut i = 0;
for off_y in 0..image.placement.height as i32 {
for off_x in 0..image.placement.width as i32 {
//TODO: blend base alpha?
f(
x + off_x,
y + off_y,
Color::rgba(
image.data[i],
image.data[i + 1],
image.data[i + 2],
image.data[i + 3]
)
);
i += 4;
}
}
}
Content::SubpixelMask => {
log::warn!("TODO: SubpixelMask");
}
}
}
}pub fn get_outline_commands(&mut self, cache_key: CacheKey) -> Option<&[Command]>
sourcepub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
cache_key: CacheKey,
base: Color,
f: F
)
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
cache_key: CacheKey,
base: Color,
f: F
)
Enumerate pixels in an Image, use with_image for better performance
Examples found in repository?
src/buffer.rs (lines 548-550)
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
pub fn draw<F>(&self, cache: &mut crate::SwashCache, color: Color, mut f: F)
where F: FnMut(i32, i32, u32, u32, Color)
{
for run in self.layout_runs() {
for glyph in run.glyphs.iter() {
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
let glyph_color = match glyph.color_opt {
Some(some) => some,
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, run.line_y + y_int + y, 1, 1, color);
});
}
}
}More examples
src/edit/editor.rs (lines 672-674)
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
fn draw<F>(&self, cache: &mut crate::SwashCache, color: Color, mut f: F)
where F: FnMut(i32, i32, u32, u32, Color)
{
let font_size = self.buffer.metrics().font_size;
let line_height = self.buffer.metrics().line_height;
for run in self.buffer.layout_runs() {
let line_i = run.line_i;
let line_y = run.line_y;
let cursor_glyph_opt = |cursor: &Cursor| -> Option<(usize, f32)> {
if cursor.line == line_i {
for (glyph_i, glyph) in run.glyphs.iter().enumerate() {
if cursor.index == glyph.start {
return Some((glyph_i, 0.0));
} else if cursor.index > glyph.start && cursor.index < glyph.end {
// Guess x offset based on characters
let mut before = 0;
let mut total = 0;
let cluster = &run.text[glyph.start..glyph.end];
for (i, _) in cluster.grapheme_indices(true) {
if glyph.start + i < cursor.index {
before += 1;
}
total += 1;
}
let offset = glyph.w * (before as f32) / (total as f32);
return Some((glyph_i, offset));
}
}
match run.glyphs.last() {
Some(glyph) => {
if cursor.index == glyph.end {
return Some((run.glyphs.len(), 0.0));
}
},
None => {
return Some((0, 0.0));
}
}
}
None
};
// Highlight selection (TODO: HIGHLIGHT COLOR!)
if let Some(select) = self.select_opt {
let (start, end) = match select.line.cmp(&self.cursor.line) {
cmp::Ordering::Greater => (self.cursor, select),
cmp::Ordering::Less => (select, self.cursor),
cmp::Ordering::Equal => {
/* select.line == self.cursor.line */
if select.index < self.cursor.index {
(select, self.cursor)
} else {
/* select.index >= self.cursor.index */
(self.cursor, select)
}
}
};
if line_i >= start.line && line_i <= end.line {
let mut range_opt = None;
for glyph in run.glyphs.iter() {
// Guess x offset based on characters
let cluster = &run.text[glyph.start..glyph.end];
let total = cluster.grapheme_indices(true).count();
let mut c_x = glyph.x;
let c_w = glyph.w / total as f32;
for (i, c) in cluster.grapheme_indices(true) {
let c_start = glyph.start + i;
let c_end = glyph.start + i + c.len();
if (start.line != line_i || c_end > start.index)
&& (end.line != line_i || c_start < end.index) {
range_opt = match range_opt.take() {
Some((min, max)) => Some((
cmp::min(min, c_x as i32),
cmp::max(max, (c_x + c_w) as i32),
)),
None => Some((
c_x as i32,
(c_x + c_w) as i32,
))
};
} else if let Some((min, max)) = range_opt.take() {
f(
min,
line_y - font_size,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33)
);
}
c_x += c_w;
}
}
if run.glyphs.is_empty() && end.line > line_i{
// Highlight all of internal empty lines
range_opt = Some((0, self.buffer.size().0));
}
if let Some((mut min, mut max)) = range_opt.take() {
if end.line > line_i {
// Draw to end of line
if run.rtl {
min = 0;
} else {
max = self.buffer.size().0;
}
}
f(
min,
line_y - font_size,
cmp::max(0, max - min) as u32,
line_height as u32,
Color::rgba(color.r(), color.g(), color.b(), 0x33)
);
}
}
}
// Draw cursor
if let Some((cursor_glyph, cursor_glyph_offset)) = cursor_glyph_opt(&self.cursor) {
let x = match run.glyphs.get(cursor_glyph) {
Some(glyph) => {
// Start of detected glyph
if glyph.level.is_rtl() {
(glyph.x + glyph.w - cursor_glyph_offset) as i32
} else {
(glyph.x + cursor_glyph_offset) as i32
}
},
None => match run.glyphs.last() {
Some(glyph) => {
// End of last glyph
if glyph.level.is_rtl() {
glyph.x as i32
} else {
(glyph.x + glyph.w) as i32
}
},
None => {
// Start of empty line
0
}
}
};
f(
x,
line_y - font_size,
1,
line_height as u32,
color,
);
}
for glyph in run.glyphs.iter() {
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
let glyph_color = match glyph.color_opt {
Some(some) => some,
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, line_y + y_int + y, 1, 1, color);
});
}
}
}