Glifi e metriche dei caratteri TrueType | Giava
Ottieni metriche di carattere
Le metriche dei caratteri si riferiscono a Informaiton come Ascender
, Descender
, typoascender
, typodescender
e untsperem
. Aspose.Font per API Java può essere utilizzato per aprire i file dei caratteri TTF e leggere le informazioni sulle metriche dei caratteri da questi utilizzando il seguente codice di esempio.
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2String fileName = Utils.getDataDir() + "Montserrat-Regular.ttf"; //Font file name with full path
3
4 FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
5 TtfFont font = (TtfFont) Font.open(fd);
6
7 String name = font.getFontName();
8 System.out.println("Font name: " + name);
9 System.out.println("Glyph count: " + font.getNumGlyphs());
10 String metrics = MessageFormat.format(
11 "Font metrics: ascender - {0}, descender - {1}, typo ascender = {2}, typo descender = {3}, UnitsPerEm = {4}",
12 font.getMetrics().getAscender(), font.getMetrics().getDescender(),
13 font.getMetrics().getTypoAscender(), font.getMetrics().getTypoDescender(), font.getMetrics().getUnitsPerEM());
14
15 System.out.println(metrics);
16
17 //Get cmap unicode encoding table from font as object TtfCMapFormatBaseTable to access information about font glyph for symbol 'A'.
18 //Also check that font has object TtfGlyfTable (table 'glyf') to access glyph.
19 TtfCMapFormatBaseTable cmapTable = null;
20 if (font.getTtfTables().getCMapTable() != null)
21 {
22 cmapTable = font.getTtfTables().getCMapTable().findUnicodeTable();
23 }
24 if (cmapTable != null && font.getTtfTables().getGlyfTable() != null)
25 {
26 System.out.println("Font cmap unicode table: PlatformID = " + cmapTable.getPlatformId() +
27 ", PlatformSpecificID = " + cmapTable.getPlatformSpecificId());
28
29 //Code for 'A' symbol
30 char unicode = (char)65;
31
32 //Glyph index for 'A'
33 long glIndex = cmapTable.getGlyphIndex(unicode);
34
35 if (glIndex != 0)
36 {
37 //Glyph for 'A'
38 Glyph glyph = font.getGlyphById(glIndex);
39 if (glyph != null)
40 {
41 //Print glyph metrics
42 System.out.println("Glyph metrics for 'A' symbol:");
43 String bbox = MessageFormat.format(
44 "Glyph BBox: Xmin = {0}, Xmax = {1}" + ", Ymin = {2}, Ymax = {3}",
45 glyph.getGlyphBBox().getXMin(), glyph.getGlyphBBox().getXMax(),
46 glyph.getGlyphBBox().getYMin(), glyph.getGlyphBBox().getYMax());
47 System.out.println(bbox);
48 System.out.println("Width:" + font.getMetrics().getGlyphWidth(new GlyphUInt32Id(glIndex)));
49 }
50 }
51 }
Rileva i simboli latini
Aspose.Font per .API consente di rilevare i simboli latini dai file di carattere TrueType. Questo può essere ottenuto utilizzando il seguente codice di esempio.
- Carica il file font utilizzando FontFileDefinition
- Decodifica il glyphid usando il metodo DecodeToGid()
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2String fileName = Utils.getDataDir() + "Montserrat-Regular.ttf"; //Font file name with full path
3
4 FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
5 TtfFont ttfFont = (TtfFont) Font.open(fd);
6
7 boolean latinText = true;
8
9
10 for (int code = 65; code < 123; code++)
11 {
12 GlyphId gid = ttfFont.getEncoding().decodeToGid(code);
13 if (gid == null || gid == GlyphUInt32Id.getNotDef())
14 {
15 latinText = false;
16 }
17 }
18
19 if (latinText)
20 {
21 System.out.println(MessageFormat.format("Font {0} supports latin symbols.", ttfFont.getFontName()));
22 }
23 else
24 {
25 System.out.println(MessageFormat.format("Latin symbols are not supported by font {0}.", ttfFont.getFontName()));
26 }
Estrai restrizioni di licenza
Usa Aspose.Font per l’API Java per estrarre le restrizioni di licenze dai file di carattere. I caratteri possono avere informazioni sulle licenze incorporate in queste in una delle seguenti modalità.
- Il carattere di incorporamento “modificabile” può essere incorporato e può essere temporaneamente caricato su altri sistemi. Inoltre, è consentito l’editing, inclusa la capacità di formattare un nuovo testo usando il carattere incorporato e le modifiche possono essere salvate.
Installable Embedding
- Il carattere può essere incorporato e può essere installato permanentemente per l’uso su un sistema remoto o per l’uso da parte di altri utenti.Anteprima e stampa Incorniciatura
- Il carattere può essere incorporato e può essere temporaneamente caricato su altri sistemi a fini di visualizzazione o stampa del documento.- Il carattere “incorporato” - Il carattere non deve essere modificato, incorporato o scambiato in alcun modo senza prima ottenere l’autorizzazione esplicita del proprietario legale.
Il seguente campione di codice mostra come utilizzare un oggetto Aspose.Font.TTF.LICENSEFLAGS per ottenere informazioni sulle restrizioni della licenza dei caratteri (flag fstype dalla tabella OS/2) in forma conveniente.
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-Java
2String fileName = Utils.getDataDir() + "Montserrat-Regular.ttf"; //Font file name with full path
3
4 FontDefinition fd = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource(fileName)));
5 TtfFont font = (TtfFont) Font.open(fd);
6
7 LicenseFlags licenseFlags = null;
8 if (font.getTtfTables().getOs2Table() != null)
9 {
10 licenseFlags = font.getTtfTables().getOs2Table().getLicenseFlags();
11 }
12
13 if (licenseFlags == null || licenseFlags.isFSTypeAbsent())
14 {
15 System.out.println(MessageFormat.format("Font {0} has no embedded license restrictions", font.getFontName()));
16 }
17 else
18 {
19 if (licenseFlags.isEditableEmbedding())
20 {
21 System.out.println(MessageFormat.format("Font {0} may be embedded, and may be temporarily loaded on other systems.", font.getFontName())
22 + " In addition, editing is permitted, including ability to format new text"
23 + " using the embedded font, and changes may be saved.");
24 }
25 else if (licenseFlags.isInstallableEmbedding())
26 {
27 System.out.println(MessageFormat.format("Font {0} may be embedded, and may be permanently installed", font.getFontName())
28 + " for use on a remote systems, or for use by other users.");
29 }
30 else if (licenseFlags.isPreviewAndPrintEmbedding())
31 {
32 System.out.println(MessageFormat.format("Font {0} may be embedded, and may be temporarily loaded", font.getFontName())
33 + " on other systems for purposes of viewing or printing the document.");
34 }
35 else if (licenseFlags.isRestrictedLicenseEmbedding())
36 {
37 System.out.println(MessageFormat.format("Font {0} must not be modified, embedded or exchanged in any manner", font.getFontName())
38 + " without first obtaining explicit permission of the legal owner.");
39 }
40 }