Itemizer
This commit is contained in:
37
src/render/context.rs
Normal file
37
src/render/context.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::ffi::CString;
|
||||
|
||||
use pangocairo::FontMap;
|
||||
use pango::prelude::*;
|
||||
use pango;
|
||||
|
||||
use sys::pango::*;
|
||||
|
||||
use ui_model::StyledLine;
|
||||
|
||||
pub struct Context {
|
||||
pango_context: pango::Context,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new(font_desc: &pango::FontDescription) -> Self {
|
||||
Context {
|
||||
pango_context: create_pango_context(font_desc),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, font_desc: &pango::FontDescription) {
|
||||
self.pango_context = create_pango_context(font_desc);
|
||||
}
|
||||
|
||||
pub fn itemize(&self, line: &StyledLine)-> Vec<item::Item> {
|
||||
pango_itemize(&self.pango_context, &line.line_str, &line.attr_list)
|
||||
}
|
||||
}
|
||||
|
||||
fn create_pango_context(font_desc: &pango::FontDescription) -> pango::Context {
|
||||
let font_map = FontMap::get_default();
|
||||
let pango_context = font_map.create_context().unwrap();
|
||||
pango_context.set_font_description(&font_desc);
|
||||
|
||||
pango_context
|
||||
}
|
||||
@@ -1,28 +1,30 @@
|
||||
mod context;
|
||||
|
||||
use sys::pango::*;
|
||||
use pango;
|
||||
use pango::prelude::*;
|
||||
use cairo;
|
||||
use pangocairo::{CairoContextExt, FontMap};
|
||||
use std::ffi::CString;
|
||||
use ui_model;
|
||||
|
||||
pub fn render(ctx: &cairo::Context, font_desc: pango::FontDescription) {
|
||||
let font_map = FontMap::get_default();
|
||||
let pango_context = font_map.create_context().unwrap();
|
||||
pango_context.set_font_description(&font_desc);
|
||||
|
||||
let text = "TEST String".to_owned().into_bytes();
|
||||
let len = text.len();
|
||||
let text = CString::new(text).unwrap();
|
||||
let text = "TEST String".to_owned();
|
||||
let attr_list = pango::AttrList::new();
|
||||
|
||||
ctx.move_to(0.0, 50.0);
|
||||
let items = pango_itemize(&pango_context, &text, 0, len, &attr_list);
|
||||
let items = pango_itemize(&pango_context, &text, &attr_list);
|
||||
for item in items {
|
||||
let mut glyphs = pango::GlyphString::new();
|
||||
let analysis = item.analysis();
|
||||
pango_shape(&text, len, &analysis, &mut glyphs);
|
||||
pango_shape(&text, &analysis, &mut glyphs);
|
||||
let font = analysis.font();
|
||||
let (ink, logical) = glyphs.extents(&font);
|
||||
ctx.show_glyph_string(&font, &glyphs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user