예제 #1
0
        void ReadAndRender(string fontfile)
        {
            if (string.IsNullOrEmpty(this.txtInputChar.Text))
            {
                p.Clear(PixelFarm.Drawing.Color.White);
                return;
            }
            var  reader     = new OpenFontReader();
            char testChar   = txtInputChar.Text[0];//only 1 char
            int  resolution = 96;
            //1. read typeface from font file

            RenderChoice renderChoice = (RenderChoice)this.cmbRenderChoices.SelectedItem;

            switch (renderChoice)
            {
            case RenderChoice.RenderWithMiniAgg:
            {
                using (var fs = new FileStream(fontfile, FileMode.Open))
                {
                    Typeface typeFace = reader.Read(fs);
                    RenderWithMiniAgg(typeFace, testChar, fontSizeInPoint);
                }
            }
            break;

            case RenderChoice.RenderWithGdiPlusPath:
            {
                using (var fs = new FileStream(fontfile, FileMode.Open))
                {
                    Typeface typeFace = reader.Read(fs);
                    RenderWithGdiPlusPath(typeFace, testChar, fontSizeInPoint, resolution);
                }
            }
            break;

            case RenderChoice.RenderWithTextPrinterAndMiniAgg:
            {
                RenderWithTextPrinterAndMiniAgg(fontfile, this.txtInputChar.Text, fontSizeInPoint, resolution);
            }
            break;

            case RenderChoice.RenderWithMsdfGen:
            case RenderChoice.RenderWithSdfGen:
            {
                using (var fs = new FileStream(fontfile, FileMode.Open))
                {
                    Typeface typeFace = reader.Read(fs);
                    RenderWithMsdfImg(typeFace, testChar, fontSizeInPoint);
                }
            }
            break;

            default:
                throw new NotSupportedException();
            }
        }
예제 #2
0
        static Fonts()
        {
            var reader            = new OpenFontReader();
            var latinMathTypeface = reader.Read(new MemoryStream(Resources.LatinModernMath, false));

            latinMathTypeface.UpdateAllCffGlyphBounds();
            GlobalTypefaces = new Typefaces(latinMathTypeface);

            var amsBlackboardBoldTypeface = reader.Read(new MemoryStream(Resources.AMSCapitalBlackboardBold, false));

            amsBlackboardBoldTypeface.UpdateAllCffGlyphBounds();
            GlobalTypefaces.AddStart(amsBlackboardBoldTypeface);
        }
예제 #3
0
        public FormTess()
        {
            InitializeComponent();

            rdoSimpleIncCurveFlattener.Checked         = true;
            rdoSimpleIncCurveFlattener.CheckedChanged += (s, e) => UpdateOutput();
            //
            rdoSubdivCureveFlattener.CheckedChanged += (s, e) => UpdateOutput();

            textBox1.KeyUp += (s, e) => UpdateOutput();


            rdoTessPoly2Tri.CheckedChanged        += (s, e) => UpdateOutput();
            rdoTessSGI.CheckedChanged             += (s, e) => UpdateOutput();
            chkShowContourAnalysis.CheckedChanged += (s, e) => UpdateOutput();

            txtIncrementalTessStep.KeyUp      += (s, e) => UpdateOutput();
            txtDivCurveRecursiveLimit.KeyUp   += (s, e) => UpdateOutput();
            txtDivAngleTolerenceEpsilon.KeyUp += (s, e) => UpdateOutput();

            string testFont = "c:\\Windows\\Fonts\\Tahoma.ttf";

            using (FileStream fs = new FileStream(testFont, FileMode.Open, FileAccess.Read))
            {
                OpenFontReader reader = new OpenFontReader();
                _typeface = reader.Read(fs);
            }

            _tovxs            = new GlyphTranslatorToVxs();
            _glyphPathBuilder = new GlyphOutlineBuilder(_typeface);
            //
            _tessTool = new TessTool();
        }
