Render fixes
This commit is contained in:
parent
5cf2a6ca3b
commit
8aeac2b819
@ -4,7 +4,7 @@ use pangocairo::FontMap;
|
|||||||
use pango::prelude::*;
|
use pango::prelude::*;
|
||||||
use pango;
|
use pango;
|
||||||
|
|
||||||
use sys::pango::*;
|
use sys::pango as sys_pango;
|
||||||
|
|
||||||
use ui_model::StyledLine;
|
use ui_model::StyledLine;
|
||||||
|
|
||||||
@ -21,8 +21,8 @@ impl Context {
|
|||||||
self.pango_context = create_pango_context(font_desc);
|
self.pango_context = create_pango_context(font_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn itemize(&self, line: &StyledLine) -> Vec<item::Item> {
|
pub fn itemize(&self, line: &StyledLine) -> Vec<sys_pango::Item> {
|
||||||
pango_itemize(&self.pango_context, &line.line_str, &line.attr_list)
|
sys_pango::pango_itemize(&self.pango_context, &line.line_str, &line.attr_list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,15 +2,15 @@ mod context;
|
|||||||
|
|
||||||
use sys::pango::*;
|
use sys::pango::*;
|
||||||
use pango;
|
use pango;
|
||||||
use pango::prelude::*;
|
|
||||||
use cairo;
|
use cairo;
|
||||||
use pangocairo::{CairoContextExt, FontMap};
|
use pangocairo::CairoContextExt;
|
||||||
use std::ffi::CString;
|
|
||||||
use ui_model;
|
use ui_model;
|
||||||
|
|
||||||
pub fn render(
|
pub fn render(
|
||||||
ctx: &cairo::Context,
|
ctx: &cairo::Context,
|
||||||
font_desc: pango::FontDescription,
|
font_desc: pango::FontDescription,
|
||||||
|
line_height: f64,
|
||||||
|
char_width: f64,
|
||||||
ui_model: &mut ui_model::UiModel,
|
ui_model: &mut ui_model::UiModel,
|
||||||
) {
|
) {
|
||||||
let font_ctx = context::Context::new(&font_desc);
|
let font_ctx = context::Context::new(&font_desc);
|
||||||
@ -18,17 +18,19 @@ pub fn render(
|
|||||||
shape_dirty(&font_ctx, ui_model);
|
shape_dirty(&font_ctx, ui_model);
|
||||||
|
|
||||||
|
|
||||||
|
let mut line_y = line_height;
|
||||||
|
|
||||||
for line in ui_model.model_mut() {
|
for line in ui_model.model_mut() {
|
||||||
|
ctx.move_to(0.0, line_y);
|
||||||
for i in 0..line.line.len() {
|
for i in 0..line.line.len() {
|
||||||
let item = line.item_line[i].as_ref();
|
let item = line.item_line[i].as_ref();
|
||||||
if let Some(item) = item {
|
if let Some(item) = item {
|
||||||
if let Some(ref glyphs) = item.glyphs {
|
if let Some(ref glyphs) = item.glyphs {
|
||||||
let analysis = item.item.analysis();
|
ctx.show_glyph_string(item.font(), glyphs);
|
||||||
let font = analysis.font();
|
|
||||||
ctx.show_glyph_string(&font, glyphs);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
line_y += line_height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +46,7 @@ fn shape_dirty(ctx: &context::Context, ui_model: &mut ui_model::UiModel) {
|
|||||||
let mut item = line.get_item_mut(i).unwrap();
|
let mut item = line.get_item_mut(i).unwrap();
|
||||||
let mut glyphs = pango::GlyphString::new();
|
let mut glyphs = pango::GlyphString::new();
|
||||||
{
|
{
|
||||||
let analysis = item.item.analysis();
|
let analysis = item.analysis();
|
||||||
let (offset, length, _) = item.item.offset();
|
let (offset, length, _) = item.item.offset();
|
||||||
pango_shape(
|
pango_shape(
|
||||||
&styled_line.line_str,
|
&styled_line.line_str,
|
||||||
|
@ -643,8 +643,10 @@ fn gtk_draw(state_arc: &Arc<UiMutex<State>>, ctx: &cairo::Context) -> Inhibit {
|
|||||||
|
|
||||||
fn render(state: &mut State, ctx: &cairo::Context) {
|
fn render(state: &mut State, ctx: &cairo::Context) {
|
||||||
let font_desc = state.create_pango_font();
|
let font_desc = state.create_pango_font();
|
||||||
|
let line_height = state.line_height.unwrap();
|
||||||
|
let char_width = state.char_width.unwrap();
|
||||||
|
|
||||||
render::render(ctx, font_desc, &mut state.model);
|
render::render(ctx, font_desc, line_height, char_width, &mut state.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc<UiMutex<State>>) {
|
fn show_nvim_start_error(err: nvim::NvimInitError, state_arc: Arc<UiMutex<State>>) {
|
||||||
|
@ -20,6 +20,18 @@ glib_wrapper! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
unsafe {
|
||||||
|
from_glib_none(pango_sys::pango_item_new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_offset(&mut self, offset: i32, length: i32, num_chars: i32) {
|
||||||
|
self.0.offset = offset;
|
||||||
|
self.0.length = length;
|
||||||
|
self.0.num_chars = num_chars;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn analysis(&self) -> analysis::Analysis {
|
pub fn analysis(&self) -> analysis::Analysis {
|
||||||
analysis::Analysis::from(&self.0.analysis)
|
analysis::Analysis::from(&self.0.analysis)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
pub mod item;
|
mod item;
|
||||||
mod analysis;
|
mod analysis;
|
||||||
|
|
||||||
|
pub use self::item::Item;
|
||||||
|
pub use self::analysis::Analysis;
|
||||||
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
use pango;
|
use pango;
|
||||||
@ -12,7 +15,7 @@ pub fn pango_itemize(
|
|||||||
context: &pango::Context,
|
context: &pango::Context,
|
||||||
text: &String,
|
text: &String,
|
||||||
attrs: &pango::AttrList
|
attrs: &pango::AttrList
|
||||||
) -> Vec<item::Item> {
|
) -> Vec<Item> {
|
||||||
unsafe {
|
unsafe {
|
||||||
FromGlibPtrContainer::from_glib_container(pango_sys::pango_itemize(
|
FromGlibPtrContainer::from_glib_container(pango_sys::pango_itemize(
|
||||||
context.to_glib_none().0,
|
context.to_glib_none().0,
|
||||||
@ -29,7 +32,7 @@ pub fn pango_shape(
|
|||||||
text: &String,
|
text: &String,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
length: usize,
|
length: usize,
|
||||||
analysis: &analysis::Analysis,
|
analysis: &Analysis,
|
||||||
glyphs: &mut pango::GlyphString,
|
glyphs: &mut pango::GlyphString,
|
||||||
) {
|
) {
|
||||||
debug_assert!(offset + length <= text.len());
|
debug_assert!(offset + length <= text.len());
|
||||||
|
@ -1,30 +1,47 @@
|
|||||||
use std::ops::{Index, IndexMut};
|
use std::ops::{Index, IndexMut};
|
||||||
|
|
||||||
use super::cell::Cell;
|
use super::cell::Cell;
|
||||||
use sys::pango::item as sys_pango;
|
use sys::pango as sys_pango;
|
||||||
use pango;
|
use pango;
|
||||||
|
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
pub item: sys_pango::Item,
|
pub item: sys_pango::Item,
|
||||||
pub glyphs: Option<pango::GlyphString>,
|
pub glyphs: Option<pango::GlyphString>,
|
||||||
|
pub ink_rect: Option<pango::Rectangle>,
|
||||||
|
font: pango::Font,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
pub fn new(item: sys_pango::Item) -> Self {
|
pub fn new(item: sys_pango::Item) -> Self {
|
||||||
Item {
|
Item {
|
||||||
|
font: item.analysis().font(),
|
||||||
item,
|
item,
|
||||||
glyphs: None,
|
glyphs: None,
|
||||||
|
ink_rect: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, item: sys_pango::Item) {
|
pub fn update(&mut self, item: sys_pango::Item) {
|
||||||
|
self.font = item.analysis().font();
|
||||||
self.item = item;
|
self.item = item;
|
||||||
self.glyphs = None;
|
self.glyphs = None;
|
||||||
|
self.ink_rect = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_glyphs(&mut self, glyphs: pango::GlyphString) {
|
pub fn set_glyphs(&mut self, glyphs: pango::GlyphString) {
|
||||||
|
let mut glyphs = glyphs;
|
||||||
|
let (ink_rect, _) = glyphs.extents(&self.font);
|
||||||
|
self.ink_rect = Some(ink_rect);
|
||||||
self.glyphs = Some(glyphs);
|
self.glyphs = Some(glyphs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn font(&self) -> &pango::Font {
|
||||||
|
&self.font
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn analysis(&self) -> sys_pango::Analysis {
|
||||||
|
self.item.analysis()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Line {
|
pub struct Line {
|
||||||
@ -112,7 +129,7 @@ impl Line {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn cell_to_item(&self, cell_idx: usize) -> usize {
|
fn cell_to_item(&self, cell_idx: usize) -> usize {
|
||||||
for i in (cell_idx..0).rev() {
|
for i in (0..cell_idx + 1).rev() {
|
||||||
if self.item_line[i].is_some() {
|
if self.item_line[i].is_some() {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -212,4 +229,24 @@ mod tests {
|
|||||||
assert_eq!(1, styled_line.cell_to_byte[1]);
|
assert_eq!(1, styled_line.cell_to_byte[1]);
|
||||||
assert_eq!(2, styled_line.cell_to_byte[2]);
|
assert_eq!(2, styled_line.cell_to_byte[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_line_merge() {
|
||||||
|
let mut line = Line::new(2);
|
||||||
|
line[0].ch = 'a';
|
||||||
|
line[1].ch = 'b';
|
||||||
|
let styled_line = StyledLine::from(&line);
|
||||||
|
|
||||||
|
let mut item1 = sys_pango::Item::new();
|
||||||
|
item1.set_offset(0, 1, 1);
|
||||||
|
let mut item2 = sys_pango::Item::new();
|
||||||
|
item2.set_offset(1, 1, 1);
|
||||||
|
|
||||||
|
let new_items = [item1, item2];
|
||||||
|
line.merge(&styled_line, &new_items);
|
||||||
|
|
||||||
|
assert_eq!(2, line.item_line.len());
|
||||||
|
assert!(line.item_line[0].is_some());
|
||||||
|
assert!(line.item_line[1].is_some());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user