wincompatlib 0.7.7

Set of interfaces to run windows applications on unix-like systems using Wine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::process::{Command, Stdio};

use serde::{Serialize, Deserialize};

use crate::wine::*;
use crate::wine::ext::WineRunExt;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize,Deserialize)]
pub enum Font {
    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | AndaleMo.TTF | andalemo.ttf | Andale Mono |
    Andale,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Arial.TTF | arial.ttf | Arial |
    /// | Arialbd.TTF | arialbd.ttf | Arial Bold |
    /// | Ariali.TTF | ariali.ttf | Arial Italic |
    /// | Arialbi.TTF | arialbi.ttf | Arial Bold Italic |
    ///
    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | AriBlk.TTF | ariblk.ttf | Arial Black |
    Arial,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Comic.TTF | comic.ttf | Comic Sans MS |
    /// | Comicbd.TTF | comicbd.ttf | Comic Sans MS Bold |
    ComicSans,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | cour.ttf | cour.ttf | Courier New |
    /// | courbd.ttf | courbd.ttf | Courier New Bold |
    /// | couri.ttf | couri.ttf | Courier New Italic |
    /// | courbi.ttf | courbi.ttf | Courier New Bold Italic |
    Courier,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Georgia.TTF | georgia.ttf | Georgia |
    /// | Georgiab.TTF | georgiab.ttf | Georgia Bold |
    /// | Georgiai.TTF | georgiai.ttf | Georgia Italic |
    /// | Georgiaz.TTF | georgiaz.ttf | Georgia Bold Italic |
    Georgia,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Impact.TTF | impact.ttf | Impact |
    Impact,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Times.TTF | times.ttf | Times New Roman |
    /// | Timesbd.TTF | timesbd.ttf | Times New Roman Bold |
    /// | Timesi.TTF | timesi.ttf | Times New Roman Italic |
    /// | Timesbi.TTF | timesbi.ttf | Times New Roman Bold Italic |
    Times,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | trebuc.ttf | trebuc.ttf | Trebuchet MS |
    /// | Trebucbd.ttf | trebucbd.ttf | Trebuchet MS Bold |
    /// | trebucit.ttf | trebucit.ttf | Trebuchet MS Italic |
    /// | trebucbi.ttf | trebucbi.ttf | Trebuchet MS Bold Italic |
    Trebuchet,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Verdana.TTF | verdana.ttf | Verdana |
    /// | Verdanab.TTF | verdanab.ttf | Verdana Bold |
    /// | Verdanai.TTF | verdanai.ttf | Verdana Italic |
    /// | Verdanaz.TTF | verdanaz.ttf | Verdana Bold Italic |
    Verdana,

    /// | File | Winetricks File | Name |
    /// | :- | :- | :- |
    /// | Webdings.TTF | webdings.ttf | Webdings |
    Webdings
}

impl Font {
    /// Get iterator over all available enum values
    pub fn iterator() -> impl IntoIterator<Item = Self> {
        [
            Self::Andale,
            Self::Arial,
            Self::ComicSans,
            Self::Courier,
            Self::Georgia,
            Self::Impact,
            Self::Times,
            Self::Trebuchet,
            Self::Verdana,
            Self::Webdings
        ].into_iter()
    }