예제 #4
0
        public ColorTypeface(string name)
        {
            m_gtf = GetGlyphTypeface(first_candidate: name);
            if (m_gtf == null)
            {
                return;
            }

            // Read the actual font data using Typography.OpenFont
            using (var s = m_gtf.GetFontStream())
            {
                var r = new OpenFontReader();
                m_openfont = r.Read(s, 0, ReadFlags.Full);
            }

            // Create a reusable layout for glyphs
            m_layout = new GlyphLayout()
            {
                Typeface = m_openfont,
                EnableBuiltinMathItalicCorrection = false, // not needed
                EnableComposition = true,
                EnableGpos        = true,
                EnableGsub        = true,
                EnableLigature    = true,
                PositionTechnique = PositionTechnique.OpenFont,
            };

            // Cache the glyph index for the zero-width joiner
            foreach (var g in StringToGlyphLayout("\u200d", use_gpos: false))
            {
                m_zwj_glyph = g.glyphIndex;
            }
        }
예제 #5
0
        static void CreateSampleMsdfTextureFont(
            string fontfile, float sizeInPoint,
            char[] chars, string outputFile)
        {
            //sample
            var reader = new OpenFontReader();

            using (var fs = new FileStream(fontfile, FileMode.Open))
            {
                //1. read typeface from font file
                Typeface typeface = reader.Read(fs);
                //sample: create sample msdf texture
                //-------------------------------------------------------------
                var builder = new GlyphPathBuilder(typeface);
                //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
                //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
                //-------------------------------------------------------------
                var atlasBuilder = new SimpleFontAtlasBuilder();

                MsdfGenParams msdfGenParams = new MsdfGenParams();

                int j = chars.Length;
                for (int i = 0; i < j; ++i)
                {
                    //build glyph
                    ushort gindex = typeface.LookupIndex(chars[i]);
                    builder.BuildFromGlyphIndex(gindex, -1);

                    var glyphToContour = new GlyphContourBuilder();
                    //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
                    builder.ReadShapes(glyphToContour);
                    msdfGenParams.shapeScale = 1f / 64;
                    GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, msdfGenParams);
                    atlasBuilder.AddGlyph(gindex, glyphImg);
                    int w = glyphImg.Width;
                    int h = glyphImg.Height;
                    using (Bitmap bmp = new Bitmap(glyphImg.Width, glyphImg.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    {
                        var   bmpdata   = bmp.LockBits(new System.Drawing.Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                        int[] imgBuffer = glyphImg.GetImageBuffer();
                        System.Runtime.InteropServices.Marshal.Copy(imgBuffer, 0, bmpdata.Scan0, imgBuffer.Length);
                        bmp.UnlockBits(bmpdata);
                        bmp.Save("d:\\WImageTest\\a001_xn2_" + (chars[i]) + ".png");
                    }
                }

                var glyphImg2 = atlasBuilder.BuildSingleImage();
                using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height),
                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                    int[] intBuffer = glyphImg2.GetImageBuffer();

                    System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length);
                    bmp.UnlockBits(bmpdata);
                    bmp.Save("d:\\WImageTest\\a_total.png");
                }
                atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml");
            }
        }
