mod.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 parley::fontique::{Collection, CollectionOptions, SourceCache, SourceCacheOptions};
  19. use std::{
  20. cell::RefCell,
  21. ops::Range,
  22. sync::{Arc, LazyLock},
  23. };
  24. use crate::mesh::Color;
  25. pub mod atlas;
  26. mod editor;
  27. pub use editor::Editor;
  28. mod render;
  29. pub use render::{render_layout, render_layout_with_opts, DebugRenderOptions};
  30. // Global shared FontContext (thread-safe via internal Arc<Mutex<>>)
  31. pub static GLOBAL_FONT_CTX: LazyLock<parley::FontContext> = LazyLock::new(|| {
  32. let collection = Collection::new(CollectionOptions { shared: true, system_fonts: false });
  33. let source_cache = SourceCache::new(SourceCacheOptions { shared: true });
  34. let mut font_ctx = parley::FontContext { collection, source_cache };
  35. let font_data = include_bytes!("../../ibm-plex-mono-regular.otf") as &[u8];
  36. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  37. let font_data = include_bytes!("../../NotoColorEmoji.ttf") as &[u8];
  38. font_ctx.collection.register_fonts(peniko::Blob::new(Arc::new(font_data)), None);
  39. font_ctx
  40. });
  41. // Thread-local LayoutContext
  42. thread_local! {
  43. pub static THREAD_LAYOUT_CTX: RefCell<parley::LayoutContext<Color>> =
  44. RefCell::new(parley::LayoutContext::new());
  45. }
  46. // Public constants
  47. pub const FONT_STACK: &[parley::FontFamily<'_>] = &[
  48. parley::FontFamily::Named(std::borrow::Cow::Borrowed("IBM Plex Mono")),
  49. parley::FontFamily::Named(std::borrow::Cow::Borrowed("Noto Color Emoji")),
  50. ];
  51. // FREE FUNCTIONS (no TextContext wrapper!)
  52. pub fn make_layout(
  53. text: &str,
  54. text_color: Color,
  55. font_size: f32,
  56. lineheight: f32,
  57. window_scale: f32,
  58. width: Option<f32>,
  59. underlines: &[Range<usize>],
  60. ) -> parley::Layout<Color> {
  61. make_layout2(text, text_color, font_size, lineheight, window_scale, width, underlines, &[])
  62. }
  63. pub fn make_layout2(
  64. text: &str,
  65. text_color: Color,
  66. font_size: f32,
  67. lineheight: f32,
  68. window_scale: f32,
  69. width: Option<f32>,
  70. underlines: &[Range<usize>],
  71. foreground_colors: &[(Range<usize>, Color)],
  72. ) -> parley::Layout<Color> {
  73. THREAD_LAYOUT_CTX.with(|layout_ctx| {
  74. let mut layout_ctx = layout_ctx.borrow_mut();
  75. let mut font_ctx = GLOBAL_FONT_CTX.clone();
  76. let mut builder = layout_ctx.ranged_builder(&mut font_ctx, text, window_scale, false);
  77. builder.push_default(parley::LineHeight::FontSizeRelative(lineheight));
  78. builder.push_default(parley::StyleProperty::FontSize(font_size));
  79. builder.push_default(parley::StyleProperty::FontStack(parley::FontStack::List(
  80. FONT_STACK.into(),
  81. )));
  82. builder.push_default(parley::StyleProperty::Brush(text_color));
  83. builder.push_default(parley::StyleProperty::OverflowWrap(parley::OverflowWrap::Anywhere));
  84. for underline in underlines {
  85. builder.push(parley::StyleProperty::Underline(true), underline.clone());
  86. }
  87. for (range, color) in foreground_colors {
  88. builder.push(parley::StyleProperty::Brush(*color), range.clone());
  89. }
  90. let mut layout: parley::Layout<Color> = builder.build(text);
  91. layout.break_all_lines(width);
  92. layout.align(width, parley::Alignment::Start, parley::AlignmentOptions::default());
  93. layout
  94. })
  95. }