Caricamento di file Web‑Open‑Font (WOFF) | .NET

Caricamento di file Web‑Open‑Font (WOFF) con Aspose.Font per .NET

Di cosa hai bisogno

ArticoloDescrizioneDove trovarlo
Aspose.Font per .NETLibreria principale che legge, scrive e converte file di font (inclusi WOFF/WOFF2).dotnet add package Aspose.Font o tramite l’interfaccia utente di NuGet (https://www.nuget.org/packages/Aspose.Font).
Framework di destinazione.NETFramework4.x / .NETCore3.x / .NET5/6/7 – qualsiasi piattaforma supportata da Aspose.Font.Non è richiesto alcun runtime aggiuntivo.
Importazioni di namespacecsharp\nusing Aspose.Font;\nusing Aspose.Font.Sources;\nusing System.IO;\n

Suggerimento: Tutta la documentazione API è disponibile su https://reference.aspose.com/font/net/.

Caricamento di un file WOFF dal file system

Passo dopo passo

  1. Individuare il file del carattere (ad esempio C:\Fonts\MyWebFont.woff).
  2. Crea un FileSystemStreamSource che fornisca alla libreria uno stream per quel file.
  3. Racchiudi l’origine del flusso in un FontFileDefinition – specifica l’estensione "woff" (la libreria la usa per scegliere il parser giusto).
  4. Crea una FontDefinition – passa il FontType (Woff) e il FontFileDefinition appropriati.
  5. Apri il carattere tramite Font.Open(): ottieni un oggetto derivato da Aspose.Font.Font (ad esempio WoffFont).
 1using System;
 2using System.IO;
 3using Aspose.Font;
 4using Aspose.Font.Sources;
 5
 6class LoadWoffExample
 7{
 8    static void Main()
 9    {
10        // Path to the .woff file
11        string woffPath = @"C:\Fonts\MyWebFont.woff";
12
13        // Stream source that reads directly from the file system
14        FileSystemStreamSource streamSrc = new FileSystemStreamSource(woffPath);
15
16        // FontFileDefinition – we tell Aspose it is a “woff” file
17        FontFileDefinition fileDef = new FontFileDefinition("woff", streamSrc);
18
19        // FontDefinition – specify the font type (Woff)
20        FontDefinition fontDef = new FontDefinition(FontType.WOFF, fileDef);
21
22        // Load the font
23        Font woffFont = Font.Open(fontDef);
24
25        // woffFont now gives you access to glyphs, metrics, encoding, etc.
26        Console.WriteLine($"Loaded font: {woffFont.GetType().Name}");
27    }
28}

Risultato: woffFont è un’istanza di Aspose.Font.Woff.WoffFont. Ora puoi visualizzare il testo, ispezionare le metriche dei glifi o convertirlo in un altro formato (TTF, SVG, ecc.).

Modi alternativi per caricare i caratteri WOFF

Caricamento da un array di byte (ad esempio quando il carattere è incorporato in una risorsa o scaricato tramite HTTP)

 1byte[] woffBytes = File.ReadAllBytes(@"C:\Fonts\MyWebFont.woff");
 2
 3// ByteContentStreamSource wraps the byte[] as a readable stream
 4ByteContentStreamSource byteSrc = new ByteContentStreamSource(woffBytes);
 5
 6// Create definition – note we still supply "woff" as extension
 7FontDefinition fd = new FontDefinition(FontType.WOFF,
 8                                      "woff",
 9                                      byteSrc);
10
11Font woffFont = Font.Open(fd);

Caricamento direttamente da un flusso senza FileDefinition intermedio

Se hai già un FileStream aperto:

1using (FileStream fs = File.OpenRead(@"C:\Fonts\MyWebFont.woff"))
2{
3    // Wrap the live FileStream in FileSystemStreamSource
4    var streamSrc = new FileSystemStreamSource(fs);
5
6    // Directly pass stream source + extension into FontDefinition
7    var fd = new FontDefinition(FontType.WOFF, "woff", streamSrc);
8    var woffFont = Font.Open(fd);
9}

Entrambi gli approcci utilizzano le stesse classi sottostanti, cambia solo l’origine dei byte (file su disco, memoria e flusso personalizzato).

Dopo il caricamento: cosa puoi fare?

Tutti questi sono dimostrati nel repository ufficiale degli esempi:

📂 https://github.com/aspose-font/Aspose.Font-Documentation/tree/master/net-examples

Lista di controllo rapida (per le pagine di documentazione)

Have any questions about Aspose.Font?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.