예제 #6
0
        public void SetCurrentFont(string fontname, InstalledFontStyle fontStyle, float fontSizeInPts, ScriptLang scLang = null)
        {
            InstalledFont installedFont = _hub._openFontStore.GetFont(fontname, fontStyle);

            if (installedFont == null)
            {
                return;                       //not found request font
            }
            if (scLang != null)
            {
                _glyphLayout.ScriptLang = scLang;
            }

            var key = new TextShapingContextKey(installedFont, _glyphLayout.ScriptLang);

            if (!_registerShapingContexts.TryGetValue(key, out _currentShapingContext))
            {
                //not found
                //the create the new one
                Typeface typeface;
                using (var fs = new System.IO.FileStream(installedFont.FontPath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    var reader = new OpenFontReader();
                    typeface = reader.Read(fs);
                }
                var shapingContext = new TextShapingContext(typeface, _glyphLayout.ScriptLang);
                //shaping context setup ...
                _registerShapingContexts.Add(key, shapingContext);
                _currentShapingContext = shapingContext;
            }
            _fontSizeInPts = fontSizeInPts;
        }
예제 #7
0
            public void GetGlyphPoints(string fontFile, char character)
            {
                Typeface typeFace;

                using (var fs = Utility.ReadFile(Utility.FontDir + fontFile))
                {
                    var reader = new OpenFontReader();
                    typeFace = reader.Read(fs);
                }

                Glyph glyph = typeFace.Lookup(character);

                o.WriteLine("GlyphPoints of '{0}':", character);
                var glyphPoints = glyph.GlyphPoints;

                foreach (var p in glyphPoints)
                {
                    o.WriteLine("{0}, {1}", p.X, p.Y);
                }

                o.WriteLine("EndPoints of '{0}':", character);
                var endPoints = glyph.EndPoints;

                foreach (var end in endPoints)
                {
                    o.WriteLine(end.ToString());
                }
            }
예제 #8
0
        public void ShowGlyphAsTextMesh_Builtin()
        {
            //load the glyph
            Typeface typeFace;

            using (var fs = Utility.ReadFile(Utility.FontDir + fontFileName))
            {
                var reader = new OpenFontReader();
                typeFace = reader.Read(fs);
            }

            Bounds boundingBox = typeFace.Bounds;
            short  ascender    = typeFace.Ascender;
            Glyph  glyph       = typeFace.Lookup(character);

            //read polygons and quadratic bezier segments
            GlyphLoader.Read(glyph, out var polygons, out var quadraticBezierSegments);

            //construct text mesh
            TextMesh textMesh         = new TextMesh();
            Color    polygonFillColor = Color.Argb(128, 10, 10, 10);

            textMesh.AddTriangles(polygons, polygonFillColor,
                                  new Vector(0, ascender), Vector.Zero, 1, false);
            textMesh.AddBezierSegments(quadraticBezierSegments, polygonFillColor,
                                       new Vector(0, ascender), Vector.Zero, 1, false);
            var command = textMesh.Commands[^ 1];
예제 #9
0
파일: Fonts.cs 프로젝트: m1090c/CSharpMath
        static Typefaces GetGlobalTypefaces()
        {
            var reader = new OpenFontReader();

            Typeface LoadFont(string fileName)
            {
                var typeface = reader.Read(
                    System.Reflection.Assembly.GetExecutingAssembly()
                    .GetManifestResourceStream($"CSharpMath.Rendering.Reference_Fonts.{fileName}")
                    );

                if (typeface == null)
                {
                    throw new Structures.InvalidCodePathException("Invalid predefined font!");
                }
                typeface.UpdateAllCffGlyphBounds();
                return(typeface);
            }

            var globalTypefaces = new Typefaces(LoadFont("latinmodern-math.otf"));

            globalTypefaces.AddOverride(LoadFont("AMS-Capital-Blackboard-Bold.otf"));
            globalTypefaces.AddSupplement(LoadFont("cyrillic-modern-nmr10.otf"));
            return(globalTypefaces);
        }
예제 #10
0
 public static void RestoreUp(this Typeface typeface, RestoreTicket ticket, OpenFontReader openFontReader, Stream fontStream)
 {
     if (typeface.IsTrimmed())
     {
         openFontReader.Read(typeface, ticket, fontStream);
     }
 }
예제 #11
0
        private void cmdShowFontAtlas_Click(object sender, EventArgs e)
        {
            if (_selectedAtlasItemSourceFile != null)
            {
                //check if this is a font file?
                Typeface selectedTypeface = null;
                switch (_selectedAtlasItemSourceFile.Extension)
                {
                case ".ttf":
                case ".otf":
                {
                    using (FileStream fs = new FileStream(_selectedAtlasItemSourceFile.AbsoluteFilename, FileMode.Open))
                    {
                        OpenFontReader reader = new OpenFontReader();
                        selectedTypeface = reader.Read(fs);
                    }
                }
                break;
                }

                if (selectedTypeface != null)
                {
                    SampleWinForms.FormFontAtlas formFontAtlas = new SampleWinForms.FormFontAtlas();
                    formFontAtlas.SetFont(selectedTypeface, 34);//load with init size
                    formFontAtlas.ShowDialog();
                }
            }
        }
예제 #12
0
        private void InitGlyphPointsAndContourEnds(char c)
        {
            try {
                using (FileStream fs = File.OpenRead(ttfPath)) {
                    OpenFontReader reader   = new OpenFontReader();
                    Typeface       typeface = reader.Read(fs);

                    var builder = new GlyphPathBuilder(typeface);
                    builder.BuildFromGlyphIndex(typeface.LookupIndex(c), 300);

                    var txToPath     = new GlyphTranslatorToPath();
                    var writablePath = new WritablePath();
                    txToPath.SetOutput(writablePath);
                    builder.ReadShapes(txToPath);

                    var     curveFlattener = new SimpleCurveFlattener();
                    float[] flattenPoints  = curveFlattener.Flatten(
                        writablePath._points, out _contourEnds);
                    _glyphPoints2 = flattenPoints;
                }
            }
            catch (Exception) {
                ClearGlyphData();
                pnlGlyph.Invalidate();
            }
        }
예제 #13
0
 public TypographyTest()
 {
     using (var fs = new FileStream(FontFile, FileMode.Open))
     {
         var fontReader = new OpenFontReader();
         typeFace = fontReader.Read(fs);
     }
 }
예제 #14
0
        static Fonts()
        {
            var bytes             = Resources.LatinModernMath;
            var reader            = new OpenFontReader();
            var latinMathTypeface = reader.Read(new MemoryStream(bytes, false));

            latinMathTypeface.UpdateAllCffGlyphBounds();
            GlobalTypefaces = new Typefaces(latinMathTypeface);
        }
예제 #15
0
        static SimpleFontAtlasBuilder CreateSampleMsdfTextureFont(string fontfile,
                                                                  float sizeInPoint,
                                                                  ushort startGlyphIndex, ushort endGlyphIndex)
        {
            //read type face from file
            Typeface typeface;

            using (var fs = new FileStream(fontfile, FileMode.Open, FileAccess.Read))
            {
                var reader = new OpenFontReader();
                //1. read typeface from font file
                typeface = reader.Read(fs);
            }
            //sample: create sample msdf texture
            //-------------------------------------------------------------
            var builder = new MyGlyphPathBuilder(typeface);
            //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
            //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
            //-------------------------------------------------------------
            var atlasBuilder = new SimpleFontAtlasBuilder();
            var msdfBuilder  = new MsdfGlyphGen();

            for (ushort n = startGlyphIndex; n <= endGlyphIndex; ++n)
            {
                //build glyph
                builder.BuildFromGlyphIndex(n, sizeInPoint);

                var msdfGlyphGen = new MsdfGlyphGen();
                var actualImg    = msdfGlyphGen.CreateMsdfImage(
                    builder.GetOutputPoints(),
                    builder.GetOutputContours(),
                    builder.GetPixelScale());
                atlasBuilder.AddGlyph((int)n, actualImg);

                //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                //{
                //    var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                //    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length);
                //    bmp.UnlockBits(bmpdata);
                //    bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png");
                //}
            }

            return(atlasBuilder);
            //var glyphImg2 = atlasBuilder.BuildSingleImage();
            //using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            //{
            //    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height),
            //        System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
            //    int[] intBuffer = glyphImg2.GetImageBuffer();

            //    System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length);
            //    bmp.UnlockBits(bmpdata);
            //    bmp.Save("d:\\WImageTest\\a_total.png");
            //}
            //atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml");
        }
예제 #16
0
 public static FontFace LoadFont(string fontpath)
 {
     using (FileStream fs = new FileStream(fontpath, FileMode.Open, FileAccess.Read))
     {
         var      reader = new OpenFontReader();
         Typeface t      = reader.Read(fs);
         t.Filename = fontpath;
         return(LoadFont(t));
     }
 }
예제 #17
0
        static void CreateSampleMsdfTextureFont(string fontfile, float sizeInPoint, ushort startGlyphIndex, ushort endGlyphIndex, string outputFile)
        {
            //sample
            var reader = new OpenFontReader();

            using (var fs = new FileStream(fontfile, FileMode.Open))
            {
                //1. read typeface from font file
                Typeface typeface = reader.Read(fs);
                //sample: create sample msdf texture
                //-------------------------------------------------------------
                var builder = new GlyphPathBuilder(typeface);
                //builder.UseTrueTypeInterpreter = this.chkTrueTypeHint.Checked;
                //builder.UseVerticalHinting = this.chkVerticalHinting.Checked;
                //-------------------------------------------------------------
                var atlasBuilder = new SimpleFontAtlasBuilder();


                for (ushort gindex = startGlyphIndex; gindex <= endGlyphIndex; ++gindex)
                {
                    //build glyph
                    builder.BuildFromGlyphIndex(gindex, sizeInPoint);

                    var glyphToContour = new GlyphContourBuilder();
                    //glyphToContour.Read(builder.GetOutputPoints(), builder.GetOutputContours());
                    var genParams = new MsdfGenParams();
                    builder.ReadShapes(glyphToContour);
                    genParams.shapeScale = 1f / 64; //we scale later (as original C++ code use 1/64)
                    GlyphImage glyphImg = MsdfGlyphGen.CreateMsdfImage(glyphToContour, genParams);
                    atlasBuilder.AddGlyph(gindex, glyphImg);

                    //using (Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                    //{
                    //    var bmpdata = bmp.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                    //    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpdata.Scan0, buffer.Length);
                    //    bmp.UnlockBits(bmpdata);
                    //    bmp.Save("d:\\WImageTest\\a001_xn2_" + n + ".png");
                    //}
                }

                var glyphImg2 = atlasBuilder.BuildSingleImage();
                using (Bitmap bmp = new Bitmap(glyphImg2.Width, glyphImg2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                {
                    var bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, glyphImg2.Width, glyphImg2.Height),
                                               System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
                    int[] intBuffer = glyphImg2.GetImageBuffer();

                    System.Runtime.InteropServices.Marshal.Copy(intBuffer, 0, bmpdata.Scan0, intBuffer.Length);
                    bmp.UnlockBits(bmpdata);
                    bmp.Save("d:\\WImageTest\\a_total.png");
                }
                atlasBuilder.SaveFontInfo("d:\\WImageTest\\a_info.xml");
            }
        }
예제 #18
0
 void LoadFont()
 {
     if (_latinModernMathFont == null)
     {
         using (FileStream fs = new FileStream("Fonts/latinmodern-math.otf", FileMode.Open))
         //using (FileStream fs = new FileStream("Fonts/Asana-Math.otf", FileMode.Open))
         {
             OpenFontReader reader = new OpenFontReader();
             _latinModernMathFont = reader.Read(fs);
         }
     }
 }
예제 #19
0
 public static FontFace LoadFont(
     string fontfile,
     TextureFontCreationParams creationParams,
     out SimpleFontAtlas fontAtlas)
 {
     using (FileStream fs = new FileStream(fontfile, FileMode.Open, FileAccess.Read))
     {
         var      reader   = new OpenFontReader();
         Typeface typeface = reader.Read(fs);
         return(LoadFont(typeface, creationParams, out fontAtlas));
     }
 }
예제 #20
0
        static void TestLoadAndReload(string filename)
        {
            //Trimmable feature tests:
            //[A] read the font file as usual => get full information about the font
            Typeface typeface = null;

            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                //read in full mode
                OpenFontReader openFontReader = new OpenFontReader();
                typeface = openFontReader.Read(fs);
            }

            //before
            bool     hasColor1  = typeface.HasColorTable();
            bool     hasSvg1    = typeface.HasSvgTable();
            bool     hasCff1    = typeface.IsCffFont;
            TrimMode glyphMode1 = typeface.GetTrimMode();
            Glyph    g1_1       = typeface.GetGlyph(1);

            //---------------------------------------------------------
            //[B] if you create paths from glyphs, or atlas from glyph
            //   and you don't want any glyph-building-detail (eg. to reduce memory usuage)
            //   but you still want to use the typeface for text-layout
            //   you can trim it down
            RestoreTicket ticket = typeface.TrimDown();//***

            //[C] you can GetGlyph() but this is ANOTHER NEW GLYPH
            //without building instruction( eg. no cff,ttf,svg data,bitmap)

            Glyph g1_2 = typeface.GetGlyph(1);

            //** if you cache the the old version of 'full-info' glyph**
            // the info is still cache on the old glyph and it can be used as 'full-info' glyph
            // TrimDown() DOES NOT go to delete that glyph.

            bool hasColor2 = typeface.HasColorTable();
            bool hasSvg2   = typeface.HasSvgTable();
            bool hasCff2   = typeface.IsCffFont;

            TrimMode glyphMode2 = typeface.GetTrimMode();

            //---------------------------------------------------------

            //[D] can we load glyph detail again?
            //yes=> this need 'ticket' from latest TrimDown()
            //if you don't have it, you can't restore it.

            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                typeface.RestoreUp(ticket, fs);
            }
        }
예제 #21
0
 public static FontFace LoadFont(
     string fontpath,
     ScriptLang scriptLang,
     WriteDirection writeDirection = WriteDirection.LTR)
 {
     using (FileStream fs = new FileStream(fontpath, FileMode.Open, FileAccess.Read))
     {
         var      reader = new OpenFontReader();
         Typeface t      = reader.Read(fs);
         t.Filename = fontpath;
         return(LoadFont(t, scriptLang, writeDirection));
     }
 }
예제 #22
0
 public void Print(float size, string str, List <GlyphPlan> glyphPlanBuffer)
 {
     if (_currentTypeface == null)
     {
         OpenFontReader reader = new OpenFontReader();
         using (FileStream fs = new FileStream(_currentFontFilename, FileMode.Open))
         {
             _currentTypeface = reader.Read(fs);
         }
     }
     //-----------
     Print(_currentTypeface, size, str, glyphPlanBuffer);
 }
예제 #23
0
        /// <summary>
        /// Create a new font instance
        /// </summary>
        /// <param name="fontStream">Stream to the font data</param>
        /// <param name="fontSizeInPixels">The desired font size in pixels</param>
        public Font(Stream fontStream, float fontSizeInPixels)
        {
            SetupWoffDecompressorIfRequired();

            FontSizeInPoints = fontSizeInPixels * PIXELS_TO_POINTS;
            loadedGlyphs     = new Dictionary <char, Glyph>();

            var reader = new OpenFontReader();

            typeface = reader.Read(fontStream);

            pathBuilder    = new GlyphPathBuilder(typeface);
            pathTranslator = new GlyphTranslatorToVertices();
        }
예제 #24
0
        public bool LoadTTF(Stream stream)
        {
            var reader = new OpenFontReader();

            ttfTypeFace = reader.Read(stream);
            if (ttfTypeFace != null)
            {
                this.ascent     = ttfTypeFace.Ascender;
                this.descent    = ttfTypeFace.Descender;
                this.unitsPerEm = ttfTypeFace.UnitsPerEm;
                return(true);
            }

            return(false);
        }
예제 #25
0
파일: Form1.cs 프로젝트: ywscr/Typography
        private void button2_Click(object sender, EventArgs e)
        {
            OpenFontReader openFontReader = new OpenFontReader();
            string         filename       = "Test/Sarabun-Regular.woff";

            //using (FileStream fs = new FileStream(filename, FileMode.Open))
            //{
            //    PreviewFontInfo previewFont = openFontReader.ReadPreview(fs);
            //}
            //assign woff decompressor here
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                openFontReader.Read(fs);
            }
        }
