[Mono-bugs] [Bug 650331] New: Text drawing issues and UIStringDrawing.h not translated into MonoTouch.CoreGraphics

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Fri Oct 29 21:47:18 EDT 2010


https://bugzilla.novell.com/show_bug.cgi?id=650331

https://bugzilla.novell.com/show_bug.cgi?id=650331#c0


           Summary: Text drawing issues and UIStringDrawing.h not
                    translated into MonoTouch.CoreGraphics
    Classification: Mono
           Product: MonoTouch
           Version: unspecified
          Platform: Macintosh
        OS/Version: Mac OS X 10.6
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Class Libraries
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: johan.otto at justenough.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: Customer
           Blocker: ---


Description of Problem:
I might be going out on a limb here after a midnight coding session....

I am in a situation where I cannot use UIView.DrawText because it's not thread
safe and causes an SIGNE and crashes my application randomly. I am using a
CATiledLayer with a DrawLayerInContext (which executes in a separate thread).
Therefore I am forced to use manual labour and CGContext. CGContext.SetFontSize
does not apply the font size correctly, secondly, I have noticed that the
UIStringDrawing.h has been omitted and cannot be found on either String or
NSString as extension methods.

I also tried to translate this (http://silverity.livejournal.com/26436.html)
code and have found that there is no equivalent in MonoTouch for
CGFont.GetGlyphsForUnichars, is this by design?

Objective-C snippet
        CGFontRef font = CGFontCreateWithFontName((CFStringRef)[[self font]
fontName]);
        CGContextSetFont(ctx, font);
        CGContextSetFontSize(ctx, [[self font] pointSize]);

        // Transform text characters to unicode glyphs.

        NSInteger length = [[self text] length];
        unichar chars[length];
        CGGlyph glyphs[length];
        [[self text] getCharacters:chars range:NSMakeRange(0, length)];
        CGFontGetGlyphsForUnichars(font, chars, glyphs, length);

C# code (not working at all)

public static void DrawText(this CGContext context, 
                                    string text, 
                                    RectangleF rect, 
                                    UIFont font, 
                                    UITextAlignment textAlignment,
                                    UIColor textColor,
                                    bool shadow,
                                    UIColor shadowColor,
                                    SizeF shadowOffset)
        {
            context.SaveState();
            try
            {

                using(CGFont fnt = CGFont.CreateWithFontName(font.Name))
                                {
                context.SetFont(fnt);
                context.SetFontSize(font.PointSize);

                // Measure text dimensions
                context.SetTextDrawingMode(CGTextDrawingMode.Invisible);
                context.TextPosition = rect.Location; // TODO check
                context.ShowText(text);
                PointF textEnd = context.TextPosition;

                PointF alignment = PointF.Empty;
              PointF anchor = new PointF(textEnd.X * (-0.5f), font.PointSize *
(-0.25f));  
              CGAffineTransform trans = new
CGAffineTransform(1f,0f,0f,-1f,0f,1f); 
/* CGPointApplyAffineTransform(anchor, CGAffineTransformMake(1, 0, 0, -1, 0,
1));*/
                PointF p = trans.TransformPoint(anchor);

                switch(textAlignment)
                {
                    case UITextAlignment.Center:
                        alignment.X = rect.Width * 0.5f + p.X;
                        break;
                    case UITextAlignment.Left:
                        alignment.X = 0;
                        break;
                    default:
                        alignment.X = rect.Width - textEnd.X;
                        break;
                }

                alignment.Y = rect.Height * 0.5f + p.Y;

                // Flip back mirrored text.
                context.TextMatrix = CGAffineTransform.MakeScale(1,-1);

                 // Draw shadow.
                context.SaveState();
                try
                {
                    context.SetTextDrawingMode(CGTextDrawingMode.Fill);
                    context.SetFillColorWithColor(shadowColor.CGColor);
                    context.SetShadowWithColor(shadowOffset, 0,
shadowColor.CGColor);
                    //context.ShowGlyphsAtPoint(
                    context.ShowTextAtPoint(alignment.X, alignment.Y, text);
                }
                finally
                {
                    context.RestoreState();
                }

                textColor.SetFill();
                context.SetTextDrawingMode(CGTextDrawingMode.Fill);
                context.ShowTextAtPoint(alignment.X, alignment.Y, text);

                context.TextMatrix = CGAffineTransform.MakeIdentity();
                           }
            }
            finally
            {
                context.RestoreState();
            }
        }


Steps to reproduce the problem:
1. Try and execute the above function to see inconsistencies between monotouch
and objective-c 
2. The UIStringDrawing methods cannot be found?


Actual Results:


Expected Results:
UIStringDrawing.h methods would be awesome for situations where thread safety
is required.

How often does this happen? 
You tell me?


Additional Information:

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list