mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_lock::Mutex as AsyncMutex;
  19. use futures::stream::{FuturesUnordered, StreamExt};
  20. use std::{
  21. cell::RefCell,
  22. fmt::Debug,
  23. ops::Range,
  24. sync::{atomic::AtomicBool, Arc, OnceLock},
  25. };
  26. use crate::mesh::Color;
  27. pub mod atlas;
  28. mod editor;
  29. pub use editor::Editor;
  30. mod render;
  31. pub use render::{render_layout, render_layout_with_opts, DebugRenderOptions};
  32. use darkfi::system::CondVar;
  33. pub struct AsyncGlobal<T> {
  34. cv: CondVar,
  35. val: OnceLock<AsyncMutex<T>>,
  36. }
  37. impl<T> AsyncGlobal<T> {
  38. const fn new() -> Self {
  39. Self { cv: CondVar::new(), val: OnceLock::new() }
  40. }
  41. fn set(&self, val: T) {
  42. self.val.set(AsyncMutex::new(val)).ok().unwrap();
  43. self.cv.notify();
  44. }
  45. pub async fn get<'a>(&'a self) -> async_lock::MutexGuard<'a, T> {
  46. self.cv.wait().await;
  47. self.val.get().unwrap().lock().await
  48. }
  49. }
  50. pub static TEXT_CTX: AsyncGlobal<TextContext> = AsyncGlobal::new();
  51. pub fn init_txt_ctx() {
  52. std::thread::spawn(|| {
  53. // This is quite slow. It takes 300ms
  54. let txt_ctx = TextContext::new();
  55. TEXT_CTX.set(txt_ctx);
  56. });
  57. }
  58. /// Initializing this is expensive ~300ms, but storage is ~2kb.
  59. /// It has to be created once and reused. Currently we use thread local storage.
  60. pub struct TextContext {
  61. font_ctx: parley::FontContext,
  62. layout_ctx: parley::LayoutContext<Color>,
  63. }
  64. impl TextContext {
  65. fn new() -> Self {
  66. let layout_ctx = parley::LayoutContext::new();
  67. let mut font_ctx = parley::FontContext::new();
  68. let font_data = include_bytes!("../../ibm-plex-mono-regular.otf") as &[u8];
  69. let font_inf =
  70. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  71. let font_data = include_bytes!("../../NotoColorEmoji.ttf") as &[u8];
  72. let font_inf =
  73. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  74. for (family_id, _) in font_inf {
  75. let family_name = font_ctx.collection.family_name(family_id).unwrap();
  76. trace!(target: "text", "Loaded font: {family_name}");
  77. }
  78. Self { font_ctx, layout_ctx }
  79. }
  80. pub fn borrow(&mut self) -> (&mut parley::FontContext, &mut parley::LayoutContext<Color>) {
  81. (&mut self.font_ctx, &mut self.layout_ctx)
  82. }
  83. pub fn make_layout(
  84. &mut self,
  85. text: &str,
  86. text_color: Color,
  87. font_size: f32,
  88. lineheight: f32,
  89. window_scale: f32,
  90. width: Option<f32>,
  91. underlines: &[Range<usize>],
  92. ) -> parley::Layout<Color> {
  93. let mut builder =
  94. self.layout_ctx.ranged_builder(&mut self.font_ctx, &text, window_scale, false);
  95. builder.push_default(parley::StyleProperty::LineHeight(lineheight));
  96. builder.push_default(parley::StyleProperty::FontSize(font_size));
  97. builder.push_default(parley::StyleProperty::FontStack(parley::FontStack::List(
  98. FONT_STACK.into(),
  99. )));
  100. builder.push_default(parley::StyleProperty::Brush(text_color));
  101. for underline in underlines {
  102. builder.push(parley::StyleProperty::Underline(true), underline.clone());
  103. }
  104. let mut layout: parley::Layout<Color> = builder.build(&text);
  105. layout.break_all_lines(width);
  106. layout.align(width, parley::Alignment::Start, parley::AlignmentOptions::default());
  107. layout
  108. }
  109. }
  110. pub const FONT_STACK: &[parley::FontFamily<'_>] = &[
  111. parley::FontFamily::Named(std::borrow::Cow::Borrowed("IBM Plex Mono")),
  112. parley::FontFamily::Named(std::borrow::Cow::Borrowed("Noto Color Emoji")),
  113. ];