Does CGContextSetTextMatrix work for offscreen bitmaps?

感情迁移 提交于 2020-01-02 09:44:46

问题


I'm creating an image offscreen using a context from CGBitmapContextCreate().

While drawing text, I tried to use the:

CGContextSetTextMatrix(contextRef, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0));

but my text was still upside down. If I used the standard transforms it was correct:

CGContextTranslateCTM(contextRef, 0.0, contextRect.size.height);
CGContextScaleCTM(contextRef, 1.0, -1.0);

My question is should the CGContextSetTextMatrix work for an offscreen bitmap? Maybe I was doing something wrong.


回答1:


No. The text matrix, as its name says, only affects text.

All drawing goes through the current transformation matrix, and only text also goes through the text matrix. So, for anything that isn't text, you need to change the CTM.

You can use the CGContextConcatCTM function to concatenate your flip matrix onto the CTM in one function call, although I would find the translate + scale easier to read. Note that concatenating one matrix onto another is not the same as replacing the old matrix with a new one.

There is no function to replace the CTM with a different matrix in Core Graphics; you can only concat onto it. You can get the CTM, invert it, and concat the inverse matrix onto the current matrix to get back to the identity matrix; then, concatenating your desired matrix onto that will result in the matrix being your desired matrix with no other influences. However, there's not much reason to go to all that effort.




回答2:


I ran into same problem, and solved by using:

CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
......
CGGlyph glyphs[text.length];
CTFontRef fontRef = CTFontCreateWithName((__bridge CFStringRef)(fontName), fontSize, NULL);
CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[text cStringUsingEncoding:NSUnicodeStringEncoding], glyphs, text.length);
CGContextShowGlyphsAtPoint(context, destX, destY, (const CGGlyph *)glyphs, ce.text.length);

Just FYI.



来源:https://stackoverflow.com/questions/3440825/does-cgcontextsettextmatrix-work-for-offscreen-bitmaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!