Exemple #1
0
        internal void ApplyToSKPaint(SKPaint skPaint, GraphicsUnit targetUnit, float dpi)
        {
            skPaint.Typeface = skTypeface;
            float sizePixel = GraphicsUnitConverter.Convert(Unit, targetUnit, Size, dpi); //注意:转换单位

            skPaint.TextSize     = sizePixel;                                             //TODO: 转换与System.Drawing不一致 20 -> 15,待修改
            skPaint.IsAntialias  = true;
            skPaint.SubpixelText = true;
        }
Exemple #2
0
        public void DrawString(string str, Font font, Color color, RectangleF rect, StringFormat format)
        {
            //TODO: 暂用TextLayout实现
            var layout = new TextLayout(str, font, format, DpiX)
            {
                Width  = GraphicsUnitConverter.Convert(PageUnit, GraphicsUnit.Pixel, rect.Width, DpiX),
                Height = GraphicsUnitConverter.Convert(PageUnit, GraphicsUnit.Pixel, rect.Height, DpiY)
            };

            DrawTextLayout(layout, color, rect.X, rect.Y);
        }
Exemple #3
0
        public void DrawString(string str, Font font, Color color, float x, float y)
        {
            font.ApplyToSKPaint(skPaint, PageUnit, DpiX);
            skPaint.Color        = new SKColor((uint)color.Value);
            skPaint.Style        = SKPaintStyle.Fill;
            skPaint.TextEncoding = SKTextEncoding.Utf16;
            unsafe
            {
                fixed(char *ptr = str)
                {
                    SkiaApi.sk_canvas_draw_text(skCanvas.Handle, ptr, new IntPtr(str.Length * 2),
                                                x, y + GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit, -font.FontMetrics.Ascent, DpiY),
                                                skPaint.Handle);
                }
            }

            skPaint.Reset();
        }
Exemple #4
0
        public unsafe void DrawTextLayout(TextLayout layout, Color color, float x, float y)
        {
            layout.Run();
            layout.Font.ApplyToSKPaint(skPaint, PageUnit, DpiX);
            skPaint.Color        = new SKColor((uint)color.Value);
            skPaint.Style        = SKPaintStyle.Fill;
            skPaint.TextEncoding = SKTextEncoding.Utf16;
            skPaint.IsAntialias  = true;

            var cy = y + GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit, layout.OffsetY, DpiY);

            fixed(char *ptr = layout.Text)
            {
                for (int i = 0; i < layout.lines.Count; i++)
                {
                    cy += GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit, -layout.Font.FontMetrics.Ascent, DpiY);
                    var      length = layout.lines[i].widths.Length;
                    SKPoint *pos    = stackalloc SKPoint[length];
                    var      cx     = x + GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit, layout.lines[i].offsetX, DpiX);
                    for (int j = 0; j < length; j++)
                    {
                        pos[j].X = cx;
                        pos[j].Y = cy;
                        cx      += GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit, layout.lines[i].widths[j], DpiX);
                    }

                    SkiaApi.sk_canvas_draw_pos_text(skCanvas.Handle, ((byte *)ptr) + layout.lines[i].startByteIndex,
                                                    new IntPtr(layout.lines[i].byteLength), pos, skPaint.Handle);

                    cy += GraphicsUnitConverter.Convert(GraphicsUnit.Pixel, PageUnit,
                                                        layout.Font.FontMetrics.Descent + layout.Font.FontMetrics.Leading, DpiX);
                }
            }

            //System.Diagnostics.Debug.WriteLine("Graphics.DrawTextLayout at {0},{1} with: {2}",x,y,layout.Text);
        }