Does CGContextSetTextMatrix work for remote bitmaps?

I am creating an image overs using the context from CGBitmapContextCreate() .

When drawing text, I tried using:

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

but my text is still upside down. If I used standard conversions, this was correct:

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

My question is, should CGContextSetTextMatrix work for an off-frame bitmap? Maybe I did something wrong.

+2
source share
2 answers

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

The entire drawing passes through the current transformation matrix, and only the text also passes through the text matrix. Thus, for anything that is not text, you need to change the CTM.

You can use the CGContextConcatCTM function to combine your flip matrix in CTM into one function call, although I would find that translation + scale is easier to read. Note that combining one matrix with another does not coincide with replacing the old matrix with the new one.

There is no function to replace CTM with another matrix in Core Graphics; you can only contact him. You can get CTM, invert it and concatenate the inverse matrix to the current matrix to return to the identity matrix; then, concatenating the desired matrix, we get the matrix, which is your desired matrix without any other influences. However, there is no reason to go to all these efforts.

0
source

I ran into the same problem and solved with:

 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.

0
source

Source: https://habr.com/ru/post/1340041/


All Articles