Advertisement
TKGP

ccm

Nov 11th, 2020
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1.     private static void ImportFont()
  2.         {
  3.             string ccmPath = $@"{ptde}\font\dbgfont14h.ccm";
  4.             var ccm = CCM.Read(SFUtil.Backup(ccmPath));
  5.             ccm.TexCount = 1;
  6.             ccm.TexWidth = 1024;
  7.             ccm.TexHeight = 1024;
  8.             ccm.FullWidth = 32;
  9.             ccm.Glyphs.Clear();
  10.  
  11.             var face = new Typeface(new FontFamily("Comic Sans MS"), FontStyles.Normal, FontWeights.ExtraBlack, FontStretches.Normal);
  12.             GlyphTypeface font;
  13.  
  14.             if (!face.TryGetGlyphTypeface(out font))
  15.                 return;
  16.  
  17.             int ColumnCount = 32;
  18.             double fontSize = ccm.FullWidth;
  19.             // the height of each cell has to include over/underhanging glyphs
  20.             Size cellSize = new Size(fontSize, fontSize * font.Height);
  21.  
  22.             // now create the visual we'll draw them to
  23.             var viz = new DrawingVisual();
  24.             int drawCount = -1;
  25.             using (DrawingContext dc = viz.RenderOpen())
  26.             {
  27.                 foreach (int code in font.CharacterToGlyphMap.Keys)
  28.                 {
  29.                     Geometry g = font.GetGlyphOutline(font.CharacterToGlyphMap[code], fontSize, 1);
  30.                     if (g.IsEmpty())
  31.                         continue; // don't draw the blank ones
  32.                     drawCount++;
  33.  
  34.                     var uv1 = new Vector2(((drawCount % ColumnCount) * (float)cellSize.Width) / ccm.TexWidth,
  35.                         ((drawCount / ColumnCount) * (float)cellSize.Height) / ccm.TexHeight);
  36.                     var uv2 = new Vector2(uv1.X + (float)cellSize.Width / ccm.TexWidth, uv1.Y + (float)cellSize.Height / ccm.TexHeight);
  37.                     var cg = new CCM.Glyph(uv1, uv2, 0, ccm.FullWidth, ccm.FullWidth, 0);
  38.                     ccm.Glyphs[code] = cg;
  39.  
  40.                     // center horizontally in the cell
  41.                     double xOffset = (drawCount % ColumnCount) * cellSize.Width + cellSize.Width / 2d - g.Bounds.Width / 2d;
  42.                     // place the character on the baseline of the cell
  43.                     double yOffset = (drawCount / ColumnCount) * cellSize.Height + fontSize * font.Baseline;
  44.                     dc.PushTransform(new TranslateTransform(xOffset, yOffset));
  45.                     dc.DrawGeometry(Brushes.White, new Pen(Brushes.Black, 2), g);
  46.                     dc.Pop(); // get rid of the transform
  47.                 }
  48.             }
  49.  
  50.             int RowCount = drawCount / ColumnCount;
  51.             if (drawCount % ColumnCount != 0)
  52.                 RowCount++; // to include partial rows
  53.             int bitWidth = (int)Math.Ceiling(cellSize.Width * ColumnCount);
  54.             int bitHeight = (int)Math.Ceiling(cellSize.Height * RowCount);
  55.             var bmp = new RenderTargetBitmap(bitWidth, bitHeight, 96, 96, PixelFormats.Pbgra32);
  56.             bmp.Render(viz);
  57.  
  58.             var encoder = new PngBitmapEncoder();
  59.             encoder.Frames.Add(BitmapFrame.Create(bmp));
  60.             using (FileStream fs = File.Create("FontTable.png"))
  61.                 encoder.Save(fs);
  62.  
  63.             ccm.Write(ccmPath);
  64.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement