示例#1
0
            private static void Recurse(int depth, IGraphics gr, Rectangle rc)
            {
                object   o;
                FontDesc fd;

                fd = new FontDesc("Arial", 10, false, false, false);
                o  = gr.GetFontHandle(fd);
                gr.PushFont(o);
                gr.MeasureText("my name is alfie");
                gr.DrawText(rc, 20, "hello", Color.Black, Color.White, -1, -1);

                fd = new FontDesc("Times New Roman", 10, false, false, false);
                o  = gr.GetFontHandle(fd);
                gr.PushFont(o);
                gr.MeasureText("my name is alfie");

                fd = new FontDesc("Arial", 10, false, false, false);
                o  = gr.GetFontHandle(fd);
                gr.PushFont(o);
                gr.MeasureText("my name is alfie");

                if (depth < 100)
                {
                    Recurse(depth + 1, gr, rc);
                }

                gr.PopFont();
                gr.PopFont();
            }
示例#2
0
        public Font FindFont(string familyName, float fontSize, FontWeight fontWeight, bool italic)
        {
            var fd = new FontDesc
            {
                Typeface = new TypefaceDesc
                {
                    FamilyName = familyName,
                    Weight     = fontWeight,
                    Italic     = italic
                },
                Size = (int)(fontSize * 10)
            };

            if (!fonts.TryGetValue(fd, out var font))
            {
                if (!MatchTypeface(fd.Typeface, out SKTypeface typeface))
                {
                    typeface = SKTypeface.FromFamilyName(familyName, fontWeight.ToSkia(), SKFontStyleWidth.Normal, italic ? SKFontStyleSlant.Italic : SKFontStyleSlant.Upright);
                }

                var skFont = typeface.ToFont(fontSize);
                font = new SkiaFont(skFont);
                if (fontWeight == FontWeight.Bold && typeface.FontWeight < (int)SKFontStyleWeight.SemiBold)
                {
                    font.SKPaint.FakeBoldText = true;
                }

                fonts.Add(fd, font);
            }

            return(font);
        }
示例#3
0
        private Font CreateFont(FontDesc fd)
        {
            Font f = new Font(fd.Family, fd.Size, fd.Style);

            fontHandles[fd] = f;
            return(f);
        }
示例#4
0
        private IntPtr CreateFont(FontDesc fd)
        {
            IntPtr ret;
            Font   f = new Font(fd.Family, fd.Size, fd.Style);

            ret             = f.ToHfont();
            fontHandles[fd] = ret;
            f.Dispose();
            return(ret);
        }
示例#5
0
        public object GetFontHandle(FontDesc fd)
        {
            object ret = fontHandles[fd];

            if (ret == null)
            {
                ret = CreateFont(fd);
            }

            return(ret);
        }
示例#6
0
            public object GetFontHandle(FontDesc fd)
            {
                Win32Util.LOGFONT lf = new Win32Util.LOGFONT();
                lf.lfFaceName = fd.Family;
                lf.lfHeight   = fd.Size;
                lf.lfItalic   = (byte)(fd.Style & FontStyle.Italic);
                if ((fd.Style & FontStyle.Bold) == FontStyle.Bold)
                {
                    lf.lfWeight = 700;
                }
                lf.lfItalic = (byte)(fd.Style & FontStyle.Underline);

                return(Win32Util.CreateFont(lf));
            }
示例#7
0
        private IntPtr CreateFont(FontDesc fd)
        {
//			Win32Util.LOGFONT lf=new Win32Util.LOGFONT();
//			lf.lfFaceName=fd.Family;
//			lf.lfHeight=fd.Size;
//			lf.lfItalic=(byte) (fd.Style & FontStyle.Italic);
//			if ( (fd.Style & FontStyle.Bold) == FontStyle.Bold )
//				lf.lfWeight=700;
//			lf.lfItalic=(byte) (fd.Style & FontStyle.Underline);
//			IntPtr ret=Win32Util.CreateFont(lf);

            Font   f   = new Font(fd.Family, fd.Size, fd.Style);
            IntPtr ret = f.ToHfont();

            fontHandles[fd] = ret;

//			Console.WriteLine("Created font {0}", ret);
            return(ret);
        }
        public void GetTextSize2()
        {
            FontDesc myFont = new FontDesc("Times New Roman", true, true, 5);
            SizeF size = CourseFormatter.GetTextSize("1234", myFont, 1.3F);
            Assert.AreEqual(13, size.Width, 0.01);
            Assert.AreEqual(4.38, size.Height, 0.01);

            Bitmap bm = new Bitmap(250, 250);
            using (Graphics g = Graphics.FromImage(bm))
            using (Font font = myFont.GetScaledFont(1.3F)) {
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                g.TranslateTransform(bm.Width / 2, bm.Height / 2);
                g.ScaleTransform((float) (bm.Width / 20.0), (float) (bm.Height / 20.0));

                StringFormat format = new StringFormat(StringFormat.GenericTypographic);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                format.FormatFlags |= StringFormatFlags.NoClip;

                RectangleF rect = new RectangleF(new PointF(-6, -6), size);
                g.Clear(Color.White);
                g.DrawString("1234", font, Brushes.Black, rect, format);
                g.DrawRectangle(new Pen(Color.Red, 0.05F), rect.Left, rect.Top, rect.Width, rect.Height);
            }

            TestUtil.CheckBitmapsBase(bm, "courseformat\\textsize2");
        }