//-------------------------------------------------------------------------------------------------- internal bool Update(GlyphTypeface glyphTypeface, ICollection <ushort> glyphs, ref string finalFontName) { // Write font file to embed var bytes = glyphTypeface.ComputeSubset(glyphs); if (bytes?.Length > 0) { _FontStream.Add(bytes); _FontStream.Attributes["Length1"] = bytes.Length; Attributes["FontFile2"] = _FontStream; finalFontName = $"AAAAAA+{finalFontName}"; _HasFontFile = true; } Attributes["FontName"] = $"/{finalFontName}"; Attributes["FontFamily"] = $"({glyphTypeface.FamilyNames[CultureInfo.GetCultureInfo("en-us")]})"; Attributes["FontWeight"] = $"{glyphTypeface.Weight.ToOpenTypeWeight()}"; Attributes["FontStretch"] = $"/{glyphTypeface.Stretch}"; // Set attributes var flags = glyphTypeface.Symbol ? Flags.Symbolic : Flags.Nonsymbolic; if (glyphTypeface.Style == FontStyles.Italic) { flags = flags.Added(Flags.Italic); } Attributes["Flags"] = (int)flags; Attributes["FontBBox"] = new[] { // Left -(glyphs.Max(glyph => glyphTypeface.LeftSideBearings[glyph]) * 1000.0).ToRoundedInt(), // Bottom -(glyphs.Max(glyph => glyphTypeface.DistancesFromHorizontalBaselineToBlackBoxBottom[glyph]) * 1000.0).ToRoundedInt(), // Right (glyphs.Max(glyph => (glyphTypeface.AdvanceWidths[glyph] + glyphTypeface.RightSideBearings[glyph]) * 1000.0)).ToRoundedInt(), // Top (glyphTypeface.Baseline * 1000.0).ToRoundedInt() // Top }; Attributes["ItalicAngle"] = glyphTypeface.Style == FontStyles.Italic ? -75 : 0; Attributes["Ascent"] = (glyphTypeface.Baseline * 1000.0).ToRoundedInt(); Attributes["Descent"] = ((glyphTypeface.Baseline - glyphTypeface.Height) * 1000.0).ToRoundedInt(); Attributes["CapHeight"] = (glyphTypeface.CapsHeight * 1000.0).ToRoundedInt(); Attributes["XHeight"] = (glyphTypeface.XHeight * 1000.0).ToRoundedInt(); // StemV seems to be not used. // https://stackoverflow.com/questions/35485179/stemv-value-of-the-truetype-font Attributes["StemV"] = 80; Attributes["StemH"] = 80; return(true); }
private byte[] CreateSubsetData(GlyphTypeface glyphTypeface, IDictionary <int, ushort> cmap) { CheckEmbeddingRights(glyphTypeface); List <ushort> glyphIndices = new List <ushort>(subsetCharacters.Count); foreach (char ch in subsetCharacters) { ushort gid; if (cmap.TryGetValue((int)ch, out gid)) { glyphIndices.Add(gid); } } return(glyphTypeface.ComputeSubset(glyphIndices)); }
public void CreateSubSet(string sourceText, Uri fontURI) { GlyphTypeface glyphTypeface = new GlyphTypeface(fontURI); System.Collections.Generic.ICollection <ushort> Index = null; Index = new System.Collections.Generic.List <ushort>(); Encoding e = Encoding.Unicode; byte[] sourceTextBytes = e.GetBytes(sourceText); char[] sourceTextChars = e.GetChars(sourceTextBytes); int sourceTextCharVal = 0; int glyphIndex = 0; for (int sourceTextCharPos = 0; sourceTextCharPos <= sourceTextChars.GetUpperBound(0); sourceTextCharPos++) { try { sourceTextCharVal = Convert.ToInt32(sourceTextChars[sourceTextCharPos]); glyphIndex = glyphTypeface.CharacterToGlyphMap[sourceTextCharVal]; Index.Add((ushort)glyphIndex); } catch { // character not in CharacterToGlyphMap! } } byte[] filebytes = glyphTypeface.ComputeSubset(Index); String fontPath = fontURI.AbsolutePath; int start = fontPath.LastIndexOf("/") + 1; int end = fontPath.LastIndexOf("."); int length = end - start; MessageBox.Show(fontPath.Substring(end)); using (FileStream fileStream = new FileStream("D:\\Users\\Henry\\Desktop\\Kleymissky_0283-sub.otf", FileMode.Create)) { fileStream.Write(filebytes, 0, filebytes.Length); } }