1#![doc = include_str!("README.md")]
5#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
6#![deny(unsafe_code)]
8
9#[cfg(feature = "proc_macro_span")]
10extern crate proc_macro;
11
12use core::future::Future;
13use core::pin::Pin;
14use std::cell::RefCell;
15use std::collections::HashMap;
16use std::rc::Rc;
17#[cfg(feature = "software-renderer")]
18use std::sync::Arc;
19
20pub mod builtin_macros;
21pub mod diagnostics;
22pub mod embedded_resources;
23pub mod expression_tree;
24pub mod fileaccess;
25pub mod generator;
26pub mod langtype;
27pub mod layout;
28pub mod lexer;
29pub mod literals;
30pub mod llr;
31pub(crate) mod load_builtins;
32pub mod lookup;
33pub mod namedreference;
34pub mod object_tree;
35pub mod parser;
36pub mod pathutils;
37#[cfg(feature = "bundle-translations")]
38pub mod translations;
39pub mod typeloader;
40pub mod typeregister;
41
42pub mod passes;
43
44use crate::generator::OutputFormat;
45use std::path::Path;
46
47#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum EmbedResourcesKind {
50 Nothing,
52 OnlyBuiltinResources,
54 ListAllResources,
56 EmbedAllResources,
58 #[cfg(feature = "software-renderer")]
59 EmbedTextures,
61}
62
63#[derive(Clone, Debug, Eq, PartialEq, Default)]
64#[non_exhaustive]
65pub enum ComponentSelection {
66 #[default]
72 ExportedWindows,
73
74 LastExported,
79
80 Named(String),
82}
83
84#[cfg(feature = "software-renderer")]
85pub type FontCache = Rc<
86 RefCell<
87 std::collections::HashMap<
88 i_slint_common::sharedfontdb::fontdb::ID,
89 fontdue::FontResult<(Arc<fontdue::Font>, Arc<dyn AsRef<[u8]> + Send + Sync>, u32)>,
90 >,
91 >,
92>;
93
94#[derive(Clone)]
96pub struct CompilerConfiguration {
97 pub embed_resources: EmbedResourcesKind,
100 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
102 pub use_sdf_fonts: bool,
103 pub include_paths: Vec<std::path::PathBuf>,
105 pub library_paths: HashMap<String, std::path::PathBuf>,
107 pub style: Option<String>,
109
110 pub open_import_fallback: Option<
115 Rc<dyn Fn(String) -> Pin<Box<dyn Future<Output = Option<std::io::Result<String>>>>>>,
116 >,
117 pub resource_url_mapper:
121 Option<Rc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = Option<String>>>>>>,
122
123 pub inline_all_elements: bool,
128
129 pub const_scale_factor: f64,
132
133 pub accessibility: bool,
135
136 pub enable_experimental: bool,
138
139 pub translation_domain: Option<String>,
141 #[cfg(feature = "bundle-translations")]
143 pub translation_path_bundle: Option<std::path::PathBuf>,
144
145 pub no_native_menu: bool,
147
148 pub cpp_namespace: Option<String>,
150
151 pub error_on_binding_loop_with_window_layout: bool,
154
155 pub debug_info: bool,
157
158 pub debug_hooks: Option<std::hash::RandomState>,
160
161 pub components_to_generate: ComponentSelection,
162
163 #[cfg(feature = "software-renderer")]
164 pub font_cache: FontCache,
165}
166
167impl CompilerConfiguration {
168 pub fn new(output_format: OutputFormat) -> Self {
169 let embed_resources = if std::env::var_os("SLINT_EMBED_TEXTURES").is_some()
170 || std::env::var_os("DEP_MCU_BOARD_SUPPORT_MCU_EMBED_TEXTURES").is_some()
171 {
172 #[cfg(not(feature = "software-renderer"))]
173 panic!("the software-renderer feature must be enabled in i-slint-compiler when embedding textures");
174 #[cfg(feature = "software-renderer")]
175 EmbedResourcesKind::EmbedTextures
176 } else if let Ok(var) = std::env::var("SLINT_EMBED_RESOURCES") {
177 let var = var.parse::<bool>().unwrap_or_else(|_|{
178 panic!("SLINT_EMBED_RESOURCES has incorrect value. Must be either unset, 'true' or 'false'")
179 });
180 match var {
181 true => EmbedResourcesKind::EmbedAllResources,
182 false => EmbedResourcesKind::OnlyBuiltinResources,
183 }
184 } else {
185 match output_format {
186 #[cfg(feature = "rust")]
187 OutputFormat::Rust => EmbedResourcesKind::EmbedAllResources,
188 OutputFormat::Interpreter => EmbedResourcesKind::Nothing,
189 _ => EmbedResourcesKind::OnlyBuiltinResources,
190 }
191 };
192
193 let inline_all_elements = match std::env::var("SLINT_INLINING") {
194 Ok(var) => var.parse::<bool>().unwrap_or_else(|_| {
195 panic!(
196 "SLINT_INLINING has incorrect value. Must be either unset, 'true' or 'false'"
197 )
198 }),
199 Err(_) => output_format == OutputFormat::Interpreter,
201 };
202
203 let const_scale_factor = std::env::var("SLINT_SCALE_FACTOR")
204 .ok()
205 .and_then(|x| x.parse::<f64>().ok())
206 .filter(|f| *f > 0.)
207 .unwrap_or(1.);
208
209 let enable_experimental = std::env::var_os("SLINT_ENABLE_EXPERIMENTAL_FEATURES").is_some();
210
211 let debug_info = std::env::var_os("SLINT_EMIT_DEBUG_INFO").is_some();
212
213 let cpp_namespace = match output_format {
214 #[cfg(feature = "cpp")]
215 OutputFormat::Cpp(config) => match config.namespace {
216 Some(namespace) => Some(namespace),
217 None => match std::env::var("SLINT_CPP_NAMESPACE") {
218 Ok(namespace) => Some(namespace),
219 Err(_) => None,
220 },
221 },
222 _ => None,
223 };
224
225 Self {
226 embed_resources,
227 include_paths: Default::default(),
228 library_paths: Default::default(),
229 style: Default::default(),
230 open_import_fallback: None,
231 resource_url_mapper: None,
232 inline_all_elements,
233 const_scale_factor,
234 accessibility: true,
235 enable_experimental,
236 translation_domain: None,
237 no_native_menu: false,
238 cpp_namespace,
239 error_on_binding_loop_with_window_layout: false,
240 debug_info,
241 debug_hooks: None,
242 components_to_generate: ComponentSelection::ExportedWindows,
243 #[cfg(feature = "software-renderer")]
244 font_cache: Default::default(),
245 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
246 use_sdf_fonts: false,
247 #[cfg(feature = "bundle-translations")]
248 translation_path_bundle: std::env::var("SLINT_BUNDLE_TRANSLATIONS")
249 .ok()
250 .map(|x| x.into()),
251 }
252 }
253
254 #[cfg(feature = "software-renderer")]
255 fn load_font_by_id(
256 &self,
257 face_id: i_slint_common::sharedfontdb::fontdb::ID,
258 ) -> fontdue::FontResult<(Arc<fontdue::Font>, Arc<dyn AsRef<[u8]> + Send + Sync>, u32)> {
259 self.font_cache
260 .borrow_mut()
261 .entry(face_id)
262 .or_insert_with(|| {
263 i_slint_common::sharedfontdb::FONT_DB.with(|fontdb| {
264 fontdb
265 .borrow()
266 .with_face_data(face_id, |font_data, face_index| {
267 fontdue::Font::from_bytes(
268 font_data,
269 fontdue::FontSettings {
270 collection_index: face_index,
271 scale: 40.,
272 ..Default::default()
273 },
274 )
275 .map(|fontdue_font| {
276 (
277 Arc::new(fontdue_font),
278 Arc::new(font_data.to_vec())
279 as Arc<dyn AsRef<[u8]> + Send + Sync>,
280 face_index,
281 )
282 })
283 })
284 .unwrap_or_else(|| fontdue::FontResult::Err("internal error: corrupt font"))
285 })
286 })
287 .clone()
288 }
289}
290
291fn prepare_for_compile(
292 diagnostics: &mut diagnostics::BuildDiagnostics,
293 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
294) -> typeloader::TypeLoader {
295 #[cfg(feature = "software-renderer")]
296 if compiler_config.embed_resources == EmbedResourcesKind::EmbedTextures {
297 compiler_config.accessibility = false;
300 }
301
302 diagnostics.enable_experimental = compiler_config.enable_experimental;
303
304 let global_type_registry = if compiler_config.enable_experimental {
305 crate::typeregister::TypeRegister::builtin_experimental()
306 } else {
307 crate::typeregister::TypeRegister::builtin()
308 };
309
310 typeloader::TypeLoader::new(global_type_registry, compiler_config, diagnostics)
311}
312
313pub async fn compile_syntax_node(
314 doc_node: parser::SyntaxNode,
315 mut diagnostics: diagnostics::BuildDiagnostics,
316 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
317) -> (object_tree::Document, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
318 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
319
320 let doc_node: parser::syntax_nodes::Document = doc_node.into();
321
322 let type_registry =
323 Rc::new(RefCell::new(typeregister::TypeRegister::new(&loader.global_type_registry)));
324 let (foreign_imports, reexports) =
325 loader.load_dependencies_recursively(&doc_node, &mut diagnostics, &type_registry).await;
326
327 let mut doc = crate::object_tree::Document::from_node(
328 doc_node,
329 foreign_imports,
330 reexports,
331 &mut diagnostics,
332 &type_registry,
333 );
334
335 if !diagnostics.has_errors() {
336 passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics).await;
337 } else {
338 passes::run_import_passes(&doc, &loader, &mut diagnostics);
340 }
341 (doc, diagnostics, loader)
342}
343
344pub async fn load_root_file(
350 path: &Path,
351 source_path: &Path,
352 source_code: String,
353 mut diagnostics: diagnostics::BuildDiagnostics,
354 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
355) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
356 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
357
358 let (path, _) =
359 loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;
360
361 (path, diagnostics, loader)
362}
363
364pub async fn load_root_file_with_raw_type_loader(
371 path: &Path,
372 source_path: &Path,
373 source_code: String,
374 mut diagnostics: diagnostics::BuildDiagnostics,
375 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
376) -> (
377 std::path::PathBuf,
378 diagnostics::BuildDiagnostics,
379 typeloader::TypeLoader,
380 Option<typeloader::TypeLoader>,
381) {
382 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
383
384 let (path, raw_type_loader) =
385 loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;
386
387 (path, diagnostics, loader, raw_type_loader)
388}