예제 #26
0
        /// <summary>
        /// Create a new font instance
        /// </summary>
        /// <param name="filePath">Path to the font file</param>
        /// <param name="fontSizeInPixels">The desired font size in pixels</param>
        public Font(string filePath, float fontSizeInPixels)
        {
            SetupWoffDecompressorIfRequired();

            FontSizeInPoints = fontSizeInPixels * PIXELS_TO_POINTS;
            loadedGlyphs     = new Dictionary <char, Glyph>();

            using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var reader = new OpenFontReader();
                typeface = reader.Read(fs);
            }

            pathBuilder    = new GlyphPathBuilder(typeface);
            pathTranslator = new GlyphTranslatorToVertices();
        }
예제 #27
0
 Typeface GetTypefaceOrCreateNew(InstalledTypeface installedFont)
 {
     //load
     //check if we have create this typeface or not
     if (!_loadedTypefaces.TryGetValue(installedFont, out Typeface typeface))
     {
         //TODO: review how to load font here
         using (var fs = new FileStream(installedFont.FontPath, FileMode.Open, FileAccess.Read))
         {
             var reader = new OpenFontReader();
             typeface = reader.Read(fs, installedFont.ActualStreamOffset);
         }
         return(_loadedTypefaces[installedFont] = typeface);
     }
     return(typeface);
 }
