示例#1
0
        void RunSampleF(PixelFarm.Drawing.Painter p)
        {
            //version 4:
            p.Clear(PixelFarm.Drawing.Color.White);
            p.UseSubPixelLcdEffect = this.EnableSubPix;
            //--------------------------
            p.StrokeColor = PixelFarm.Drawing.Color.Black;
            p.StrokeWidth = 2.0f;
            p.DrawLine(2, 0, 10, 15);

            int lineLen = 10;
            int x       = 30;
            int y       = 30;

            p.FillColor = PixelFarm.Drawing.Color.Black;

            VertexStorePool pool = new VertexStorePool();

            using (System.IO.FileStream fs = new System.IO.FileStream("c:\\Windows\\Fonts\\tahoma.ttf", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                Typography.OpenFont.OpenFontReader reader   = new Typography.OpenFont.OpenFontReader();
                Typography.OpenFont.Typeface       typeface = reader.Read(fs);


                var builder = new Typography.Contours.GlyphPathBuilder(typeface);
                builder.BuildFromGlyphIndex((ushort)typeface.LookupIndex('C'), 16);
                PixelFarm.Drawing.Fonts.GlyphTranslatorToVxs tovxs = new Drawing.Fonts.GlyphTranslatorToVxs();
                builder.ReadShapes(tovxs);
                VertexStore vxs = new VertexStore();
                tovxs.WriteOutput(vxs);
                p.Fill(vxs);
            }
            p.FillRect(0, 0, 20, 20);
        }
示例#2
0
        static EmojiCharacters()
        {
            var typeface = new System.Windows.Media.Typeface(EmojiFontName);

            _Emojis = new Dictionary <int, EmojiCharacter>();
            if (typeface.TryGetGlyphTypeface(out _Typeface))
            {
                using (var s = _Typeface.GetFontStream())
                {
                    var r          = new Typography.OpenFont.OpenFontReader();
                    var m_openfont = r.Read(s, Typography.OpenFont.ReadFlags.Full);

                    _UnitsPerEm = m_openfont.UnitsPerEm;

                    var  colrTable = m_openfont.COLRTable;
                    int  palette = 0; // FIXME: support multiple palettes?
                    var  brushs = new Dictionary <Color, Brush>();
                    byte R, G, B, A;
                    foreach (var kv in _Typeface.CharacterToGlyphMap)
                    {
                        var glyphIndex = kv.Value;
                        if (colrTable.LayerIndices.TryGetValue(glyphIndex, out var layer_index))
                        {
                            int endIndex = layer_index + colrTable.LayerCounts[glyphIndex];
                            var layers   = new EmojiGlyphLayer[endIndex - layer_index];
                            for (int i = layer_index; i < endIndex; ++i)
                            {
                                ushort sub_gid = colrTable.GlyphLayers[i];
                                int    cid     = m_openfont.CPALTable.Palettes[palette] + colrTable.GlyphPalettes[i];
                                m_openfont.CPALTable.GetColor(cid, out R, out G, out B, out A);
                                var color = Color.FromArgb(A, R, G, B);
                                if (!brushs.TryGetValue(color, out Brush brush))
                                {
                                    brush = new SolidColorBrush(color);
                                    brushs.Add(color, brush);
                                }
                                layers[i - layer_index] = new EmojiGlyphLayer()
                                {
                                    Brush = brush,
                                    //Color = Color.FromArgb(A, R, G, B),
                                    LayerIndex = sub_gid
                                };
                            }

                            var glyph = m_openfont.Lookup(kv.Key);
                            var width = glyph.OriginalAdvanceWidth;
                            if (!glyph.HasOriginalAdvancedWidth)
                            {
                                width = m_openfont.GetHAdvanceWidthFromGlyphIndex(glyphIndex);
                            }
                            _Emojis.Add(kv.Key, new EmojiCharacter(layers, width,
                                                                   Char.ConvertFromUtf32(kv.Key).Length));
                        }
                    }
                }
            }
        }
示例#3
0
        public static Typography.OpenFont.Typeface TypefaceFromFile(string fontFile)
        {
            Typography.OpenFont.Typeface tf;

            using (System.IO.FileStream fs = new System.IO.FileStream(fontFile, System.IO.FileMode.Open))
            {
                Typography.OpenFont.OpenFontReader fontReader = new Typography.OpenFont.OpenFontReader();
                tf = fontReader.Read(fs);
            }

            return(tf);
        } // End Function TypefaceFromFile
示例#4
0
        public bool LoadTTF(Stream stream)
        {
            var reader = new Typography.OpenFont.OpenFontReader();

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

            return(false);
        }
示例#5
0
        public bool LoadTTF(Stream stream)
        {
            var reader = new Typography.OpenFont.OpenFontReader();

            _ofTypeface = reader.Read(stream);
            if (_ofTypeface != null)
            {
                this.ascent             = _ofTypeface.Ascender;
                this.descent            = _ofTypeface.Descender;
                this.unitsPerEm         = _ofTypeface.UnitsPerEm;
                this.underline_position = _ofTypeface.UnderlinePosition;
                var bounds = _ofTypeface.Bounds;
                this.boundingBox = new RectangleInt(bounds.XMin, bounds.YMin, bounds.XMax, bounds.YMax);
                return(true);
            }

            return(false);
        }
示例#6
0
        public static VgVisualElement CreateVgVisualElementFromGlyph(string actualFontFile, float sizeInPts, char c)
        {
            if (!s_loadedTypefaces.TryGetValue(actualFontFile, out Typography.OpenFont.Typeface typeface))
            {
                //create vgrender vx from font-glyph
                //
                using (System.IO.FileStream fs = new FileStream(actualFontFile, FileMode.Open))
                {
                    Typography.OpenFont.OpenFontReader reader = new Typography.OpenFont.OpenFontReader();
                    typeface = reader.Read(fs);
                }
            }
            if (_glyphMaskStore == null)
            {
                _glyphMaskStore = new Typography.Contours.GlyphMeshStore();
                _glyphMaskStore.FlipGlyphUpward = true;
            }
            _glyphMaskStore.SetFont(typeface, sizeInPts);
            //-----------------
            VertexStore vxs  = _glyphMaskStore.GetGlyphMesh(typeface.GetGlyphIndex(c));
            var         spec = new SvgPathSpec()
            {
                FillColor = Color.Red
            };
            VgVisualDoc     renderRoot = new VgVisualDoc();
            VgVisualElement renderE    = new VgVisualElement(WellknownSvgElementName.Path, spec, renderRoot);


            //offset the original vxs to (0,0) bounds
            //PixelFarm.CpuBlit.RectD bounds = vxs.GetBoundingRect();
            //Affine translate = Affine.NewTranslation(-bounds.Left, -bounds.Bottom);
            //renderE._vxsPath = vxs.CreateTrim(translate);


            PixelFarm.CpuBlit.RectD bounds = vxs.GetBoundingRect();
            Affine translate = Affine.NewTranslation(-bounds.Left, -bounds.Bottom);

            renderE.VxsPath = vxs.CreateTrim(translate);
            return(renderE);
        }
示例#7
0
        void RunSampleF(PixelFarm.Drawing.Painter p)
        {
            //version 4:
            p.Clear(PixelFarm.Drawing.Color.White);
            p.UseLcdEffectSubPixelRendering = this.EnableSubPix;
            //--------------------------
            p.StrokeColor = PixelFarm.Drawing.Color.Black;
            p.StrokeWidth = 2.0f;
            //p.DrawLine(2, 0, 10, 15);

            int lineLen = 10;
            int x       = 30;
            int y       = 30;

            p.FillColor = PixelFarm.Drawing.Color.Black;


            using (System.IO.FileStream fs = new System.IO.FileStream("c:\\Windows\\Fonts\\tahoma.ttf", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                Typography.OpenFont.OpenFontReader reader   = new Typography.OpenFont.OpenFontReader();
                Typography.OpenFont.Typeface       typeface = reader.Read(fs);


                var builder = new Typography.OpenFont.Contours.GlyphOutlineBuilder(typeface);
                builder.BuildFromGlyphIndex((ushort)typeface.GetGlyphIndex('C'), 24);

                var tovxs = new Typography.OpenFont.Contours.GlyphTranslatorToVxs();
                builder.ReadShapes(tovxs);

                using (Tools.BorrowVxs(out var vxs))
                {
                    tovxs.WriteOutput(vxs);
                    p.Fill(vxs);
                }
            }
            // p.FillRect(0, 0, 20, 20);
        }
示例#8
0
        private async static Task MeasureStringFor(FontDownload loader, string path)
        {
            byte[] data = null;

            data = await loader.LoadFrom(path);


#if UseOpenFont
            ZlibDecompressStreamFunc zipfunc = (byte[] dataIn, byte[] output) =>
            {
                using (var streamIn = new MemoryStream(dataIn))
                {
#if NET6_0
                    ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream input = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(streamIn);

                    using (var streamOut = new MemoryStream(output))
                        input.CopyTo(streamOut);
#endif
                    return(true);
                }
            };
            WoffDefaultZlibDecompressFunc.DecompressHandler = zipfunc;

            BrotliDecompressStreamFunc brotlifunc = (byte[] dataIn, Stream dataOut) =>
            {
                using (var streamIn = new MemoryStream(dataIn))
                {
                    System.IO.Compression.BrotliStream deflate = new System.IO.Compression.BrotliStream(streamIn, System.IO.Compression.CompressionMode.Decompress);

                    deflate.CopyTo(dataOut);

                    return(true);
                }
            };
            Woff2DefaultBrotliDecompressFunc.DecompressHandler = brotlifunc;


            using (var ms = new System.IO.MemoryStream(data))
            {
                var reader  = new Typography.OpenFont.OpenFontReader();
                var preview = reader.ReadPreview(ms);

                Console.WriteLine("Loaded font reference " + preview.Name);
            }

            using (var ms = new System.IO.MemoryStream(data))
            {
                var    reader   = new Typography.OpenFont.OpenFontReader();
                var    full     = reader.Read(ms);
                string text     = "This is the text to measure";
                var    encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode;
                int    fitted;
                var    size = full.MeasureString(text, 0, 12, 10000, true, out fitted);


                Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length);
            }
#else
            TypefaceReader tfreader = new TypefaceReader();
            ITypefaceInfo  info;

            using (var ms = new System.IO.MemoryStream(data))
            {
                info = tfreader.ReadTypeface(ms, path);
                if (null == info)
                {
                    ExitClean("Could not read the info from the font file");
                    return;
                }
                else if (info.FontCount == 0)
                {
                    ExitClean("No fonts could be read from the data: " + info.ErrorMessage ?? "Unknown error");
                    return;
                }
                else
                {
                    Console.WriteLine("Read  " + info.FontCount + " typefaces from the font file " + info.Source);
                    foreach (var reference in info.Fonts)
                    {
                        Console.WriteLine("    " + reference.FamilyName + " (weight: " + reference.FontWeight.ToString() + ", width: " + reference.FontWidth + ", restrictions : " + reference.Restrictions + ", selections : " + reference.Selections.ToString() + ")");
                    }
                }
            }
            TypeMeasureOptions options = new TypeMeasureOptions();

            using (var ms = new System.IO.MemoryStream(data))
            {
                foreach (var fref in info.Fonts)
                {
                    Console.WriteLine();

                    ms.Position = 0;
                    var typeface = tfreader.GetFont(ms, info.Source, fref);

                    if (null == typeface || typeface.IsValid == false)
                    {
                        ExitClean("The font " + fref.ToString() + " was not valid");
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Loaded font reference " + typeface.ToString());
                    }

                    var metrics = typeface.GetMetrics(options);

                    if (null != metrics)
                    {
                        var line = metrics.MeasureLine("This is the text to measure", 0, 12.0, 10000, options);
                        Console.WriteLine("Measured the string to " + line.RequiredWidth + ", " + line.RequiredHeight + " points, and fitted " + line.CharsFitted + " chars" + (line.OnWordBoudary ? " breaking on the word boundary." : "."));
                    }
                    else
                    {
                        ExitClean("No metrics were returned for the font " + typeface.ToString());
                        return;
                    }

                    var ttfData = typeface.GetFileData(DataFormat.TTF);
                    if (null == ttfData)
                    {
                        ExitClean("No data was returned in TTF format for the font " + typeface.ToString());
                    }
                    else
                    {
                        Console.WriteLine("TrueType font data was extracted at " + ttfData.Length + " bytes from the original data of " + data.Length);
                    }

                    var name = typeface.FamilyName;
                    if (typeface.FontWeight != WeightClass.Normal)
                    {
                        if (name.IndexOf(typeface.FontWeight.ToString()) < 0)
                        {
                            name += " " + typeface.FontWeight.ToString();
                        }
                    }
                    if ((typeface.Selections & FontSelection.Italic) > 0)
                    {
                        if (name.IndexOf("Italic") < 0)
                        {
                            name += " Italic";
                        }
                    }

                    loader.SaveToLocal("Output", name + ".ttf", ttfData);
                }
            }

#if Legacy
            var ttf = new Scryber.OpenType.TTFFile(data, 0);

            Console.WriteLine("Loaded font file " + ttf.ToString());

            var    encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode;
            string text     = "This is the text to measure";
            int    fitted;

            var size = ttf.MeasureString(encoding, text, 0, 12, 1000, true, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length);

            //Measure 90 wiout word boundary
            size = ttf.MeasureString(encoding, text, 0, 12, 90, false, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'");

            //Measure 90 with word boundary
            size = ttf.MeasureString(encoding, text, 0, 12, 90, true, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'");
#endif

#if Performance
            var stopWatch = System.Diagnostics.Stopwatch.StartNew();

            MeasureStringMeasurer(ttf);

            stopWatch.Stop();

            Console.WriteLine("To measure 4 different strings " + maxRepeat + " times took " + stopWatch.Elapsed.TotalMilliseconds + "ms");
#endif
#endif
        }