use anyhow::Result;
use gpui_shared_string::SharedString;
use gpui_types::{Bounds, DevicePixels, Hsla, Pixels, Size};
use std::any::Any;
use std::borrow::Cow;
use std::future::Future;
use std::ops::Range;
use std::pin::Pin;
use std::sync::Arc;
use crate::{
Font, FontId, FontRun, LineLayout, LineLayoutIndex, LineWrapperHandle, MissingGlyph,
PlatformTextSystem, RenderGlyphParams, TextRenderingMode, WrappedLineLayout,
};
pub trait MissingGlyphReports: Send {
fn recv(&mut self) -> Pin<Box<dyn Future<Output = Option<Vec<MissingGlyph>>> + Send + '_>>;
}
pub trait TextSystem: Send + Sync + Any {
fn platform_text_system(&self) -> &Arc<dyn PlatformTextSystem>;
fn all_font_names(&self) -> Vec<String>;
fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>;
fn take_missing_glyph_receiver(&self) -> Option<Box<dyn MissingGlyphReports>>;
fn enable_missing_glyph_reporting(&self);
fn disable_missing_glyph_reporting(&self);
fn get_font_for_id(&self, id: FontId) -> Option<Font>;
fn resolve_font(&self, font: &Font) -> FontId;
fn prewarm_fonts(&self, fonts: &[Font]);
fn bounding_box(&self, font_id: FontId, font_size: Pixels) -> Bounds<Pixels>;
fn typographic_bounds(
&self,
font_id: FontId,
font_size: Pixels,
character: char,
) -> Result<Bounds<Pixels>>;
fn advance(&self, font_id: FontId, font_size: Pixels, ch: char) -> Result<Size<Pixels>>;
fn layout_width(&self, font_id: FontId, font_size: Pixels, ch: char) -> Pixels;
fn em_width(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels>;
fn em_advance(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels>;
fn ch_width(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels>;
fn ch_advance(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels>;
fn units_per_em(&self, font_id: FontId) -> u32;
fn cap_height(&self, font_id: FontId, font_size: Pixels) -> Pixels;
fn x_height(&self, font_id: FontId, font_size: Pixels) -> Pixels;
fn ascent(&self, font_id: FontId, font_size: Pixels) -> Pixels;
fn descent(&self, font_id: FontId, font_size: Pixels) -> Pixels;
fn baseline_offset(&self, font_id: FontId, font_size: Pixels, line_height: Pixels) -> Pixels;
fn take_font_runs(&self) -> Vec<FontRun>;
fn recycle_font_runs(&self, font_runs: Vec<FontRun>);
fn line_wrapper(self: Arc<Self>, font: Font, font_size: Pixels) -> LineWrapperHandle;
fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>;
fn rasterize_glyph(&self, params: &RenderGlyphParams) -> Result<(Size<DevicePixels>, Vec<u8>)>;
fn glyph_dilation_for_color(&self, color: Hsla) -> u8;
fn recommended_rendering_mode(&self, font_id: FontId, font_size: Pixels) -> TextRenderingMode;
fn layout_index(&self) -> LineLayoutIndex;
fn reuse_layouts(&self, range: Range<LineLayoutIndex>);
fn truncate_layouts(&self, index: LineLayoutIndex);
fn finish_frame(&self);
fn layout_wrapped_line(
&self,
text: &str,
font_size: Pixels,
runs: &[FontRun],
wrap_width: Option<Pixels>,
max_lines: Option<usize>,
) -> Arc<WrappedLineLayout>;
fn layout_line(
&self,
text: &str,
font_size: Pixels,
runs: &[FontRun],
force_width: Option<Pixels>,
) -> Arc<LineLayout>;
fn try_layout_line_by_hash(
&self,
text_hash: u64,
text_len: usize,
font_size: Pixels,
runs: &[FontRun],
force_width: Option<Pixels>,
) -> Option<Arc<LineLayout>>;
fn layout_line_by_hash(
&self,
text_hash: u64,
text_len: usize,
font_size: Pixels,
runs: &[FontRun],
force_width: Option<Pixels>,
materialize_text: Box<dyn FnOnce() -> SharedString>,
) -> Arc<LineLayout>;
}
impl dyn TextSystem {
pub fn as_any(&self) -> &dyn Any {
self as &dyn Any
}
}