예제 #28
0
        public static Typeface ResolveTypefaceFromFile(this InstalledTypefaceCollection fontCollection, string filename)
        {
            //from user input typeface filename

            bool enable_absPath = false; //enable abs path or not

#if DEBUG
            enable_absPath = true;//in debug mode
#endif
            //TODO: review here again!!!
            if (!fontCollection._installedTypefacesByFilenames.TryGetValue(filename, out InstalledTypeface found))
            {
                if (!enable_absPath && Path.IsPathRooted(filename))
                {
                    return(null);//***
                }

                //search for a file
                if (File.Exists(filename))
                {
                    //TODO: handle duplicated font!!
                    //try read this
                    InstalledTypeface instTypeface;
                    using (FileStream fs = new FileStream(filename, FileMode.Open))
                    {
                        OpenFontReader reader   = new OpenFontReader();
                        Typeface       typeface = reader.Read(fs);

                        //
                        OpenFont.Extensions.TypefaceExtensions.SetCustomTypefaceKey(
                            typeface,
                            TinyCRC32Calculator.CalculateCrc32(typeface.Name.ToUpper()));

                        instTypeface = new InstalledTypeface(typeface, filename);
                        fontCollection._installedTypefacesByFilenames.Add(filename, instTypeface);

                        return(instTypeface.ResolvedTypeface = typeface);//assign  and return
                    }
                }
            }
            else
            {
                //found inst type
                return(ResolveTypeface(fontCollection, found));
            }
            return(null);
        }
