Skip to content

Commit

Permalink
Use shared text system on Linux and Windows (#10098)
Browse files Browse the repository at this point in the history
closes #10129
closes #10269

Release Notes:

- N/A
  • Loading branch information
zaucy committed Apr 10, 2024
1 parent 8f69eac commit bfd9bb8
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 481 deletions.
6 changes: 6 additions & 0 deletions crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
mod app_menu;
mod keystroke;

#[cfg(not(target_os = "macos"))]
mod cosmic_text;

#[cfg(target_os = "linux")]
mod linux;

Expand Down Expand Up @@ -49,6 +52,9 @@ use uuid::Uuid;

pub use app_menu::*;
pub use keystroke::*;

#[cfg(not(target_os = "macos"))]
pub(crate) use cosmic_text::*;
#[cfg(target_os = "linux")]
pub(crate) use linux::*;
#[cfg(target_os = "macos")]
Expand Down
3 changes: 3 additions & 0 deletions crates/gpui/src/platform/cosmic_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod text_system;

pub(crate) use text_system::*;
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use pathfinder_geometry::{
use smallvec::SmallVec;
use std::{borrow::Cow, sync::Arc};

pub(crate) struct LinuxTextSystem(RwLock<LinuxTextSystemState>);
pub(crate) struct CosmicTextSystem(RwLock<CosmicTextSystemState>);

struct LinuxTextSystemState {
struct CosmicTextSystemState {
swash_cache: SwashCache,
font_system: FontSystem,
/// Contains all already loaded fonts, including all faces. Indexed by `FontId`.
Expand All @@ -32,14 +32,14 @@ struct LinuxTextSystemState {
postscript_names: HashMap<FontId, String>,
}

impl LinuxTextSystem {
impl CosmicTextSystem {
pub(crate) fn new() -> Self {
let mut font_system = FontSystem::new();

// todo(linux) make font loading non-blocking
font_system.db_mut().load_system_fonts();

Self(RwLock::new(LinuxTextSystemState {
Self(RwLock::new(CosmicTextSystemState {
font_system,
swash_cache: SwashCache::new(),
loaded_fonts_store: Vec::new(),
Expand All @@ -49,13 +49,13 @@ impl LinuxTextSystem {
}
}

impl Default for LinuxTextSystem {
impl Default for CosmicTextSystem {
fn default() -> Self {
Self::new()
}
}

impl PlatformTextSystem for LinuxTextSystem {
impl PlatformTextSystem for CosmicTextSystem {
fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
self.0.write().add_fonts(fonts)
}
Expand Down Expand Up @@ -189,7 +189,7 @@ impl PlatformTextSystem for LinuxTextSystem {
}
}

impl LinuxTextSystemState {
impl CosmicTextSystemState {
#[profiling::function]
fn add_fonts(&mut self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
let db = self.font_system.db_mut();
Expand Down Expand Up @@ -235,8 +235,15 @@ impl LinuxTextSystemState {
.get_font(font_id)
.ok_or_else(|| anyhow!("Could not load font"))?;

// HACK: to let the storybook run, we should actually do better font fallback
if font.as_swash().charmap().map('m') == 0 || postscript_name == "Segoe Fluent Icons" {
// HACK: To let the storybook run and render Windows caption icons. We should actually do better font fallback.
let allowed_bad_font_names = [
"SegoeFluentIcons", // NOTE: Segoe fluent icons postscript name is inconsistent
"Segoe Fluent Icons",
];

if font.as_swash().charmap().map('m') == 0
&& !allowed_bad_font_names.contains(&postscript_name.as_str())
{
self.font_system.db_mut().remove_face(font.id());
continue;
};
Expand Down
2 changes: 0 additions & 2 deletions crates/gpui/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

mod dispatcher;
mod platform;
mod text_system;
mod wayland;
mod x11;

pub(crate) use dispatcher::*;
pub(crate) use platform::*;
pub(crate) use text_system::*;
pub(crate) use wayland::*;
pub(crate) use x11::*;
8 changes: 4 additions & 4 deletions crates/gpui/src/platform/linux/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use xkbcommon::xkb::{self, Keycode, Keysym, State};

use crate::platform::linux::wayland::WaylandClient;
use crate::{
px, Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CursorStyle, DisplayId,
ForegroundExecutor, Keymap, Keystroke, LinuxDispatcher, LinuxTextSystem, Menu, Modifiers,
px, Action, AnyWindowHandle, BackgroundExecutor, ClipboardItem, CosmicTextSystem, CursorStyle,
DisplayId, ForegroundExecutor, Keymap, Keystroke, LinuxDispatcher, Menu, Modifiers,
PathPromptOptions, Pixels, Platform, PlatformDisplay, PlatformInput, PlatformInputHandler,
PlatformTextSystem, PlatformWindow, Point, PromptLevel, Result, SemanticVersion, Size, Task,
WindowAppearance, WindowOptions, WindowParams,
Expand Down Expand Up @@ -134,15 +134,15 @@ pub(crate) struct PlatformHandlers {
pub(crate) struct LinuxCommon {
pub(crate) background_executor: BackgroundExecutor,
pub(crate) foreground_executor: ForegroundExecutor,
pub(crate) text_system: Arc<LinuxTextSystem>,
pub(crate) text_system: Arc<CosmicTextSystem>,
pub(crate) callbacks: PlatformHandlers,
pub(crate) signal: LoopSignal,
}

impl LinuxCommon {
pub fn new(signal: LoopSignal) -> (Self, Channel<Runnable>) {
let (main_sender, main_receiver) = calloop::channel::channel::<Runnable>();
let text_system = Arc::new(LinuxTextSystem::new());
let text_system = Arc::new(CosmicTextSystem::new());
let callbacks = PlatformHandlers::default();

let dispatcher = Arc::new(LinuxDispatcher::new(main_sender));
Expand Down
7 changes: 2 additions & 5 deletions crates/gpui/src/platform/test/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,11 @@ impl Platform for TestPlatform {
}

fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
#[cfg(target_os = "linux")]
return Arc::new(crate::platform::linux::LinuxTextSystem::new());

#[cfg(target_os = "macos")]
return Arc::new(crate::platform::mac::MacTextSystem::new());

#[cfg(target_os = "windows")]
return Arc::new(crate::platform::windows::WindowsTextSystem::new());
#[cfg(not(target_os = "macos"))]
return Arc::new(crate::platform::cosmic_text::CosmicTextSystem::new());
}

fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {
Expand Down
2 changes: 0 additions & 2 deletions crates/gpui/src/platform/windows.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod dispatcher;
mod display;
mod platform;
mod text_system;
mod util;
mod window;

pub(crate) use dispatcher::*;
pub(crate) use display::*;
pub(crate) use platform::*;
pub(crate) use text_system::*;
pub(crate) use util::*;
pub(crate) use window::*;

Expand Down
4 changes: 2 additions & 2 deletions crates/gpui/src/platform/windows/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) struct WindowsPlatformInner {
background_executor: BackgroundExecutor,
pub(crate) foreground_executor: ForegroundExecutor,
main_receiver: flume::Receiver<Runnable>,
text_system: Arc<WindowsTextSystem>,
text_system: Arc<CosmicTextSystem>,
callbacks: Mutex<Callbacks>,
pub raw_window_handles: RwLock<SmallVec<[HWND; 4]>>,
pub(crate) dispatch_event: OwnedHandle,
Expand Down Expand Up @@ -155,7 +155,7 @@ impl WindowsPlatform {
let dispatcher = Arc::new(WindowsDispatcher::new(main_sender, dispatch_event.to_raw()));
let background_executor = BackgroundExecutor::new(dispatcher.clone());
let foreground_executor = ForegroundExecutor::new(dispatcher);
let text_system = Arc::new(WindowsTextSystem::new());
let text_system = Arc::new(CosmicTextSystem::new());
let callbacks = Mutex::new(Callbacks::default());
let raw_window_handles = RwLock::new(SmallVec::new());
let settings = RefCell::new(WindowsPlatformSystemSettings::new());
Expand Down

0 comments on commit bfd9bb8

Please sign in to comment.