1 /*****************************************************************************
2 * quartztext.c : Put text on the video, using Mac OS X Quartz Engine
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Bernie Purcell <b dot purcell at adbglobal dot com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 //////////////////////////////////////////////////////////////////////////////
26 //////////////////////////////////////////////////////////////////////////////
27 #include <stdlib.h> // malloc(), free()
33 #include <vlc_block.h>
34 #include <vlc_filter.h>
35 #include <vlc_stream.h>
40 #include <Carbon/Carbon.h>
42 #define DEFAULT_FONT "Verdana"
43 #define DEFAULT_FONT_COLOR 0xffffff
44 #define DEFAULT_REL_FONT_SIZE 16
46 #define VERTICAL_MARGIN 3
47 #define HORIZONTAL_MARGIN 10
49 //////////////////////////////////////////////////////////////////////////////
51 //////////////////////////////////////////////////////////////////////////////
52 static int Create ( vlc_object_t * );
53 static void Destroy( vlc_object_t * );
55 static int RenderText( filter_t *, subpicture_region_t *,
56 subpicture_region_t * );
57 static int RenderHtml( filter_t *, subpicture_region_t *,
58 subpicture_region_t * );
60 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
61 UniChar *psz_utfString, uint32_t i_text_len,
62 uint32_t i_runs, uint32_t *pi_run_lengths,
63 ATSUStyle *pp_styles );
64 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size,
65 int i_font_color, int i_font_alpha,
66 vlc_bool_t b_bold, vlc_bool_t b_italic,
68 //////////////////////////////////////////////////////////////////////////////
70 //////////////////////////////////////////////////////////////////////////////
72 // The preferred way to set font style information is for it to come from the
73 // subtitle file, and for it to be rendered with RenderHtml instead of
74 // RenderText. This module, unlike Freetype, doesn't provide any options to
75 // override the fallback font selection used when this style information is
78 set_shortname( _("Mac Text renderer"));
79 set_description( _("Quartz font renderer") );
80 set_category( CAT_VIDEO );
81 set_subcategory( SUBCAT_VIDEO_SUBPIC );
83 set_capability( "text renderer", 120 );
84 add_shortcut( "text" );
85 set_callbacks( Create, Destroy );
88 typedef struct font_stack_t font_stack_t;
99 typedef struct offscreen_bitmap_t offscreen_bitmap_t;
100 struct offscreen_bitmap_t
103 int i_bitsPerChannel;
109 //////////////////////////////////////////////////////////////////////////////
110 // filter_sys_t: quartztext local data
111 //////////////////////////////////////////////////////////////////////////////
112 // This structure is part of the video output thread descriptor.
113 // It describes the freetype specific properties of an output thread.
114 //////////////////////////////////////////////////////////////////////////////
118 uint8_t i_font_opacity;
123 //////////////////////////////////////////////////////////////////////////////
124 // Create: allocates osd-text video thread output method
125 //////////////////////////////////////////////////////////////////////////////
126 // This function allocates and initializes a Clone vout method.
127 //////////////////////////////////////////////////////////////////////////////
128 static int Create( vlc_object_t *p_this )
130 filter_t *p_filter = (filter_t *)p_this;
133 // Allocate structure
134 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
137 msg_Err( p_filter, "out of memory" );
140 p_sys->psz_font_name = strdup( DEFAULT_FONT );
141 p_sys->i_font_opacity = 255;
142 p_sys->i_font_color = DEFAULT_FONT_COLOR;
143 p_sys->i_font_size = p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
145 p_filter->pf_render_text = RenderText;
146 p_filter->pf_render_html = RenderHtml;
151 //////////////////////////////////////////////////////////////////////////////
152 // Destroy: destroy Clone video thread output method
153 //////////////////////////////////////////////////////////////////////////////
154 // Clean up all data and library connections
155 //////////////////////////////////////////////////////////////////////////////
156 static void Destroy( vlc_object_t *p_this )
158 filter_t *p_filter = (filter_t *)p_this;
159 filter_sys_t *p_sys = p_filter->p_sys;
161 free( p_sys->psz_font_name );
165 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
166 // Original version of these functions available on:
167 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
169 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
171 static CMProfileRef OpenGenericProfile( void )
173 static CMProfileRef cached_rgb_prof = NULL;
175 // Create the profile reference only once
176 if( cached_rgb_prof == NULL )
179 CMProfileLocation loc;
181 loc.locType = cmPathBasedProfile;
182 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
184 err = CMOpenProfile( &cached_rgb_prof, &loc );
188 cached_rgb_prof = NULL;
192 if( cached_rgb_prof )
194 // Clone the profile reference so that the caller has
195 // their own reference, not our cached one.
196 CMCloneProfileRef( cached_rgb_prof );
199 return cached_rgb_prof;
202 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
204 static CGColorSpaceRef p_generic_rgb_cs = NULL;
206 if( p_generic_rgb_cs == NULL )
208 CMProfileRef generic_rgb_prof = OpenGenericProfile();
210 if( generic_rgb_prof )
212 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
214 CMCloseProfile( generic_rgb_prof );
218 return p_generic_rgb_cs;
222 static char *EliminateCRLF( char *psz_string )
227 for( p = psz_string; p && *p; p++ )
229 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
231 for( q = p + 1; *q; q++ )
240 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
241 // we don't need to worry about bidirectional text conversion as ATSUI should
242 // handle that for us automatically
243 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
245 CFStringRef p_cfString;
248 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
249 i_string_length = CFStringGetLength( p_cfString );
252 *pi_strlen = i_string_length;
254 if( !*ppsz_utf16_str )
255 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
257 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
259 CFRelease( p_cfString );
262 // Renders a text subpicture region into another one.
263 // It is used as pf_add_string callback in the vout method by this module
264 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
265 subpicture_region_t *p_region_in )
267 filter_sys_t *p_sys = p_filter->p_sys;
268 UniChar *psz_utf16_str = NULL;
269 uint32_t i_string_length;
271 int i_font_color, i_font_alpha, i_font_size;
274 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
275 psz_string = p_region_in->psz_text;
276 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
278 if( p_region_in->p_style )
280 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
281 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
282 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 );
286 i_font_color = p_sys->i_font_color;
287 i_font_alpha = 255 - p_sys->i_font_opacity;
288 i_font_size = p_sys->i_font_size;
291 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
293 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
295 p_region_out->i_x = p_region_in->i_x;
296 p_region_out->i_y = p_region_in->i_y;
298 if( psz_utf16_str != NULL )
300 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
301 i_font_color, i_font_alpha,
302 VLC_FALSE, VLC_FALSE, VLC_FALSE );
305 RenderYUVA( p_filter, p_region_out, psz_utf16_str, i_string_length,
306 1, &i_string_length, &p_style );
309 ATSUDisposeStyle( p_style );
310 free( psz_utf16_str );
317 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size, int i_font_color, int i_font_alpha,
318 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
324 float f_red = (float)(( i_font_color & 0x00FF0000 ) >> 16) / 255.0;
325 float f_green = (float)(( i_font_color & 0x0000FF00 ) >> 8) / 255.0;
326 float f_blue = (float)( i_font_color & 0x000000FF ) / 255.0;
327 float f_alpha = ( 255.0 - (float)i_font_alpha) / 255.0;
330 Fixed font_size = IntToFixed( i_font_size );
331 ATSURGBAlphaColor font_color = { f_red, f_green, f_blue, f_alpha };
332 Boolean bold = b_bold;
333 Boolean italic = b_italic;
334 Boolean uline = b_uline;
336 ATSUAttributeTag tags[] = { kATSUSizeTag, kATSURGBAlphaColorTag, kATSUQDItalicTag,
337 kATSUQDBoldfaceTag, kATSUQDUnderlineTag, kATSUFontTag };
338 ByteCount sizes[] = { sizeof( Fixed ), sizeof( ATSURGBAlphaColor ), sizeof( Boolean ),
339 sizeof( Boolean ), sizeof( Boolean ), sizeof( ATSUFontID )};
340 ATSUAttributeValuePtr values[] = { &font_size, &font_color, &italic, &bold, &uline, &font };
342 i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
344 status = ATSUFindFontFromName( psz_fontname,
345 strlen( psz_fontname ),
352 if( status != noErr )
354 // If we can't find a suitable font, just do everything else
358 if( noErr == ATSUCreateStyle( &p_style ) )
360 if( noErr == ATSUSetAttributes( p_style, i_tag_cnt, tags, sizes, values ) )
364 ATSUDisposeStyle( p_style );
369 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
370 int i_color, int i_alpha )
377 p_new = malloc( sizeof( font_stack_t ) );
378 p_new->p_next = NULL;
381 p_new->psz_name = strdup( psz_name );
383 p_new->psz_name = NULL;
385 p_new->i_size = i_size;
386 p_new->i_color = i_color;
387 p_new->i_alpha = i_alpha;
395 font_stack_t *p_last;
397 for( p_last = *p_font;
399 p_last = p_last->p_next )
402 p_last->p_next = p_new;
407 static int PopFont( font_stack_t **p_font )
409 font_stack_t *p_last, *p_next_to_last;
411 if( !p_font || !*p_font )
414 p_next_to_last = NULL;
415 for( p_last = *p_font;
417 p_last = p_last->p_next )
419 p_next_to_last = p_last;
423 p_next_to_last->p_next = NULL;
427 free( p_last->psz_name );
433 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
434 int *i_color, int *i_alpha )
436 font_stack_t *p_last;
438 if( !p_font || !*p_font )
443 p_last=p_last->p_next )
446 *psz_name = p_last->psz_name;
447 *i_size = p_last->i_size;
448 *i_color = p_last->i_color;
449 *i_alpha = p_last->i_alpha;
454 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys, font_stack_t **p_fonts,
455 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
457 ATSUStyle p_style = NULL;
459 char *psz_fontname = NULL;
460 int i_font_color = p_sys->i_font_color;
461 int i_font_alpha = 0;
462 int i_font_size = p_sys->i_font_size;
464 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ) )
466 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color, i_font_alpha,
467 b_bold, b_italic, b_uline );
472 static void ProcessNodes( filter_t *p_filter, xml_reader_t *p_xml_reader,
473 text_style_t *p_font_style, UniChar *psz_text, int *pi_len,
474 uint32_t *pi_runs, uint32_t **ppi_run_lengths,
475 ATSUStyle **ppp_styles)
477 filter_sys_t *p_sys = p_filter->p_sys;
478 UniChar *psz_text_orig = psz_text;
479 font_stack_t *p_fonts = NULL;
481 char *psz_node = NULL;
483 vlc_bool_t b_italic = VLC_FALSE;
484 vlc_bool_t b_bold = VLC_FALSE;
485 vlc_bool_t b_uline = VLC_FALSE;
490 p_font_style->psz_fontname,
491 p_font_style->i_font_size,
492 p_font_style->i_font_color,
493 p_font_style->i_font_alpha );
495 if( p_font_style->i_style_flags & STYLE_BOLD )
497 if( p_font_style->i_style_flags & STYLE_ITALIC )
499 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
504 PushFont( &p_fonts, p_sys->psz_font_name, p_sys->i_font_size, p_sys->i_font_color, 0 );
507 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
509 switch ( xml_ReaderNodeType( p_xml_reader ) )
511 case XML_READER_NONE:
513 case XML_READER_ENDELEM:
514 psz_node = xml_ReaderName( p_xml_reader );
518 if( !strcasecmp( "font", psz_node ) )
520 else if( !strcasecmp( "b", psz_node ) )
522 else if( !strcasecmp( "i", psz_node ) )
523 b_italic = VLC_FALSE;
524 else if( !strcasecmp( "u", psz_node ) )
530 case XML_READER_STARTELEM:
531 psz_node = xml_ReaderName( p_xml_reader );
534 if( !strcasecmp( "font", psz_node ) )
536 char *psz_fontname = NULL;
537 int i_font_color = 0xffffff;
538 int i_font_alpha = 0;
539 int i_font_size = 24;
541 // Default all attributes to the top font in the stack -- in case not
542 // all attributes are specified in the sub-font
543 if( VLC_SUCCESS == PeekFont( &p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ))
545 psz_fontname = strdup( psz_fontname );
548 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
550 char *psz_name = xml_ReaderName ( p_xml_reader );
551 char *psz_value = xml_ReaderValue ( p_xml_reader );
553 if( psz_name && psz_value )
555 if( !strcasecmp( "face", psz_name ) )
557 if( psz_fontname ) free( psz_fontname );
558 psz_fontname = strdup( psz_value );
560 else if( !strcasecmp( "size", psz_name ) )
562 i_font_size = atoi( psz_value );
564 else if( !strcasecmp( "color", psz_name ) &&
565 ( psz_value[0] == '#' ) )
567 i_font_color = strtol( psz_value+1, NULL, 16 );
568 i_font_color &= 0x00ffffff;
570 else if( !strcasecmp( "alpha", psz_name ) &&
571 ( psz_value[0] == '#' ) )
573 i_font_alpha = strtol( psz_value+1, NULL, 16 );
574 i_font_alpha &= 0xff;
580 PushFont( &p_fonts, psz_fontname, i_font_size, i_font_color, i_font_alpha );
581 free( psz_fontname );
583 else if( !strcasecmp( "b", psz_node ) )
587 else if( !strcasecmp( "i", psz_node ) )
591 else if( !strcasecmp( "u", psz_node ) )
595 else if( !strcasecmp( "br", psz_node ) )
597 uint32_t i_string_length;
599 ConvertToUTF16( "\n", &i_string_length, &psz_text );
600 psz_text += i_string_length;
605 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
607 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
609 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
611 if( *ppi_run_lengths )
612 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
614 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
616 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
621 case XML_READER_TEXT:
622 psz_node = xml_ReaderValue( p_xml_reader );
625 uint32_t i_string_length;
627 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
628 psz_text += i_string_length;
633 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
635 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
637 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
639 if( *ppi_run_lengths )
640 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
642 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
644 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
652 *pi_len = psz_text - psz_text_orig;
654 while( VLC_SUCCESS == PopFont( &p_fonts ) );
657 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
658 subpicture_region_t *p_region_in )
660 int rv = VLC_SUCCESS;
661 stream_t *p_sub = NULL;
663 xml_reader_t *p_xml_reader = NULL;
665 if( !p_region_in || !p_region_in->psz_html )
668 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
669 (uint8_t *) p_region_in->psz_html,
670 strlen( p_region_in->psz_html ),
674 p_xml = xml_Create( p_filter );
677 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
683 uint32_t *pi_run_lengths = NULL;
684 ATSUStyle *pp_styles = NULL;
686 psz_text = (UniChar *) calloc( strlen( p_region_in->psz_html ), sizeof( UniChar ) );
691 ProcessNodes( p_filter, p_xml_reader, p_region_in->p_style, psz_text,
692 &i_len, &i_runs, &pi_run_lengths, &pp_styles );
694 p_region_out->i_x = p_region_in->i_x;
695 p_region_out->i_y = p_region_in->i_y;
697 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs, pi_run_lengths, pp_styles);
699 for( k=0; k<i_runs; k++)
700 ATSUDisposeStyle( pp_styles[k] );
702 free( pi_run_lengths );
707 xml_ReaderDelete( p_xml, p_xml_reader );
711 stream_Delete( p_sub );
713 // No longer need a HTML version of the text
714 free( p_region_in->psz_html );
715 p_region_in->psz_html = NULL;
720 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
721 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
723 offscreen_bitmap_t *p_bitmap;
724 CGContextRef p_context = NULL;
726 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
729 p_bitmap->i_bitsPerChannel = 8;
730 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
731 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
732 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
734 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
736 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
737 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
739 *pp_colorSpace = CreateGenericRGBColorSpace();
742 if( p_bitmap->p_data && *pp_colorSpace )
744 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
745 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
746 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
750 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
751 // OS X 10.1 doesn't support weak linking of this call which is only available
752 // int 10.4 and later
753 if( CGContextSetAllowsAntialiasing != NULL )
755 CGContextSetAllowsAntialiasing( p_context, true );
759 *pp_memory = p_bitmap;
765 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
766 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
767 int i_width, int i_height, int *pi_textblock_height )
769 offscreen_bitmap_t *p_offScreen = NULL;
770 CGColorSpaceRef p_colorSpace = NULL;
771 CGContextRef p_context = NULL;
773 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
777 ATSUTextLayout p_textLayout;
778 OSStatus status = noErr;
780 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
782 (const UniCharCount *) pi_run_lengths,
785 if( status == noErr )
787 // Attach our offscreen Image Graphics Context to the text style
788 // and setup the line alignment (have to specify the line width
789 // also in order for our chosen alignment to work)
791 Fract alignment = kATSUStartAlignment;
792 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
794 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
795 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
796 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
798 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
800 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
802 alignment = kATSUEndAlignment;
804 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
806 alignment = kATSUCenterAlignment;
809 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
811 // let ATSUI deal with characters not-in-our-specified-font
812 ATSUSetTransientFontMatching( p_textLayout, true );
814 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
815 Fixed y = Long2Fix( i_height );
817 // Set the line-breaks and draw individual lines
818 uint32_t i_start = 0;
819 uint32_t i_end = i_text_len;
821 // Set up black outlining of the text --
822 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
823 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
827 // ATSUBreakLine will automatically pick up any manual '\n's also
828 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
829 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
833 uint32_t i_actualSize;
835 // Come down far enough to fit the height of this line --
836 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
837 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
839 // Quartz uses an upside-down co-ordinate space -> y values decrease as
840 // you move down the page
843 // Set the outlining for this line to be dependant on the size of the line -
844 // make it about 5% of the ascent, with a minimum at 1.0
845 float f_thickness = FixedToFloat( ascent ) * 0.05;
846 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
848 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
850 // and now prepare for the next line by coming down far enough for our
852 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
853 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
861 while( i_end < i_text_len );
863 *pi_textblock_height = i_height - Fix2Long( y );
864 CGContextFlush( p_context );
866 ATSUDisposeTextLayout( p_textLayout );
869 CGContextRelease( p_context );
871 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
876 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
877 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
879 offscreen_bitmap_t *p_offScreen = NULL;
880 int i_textblock_height = 0;
882 int i_width = p_filter->fmt_out.video.i_visible_width;
883 int i_height = p_filter->fmt_out.video.i_visible_height;
885 if( psz_utf16_str != NULL )
887 int i_text_align = 0;
889 if( p_region->p_style )
890 i_text_align = p_region->p_style->i_text_align;
892 i_text_align = p_region->i_align & 0x3;
894 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
895 i_runs, pi_run_lengths, pp_styles,
896 i_width, i_height, &i_textblock_height );
899 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
901 int x, y, i_offset, i_pitch;
902 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
903 subpicture_region_t *p_region_tmp;
905 // Create a new subpicture region
906 memset( &fmt, 0, sizeof(video_format_t) );
907 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
909 fmt.i_width = fmt.i_visible_width = i_width;
910 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
911 fmt.i_x_offset = fmt.i_y_offset = 0;
912 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
915 msg_Err( p_filter, "cannot allocate SPU region" );
918 p_region->fmt = p_region_tmp->fmt;
919 p_region->picture = p_region_tmp->picture;
920 free( p_region_tmp );
922 p_dst_y = p_region->picture.Y_PIXELS;
923 p_dst_u = p_region->picture.U_PIXELS;
924 p_dst_v = p_region->picture.V_PIXELS;
925 p_dst_a = p_region->picture.A_PIXELS;
926 i_pitch = p_region->picture.A_PITCH;
928 i_offset = VERTICAL_MARGIN *i_pitch;
929 for( y=0; y<i_textblock_height; y++)
931 for( x=0; x<i_width; x++)
933 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
934 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
935 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
936 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
938 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
939 802 * i_blue + 4096 + 131072 ) >> 13, 235);
940 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
941 3598 * i_blue + 4096 + 1048576) >> 13, 240);
942 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
943 -585 * i_blue + 4096 + 1048576) >> 13, 240);
945 p_dst_y[ i_offset + x ] = i_y;
946 p_dst_u[ i_offset + x ] = i_u;
947 p_dst_v[ i_offset + x ] = i_v;
948 p_dst_a[ i_offset + x ] = i_alpha;
953 free( p_offScreen->p_data );