    /// Get corefont code name
    ///
    /// | Corefont enum | Font code name |
    /// | :- | :- |
    /// | Andale | andalemo |
    /// | Arial | arial |
    /// | ComicSans | comic |
    /// | Courier | cour |
    /// | Georgia | georgia |
    /// | Impact | impact |
    /// | Times | times |
    /// | Trebuchet | trebuc |
    /// | Verdana | verdana |
    /// | Webdings | webdings |
    pub fn code(&'_ self) -> &'_ str {
        match self {
            Self::Andale    => "andalemo",
            Self::Arial     => "arial",
            Self::ComicSans => "comic",
            Self::Courier   => "cour",
            Self::Georgia   => "georgia",
            Self::Impact    => "impact",
            Self::Times     => "times",
            Self::Trebuchet => "trebuc",
            Self::Verdana   => "verdana",
            Self::Webdings  => "webdings"
        }
    }

    /// Get full corefont name
    ///
    /// | Corefont enum | Font name |
    /// | :- | :- |
    /// | Andale | Andale |
    /// | Arial | Arial |
    /// | ComicSans | Comic Sans MS |
    /// | Courier | Courier New |
    /// | Georgia | Georgia |
    /// | Impact | Impact |
    /// | Times | Times New Roman |
    /// | Trebuchet | Trebuchet MS |
    /// | Verdana | Verdana |
    /// | Webdings | Webdings |
    pub fn name(&'_ self) -> &'_ str {
        match self {
            Self::Andale    => "Andale",
            Self::Arial     => "Arial",
            Self::ComicSans => "Comic Sans MS",
            Self::Courier   => "Courier New",
            Self::Georgia   => "Georgia",
            Self::Impact    => "Impact",
            Self::Times     => "Times New Roman",
            Self::Trebuchet => "Trebuchet MS",
            Self::Verdana   => "Verdana",
            Self::Webdings  => "Webdings"
        }
    }

    /// Check if current font is installed in the wine prefix's fonts folder
    pub fn is_installed(&self, prefix: impl AsRef<Path>) -> bool {
        let prefix = prefix.as_ref();
        let font = self.code();

        prefix.join("drive_c/windows/Fonts").join(format!("{font}.ttf")).exists() |
        prefix.join("drive_c/windows/Fonts").join(format!("{font}.TTF")).exists() |

        // Didn't see such situations in real life but it's listed in the winetricks so I guess they can occur?
        prefix.join("drive_c/windows/fonts").join(format!("{font}.ttf")).exists() |
        prefix.join("drive_c/windows/fonts").join(format!("{font}.TTF")).exists()
    }
}

pub trait WineFontsExt {
    /// Register font in the wine registry
    ///
    /// ```no_run
    /// use wincompatlib::wine::Wine;
    /// use wincompatlib::wine::ext::WineFontsExt;
    ///
    /// // times.ttf should be in the wine fonts directory
    /// if let Err(err) = Wine::default().register_font("times.ttf", "Times New Roman") {
    ///     eprintln!("Failed to register Times New Roman font: {err}");
    /// }
    /// ```
    fn register_font(&self, ttf: impl AsRef<str>, font_name: impl AsRef<str>) -> anyhow::Result<()>;

    /// Check if ttf with given name is installed in the wine fonts folder
    ///
    /// ```
    /// use wincompatlib::wine::Wine;
    /// use wincompatlib::wine::ext::WineFontsExt;
    ///
    /// let installed = Wine::default().font_is_installed("times");
    ///
    /// println!("Is Times fonts installed: {:?}", installed);
    /// ```
    fn font_is_installed(&self, ttf: impl AsRef<str>) -> bool;

    /// Install given font
    ///
    /// ```no_run
    /// use wincompatlib::wine::Wine;
    /// use wincompatlib::wine::ext::{WineFontsExt, Font};
    ///
    /// if let Err(err) = Wine::default().install_font(Font::Times) {
    ///     eprintln!("Failed to install Times New Roman: {err}");
    /// }
    /// ```
    fn install_font(&self, font: Font) -> anyhow::Result<()>;
}

impl WineFontsExt for Wine {
    fn register_font(&self, font_file: impl AsRef<str>, font_name: impl AsRef<str>) -> anyhow::Result<()> {
        // "$wine" reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts /f font.ttf /d "Font Name" /f
        let output = self.run_args(["reg", "add", "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts", "/v", font_name.as_ref(), "/d", font_file.as_ref(), "/f"])?
            .wait_with_output()?;

        if !output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let error = stdout.trim_end().lines().last().unwrap_or(&stdout);

            anyhow::bail!("Failed to register font: {error}");
        }

        // HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Fonts
        let output = self.run_args(["reg", "add", "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Fonts", "/v", font_name.as_ref(), "/d", font_file.as_ref(), "/f"])?
            .wait_with_output()?;

        if !output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let error = stdout.trim_end().lines().last().unwrap_or(&stdout);

            anyhow::bail!("Failed to register font: {error}");
        }

        Ok(())
    }