예제 #29
0
        public static PixelFarm.Drawing.Fonts.FontFace LoadFont(string fontpath)
        {
            // Typeface: Typography.OpenFont
            // GlyphPathBuilder: Typography.Contours
            // Typography.OpenFont: OpenFontReader


            using (FileStream fs = new FileStream(fontpath, FileMode.Open, FileAccess.Read))
            {
                var      reader = new OpenFontReader();
                Typeface t      = reader.Read(fs);
                t.Filename = fontpath;

                // t.IsCffFont // (CFF: Compact Font Format) https://www.linotype.com/8120/the-difference-between-cff-and-ttf.html
                t.GetAdvanceWidth(123);

                // font->potm->otmfsType = pOS2->fsType & 0x30e;
                int otmfsType = t.OS2Table.fsType & 0x30e;



                short a = t.Ascender;
                short b = t.Descender;
                short c = t.LineGap;
                // t.CalculateScaleToPixelFromPointSize
                // t.CalculateScaleToPixel

                // var list = t.GetRawGlyphList();
                //list[0].


                // t.GetGlyphByIndex(123);

                var gi = t.GetGlyphByIndex(t.LookupIndex('g'));



                // gi.Bounds.Equals
                // t.GetGlyphByIndex(t.LookupIndex('g')).MathGlyphInfo.

                var builder = new Typography.Contours.GlyphPathBuilder(t);



                return(LoadFont(t));
            }
        }
예제 #30
0
        private static Typeface GetTypeFace(string fontFamily)
        {
            Typeface typeFace;

            if (!TypefaceCache.TryGetValue(fontFamily, out typeFace))
            {
                using (var fs = Utility.ReadFile(fontFamily))
                {
                    var reader = new OpenFontReader();
                    Profile.Start("OpenFontReader.Read");
                    typeFace = reader.Read(fs);
                    Profile.End();
                }
                TypefaceCache.Add(fontFamily, typeFace);
            }
            return(typeFace);
        }