Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void ImportFont()
- {
- string ccmPath = $@"{ptde}\font\dbgfont14h.ccm";
- var ccm = CCM.Read(SFUtil.Backup(ccmPath));
- ccm.TexCount = 1;
- ccm.TexWidth = 1024;
- ccm.TexHeight = 1024;
- ccm.FullWidth = 32;
- ccm.Glyphs.Clear();
- var face = new Typeface(new FontFamily("Comic Sans MS"), FontStyles.Normal, FontWeights.ExtraBlack, FontStretches.Normal);
- GlyphTypeface font;
- if (!face.TryGetGlyphTypeface(out font))
- return;
- int ColumnCount = 32;
- double fontSize = ccm.FullWidth;
- // the height of each cell has to include over/underhanging glyphs
- Size cellSize = new Size(fontSize, fontSize * font.Height);
- // now create the visual we'll draw them to
- var viz = new DrawingVisual();
- int drawCount = -1;
- using (DrawingContext dc = viz.RenderOpen())
- {
- foreach (int code in font.CharacterToGlyphMap.Keys)
- {
- Geometry g = font.GetGlyphOutline(font.CharacterToGlyphMap[code], fontSize, 1);
- if (g.IsEmpty())
- continue; // don't draw the blank ones
- drawCount++;
- var uv1 = new Vector2(((drawCount % ColumnCount) * (float)cellSize.Width) / ccm.TexWidth,
- ((drawCount / ColumnCount) * (float)cellSize.Height) / ccm.TexHeight);
- var uv2 = new Vector2(uv1.X + (float)cellSize.Width / ccm.TexWidth, uv1.Y + (float)cellSize.Height / ccm.TexHeight);
- var cg = new CCM.Glyph(uv1, uv2, 0, ccm.FullWidth, ccm.FullWidth, 0);
- ccm.Glyphs[code] = cg;
- // center horizontally in the cell
- double xOffset = (drawCount % ColumnCount) * cellSize.Width + cellSize.Width / 2d - g.Bounds.Width / 2d;
- // place the character on the baseline of the cell
- double yOffset = (drawCount / ColumnCount) * cellSize.Height + fontSize * font.Baseline;
- dc.PushTransform(new TranslateTransform(xOffset, yOffset));
- dc.DrawGeometry(Brushes.White, new Pen(Brushes.Black, 2), g);
- dc.Pop(); // get rid of the transform
- }
- }
- int RowCount = drawCount / ColumnCount;
- if (drawCount % ColumnCount != 0)
- RowCount++; // to include partial rows
- int bitWidth = (int)Math.Ceiling(cellSize.Width * ColumnCount);
- int bitHeight = (int)Math.Ceiling(cellSize.Height * RowCount);
- var bmp = new RenderTargetBitmap(bitWidth, bitHeight, 96, 96, PixelFormats.Pbgra32);
- bmp.Render(viz);
- var encoder = new PngBitmapEncoder();
- encoder.Frames.Add(BitmapFrame.Create(bmp));
- using (FileStream fs = File.Create("FontTable.png"))
- encoder.Save(fs);
- ccm.Write(ccmPath);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement