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 <bitmap@videolan.org>
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 //////////////////////////////////////////////////////////////////////////////
31 #include <vlc_block.h>
32 #include <vlc_filter.h>
33 #include <vlc_stream.h>
35 #include <vlc_input.h>
39 #include <Carbon/Carbon.h>
41 #define DEFAULT_FONT "Verdana"
42 #define DEFAULT_FONT_COLOR 0xffffff
43 #define DEFAULT_REL_FONT_SIZE 16
45 #define VERTICAL_MARGIN 3
46 #define HORIZONTAL_MARGIN 10
48 //////////////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////////////
51 static int Create ( vlc_object_t * );
52 static void Destroy( vlc_object_t * );
54 static int LoadFontsFromAttachments( filter_t *p_filter );
56 static int RenderText( filter_t *, subpicture_region_t *,
57 subpicture_region_t * );
58 static int RenderHtml( filter_t *, subpicture_region_t *,
59 subpicture_region_t * );
61 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
62 UniChar *psz_utfString, uint32_t i_text_len,
63 uint32_t i_runs, uint32_t *pi_run_lengths,
64 ATSUStyle *pp_styles );
65 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size,
66 int i_font_color, int i_font_alpha,
67 vlc_bool_t b_bold, vlc_bool_t b_italic,
69 //////////////////////////////////////////////////////////////////////////////
71 //////////////////////////////////////////////////////////////////////////////
73 // The preferred way to set font style information is for it to come from the
74 // subtitle file, and for it to be rendered with RenderHtml instead of
75 // RenderText. This module, unlike Freetype, doesn't provide any options to
76 // override the fallback font selection used when this style information is
79 set_shortname( _("Mac Text renderer"));
80 set_description( _("Quartz font renderer") );
81 set_category( CAT_VIDEO );
82 set_subcategory( SUBCAT_VIDEO_SUBPIC );
84 set_capability( "text renderer", 120 );
85 add_shortcut( "text" );
86 set_callbacks( Create, Destroy );
89 typedef struct font_stack_t font_stack_t;
100 typedef struct offscreen_bitmap_t offscreen_bitmap_t;
101 struct offscreen_bitmap_t
104 int i_bitsPerChannel;
110 //////////////////////////////////////////////////////////////////////////////
111 // filter_sys_t: quartztext local data
112 //////////////////////////////////////////////////////////////////////////////
113 // This structure is part of the video output thread descriptor.
114 // It describes the freetype specific properties of an output thread.
115 //////////////////////////////////////////////////////////////////////////////
119 uint8_t i_font_opacity;
123 ATSFontContainerRef *p_fonts;
127 //////////////////////////////////////////////////////////////////////////////
128 // Create: allocates osd-text video thread output method
129 //////////////////////////////////////////////////////////////////////////////
130 // This function allocates and initializes a Clone vout method.
131 //////////////////////////////////////////////////////////////////////////////
132 static int Create( vlc_object_t *p_this )
134 filter_t *p_filter = (filter_t *)p_this;
137 // Allocate structure
138 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
141 msg_Err( p_filter, "out of memory" );
144 p_sys->psz_font_name = strdup( DEFAULT_FONT );
145 p_sys->i_font_opacity = 255;
146 p_sys->i_font_color = DEFAULT_FONT_COLOR;
147 p_sys->i_font_size = p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
149 p_filter->pf_render_text = RenderText;
150 p_filter->pf_render_html = RenderHtml;
152 p_sys->p_fonts = NULL;
155 LoadFontsFromAttachments( p_filter );
160 //////////////////////////////////////////////////////////////////////////////
161 // Destroy: destroy Clone video thread output method
162 //////////////////////////////////////////////////////////////////////////////
163 // Clean up all data and library connections
164 //////////////////////////////////////////////////////////////////////////////
165 static void Destroy( vlc_object_t *p_this )
167 filter_t *p_filter = (filter_t *)p_this;
168 filter_sys_t *p_sys = p_filter->p_sys;
174 for( k = 0; k < p_sys->i_fonts; k++ )
176 ATSFontDeactivate( p_sys->p_fonts[k], NULL, kATSOptionFlagsDefault );
179 free( p_sys->p_fonts );
182 if( p_sys->psz_font_name ) free( p_sys->psz_font_name );
187 //////////////////////////////////////////////////////////////////////////////
188 // Make any TTF/OTF fonts present in the attachments of the media file
189 // available to the Quartz engine for text rendering
190 //////////////////////////////////////////////////////////////////////////////
191 static int LoadFontsFromAttachments( filter_t *p_filter )
193 filter_sys_t *p_sys = p_filter->p_sys;
194 input_thread_t *p_input;
195 input_attachment_t **pp_attachments;
196 int i_attachments_cnt;
198 int rv = VLC_SUCCESS;
200 p_input = (input_thread_t *)vlc_object_find( p_filter, VLC_OBJECT_INPUT, FIND_PARENT );
204 if( VLC_SUCCESS != input_Control( p_input, INPUT_GET_ATTACHMENTS, &pp_attachments, &i_attachments_cnt ))
206 vlc_object_release(p_input);
211 p_sys->p_fonts = malloc( i_attachments_cnt * sizeof( ATSFontContainerRef ) );
212 if(! p_sys->p_fonts )
215 for( k = 0; k < i_attachments_cnt; k++ )
217 input_attachment_t *p_attach = pp_attachments[k];
221 if(( !strcmp( p_attach->psz_mime, "application/x-truetype-font" ) || // TTF
222 !strcmp( p_attach->psz_mime, "application/x-font-otf" ) ) && // OTF
223 ( p_attach->i_data > 0 ) &&
224 ( p_attach->p_data != NULL ) )
226 ATSFontContainerRef container;
228 if( noErr == ATSFontActivateFromMemory( p_attach->p_data,
230 kATSFontContextLocal,
231 kATSFontFormatUnspecified,
233 kATSOptionFlagsDefault,
236 p_sys->p_fonts[ p_sys->i_fonts++ ] = container;
240 vlc_input_attachment_Delete( p_attach );
242 free( pp_attachments );
244 vlc_object_release(p_input);
249 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
250 // Original version of these functions available on:
251 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
253 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
255 static CMProfileRef OpenGenericProfile( void )
257 static CMProfileRef cached_rgb_prof = NULL;
259 // Create the profile reference only once
260 if( cached_rgb_prof == NULL )
263 CMProfileLocation loc;
265 loc.locType = cmPathBasedProfile;
266 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
268 err = CMOpenProfile( &cached_rgb_prof, &loc );
272 cached_rgb_prof = NULL;
276 if( cached_rgb_prof )
278 // Clone the profile reference so that the caller has
279 // their own reference, not our cached one.
280 CMCloneProfileRef( cached_rgb_prof );
283 return cached_rgb_prof;
286 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
288 static CGColorSpaceRef p_generic_rgb_cs = NULL;
290 if( p_generic_rgb_cs == NULL )
292 CMProfileRef generic_rgb_prof = OpenGenericProfile();
294 if( generic_rgb_prof )
296 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
298 CMCloseProfile( generic_rgb_prof );
302 return p_generic_rgb_cs;
306 static char *EliminateCRLF( char *psz_string )
311 for( p = psz_string; p && *p; p++ )
313 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
315 for( q = p + 1; *q; q++ )
324 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
325 // we don't need to worry about bidirectional text conversion as ATSUI should
326 // handle that for us automatically
327 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
329 CFStringRef p_cfString;
332 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
333 i_string_length = CFStringGetLength( p_cfString );
336 *pi_strlen = i_string_length;
338 if( !*ppsz_utf16_str )
339 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
341 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
343 CFRelease( p_cfString );
346 // Renders a text subpicture region into another one.
347 // It is used as pf_add_string callback in the vout method by this module
348 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
349 subpicture_region_t *p_region_in )
351 filter_sys_t *p_sys = p_filter->p_sys;
352 UniChar *psz_utf16_str = NULL;
353 uint32_t i_string_length;
355 int i_font_color, i_font_alpha, i_font_size;
358 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
359 psz_string = p_region_in->psz_text;
360 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
362 if( p_region_in->p_style )
364 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
365 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
366 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 );
370 i_font_color = p_sys->i_font_color;
371 i_font_alpha = 255 - p_sys->i_font_opacity;
372 i_font_size = p_sys->i_font_size;
375 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
377 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
379 p_region_out->i_x = p_region_in->i_x;
380 p_region_out->i_y = p_region_in->i_y;
382 if( psz_utf16_str != NULL )
384 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
385 i_font_color, i_font_alpha,
386 VLC_FALSE, VLC_FALSE, VLC_FALSE );
389 RenderYUVA( p_filter, p_region_out, psz_utf16_str, i_string_length,
390 1, &i_string_length, &p_style );
393 ATSUDisposeStyle( p_style );
394 free( psz_utf16_str );
401 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size, int i_font_color, int i_font_alpha,
402 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
408 float f_red = (float)(( i_font_color & 0x00FF0000 ) >> 16) / 255.0;
409 float f_green = (float)(( i_font_color & 0x0000FF00 ) >> 8) / 255.0;
410 float f_blue = (float)( i_font_color & 0x000000FF ) / 255.0;
411 float f_alpha = ( 255.0 - (float)i_font_alpha) / 255.0;
414 Fixed font_size = IntToFixed( i_font_size );
415 ATSURGBAlphaColor font_color = { f_red, f_green, f_blue, f_alpha };
416 Boolean bold = b_bold;
417 Boolean italic = b_italic;
418 Boolean uline = b_uline;
420 ATSUAttributeTag tags[] = { kATSUSizeTag, kATSURGBAlphaColorTag, kATSUQDItalicTag,
421 kATSUQDBoldfaceTag, kATSUQDUnderlineTag, kATSUFontTag };
422 ByteCount sizes[] = { sizeof( Fixed ), sizeof( ATSURGBAlphaColor ), sizeof( Boolean ),
423 sizeof( Boolean ), sizeof( Boolean ), sizeof( ATSUFontID )};
424 ATSUAttributeValuePtr values[] = { &font_size, &font_color, &italic, &bold, &uline, &font };
426 i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
428 status = ATSUFindFontFromName( psz_fontname,
429 strlen( psz_fontname ),
436 if( status != noErr )
438 // If we can't find a suitable font, just do everything else
442 if( noErr == ATSUCreateStyle( &p_style ) )
444 if( noErr == ATSUSetAttributes( p_style, i_tag_cnt, tags, sizes, values ) )
448 ATSUDisposeStyle( p_style );
453 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
454 int i_color, int i_alpha )
461 p_new = malloc( sizeof( font_stack_t ) );
465 p_new->p_next = NULL;
468 p_new->psz_name = strdup( psz_name );
470 p_new->psz_name = NULL;
472 p_new->i_size = i_size;
473 p_new->i_color = i_color;
474 p_new->i_alpha = i_alpha;
482 font_stack_t *p_last;
484 for( p_last = *p_font;
486 p_last = p_last->p_next )
489 p_last->p_next = p_new;
494 static int PopFont( font_stack_t **p_font )
496 font_stack_t *p_last, *p_next_to_last;
498 if( !p_font || !*p_font )
501 p_next_to_last = NULL;
502 for( p_last = *p_font;
504 p_last = p_last->p_next )
506 p_next_to_last = p_last;
510 p_next_to_last->p_next = NULL;
514 free( p_last->psz_name );
520 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
521 int *i_color, int *i_alpha )
523 font_stack_t *p_last;
525 if( !p_font || !*p_font )
530 p_last=p_last->p_next )
533 *psz_name = p_last->psz_name;
534 *i_size = p_last->i_size;
535 *i_color = p_last->i_color;
536 *i_alpha = p_last->i_alpha;
541 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys, font_stack_t **p_fonts,
542 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
544 ATSUStyle p_style = NULL;
546 char *psz_fontname = NULL;
547 int i_font_color = p_sys->i_font_color;
548 int i_font_alpha = 0;
549 int i_font_size = p_sys->i_font_size;
551 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ) )
553 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color, i_font_alpha,
554 b_bold, b_italic, b_uline );
559 static void ProcessNodes( filter_t *p_filter, xml_reader_t *p_xml_reader,
560 text_style_t *p_font_style, UniChar *psz_text, int *pi_len,
561 uint32_t *pi_runs, uint32_t **ppi_run_lengths,
562 ATSUStyle **ppp_styles)
564 filter_sys_t *p_sys = p_filter->p_sys;
565 UniChar *psz_text_orig = psz_text;
566 font_stack_t *p_fonts = NULL;
568 char *psz_node = NULL;
570 vlc_bool_t b_italic = VLC_FALSE;
571 vlc_bool_t b_bold = VLC_FALSE;
572 vlc_bool_t b_uline = VLC_FALSE;
576 rv = PushFont( &p_fonts,
577 p_font_style->psz_fontname,
578 p_font_style->i_font_size,
579 p_font_style->i_font_color,
580 p_font_style->i_font_alpha );
582 if( p_font_style->i_style_flags & STYLE_BOLD )
584 if( p_font_style->i_style_flags & STYLE_ITALIC )
586 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
591 rv = PushFont( &p_fonts,
592 p_sys->psz_font_name,
594 p_sys->i_font_color, 0 );
596 if( rv != VLC_SUCCESS )
599 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
601 switch ( xml_ReaderNodeType( p_xml_reader ) )
603 case XML_READER_NONE:
605 case XML_READER_ENDELEM:
606 psz_node = xml_ReaderName( p_xml_reader );
610 if( !strcasecmp( "font", psz_node ) )
612 else if( !strcasecmp( "b", psz_node ) )
614 else if( !strcasecmp( "i", psz_node ) )
615 b_italic = VLC_FALSE;
616 else if( !strcasecmp( "u", psz_node ) )
622 case XML_READER_STARTELEM:
623 psz_node = xml_ReaderName( p_xml_reader );
626 if( !strcasecmp( "font", psz_node ) )
628 char *psz_fontname = NULL;
629 int i_font_color = 0xffffff;
630 int i_font_alpha = 0;
631 int i_font_size = 24;
633 // Default all attributes to the top font in the stack -- in case not
634 // all attributes are specified in the sub-font
635 if( VLC_SUCCESS == PeekFont( &p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ))
637 psz_fontname = strdup( psz_fontname );
640 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
642 char *psz_name = xml_ReaderName ( p_xml_reader );
643 char *psz_value = xml_ReaderValue ( p_xml_reader );
645 if( psz_name && psz_value )
647 if( !strcasecmp( "face", psz_name ) )
649 if( psz_fontname ) free( psz_fontname );
650 psz_fontname = strdup( psz_value );
652 else if( !strcasecmp( "size", psz_name ) )
654 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
656 int i_value = atoi( psz_value );
658 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
659 i_font_size += ( i_value * i_font_size ) / 10;
660 else if( i_value < -5 )
661 i_font_size = - i_value;
662 else if( i_value > 5 )
663 i_font_size = i_value;
666 i_font_size = atoi( psz_value );
668 else if( !strcasecmp( "color", psz_name ) &&
669 ( psz_value[0] == '#' ) )
671 i_font_color = strtol( psz_value+1, NULL, 16 );
672 i_font_color &= 0x00ffffff;
674 else if( !strcasecmp( "alpha", psz_name ) &&
675 ( psz_value[0] == '#' ) )
677 i_font_alpha = strtol( psz_value+1, NULL, 16 );
678 i_font_alpha &= 0xff;
684 PushFont( &p_fonts, psz_fontname, i_font_size, i_font_color, i_font_alpha );
685 free( psz_fontname );
687 else if( !strcasecmp( "b", psz_node ) )
691 else if( !strcasecmp( "i", psz_node ) )
695 else if( !strcasecmp( "u", psz_node ) )
699 else if( !strcasecmp( "br", psz_node ) )
701 uint32_t i_string_length;
703 ConvertToUTF16( "\n", &i_string_length, &psz_text );
704 psz_text += i_string_length;
709 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
711 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
713 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
715 if( *ppi_run_lengths )
716 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
718 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
720 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
725 case XML_READER_TEXT:
726 psz_node = xml_ReaderValue( p_xml_reader );
729 uint32_t i_string_length;
731 // Turn any multiple-whitespaces into single spaces
732 char *s = strpbrk( psz_node, "\t\r\n " );
735 int i_whitespace = strspn( s, "\t\r\n " );
737 if( i_whitespace > 1 )
740 strlen( s ) - i_whitespace + 1 );
743 s = strpbrk( s, "\t\r\n " );
746 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
747 psz_text += i_string_length;
752 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
754 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
756 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
758 if( *ppi_run_lengths )
759 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
761 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
763 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
771 *pi_len = psz_text - psz_text_orig;
773 while( VLC_SUCCESS == PopFont( &p_fonts ) );
776 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
777 subpicture_region_t *p_region_in )
779 int rv = VLC_SUCCESS;
780 stream_t *p_sub = NULL;
782 xml_reader_t *p_xml_reader = NULL;
784 if( !p_region_in || !p_region_in->psz_html )
787 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
788 (uint8_t *) p_region_in->psz_html,
789 strlen( p_region_in->psz_html ),
793 p_xml = xml_Create( p_filter );
796 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
802 uint32_t *pi_run_lengths = NULL;
803 ATSUStyle *pp_styles = NULL;
805 psz_text = (UniChar *) calloc( strlen( p_region_in->psz_html ), sizeof( UniChar ) );
810 ProcessNodes( p_filter, p_xml_reader, p_region_in->p_style, psz_text,
811 &i_len, &i_runs, &pi_run_lengths, &pp_styles );
813 p_region_out->i_x = p_region_in->i_x;
814 p_region_out->i_y = p_region_in->i_y;
816 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs, pi_run_lengths, pp_styles);
818 for( k=0; k<i_runs; k++)
819 ATSUDisposeStyle( pp_styles[k] );
821 free( pi_run_lengths );
826 xml_ReaderDelete( p_xml, p_xml_reader );
830 stream_Delete( p_sub );
836 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
837 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
839 offscreen_bitmap_t *p_bitmap;
840 CGContextRef p_context = NULL;
842 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
845 p_bitmap->i_bitsPerChannel = 8;
846 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
847 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
848 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
850 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
852 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
853 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
855 *pp_colorSpace = CreateGenericRGBColorSpace();
858 if( p_bitmap->p_data && *pp_colorSpace )
860 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
861 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
862 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
866 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
867 // OS X 10.1 doesn't support weak linking of this call which is only available
868 // int 10.4 and later
869 if( CGContextSetAllowsAntialiasing != NULL )
871 CGContextSetAllowsAntialiasing( p_context, true );
875 *pp_memory = p_bitmap;
881 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
882 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
883 int i_width, int i_height, int *pi_textblock_height )
885 offscreen_bitmap_t *p_offScreen = NULL;
886 CGColorSpaceRef p_colorSpace = NULL;
887 CGContextRef p_context = NULL;
889 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
893 ATSUTextLayout p_textLayout;
894 OSStatus status = noErr;
896 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
898 (const UniCharCount *) pi_run_lengths,
901 if( status == noErr )
903 // Attach our offscreen Image Graphics Context to the text style
904 // and setup the line alignment (have to specify the line width
905 // also in order for our chosen alignment to work)
907 Fract alignment = kATSUStartAlignment;
908 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
910 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
911 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
912 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
914 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
916 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
918 alignment = kATSUEndAlignment;
920 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
922 alignment = kATSUCenterAlignment;
925 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
927 // let ATSUI deal with characters not-in-our-specified-font
928 ATSUSetTransientFontMatching( p_textLayout, true );
930 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
931 Fixed y = Long2Fix( i_height );
933 // Set the line-breaks and draw individual lines
934 uint32_t i_start = 0;
935 uint32_t i_end = i_text_len;
937 // Set up black outlining of the text --
938 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
939 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
943 // ATSUBreakLine will automatically pick up any manual '\n's also
944 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
945 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
949 uint32_t i_actualSize;
951 // Come down far enough to fit the height of this line --
952 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
953 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
955 // Quartz uses an upside-down co-ordinate space -> y values decrease as
956 // you move down the page
959 // Set the outlining for this line to be dependant on the size of the line -
960 // make it about 5% of the ascent, with a minimum at 1.0
961 float f_thickness = FixedToFloat( ascent ) * 0.05;
962 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
964 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
966 // and now prepare for the next line by coming down far enough for our
968 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
969 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
977 while( i_end < i_text_len );
979 *pi_textblock_height = i_height - Fix2Long( y );
980 CGContextFlush( p_context );
982 ATSUDisposeTextLayout( p_textLayout );
985 CGContextRelease( p_context );
987 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
992 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
993 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
995 offscreen_bitmap_t *p_offScreen = NULL;
996 int i_textblock_height = 0;
998 int i_width = p_filter->fmt_out.video.i_visible_width;
999 int i_height = p_filter->fmt_out.video.i_visible_height;
1000 int i_text_align = p_region->i_align & 0x3;
1002 if( !psz_utf16_str )
1004 msg_Err( p_filter, "Invalid argument to RenderYUVA" );
1005 return VLC_EGENERIC;
1008 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
1009 i_runs, pi_run_lengths, pp_styles,
1010 i_width, i_height, &i_textblock_height );
1014 msg_Err( p_filter, "No offscreen buffer" );
1015 return VLC_EGENERIC;
1018 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
1020 int x, y, i_offset, i_pitch;
1021 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
1022 subpicture_region_t *p_region_tmp;
1024 // Create a new subpicture region
1025 memset( &fmt, 0, sizeof(video_format_t) );
1026 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1028 fmt.i_width = fmt.i_visible_width = i_width;
1029 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
1030 fmt.i_x_offset = fmt.i_y_offset = 0;
1031 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
1034 msg_Err( p_filter, "cannot allocate SPU region" );
1035 return VLC_EGENERIC;
1037 p_region->fmt = p_region_tmp->fmt;
1038 p_region->picture = p_region_tmp->picture;
1039 free( p_region_tmp );
1041 p_dst_y = p_region->picture.Y_PIXELS;
1042 p_dst_u = p_region->picture.U_PIXELS;
1043 p_dst_v = p_region->picture.V_PIXELS;
1044 p_dst_a = p_region->picture.A_PIXELS;
1045 i_pitch = p_region->picture.A_PITCH;
1047 i_offset = VERTICAL_MARGIN *i_pitch;
1048 for( y=0; y<i_textblock_height; y++)
1050 for( x=0; x<i_width; x++)
1052 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
1053 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
1054 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
1055 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
1057 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
1058 802 * i_blue + 4096 + 131072 ) >> 13, 235);
1059 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
1060 3598 * i_blue + 4096 + 1048576) >> 13, 240);
1061 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
1062 -585 * i_blue + 4096 + 1048576) >> 13, 240);
1064 p_dst_y[ i_offset + x ] = i_y;
1065 p_dst_u[ i_offset + x ] = i_u;
1066 p_dst_v[ i_offset + x ] = i_v;
1067 p_dst_a[ i_offset + x ] = i_alpha;
1069 i_offset += i_pitch;
1072 free( p_offScreen->p_data );
1073 free( p_offScreen );