mod.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 std::{
  20. ops::Range,
  21. sync::{Arc, OnceLock},
  22. };
  23. use crate::{mesh::Color, util::spawn_thread};
  24. pub mod atlas;
  25. mod editor;
  26. pub use editor::Editor;
  27. mod render;
  28. pub use render::{render_layout, render_layout_with_opts, DebugRenderOptions};
  29. use darkfi::system::CondVar;
  30. pub struct AsyncGlobal<T> {
  31. cv: CondVar,
  32. val: OnceLock<AsyncMutex<T>>,
  33. }
  34. impl<T> AsyncGlobal<T> {
  35. const fn new() -> Self {
  36. Self { cv: CondVar::new(), val: OnceLock::new() }
  37. }
  38. fn set(&self, val: T) {
  39. self.val.set(AsyncMutex::new(val)).ok().unwrap();
  40. self.cv.notify();
  41. }
  42. pub async fn get<'a>(&'a self) -> async_lock::MutexGuard<'a, T> {
  43. self.cv.wait().await;
  44. self.val.get().unwrap().lock().await
  45. }
  46. }
  47. pub static TEXT_CTX: AsyncGlobal<TextContext> = AsyncGlobal::new();
  48. pub fn init_txt_ctx() {
  49. spawn_thread("init_txt_ctx", || {
  50. // This is quite slow. It takes 300ms
  51. let txt_ctx = TextContext::new();
  52. TEXT_CTX.set(txt_ctx);
  53. });
  54. }
  55. /// Initializing this is expensive ~300ms, but storage is ~2kb.
  56. /// It has to be created once and reused. Currently we use thread local storage.
  57. pub struct TextContext {
  58. font_ctx: parley::FontContext,
  59. layout_ctx: parley::LayoutContext<Color>,
  60. }
  61. impl TextContext {
  62. fn new() -> Self {
  63. let layout_ctx = parley::LayoutContext::new();
  64. let mut font_ctx = parley::FontContext::new();
  65. let font_data = include_bytes!("../../ibm-plex-mono-regular.otf") as &[u8];
  66. let _font_inf =
  67. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  68. let font_data = include_bytes!("../../NotoColorEmoji.ttf") as &[u8];
  69. let _font_inf =
  70. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  71. //for (family_id, _) in font_inf {
  72. // let family_name = font_ctx.collection.family_name(family_id).unwrap();
  73. // trace!(target: "text", "Loaded font: {family_name}");
  74. //}
  75. Self { font_ctx, layout_ctx }
  76. }
  77. #[cfg(not(target_os = "android"))]
  78. pub fn borrow(&mut self) -> (&mut parley::FontContext, &mut parley::LayoutContext<Color>) {
  79. (&mut self.font_ctx, &mut self.layout_ctx)
  80. }
  81. pub fn make_layout(
  82. &mut self,
  83. text: &str,
  84. text_color: Color,
  85. font_size: f32,
  86. lineheight: f32,
  87. window_scale: f32,
  88. width: Option<f32>,
  89. underlines: &[Range<usize>],
  90. ) -> parley::Layout<Color> {
  91. self.make_layout2(
  92. text,
  93. text_color,
  94. font_size,
  95. lineheight,
  96. window_scale,
  97. width,
  98. underlines,
  99. &[],
  100. )
  101. }
  102. pub fn make_layout2(
  103. &mut self,
  104. text: &str,
  105. text_color: Color,
  106. font_size: f32,
  107. lineheight: f32,
  108. window_scale: f32,
  109. width: Option<f32>,
  110. underlines: &[Range<usize>],
  111. foreground_colors: &[(Range<usize>, Color)],
  112. ) -> parley::Layout<Color> {
  113. let mut builder =
  114. self.layout_ctx.ranged_builder(&mut self.font_ctx, &text, window_scale, false);
  115. builder.push_default(parley::LineHeight::FontSizeRelative(lineheight));
  116. builder.push_default(parley::StyleProperty::FontSize(font_size));
  117. builder.push_default(parley::StyleProperty::FontStack(parley::FontStack::List(
  118. FONT_STACK.into(),
  119. )));
  120. builder.push_default(parley::StyleProperty::Brush(text_color));
  121. builder.push_default(parley::StyleProperty::OverflowWrap(parley::OverflowWrap::Anywhere));
  122. for underline in underlines {
  123. builder.push(parley::StyleProperty::Underline(true), underline.clone());
  124. }
  125. for (range, color) in foreground_colors {
  126. builder.push(parley::StyleProperty::Brush(*color), range.clone());
  127. }
  128. let mut layout: parley::Layout<Color> = builder.build(&text);
  129. layout.break_all_lines(width);
  130. layout.align(width, parley::Alignment::Start, parley::AlignmentOptions::default());
  131. layout
  132. }
  133. }
  134. pub const FONT_STACK: &[parley::FontFamily<'_>] = &[
  135. parley::FontFamily::Named(std::borrow::Cow::Borrowed("IBM Plex Mono")),
  136. parley::FontFamily::Named(std::borrow::Cow::Borrowed("Noto Color Emoji")),
  137. ];