Exemplo n.º 1
0
        public void CreatePDF(Stream stream)
        {
            // Test string using EUDC codes and two regular chars (& and !): 0xE620 0xE621 0xE622 0xE624 & 0xE623 !
            const string tstr = "&!";
            // Set up:
            var doc  = new GcPdfDocument();
            var page = doc.NewPage();
            var g    = page.Graphics;
            var tf   = new TextFormat()
            {
                FontSize = 20
            };
            var rc = Common.Util.AddNote(
                "This sample demonstrates how to render private use Unicode characters (PUA) with custom EUDC fonts (.tte).\n" +
                "A GrapeCity.Documents.Text.Font can be created from an EUDC .tte file, " +
                "and linked to one or more fonts using Font.AddEudcFont() method.",
                page);
            const float dy = 36;
            var         ip = new PointF(rc.X, rc.Bottom + dy / 2);

            // Use FontCollection to allow fetching fonts by family names:
            var fc = new FontCollection();

            // Assign the font collection to the graphics so that MeasureString/DrawString
            // methods on the graphics can find fallback fonts:
            g.FontCollection = fc;

            // Register some regular fonts with the FontCollection:
            fc.RegisterFont(Path.Combine("Resources", "Fonts", "arial.ttf"));
            fc.RegisterFont(Path.Combine("Resources", "Fonts", "times.ttf"));
            fc.RegisterFont(Path.Combine("Resources", "Fonts", "yumin.ttf"));
            fc.RegisterFont(Path.Combine("Resources", "Fonts", "msgothic.ttc"));
            fc.RegisterFont(Path.Combine("Resources", "Fonts", "YuGothR.ttc"));

            // Tell the font collection to use Yu Mincho as a fallback:
            fc.AppendFallbackFonts(fc.FindFamilyName("Yu Mincho"));

            // Using Arial font renders the test string as empty rectangles, as suitable glyphs are not present in Arial:
            tf.Font = fc.FindFamilyName("Arial", false, false);
            g.DrawString($"Arial: {tstr} (no EUDC font has been linked yet)", tf, ip);
            ip.Y += dy;

            // Load two custome EUDC fonts:
            var eudcF0 = Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc0.tte"));
            var eudcF1 = Font.FromFile(Path.Combine("Resources", "Fonts", "Eudc1.tte"));

            // Link one EUDC font to Arial - now in strings rendered with Arial, EUDC chars will be looked up in this font:
            var font = fc.FindFamilyName("Arial");

            font.AddEudcFont(eudcF0);
            // Ditto for Yu Mincho font:
            font = fc.FindFamilyName("Yu Mincho");
            font.AddEudcFont(eudcF0);
            // Link another EUDC font to Yu Gothic:
            font = fc.FindFamilyName("Yu Gothic");
            font.AddEudcFont(eudcF1);

            // Render strings with EUDC chars using fonts to which our custom EUDC font is linked:
            tf.Font = fc.FindFamilyName("Arial", false, false);
            g.DrawString($"Arial, linked with Eudc0.tte: {tstr}", tf, ip);
            ip.Y   += dy;
            tf.Font = fc.FindFileName("times.ttf");
            g.DrawString($"Times, fallback via Yu Mincho: {tstr}", tf, ip);
            ip.Y   += dy;
            tf.Font = fc.FindFamilyName("MS Gothic");
            g.DrawString($"MS Gothic, fallback via Yu Mincho: {tstr}", tf, ip);
            ip.Y   += dy;
            tf.Font = fc.FindFamilyName("Yu Gothic");
            g.DrawString($"Yu Gothic, linked with Eudc1.tte: {tstr}", tf, ip);
            ip.Y += dy;

            // FontCollection adds some services (like font lookup by family name),
            // but EUDC fonts can be linked to fonts that are not in a collection:
            font = Font.FromFile(Path.Combine("Resources", "Fonts", "Gabriola.ttf"));
            font.AddEudcFont(eudcF0);
            tf.Font = font;
            g.DrawString($"Gabriola Font, linked with Eudc0.tte: {tstr}", tf, ip);
            ip.Y += dy;
            // Done:
            doc.Save(stream);
        }
