/// <summary> /// 用一个纹理绘制指定范围内的所有可见字符 /// </summary> /// <param name="ttfFullname"></param> /// <param name="fontHeight">此值越大,绘制文字的清晰度越高,但占用的纹理资源就越多。</param> /// <param name="firstChar"></param> /// <param name="lastChar"></param> /// <param name="maxTextureWidth">生成的纹理的最大宽度。</param> /// <returns></returns> public static IEnumerable <TTFTextureYeildingState> GetTTFTexture( string ttfFullname, int fontHeight, int maxTextureWidth, char firstChar, char lastChar) { FreeTypeLibrary library = new FreeTypeLibrary(); FreeTypeFace face = new FreeTypeFace(library, ttfFullname); FullDictionary <char, CharacterInfo> charInfoDict = null; int textureWidth = 0, textureHeight = 0; System.Drawing.Bitmap bigBitmap = null; foreach (var item in GetTextureBlueprint(face, fontHeight, maxTextureWidth, firstChar, lastChar)) { charInfoDict = item.dict; textureWidth = item.textureWidth; textureHeight = item.textureHeight; yield return(item); } if (textureWidth == 0) { textureWidth = 1; } if (textureHeight == 0) { textureHeight = 1; } foreach (var item in GetBigBitmap(face, fontHeight, maxTextureWidth, firstChar, lastChar, charInfoDict, textureWidth, textureHeight)) { bigBitmap = item.bigBitmap; yield return(item); } face.Dispose(); library.Dispose(); var result = new FontTexture() { TtfFullname = ttfFullname, FontHeight = fontHeight, FirstChar = firstChar, LastChar = lastChar, BigBitmap = bigBitmap, CharInfoDict = charInfoDict, }; FileInfo fileInfo = new FileInfo(ttfFullname); yield return(new TTFTextureYeildingState() { percent = 100, ttfTexture = result, message = string.Format("got font texture for {0}", fileInfo.Name), }); }
public static FontResource Load(Stream stream, IEnumerable<char> targets, int pixelSize) { InitStandardWidths(); int count = targets.Count(); int maxWidth = GetMaxWidth(pixelSize, count); var dict = new FullDictionary<char, CharacterInfo>(CharacterInfo.Default); Bitmap finalBitmap; using (var bitmap = new Bitmap(maxWidth, maxWidth, PixelFormat.Format24bppRgb)) { int currentX = 0, currentY = 0; using (Graphics graphics = Graphics.FromImage(bitmap)) { var typeface = new FontFace(stream); foreach (char c in targets) { BlitCharacter(pixelSize, maxWidth, dict, ref currentX, ref currentY, graphics, typeface, c); } } finalBitmap = ShortenBitmap(bitmap, maxWidth, currentY + yInterval + pixelSize + (pixelSize / 10 > 1 ? pixelSize / 10 : 1)); } var fontResource = new FontResource(); fontResource.FontHeight = pixelSize + yInterval; fontResource.CharInfoDict = dict; fontResource.InitTexture(finalBitmap); finalBitmap.Dispose(); return fontResource; }
public static XElement ToXElement(this FullDictionary <char, CharacterInfo> dict) { XElement result = new XElement(strCharacterInfoDict, from item in dict select KeyValuePairHelper.ToXElement(item)); return(result); }
private static void BlitCharacter(int pixelSize, int maxWidth, FullDictionary<char, CharacterInfo> dict, ref int currentX, ref int currentY, Graphics g, FontFace typeface, char c) { if (c == ' ' || c == '\t') { int width = (c == ' ') ? pixelSize / 2 : pixelSize * 4; if (currentX + xInterval + width >= maxWidth) { currentX = 0; currentY += yInterval + pixelSize; if (currentY + yInterval + pixelSize >= maxWidth) { throw new Exception("Texture Size not big enough for required characters."); } } Bitmap glyphBitmap = new Bitmap(width + xInterval, pixelSize + yInterval); //float yoffset = pixelSize * 3 / 4 - glyph.HorizontalMetrics.Bearing.Y; g.DrawImage(glyphBitmap, currentX + xInterval, currentY + yInterval); CharacterInfo info = new CharacterInfo(currentX, currentY, width + xInterval, pixelSize + yInterval); dict.Add(c, info); glyphBitmap.Dispose(); currentX += width; } else { Surface surface; Glyph glyph; if (RenderGlyph(typeface, c, pixelSize, out surface, out glyph)) { if (currentX + xInterval + surface.Width >= maxWidth) { currentX = 0; currentY += yInterval + pixelSize; if (currentY + yInterval + pixelSize >= maxWidth) { throw new Exception("Texture Size not big enough for required characters."); } } Bitmap glyphBitmap = GetGlyphBitmap(surface); const int a = 5; const int b = 8; //float yoffset = pixelSize * a / b - glyph.HorizontalMetrics.Bearing.Y; float skyHeight = yInterval + pixelSize * a / b - glyph.HorizontalMetrics.Bearing.Y; if (skyHeight < 0) { skyHeight = 0; } if (skyHeight < 0) { skyHeight = 0; } else if (skyHeight + glyphBitmap.Height > yInterval + pixelSize) { skyHeight -= glyphBitmap.Height - (yInterval + pixelSize); } g.DrawImage(glyphBitmap, currentX + xInterval, currentY + skyHeight, glyphBitmap.Width, glyphBitmap.Height); #if DEBUG g.DrawRectangle(greenPen, currentX, currentY, glyphBitmap.Width + xInterval, yInterval + pixelSize - 1); g.DrawRectangle(redPen, currentX + xInterval, currentY + skyHeight, glyphBitmap.Width, glyphBitmap.Height); g.DrawLine(bluePen, currentX, currentY + yInterval + pixelSize * a / b, currentX + glyphBitmap.Width, currentY + yInterval + pixelSize * a / b); #endif CharacterInfo info = new CharacterInfo(currentX, currentY, glyphBitmap.Width + xInterval, yInterval + pixelSize - 1); dict.Add(c, info); glyphBitmap.Dispose(); currentX += xInterval + surface.Width; } surface.Dispose(); } }
/// <summary> /// Save the dictionary in file /// </summary> /// <param name="dict"></param> public static void WriteFile(SortedDictionary <string, string> dict) { //Convert dictionary back to FullDictionary FullDictionary fd = new FullDictionary(); foreach (var item in dict) { fd.fullDict.Add(new DictionaryEntry(item.Key, item.Value)); } //convert FullDictionary to json and write in file string json = JsonUtility.ToJson(fd); File.WriteAllText(Path.Combine(Application.persistentDataPath, filename), json); }
public static FullDictionary <char, CharacterInfo> Parse(XElement xElement) { FullDictionary <char, CharacterInfo> result = new FullDictionary <char, CharacterInfo>( new CharacterInfo() { width = 0, height = 0, xoffset = 0, yoffset = 0, }); foreach (var item in xElement.Elements(KeyValuePairHelper.strKeyValuePair)) { KeyValuePair <char, CharacterInfo> pair = KeyValuePairHelper.Parse(item); result.Add(pair.Key, pair.Value); } return(result); }
/// <summary> /// Read list from Json and create a SortedDictionary of entries /// </summary> /// <returns>Returns processed sorted dictionary</returns> public static SortedDictionary <string, string> ImportDictionary() { SortedDictionary <string, string> importedDictionary = new SortedDictionary <string, string>(); FullDictionary fd = new FullDictionary(); //read the json file and convert it to FullDictionary if (File.Exists(Path.Combine(Application.persistentDataPath, filename))) { string json = File.ReadAllText(Path.Combine(Application.persistentDataPath, filename)); fd = JsonUtility.FromJson <FullDictionary>(json); } //convert FullDictionary to SortedDictionary<string,string> foreach (var item in fd.fullDict) { importedDictionary.Add(item.Name, item.Description); } //return imported dicitionary return(importedDictionary); }
/// <summary> /// 根据<paramref name="firstChar"/>等信息获取要制作的贴图的宽高和各个字形的位置信息。 /// </summary> /// <param name="face"></param> /// <param name="fontHeight"></param> /// <param name="firstChar"></param> /// <param name="lastChar"></param> /// <param name="maxTextureWidth"></param> /// <param name="charInfoDict"></param> /// <param name="textureWidth"></param> /// <param name="textureHeight"></param> private static void GetTextureBlueprint(FreeTypeFace face, int fontHeight, int maxTextureWidth, char firstChar, char lastChar, out FullDictionary <char, CharacterInfo> charInfoDict, out int textureWidth, out int textureHeight) { charInfoDict = new FullDictionary <char, CharacterInfo>(CharacterInfo.Default); textureWidth = 0; textureHeight = fontHeight; int glyphXOffset = 0; int glyphYOffset = 0; for (char c = firstChar; c <= lastChar; c++) { FreeTypeBitmapGlyph glyph = new FreeTypeBitmapGlyph(face, c, fontHeight); bool zeroSize = (glyph.obj.bitmap.rows == 0 && glyph.obj.bitmap.width == 0); { bool zeroBuffer = glyph.obj.bitmap.buffer == IntPtr.Zero; if (zeroSize && (!zeroBuffer)) { throw new Exception(); } if ((!zeroSize) && zeroBuffer) { throw new Exception(); } } int glyphWidth = glyph.obj.bitmap.width; int glyphHeight = glyph.obj.bitmap.rows; if (c == ' ' && zeroSize)// 空格需要特殊处理 { zeroSize = false; FreeTypeBitmapGlyph tabGlyph = new FreeTypeBitmapGlyph(face, '\t', fontHeight); glyphWidth = tabGlyph.obj.bitmap.width / 8; if (glyphWidth < 1) { glyphWidth = fontHeight / 2; } glyphHeight = tabGlyph.obj.bitmap.rows; //if (glyphHeight < 1) { glyphHeight = 1; } } else if (c == '\t' && zeroSize)// tab可能需要特殊处理 { zeroSize = false; glyphWidth = glyph.obj.bitmap.width; if (glyphWidth < 1) { glyphWidth = fontHeight * 2; } glyphHeight = glyph.obj.bitmap.rows; //if (glyphHeight < 1) { glyphHeight = 1; } } if (!zeroSize) { if (glyphXOffset + glyphWidth + 1 <= maxTextureWidth) { textureWidth = Math.Max(textureWidth, glyphXOffset + glyphWidth + 1); } else// 此字形将超出最大宽度,所以要换行。 { textureHeight += fontHeight; glyphXOffset = 0; glyphYOffset = textureHeight - fontHeight; } CharacterInfo cInfo = new CharacterInfo(); cInfo.xoffset = glyphXOffset; cInfo.yoffset = glyphYOffset; cInfo.width = glyphWidth; cInfo.height = glyphHeight; charInfoDict.Add(c, cInfo); glyphXOffset += glyphWidth + 1; } if (c == char.MaxValue) { break; } } }
/// <summary> /// 根据<paramref name="charInfoDict"/>等信息把各个字形写入一个大的位图并返回之。 /// </summary> /// <param name="face"></param> /// <param name="fontHeight"></param> /// <param name="firstChar"></param> /// <param name="lastChar"></param> /// <param name="maxTextureWidth"></param> /// <param name="charInfoDict"></param> /// <param name="widthOfTexture"></param> /// <param name="heightOfTexture"></param> /// <returns></returns> private static System.Drawing.Bitmap GetBigBitmap(FreeTypeFace face, int fontHeight, int maxTextureWidth, char firstChar, char lastChar, FullDictionary <char, CharacterInfo> charInfoDict, int widthOfTexture, int heightOfTexture) { System.Drawing.Bitmap bigBitmap = new System.Drawing.Bitmap(widthOfTexture, heightOfTexture); Graphics graphics = Graphics.FromImage(bigBitmap); for (char c = firstChar; c <= lastChar; c++) { FreeTypeBitmapGlyph glyph = new FreeTypeBitmapGlyph(face, c, fontHeight); int size = glyph.obj.bitmap.width * glyph.obj.bitmap.rows; { bool zeroBuffer = glyph.obj.bitmap.buffer == IntPtr.Zero; if ((size == 0) && (!zeroBuffer)) { throw new Exception(string.Format("glyph size({0}) for non zero buffer({1})", 0, glyph.obj.bitmap.buffer)); } if ((size != 0) && zeroBuffer) { throw new Exception(string.Format("glyph size({0}) for zero buffer({1})", size, glyph.obj.bitmap.buffer)); } } byte[] byteBitmap = null; if ((c == ' ') && (size == 0)) { size = charInfoDict[' '].width * charInfoDict[' '].height; byteBitmap = new byte[size]; } else if ((c == '\t') && (size == 0)) { size = charInfoDict['\t'].width * charInfoDict['\t'].height; byteBitmap = new byte[size]; } else if (size != 0) { byteBitmap = new byte[size]; Marshal.Copy(glyph.obj.bitmap.buffer, byteBitmap, 0, byteBitmap.Length); } if (size != 0) { CharacterInfo cInfo; if (charInfoDict.TryGetValue(c, out cInfo)) { if (cInfo.width > 0 && cInfo.height > 0) { System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format32bppRgb; System.Drawing.Imaging.ImageLockMode lockMode = System.Drawing.Imaging.ImageLockMode.WriteOnly; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(cInfo.width, cInfo.height, format); Rectangle bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height); System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(bitmapRect, lockMode, format); int length = Math.Abs(bmpData.Stride) * bitmap.Height; byte[] bitmapBytes = new byte[length]; for (int row = 0; row < cInfo.height; row++) { for (int col = 0; col < cInfo.width; col++) { byte color = byteBitmap[row * cInfo.width + col]; bitmapBytes[row * bmpData.Stride + col * 4 + 0] = color; bitmapBytes[row * bmpData.Stride + col * 4 + 1] = color; bitmapBytes[row * bmpData.Stride + col * 4 + 2] = color; //bitmapBytes[row * bmpData.Stride + col * 4 + 3] = color; } } System.Runtime.InteropServices.Marshal.Copy(bitmapBytes, 0, bmpData.Scan0, length); bitmap.UnlockBits(bmpData); //int baseLine = fontHeight * 3 / 4 + 4; //graphics.DrawImage(bitmap, cInfo.xoffset, cInfo.yoffset + baseLine - glyph.obj.top); int skyHeight = fontHeight * 3 / 4 - glyph.obj.top; if (skyHeight < 0) { skyHeight = 0; } graphics.DrawImage(bitmap, cInfo.xoffset, cInfo.yoffset + skyHeight); } } else { throw new Exception(string.Format("Not support for display the char [{0}]", c)); } } if (c == char.MaxValue) { break; } } graphics.Dispose(); return(bigBitmap); }
/// <summary> /// 根据<paramref name="firstChar"/>等信息获取要制作的贴图的宽高和各个字形的位置信息。 /// </summary> /// <param name="face"></param> /// <param name="fontHeight"></param> /// <param name="firstChar"></param> /// <param name="lastChar"></param> /// <param name="maxTextureWidth"></param> /// <param name="charInfoDict"></param> /// <param name="textureWidth"></param> /// <param name="textureHeight"></param> private static IEnumerable <TTFTextureYeildingState> GetTextureBlueprint( FreeTypeFace face, int fontHeight, int maxTextureWidth, char firstChar, char lastChar) { int count = lastChar - firstChar; int index = 0; var charInfoDict = new FullDictionary <char, CharacterInfo>(CharacterInfo.Default); int textureWidth = 0; int textureHeight = fontHeight; int glyphXOffset = 0; int glyphYOffset = 0; for (char c = firstChar; c <= lastChar; c++) { FreeTypeBitmapGlyph glyph = new FreeTypeBitmapGlyph(face, c, fontHeight); { var rect = glyph.glyphRec; var glyphChar = glyph.glyphChar; var left = glyph.obj.left; var top = glyph.obj.top; var x = glyph.obj.root.advance.x >> 6; var y = glyph.obj.root.advance.y >> 6; var clazz = glyph.obj.root.clazz; var format = glyph.obj.root.format; var library = glyph.obj.root.library; var num_grays = glyph.obj.bitmap.num_grays; var palette = glyph.obj.bitmap.palette; var palette_mode = glyph.obj.bitmap.palette_mode; var pitch = glyph.obj.bitmap.pitch; var pixel_mode = glyph.obj.bitmap.pixel_mode; } bool zeroSize = (glyph.obj.bitmap.rows == 0 && glyph.obj.bitmap.width == 0); { bool zeroBuffer = glyph.obj.bitmap.buffer == IntPtr.Zero; if (zeroSize && (!zeroBuffer)) { throw new Exception(); } if ((!zeroSize) && zeroBuffer) { throw new Exception(); } } int glyphWidth = glyph.obj.bitmap.width; int glyphHeight = glyph.obj.bitmap.rows; if (c == ' ' && zeroSize)// 空格需要特殊处理 { zeroSize = false; FreeTypeBitmapGlyph tabGlyph = new FreeTypeBitmapGlyph(face, ' ', fontHeight); glyphWidth = tabGlyph.obj.bitmap.width / 8; if (glyphWidth < 1) { glyphWidth = fontHeight / 3; } glyphHeight = tabGlyph.obj.bitmap.rows; if (glyphHeight < 1) { glyphHeight = 1; } } else if (c == '\t' && zeroSize)// tab可能需要特殊处理 { zeroSize = false; glyphWidth = glyph.obj.bitmap.width; if (glyphWidth < 1) { glyphWidth = fontHeight * 2; } glyphHeight = glyph.obj.bitmap.rows; if (glyphHeight < 1) { glyphHeight = 1; } } if (!zeroSize) { if (glyphXOffset + glyphWidth + 1 <= maxTextureWidth) { textureWidth = Math.Max(textureWidth, glyphXOffset + glyphWidth + 1); } else// 此字形将超出最大宽度,所以要换行。 { textureHeight += fontHeight; glyphXOffset = 0; glyphYOffset = textureHeight - fontHeight; } CharacterInfo cInfo = new CharacterInfo(); cInfo.xoffset = glyphXOffset; cInfo.yoffset = glyphYOffset; cInfo.width = glyphWidth; cInfo.height = glyphHeight; charInfoDict.Add(c, cInfo); glyphXOffset += glyphWidth + 1; } if (c == char.MaxValue) { break; } yield return(new TTFTextureYeildingState() { percent = index++ *100 / count, //message = "generating blueprint", message = "blueprint", }); } yield return(new TTFTextureYeildingState() { percent = index++ *100 / count, dict = charInfoDict, textureWidth = textureWidth, textureHeight = textureHeight, message = "blueprint done", }); }
public Server(WebServerSettings settings) { Settings = settings; WebServiceGroups = new FullDictionary <ServerStage, WebServiceGroup>((k) => new WebServiceGroup(k)); WebServiceGroups.FullEnumKeys(); }
internal FontBitmap() { this.GlyphInfoDictionary = new FullDictionary<char, GlyphInfo>(GlyphInfo.Default); }