Exemplo n.º 1
0
 public TrueTypeFont(string filename, int fontSize)
 {
     _fontFace   = new SharpFont.Face(_library, filename);
     fontSize    = Math.Max(fontSize, 10);
     fontSize    = Math.Min(1024 / 16, fontSize);
     _lineHeight = 0;
     SetFontSize(fontSize);
 }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Version string: " + HB.VersionString);
            Version v = HB.Version;

            Console.WriteLine("Version: " + v.Major + "." + v.Minor + "." + v.Build);
            Console.WriteLine("VersionCheck: " + HB.VersionAtLeast(v));

            var lib  = new Library();
            var face = new SharpFont.Face(lib, @"/usr/share/fonts/noto/NotoSans-Regular.ttf");

            face.SetCharSize(0, 50, 72, 72);

            var font = HarfBuzz.Font.FromFTFace(face);
            var buf  = new HarfBuzz.Buffer();

            buf.Direction = Direction.RightToLeft;
            buf.Script    = Script.Arabic;
            buf.AddText("متن");
            font.Shape(buf);

            var glyphInfos     = buf.GlyphInfo();
            var glyphPositions = buf.GlyphPositions();

            int height = (face.MaxAdvanceHeight - face.Descender) >> 6;
            int width  = 0;

            for (int i = 0; i < glyphInfos.Length; ++i)
            {
                width += glyphPositions[i].xAdvance >> 6;
            }

            Bitmap   bmp = new Bitmap(width, height);
            Graphics g   = Graphics.FromImage(bmp);

            g.Clear(Color.Gray);

            int penX = 0, penY = face.MaxAdvanceHeight >> 6;

            //draw the string
            for (int i = 0; i < glyphInfos.Length; ++i)
            {
                face.LoadGlyph(glyphInfos[i].codepoint, LoadFlags.Default, LoadTarget.Normal);
                face.Glyph.RenderGlyph(RenderMode.Normal);

                Bitmap cBmp = face.Glyph.Bitmap.ToGdipBitmap(Color.Firebrick);
                g.DrawImageUnscaled(cBmp,
                                    penX + (glyphPositions[i].xOffset >> 6) + face.Glyph.BitmapLeft,
                                    penY - (glyphPositions[i].yOffset >> 6) - face.Glyph.BitmapTop);

                penX += glyphPositions[i].xAdvance >> 6;
                penY -= glyphPositions[i].yAdvance >> 6;
            }

            g.Dispose();

            bmp.Save("output.bmp");
        }
Exemplo n.º 3
0
        private bool LoadFontFile(string path)
        {
            AppSettings.fontFamilies.Clear();
            SharpFont.Face face = new SharpFont.Face(ftLib, path, -1);
            //Console.WriteLine("Count {0}, Index {1}, Family {2}", face.FaceCount, face.FaceIndex, face.FamilyName);
            var faceCount = face.FaceCount;

            if (faceCount > 1)
            {
                for (var i = 0; i < faceCount; i++)
                {
                    face = new SharpFont.Face(ftLib, path, i);
                    AppSettings.fontFamilies.Add(face.FamilyName);
                    Console.WriteLine("Index {0}, Family {1}", face.FaceIndex, face.FamilyName);
                }
            }
            else
            {
                AppSettings.fontFamilies.Add(face.FamilyName);
                Console.WriteLine("Index {0}, Family {1}", face.FaceIndex, face.FamilyName);
            }
            return(true);
        }
Exemplo n.º 4
0
 public Font(SharpFont.Face fontFace, float size = 12f)
 {
     RenderMap        = new FontFaceRenderMap(fontFace, size);
     ShaderParameters = RenderMap.CreateShaderParameters();
     Size             = size;
 }
Exemplo n.º 5
0
 private bool LoadFontFamily(int index)
 {
     ftFace = new SharpFont.Face(ftLib, appSettings.FontFile, index);
     return(true);
 }
Exemplo n.º 6
0
        static FontDef ToFontDef(string fileName, ZipFile archive, out string name)
        {
            JsonData json;
            using (MemoryStream ms = new MemoryStream()) {
                archive[fileName].Extract(ms);
                json = JsonMapper.ToObject(Encoding.UTF8.GetString(ms.ToArray()));
            }

            string fontFile = (string)json["file"];
            int size = (int)json["size"];

            FontDef font;
            // find font file
            using (MemoryStream ms = new MemoryStream()) {
                archive["fonts/" + fontFile].Extract(ms);
                SharpFont.Face face = new SharpFont.Face(fontLibrary, ms.ToArray(), 0);
                font = new FontDef(face, size);
            }

            name = fileName.Substring("fonts/".Length);
            name = name.Substring(0, name.Length - 5);
            name = name.Replace('/', '.');
            return font;
        }