Exemplo n.º 2
0
        public void CreatePDF(Stream stream)
        {
            var doc  = new GcPdfDocument();
            var page = doc.NewPage();
            var g    = page.Graphics;
            // As in the Surrogates sample, we specify a standard font
            // (which lacks many of the glyphs we will be rendering),
            // and will rely on font fallback support provided by FontCollection:
            var font = StandardFonts.Helvetica;

            // Set up text formats for captions, "interesting chars" and spacing:
            TextFormat tf = new TextFormat()
            {
                Font = font, FontSize = 12
            };
            TextFormat tf1 = new TextFormat(tf)
            {
                FontSize = 14
            };
            TextFormat tfs = new TextFormat(tf)
            {
                FontSize = 6
            };

            // Create a font collection to use:
            FontCollection fc = new FontCollection();

            // Add our own fallback fonts to use (note that order is imortant here,
            // first come first found):
            fc.AppendFallbackFonts(
                Font.FromFile(Path.Combine("Resources", "Fonts", "arialuni.ttf")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "l_10646.ttf")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "seguiemj.ttf")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "seguisym.ttf")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "simsun.ttc")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                Font.FromFile(Path.Combine("Resources", "Fonts", "YuGothR.ttc"))
                );
            // Restricting default font lookup is done in the TextLayout, so unlike the
            // {Surrogates} sample, here we cannot use DrawString, but must use
            // TextLayout and DrawTextLayout directly:
            // - specify the font collection to use;
            // - restrict default fonts/fallbacks lookup to the specified collection only;
            // - set up other props to render the text.
            TextLayout tl = new TextLayout(72)
            {
                FontCollection       = fc,
                RestrictedFontLookup = true,
                FontFallbackScope    = FontFallbackScope.FontCollectionOnly,
                MaxWidth             = page.Size.Width,
                MaxHeight            = page.Size.Height,
                MarginLeft           = 72,
                MarginRight          = 72,
                MarginTop            = 36,
                MarginBottom         = 36,
                TextAlignment        = TextAlignment.Center,
            };

            tl.Append("Some Interesting Unicode Characters (system-independent)",
                      new TextFormat(tf)
            {
                Underline = true, FontSize = tf.FontSize + 2
            });
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);

            tl.MarginTop = tl.ContentRectangle.Bottom + 20;
            tl.Clear();
            tl.TextAlignment = TextAlignment.Leading;

            // Draw the strings:
            tl.Append("Surrogate Pairs:\n", tf);
            tl.Append("\uD867\uDEDB \uD840\uDC0B \uD834\uDD1E \uD834\uDD61 \uD83D\uDC04\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Currency Symbols:\n", tf);
            tl.Append("\u0024 \u20A0 \u20A1 \u20A2 \u20A3 \u20A4 \u20AC \u20B9 \x20BD\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Mathematical Operators:\n", tf);
            tl.Append("\u221A \u222B \u2211 \u2210 \u2264 \u2265 \u2202 \u2208\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("CJK Ideographs Extension A:\n", tf);
            tl.Append("\u3400 \u3401 \u3402 \u3403 \u3404 \u3405 \u3406 \u3407\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Letterlike Symbols:\n", tf);
            tl.Append("\u2110 \u2111 \u2112 \u2113 \u2114 \u2115 \u211B \u211C\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Private Use Area:\n", tf);
            tl.Append("\uE000 \uE001 \uE010 \uE011 \uE012 \uE013 \uE014 \uE015\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Arrows:\n", tf);
            tl.Append("\u2190 \u2191 \u2192 \u2193 \u21B0 \u21E6 \u21CB \u21A9\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Dingbats:\n", tf);
            tl.Append("\u2714 \u2717 \u275B \u275C \u2706 \u2707 \u2708 \u2709\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Braille Patterns:\n", tf);
            tl.Append("\u2830 \u2831 \u2832 \u2833 \u2834 \u2835 \u2836 \u2837\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Geometric Shapes:\n", tf);
            tl.Append("\u25D0 \u25D1 \u25D2 \u25D3 \u25A4 \u25F0 \u25BC \u25CE\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Latin Extended A:\n", tf);
            tl.Append("\u0100 \u0101 \u0102 \u0103 \u0104 \u0105 \u0106 \u0107\n", tf1);
            tl.Append("\n", tfs);

            tl.Append("Miscellaneous Symbols:\n", tf);
            tl.Append("\u2600 \u2601 \u2602 \u2603 \u2604 \u2605 \u2606 \u2607 \u2608 \u2609 \u2614 \u2615 \u26F0\n", tf1);
            tl.Append("\n", tfs);

            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);

            // Done:
            doc.Save(stream);
        }