| 600 | |
| 601 | #if defined( CINDER_COCOA_TOUCH ) |
| 602 | Surface renderStringPow2( const string &str, const Font &font, const ColorA &color, ivec2 *actualSize, float *baselineOffset ) |
| 603 | { |
| 604 | ivec2 pixelSize, pow2PixelSize; |
| 605 | { // render "invisible" to a dummy context to determine string width |
| 606 | Surface temp( 1, 1, true, SurfaceChannelOrder::RGBA ); |
| 607 | ::CGContextRef cgContext = cocoa::createCgBitmapContext( temp ); |
| 608 | |
| 609 | ::CGContextSelectFont( cgContext, font.getName().c_str(), font.getSize(), kCGEncodingMacRoman ); |
| 610 | ::CGContextSetTextDrawingMode( cgContext, kCGTextInvisible ); |
| 611 | |
| 612 | ::CGPoint startPoint = ::CGContextGetTextPosition( cgContext ); |
| 613 | ::CGContextShowText( cgContext, str.c_str(), str.length() ); |
| 614 | ::CGPoint endPoint = ::CGContextGetTextPosition( cgContext ); |
| 615 | pixelSize = ivec2( math<float>::ceil( endPoint.x - startPoint.x ), math<float>::ceil( font.getAscent() + font.getDescent() ) ); |
| 616 | ::CGContextRelease( cgContext ); |
| 617 | } |
| 618 | |
| 619 | pow2PixelSize = ivec2( nextPowerOf2( pixelSize.x ), nextPowerOf2( pixelSize.y ) ); |
| 620 | Surface result( pow2PixelSize.x, pow2PixelSize.y, true ); |
| 621 | ::CGContextRef cgContext = cocoa::createCgBitmapContext( result ); |
| 622 | ip::fill( &result, ColorA( 0, 0, 0, 0 ) ); |
| 623 | ::CGContextSelectFont( cgContext, font.getName().c_str(), font.getSize(), kCGEncodingMacRoman ); |
| 624 | ::CGContextSetTextDrawingMode( cgContext, kCGTextFill ); |
| 625 | ::CGContextSetRGBFillColor( cgContext, color.r, color.g, color.b, color.a ); |
| 626 | ::CGContextSetTextPosition( cgContext, 0, font.getDescent() + 1 ); |
| 627 | ::CGContextShowText( cgContext, str.c_str(), str.length() ); |
| 628 | |
| 629 | if( baselineOffset ) |
| 630 | *baselineOffset = font.getAscent() - pixelSize.y; |
| 631 | if( actualSize ) |
| 632 | *actualSize = pixelSize; |
| 633 | |
| 634 | ::CGContextRelease( cgContext ); |
| 635 | return result; |
| 636 | } |
| 637 | #elif defined( CINDER_MAC) || defined( CINDER_MSW_DESKTOP ) || defined( CINDER_ANDROID ) || defined( CINDER_LINUX ) |
| 638 | Surface renderString( const string &str, const Font &font, const ColorA &color, float *baselineOffset ) |
| 639 | { |
no test coverage detected