    fn font_is_installed(&self, font_file: impl AsRef<str>) -> bool {
        self.prefix.join("drive_c/windows/Fonts").join(font_file.as_ref()).exists() |
        self.prefix.join("drive_c/windows/Fonts").join(format!("{}.ttf", font_file.as_ref())).exists() |
        self.prefix.join("drive_c/windows/Fonts").join(format!("{}.TTF", font_file.as_ref())).exists() |

        // Didn't see such situations in real life but it's listed in the winetricks so I guess they can occur?
        self.prefix.join("drive_c/windows/fonts").join(font_file.as_ref()).exists() |
        self.prefix.join("drive_c/windows/fonts").join(format!("{}.ttf", font_file.as_ref())).exists() |
        self.prefix.join("drive_c/windows/fonts").join(format!("{}.TTF", font_file.as_ref())).exists()
    }

    // TODO: I've made a merge request to minreq to add is_ok method. Use it once it will be merged

    fn install_font(&self, font: Font) -> anyhow::Result<()> {
        fn install_fonts(wine: &Wine, font_name: &str, install: impl IntoIterator<Item = (impl AsRef<str>, impl AsRef<str>, impl AsRef<str>)>) -> anyhow::Result<()> {
            // Extracted from https://sourceforge.net/p/forge/documentation/Mirrors/
            const CDN_BASE_URLS: &[&str] = &[
                "https://downloads.sourceforge.net/corefonts",
                "https://jaist.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://nchc.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://netcologne.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://altushost-swe.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://cfhcable.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://cyfuture.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://cytranet-dal.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://deac-riga.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://excellmedia.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://freefr.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://gigenet.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://icolo.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://ixpeering.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://kumisystems.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://liquidtelecom.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://netactuate.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://onboardcloud.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://phoenixnap.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://pilotfiber.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://psychz.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://razaoinfo.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://sinalbr.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://sitsa.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://tenet.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://unlimited.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://versaweb.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://webwerks.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://yer.dl.sourceforge.net/project/corefonts/the%20fonts/final",
                "https://zenlayer.dl.sourceforge.net/project/corefonts/the%20fonts/final",
            ];

            // Fonts blake3 hashes to verify their correctness
            const FONTS_HASHES: &[(&str, &str)] = &[
                ("andale32", "f794d32548caba2a2a2efd9625f9e268866445ddc3aea4a1353be86c529018fb"),
                ("arial32",  "3e1018c47291d18d94281dc94e2b36d1572dc28a08715507e1f05e1b710eccc7"),
                ("arialb32", "2b6f2332b61da519c535a3074f0ac1c76427c1db458833ab4ab20bd30c325296"),
                ("comic32",  "5df2f0d4f3a2af489b3cb6213ef4d1ff1dffe67d1842953a448ee0a1ce875896"),
                ("courie32", "6a1287b2e574cce551528d55457269d18f7930c8d4cf694caaea9f56913cc554"),
                ("georgi32", "2c53bcfa1bb77b4679e309db1261d08e0c896a7374b282f8b9a8080d1f05f54b"),
                ("impact32", "fe450901803f732a21d1d1b8081c62d7dfba1eba9b4a9501d56996b1e664681b"),
                ("times32",  "d1bb288a928748d31770eb70af0d0073cb0efeccde6108420a39d044c25d9006"),
                ("trebuc32", "7c5f5e3e6904f01803d0798f295b2a8152aa54912ca31f8ea675028a0dca71a1"),
                ("verdan32", "01f8aa9820d516b5e6109a215369726a9e4abbceb2bd77f77fbfad9d047a9994"),
                ("webdin32", "fe885f86c98d2bf96251088804e07e6e1164d0b9b05deedf12ea72aff6f6e894")
            ];

            // FIXME: folder name can be lowercased?
            let fonts = wine.prefix.join("drive_c/windows/Fonts");
            let cabextract_temp = fonts.join(format!(".{font_name}-cabextract"));

            if cabextract_temp.exists() {
                std::fs::remove_dir_all(&cabextract_temp)?;
            }

            std::fs::create_dir(&cabextract_temp)?;

            let path = cabextract_temp.join(format!("{font_name}.exe"));
            let temp = cabextract_temp.join(font_name);

            for url in CDN_BASE_URLS {
                if let Ok(content) = minreq::get(format!("{url}/{font_name}.exe")).send() {
                    let content = content.as_bytes();
                    let hash = blake3::hash(content).to_string();

                    for (font, font_hash) in FONTS_HASHES {
                        if font == &font_name && font_hash != &hash {
                            anyhow::bail!("Font {font_name} was downloaded from the CDN, but its hash is incorrect");
                        }
                    }

                    std::fs::write(&path, content)?;

                    let output = Command::new("cabextract")
                        .arg("-d")
                        .arg(&temp)
                        .arg(&path)
                        .stdout(Stdio::piped())
                        .stderr(Stdio::piped())
                        .spawn()?
                        .wait_with_output()?;

                    if !output.status.success() {
                        anyhow::bail!("Failed to cabextract font: {}", String::from_utf8_lossy(&output.stderr));
                    }

                    for (original, new, name) in install {
                        std::fs::copy(temp.join(original.as_ref()), fonts.join(new.as_ref()))?;

                        wine.register_font(new, name)?;
                    }

                    std::fs::remove_dir_all(cabextract_temp)?;

                    return Ok(());
                }
            }

            anyhow::bail!("Couldn't connect to any of the CDNs to download the {font_name} font");
        }

        match font {
            Font::Andale => install_fonts(self, "andale32", [
                ("AndaleMo.TTF", "andalemo.ttf", "Andale Mono")
            ])?,

            Font::Arial => {
                install_fonts(self, "arial32", [
                    ("Arial.TTF",   "arial.ttf",   "Arial"),
                    ("Arialbd.TTF", "arialbd.ttf", "Arial Bold"),
                    ("Ariali.TTF",  "ariali.ttf",  "Arial Italic"),
                    ("Arialbi.TTF", "arialbi.ttf", "Arial Bold Italic")
                ])?;

                install_fonts(self, "arialb32", [
                    ("AriBlk.TTF", "ariblk.ttf", "Arial Black")
                ])?;
            }

            Font::ComicSans => install_fonts(self, "comic32", [
                ("Comic.TTF",   "comic.ttf",   "Comic Sans MS"),
                ("Comicbd.TTF", "comicbd.ttf", "Comic Sans MS Bold"),
            ])?,

            Font::Courier => install_fonts(self, "courie32", [
                ("cour.ttf",   "cour.ttf",   "Courier New"),
                ("courbd.ttf", "courbd.ttf", "Courier New Bold"),
                ("couri.ttf",  "couri.ttf",  "Courier New Italic"),
                ("courbi.ttf", "courbi.ttf", "Courier New Bold Italic")
            ])?,

            Font::Georgia => install_fonts(self, "georgi32", [
                ("Georgia.TTF",  "georgia.ttf",  "Georgia"),
                ("Georgiab.TTF", "georgiab.ttf", "Georgia Bold"),
                ("Georgiai.TTF", "georgiai.ttf", "Georgia Italic"),
                ("Georgiaz.TTF", "georgiaz.ttf", "Georgia Bold Italic")
            ])?,

            Font::Impact => install_fonts(self, "impact32", [
                ("Impact.TTF", "impact.ttf", "Impact")
            ])?,

            Font::Times => install_fonts(self, "times32", [
                ("Times.TTF",   "times.ttf",   "Times New Roman"),
                ("Timesbd.TTF", "timesbd.ttf", "Times New Roman Bold"),
                ("Timesi.TTF",  "timesi.ttf",  "Times New Roman Italic"),
                ("Timesbi.TTF", "timesbi.ttf", "Times New Roman Bold Italic")
            ])?,

            Font::Trebuchet => install_fonts(self, "trebuc32", [
                ("trebuc.ttf",   "trebuc.ttf",   "Trebuchet MS"),
                ("Trebucbd.ttf", "trebucbd.ttf", "Trebuchet MS Bold"),
                ("trebucit.ttf", "trebucit.ttf", "Trebuchet MS Italic"),
                ("trebucbi.ttf", "trebucbi.ttf", "Trebuchet MS Bold Italic")
            ])?,

            Font::Verdana => install_fonts(self, "verdan32", [
                ("Verdana.TTF",  "verdana.ttf",  "Verdana"),
                ("Verdanab.TTF", "verdanab.ttf", "Verdana Bold"),
                ("Verdanai.TTF", "verdanai.ttf", "Verdana Italic"),
                ("Verdanaz.TTF", "verdanaz.ttf", "Verdana Bold Italic")
            ])?,

            Font::Webdings => install_fonts(self, "webdin32", [
                ("Webdings.TTF", "webdings.ttf", "Webdings")
            ])?,
        }

        Ok(())
    }
}