示例#1
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));
                        }
                    }
                }
            }
        }
        public void RealizeFont(Glyphs glyphs)
        {
            var absURI = glyphs.FontUri;

            if (!glyphs.FontUri.IsAbsoluteUri)
            {
                var uriCtx = glyphs as System.Windows.Markup.IUriContext;

                var host = uriCtx.BaseUri.Host;
                absURI = new Uri("pack://" + host + glyphs.FontUri.OriginalString, UriKind.Absolute);
            }

            var nameInPdf = "";
            KeyValuePair <string, PdfFont> exist;

            if (_fonts.TryGetValue(absURI.OriginalString, out exist))
            {
                this.realizedFont = exist.Value;
                nameInPdf         = exist.Key;
            }
            else
            {
                GlyphTypeface typeFace =
                    new GlyphTypeface(absURI);

                string name = String.Format("XPS-Font-{0:00}", ++fontCount);
                name = PdfFont.CreateEmbeddedFontSubsetName(name);

                var fontStream = typeFace.GetFontStream();
                var memStream  = new System.IO.MemoryStream();
                var buff       = new byte[512];
                int len        = 0;
                while ((len = fontStream.Read(buff, 0, buff.Length)) > 0)
                {
                    memStream.Write(buff, 0, len);
                }
                var  fontData = memStream.ToArray();
                Font font     = new Font(name, fontData);

                nameInPdf = writer.GetFontName(font);
                _fonts.Add(absURI.OriginalString, new KeyValuePair <string, PdfFont>(
                               nameInPdf, font.PdfFont));
                this.realizedFont = font.PdfFont;
            }
            //if (fontName != this.realizedFontName || this.realizedFontSize != font.Size)
            {
                //this.writer.WriteLiteral("{0} {1:0.###} Tf\n", fontName, -glyphs.FontRenderingEmSize);
                this.writer.WriteLiteral("{0} {1:0.###} Tf\n", nameInPdf, -glyphs.FontRenderingEmSize);

                //this.realizedFontName = fontName;
                //this.realizedFontSize = font.Size;
            }
        }
示例#3
0
        CopyFontStream()
        {
            Uri    sourceUri    = _fontUri;
            Uri    destUri      = _fontResourceStream.Uri;
            Stream destStream   = _fontResourceStream.Stream;
            Stream sourceStream = null;

            byte []              memoryFont;
            GlyphTypeface        glyphTypeface      = new GlyphTypeface(sourceUri);
            CodeAccessPermission fontReadPermission = glyphTypeface.CriticalFileReadPermission;

            if (fontReadPermission != null)
            {
                fontReadPermission.Assert();
            }

            try
            {
                sourceStream = glyphTypeface.GetFontStream();
            }
            finally
            {
                if (fontReadPermission != null)
                {
                    CodeAccessPermission.RevertAssert();
                }
            }

            memoryFont = new byte[_readBlockSize];
            Guid guid = ParseGuidFromUri(destUri);

            int bytesRead = PackagingUtilities.ReliableRead(sourceStream, memoryFont, 0, _readBlockSize);

            if (bytesRead > 0)
            {
                // Obfuscate the first block
                ObfuscateData(memoryFont, guid);
            }

            while (bytesRead > 0)
            {
                destStream.Write(memoryFont, 0, bytesRead);
                bytesRead = PackagingUtilities.ReliableRead(sourceStream, memoryFont, 0, _readBlockSize);
            }

            Uri    fontUri         = new Uri(_fontUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.SafeUnescaped), UriKind.RelativeOrAbsolute);
            string fontUriAsString = fontUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);

            _packagingPolicy.ReleaseResourceStreamForXpsFont(fontUriAsString);

            _streamWritten = true;
            return(destUri);
        }