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 uint32_t i_font_color,
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;
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;
122 ATSFontContainerRef *p_fonts;
126 //////////////////////////////////////////////////////////////////////////////
127 // Create: allocates osd-text video thread output method
128 //////////////////////////////////////////////////////////////////////////////
129 // This function allocates and initializes a Clone vout method.
130 //////////////////////////////////////////////////////////////////////////////
131 static int Create( vlc_object_t *p_this )
133 filter_t *p_filter = (filter_t *)p_this;
136 // Allocate structure
137 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
140 msg_Err( p_filter, "out of memory" );
143 p_sys->psz_font_name = strdup( DEFAULT_FONT );
144 p_sys->i_font_opacity = 255;
145 p_sys->i_font_color = DEFAULT_FONT_COLOR;
146 p_sys->i_font_size = p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
148 p_filter->pf_render_text = RenderText;
149 p_filter->pf_render_html = RenderHtml;
151 p_sys->p_fonts = NULL;
154 LoadFontsFromAttachments( p_filter );
159 //////////////////////////////////////////////////////////////////////////////
160 // Destroy: destroy Clone video thread output method
161 //////////////////////////////////////////////////////////////////////////////
162 // Clean up all data and library connections
163 //////////////////////////////////////////////////////////////////////////////
164 static void Destroy( vlc_object_t *p_this )
166 filter_t *p_filter = (filter_t *)p_this;
167 filter_sys_t *p_sys = p_filter->p_sys;
173 for( k = 0; k < p_sys->i_fonts; k++ )
175 ATSFontDeactivate( p_sys->p_fonts[k], NULL, kATSOptionFlagsDefault );
178 free( p_sys->p_fonts );
181 if( p_sys->psz_font_name ) free( p_sys->psz_font_name );
186 //////////////////////////////////////////////////////////////////////////////
187 // Make any TTF/OTF fonts present in the attachments of the media file
188 // available to the Quartz engine for text rendering
189 //////////////////////////////////////////////////////////////////////////////
190 static int LoadFontsFromAttachments( filter_t *p_filter )
192 filter_sys_t *p_sys = p_filter->p_sys;
193 input_thread_t *p_input;
194 input_attachment_t **pp_attachments;
195 int i_attachments_cnt;
197 int rv = VLC_SUCCESS;
199 p_input = (input_thread_t *)vlc_object_find( p_filter, VLC_OBJECT_INPUT, FIND_PARENT );
203 if( VLC_SUCCESS != input_Control( p_input, INPUT_GET_ATTACHMENTS, &pp_attachments, &i_attachments_cnt ))
205 vlc_object_release(p_input);
210 p_sys->p_fonts = malloc( i_attachments_cnt * sizeof( ATSFontContainerRef ) );
211 if(! p_sys->p_fonts )
214 for( k = 0; k < i_attachments_cnt; k++ )
216 input_attachment_t *p_attach = pp_attachments[k];
220 if(( !strcmp( p_attach->psz_mime, "application/x-truetype-font" ) || // TTF
221 !strcmp( p_attach->psz_mime, "application/x-font-otf" ) ) && // OTF
222 ( p_attach->i_data > 0 ) &&
223 ( p_attach->p_data != NULL ) )
225 ATSFontContainerRef container;
227 if( noErr == ATSFontActivateFromMemory( p_attach->p_data,
229 kATSFontContextLocal,
230 kATSFontFormatUnspecified,
232 kATSOptionFlagsDefault,
235 p_sys->p_fonts[ p_sys->i_fonts++ ] = container;
239 vlc_input_attachment_Delete( p_attach );
241 free( pp_attachments );
243 vlc_object_release(p_input);
248 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
249 // Original version of these functions available on:
250 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
252 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
254 static CMProfileRef OpenGenericProfile( void )
256 static CMProfileRef cached_rgb_prof = NULL;
258 // Create the profile reference only once
259 if( cached_rgb_prof == NULL )
262 CMProfileLocation loc;
264 loc.locType = cmPathBasedProfile;
265 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
267 err = CMOpenProfile( &cached_rgb_prof, &loc );
271 cached_rgb_prof = NULL;
275 if( cached_rgb_prof )
277 // Clone the profile reference so that the caller has
278 // their own reference, not our cached one.
279 CMCloneProfileRef( cached_rgb_prof );
282 return cached_rgb_prof;
285 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
287 static CGColorSpaceRef p_generic_rgb_cs = NULL;
289 if( p_generic_rgb_cs == NULL )
291 CMProfileRef generic_rgb_prof = OpenGenericProfile();
293 if( generic_rgb_prof )
295 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
297 CMCloseProfile( generic_rgb_prof );
301 return p_generic_rgb_cs;
305 static char *EliminateCRLF( char *psz_string )
310 for( p = psz_string; p && *p; p++ )
312 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
314 for( q = p + 1; *q; q++ )
323 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
324 // we don't need to worry about bidirectional text conversion as ATSUI should
325 // handle that for us automatically
326 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
328 CFStringRef p_cfString;
331 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
332 i_string_length = CFStringGetLength( p_cfString );
335 *pi_strlen = i_string_length;
337 if( !*ppsz_utf16_str )
338 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
340 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
342 CFRelease( p_cfString );
345 // Renders a text subpicture region into another one.
346 // It is used as pf_add_string callback in the vout method by this module
347 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
348 subpicture_region_t *p_region_in )
350 filter_sys_t *p_sys = p_filter->p_sys;
351 UniChar *psz_utf16_str = NULL;
352 uint32_t i_string_length;
354 int i_font_color, i_font_alpha, i_font_size;
357 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
358 psz_string = p_region_in->psz_text;
359 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
361 if( p_region_in->p_style )
363 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
364 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
365 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 );
369 i_font_color = p_sys->i_font_color;
370 i_font_alpha = 255 - p_sys->i_font_opacity;
371 i_font_size = p_sys->i_font_size;
374 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
376 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
378 p_region_out->i_x = p_region_in->i_x;
379 p_region_out->i_y = p_region_in->i_y;
381 if( psz_utf16_str != NULL )
383 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
384 (i_font_color & 0xfffffff) |
385 ((i_font_alpha & 0xff) << 24),
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, uint32_t i_font_color,
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_color & 0xFF000000 ) >> 24)) / 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,
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;
481 font_stack_t *p_last;
483 for( p_last = *p_font;
485 p_last = p_last->p_next )
488 p_last->p_next = p_new;
493 static int PopFont( font_stack_t **p_font )
495 font_stack_t *p_last, *p_next_to_last;
497 if( !p_font || !*p_font )
500 p_next_to_last = NULL;
501 for( p_last = *p_font;
503 p_last = p_last->p_next )
505 p_next_to_last = p_last;
509 p_next_to_last->p_next = NULL;
513 free( p_last->psz_name );
519 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
522 font_stack_t *p_last;
524 if( !p_font || !*p_font )
529 p_last=p_last->p_next )
532 *psz_name = p_last->psz_name;
533 *i_size = p_last->i_size;
534 *i_color = p_last->i_color;
539 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys, font_stack_t **p_fonts,
540 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
542 ATSUStyle p_style = NULL;
544 char *psz_fontname = NULL;
545 uint32_t i_font_color = p_sys->i_font_color;
546 int i_font_size = p_sys->i_font_size;
548 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size, &i_font_color ) )
550 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color,
551 b_bold, b_italic, b_uline );
556 static int HandleFontAttributes( xml_reader_t *p_xml_reader,
557 font_stack_t **p_fonts )
560 char *psz_fontname = NULL;
561 uint32_t i_font_color = 0xffffff;
562 int i_font_alpha = 0;
563 int i_font_size = 24;
565 // Default all attributes to the top font in the stack -- in case not
566 // all attributes are specified in the sub-font
567 if( VLC_SUCCESS == PeekFont( p_fonts,
572 psz_fontname = strdup( psz_fontname );
574 i_font_alpha = (i_font_color >> 24) & 0xff;
575 i_font_color &= 0x00ffffff;
577 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
579 char *psz_name = xml_ReaderName( p_xml_reader );
580 char *psz_value = xml_ReaderValue( p_xml_reader );
582 if( psz_name && psz_value )
584 if( !strcasecmp( "face", psz_name ) )
586 if( psz_fontname ) free( psz_fontname );
587 psz_fontname = strdup( psz_value );
589 else if( !strcasecmp( "size", psz_name ) )
591 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
593 int i_value = atoi( psz_value );
595 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
596 i_font_size += ( i_value * i_font_size ) / 10;
597 else if( i_value < -5 )
598 i_font_size = - i_value;
599 else if( i_value > 5 )
600 i_font_size = i_value;
603 i_font_size = atoi( psz_value );
605 else if( !strcasecmp( "color", psz_name ) &&
606 ( psz_value[0] == '#' ) )
608 i_font_color = strtol( psz_value + 1, NULL, 16 );
609 i_font_color &= 0x00ffffff;
611 else if( !strcasecmp( "alpha", psz_name ) &&
612 ( psz_value[0] == '#' ) )
614 i_font_alpha = strtol( psz_value + 1, NULL, 16 );
615 i_font_alpha &= 0xff;
621 rv = PushFont( p_fonts,
624 (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24));
626 free( psz_fontname );
631 static int ProcessNodes( filter_t *p_filter,
632 xml_reader_t *p_xml_reader,
633 text_style_t *p_font_style,
638 uint32_t **ppi_run_lengths,
639 ATSUStyle **ppp_styles )
641 filter_sys_t *p_sys = p_filter->p_sys;
642 UniChar *psz_text_orig = psz_text;
643 font_stack_t *p_fonts = NULL;
644 int rv = VLC_SUCCESS;
646 char *psz_node = NULL;
648 vlc_bool_t b_italic = VLC_FALSE;
649 vlc_bool_t b_bold = VLC_FALSE;
650 vlc_bool_t b_uline = VLC_FALSE;
654 rv = PushFont( &p_fonts,
655 p_font_style->psz_fontname,
656 p_font_style->i_font_size,
657 (p_font_style->i_font_color & 0xffffff) |
658 ((p_font_style->i_font_alpha & 0xff) << 24) );
660 if( p_font_style->i_style_flags & STYLE_BOLD )
662 if( p_font_style->i_style_flags & STYLE_ITALIC )
664 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
669 rv = PushFont( &p_fonts,
670 p_sys->psz_font_name,
672 p_sys->i_font_color );
674 if( rv != VLC_SUCCESS )
677 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
679 switch ( xml_ReaderNodeType( p_xml_reader ) )
681 case XML_READER_NONE:
683 case XML_READER_ENDELEM:
684 psz_node = xml_ReaderName( p_xml_reader );
688 if( !strcasecmp( "font", psz_node ) )
690 else if( !strcasecmp( "b", psz_node ) )
692 else if( !strcasecmp( "i", psz_node ) )
693 b_italic = VLC_FALSE;
694 else if( !strcasecmp( "u", psz_node ) )
700 case XML_READER_STARTELEM:
701 psz_node = xml_ReaderName( p_xml_reader );
704 if( !strcasecmp( "font", psz_node ) )
705 rv = HandleFontAttributes( p_xml_reader, &p_fonts );
706 else if( !strcasecmp( "b", psz_node ) )
708 else if( !strcasecmp( "i", psz_node ) )
710 else if( !strcasecmp( "u", psz_node ) )
712 else if( !strcasecmp( "br", psz_node ) )
714 uint32_t i_string_length;
716 ConvertToUTF16( "\n", &i_string_length, &psz_text );
717 psz_text += i_string_length;
722 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
724 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
726 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
728 if( *ppi_run_lengths )
729 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
731 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
733 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
738 case XML_READER_TEXT:
739 psz_node = xml_ReaderValue( p_xml_reader );
742 uint32_t i_string_length;
744 // Turn any multiple-whitespaces into single spaces
745 char *s = strpbrk( psz_node, "\t\r\n " );
748 int i_whitespace = strspn( s, "\t\r\n " );
750 if( i_whitespace > 1 )
753 strlen( s ) - i_whitespace + 1 );
756 s = strpbrk( s, "\t\r\n " );
759 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
760 psz_text += i_string_length;
765 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
767 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
769 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
771 if( *ppi_run_lengths )
772 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
774 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
776 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
784 *pi_len = psz_text - psz_text_orig;
786 while( VLC_SUCCESS == PopFont( &p_fonts ) );
791 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
792 subpicture_region_t *p_region_in )
794 int rv = VLC_SUCCESS;
795 stream_t *p_sub = NULL;
797 xml_reader_t *p_xml_reader = NULL;
799 if( !p_region_in || !p_region_in->psz_html )
802 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
803 (uint8_t *) p_region_in->psz_html,
804 strlen( p_region_in->psz_html ),
808 p_xml = xml_Create( p_filter );
811 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
817 uint32_t *pi_run_lengths = NULL;
818 ATSUStyle *pp_styles = NULL;
820 psz_text = (UniChar *) calloc( strlen( p_region_in->psz_html ), sizeof( UniChar ) );
825 ProcessNodes( p_filter, p_xml_reader, p_region_in->p_style, psz_text,
826 &i_len, &i_runs, &pi_run_lengths, &pp_styles );
828 p_region_out->i_x = p_region_in->i_x;
829 p_region_out->i_y = p_region_in->i_y;
831 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs, pi_run_lengths, pp_styles);
833 for( k=0; k<i_runs; k++)
834 ATSUDisposeStyle( pp_styles[k] );
836 free( pi_run_lengths );
841 xml_ReaderDelete( p_xml, p_xml_reader );
845 stream_Delete( p_sub );
851 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
852 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
854 offscreen_bitmap_t *p_bitmap;
855 CGContextRef p_context = NULL;
857 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
860 p_bitmap->i_bitsPerChannel = 8;
861 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
862 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
863 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
865 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
867 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
868 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
870 *pp_colorSpace = CreateGenericRGBColorSpace();
873 if( p_bitmap->p_data && *pp_colorSpace )
875 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
876 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
877 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
881 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
882 // OS X 10.1 doesn't support weak linking of this call which is only available
883 // int 10.4 and later
884 if( CGContextSetAllowsAntialiasing != NULL )
886 CGContextSetAllowsAntialiasing( p_context, true );
890 *pp_memory = p_bitmap;
896 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
897 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
898 int i_width, int i_height, int *pi_textblock_height )
900 offscreen_bitmap_t *p_offScreen = NULL;
901 CGColorSpaceRef p_colorSpace = NULL;
902 CGContextRef p_context = NULL;
904 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
908 ATSUTextLayout p_textLayout;
909 OSStatus status = noErr;
911 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
913 (const UniCharCount *) pi_run_lengths,
916 if( status == noErr )
918 // Attach our offscreen Image Graphics Context to the text style
919 // and setup the line alignment (have to specify the line width
920 // also in order for our chosen alignment to work)
922 Fract alignment = kATSUStartAlignment;
923 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
925 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
926 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
927 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
929 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
931 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
933 alignment = kATSUEndAlignment;
935 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
937 alignment = kATSUCenterAlignment;
940 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
942 // let ATSUI deal with characters not-in-our-specified-font
943 ATSUSetTransientFontMatching( p_textLayout, true );
945 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
946 Fixed y = Long2Fix( i_height );
948 // Set the line-breaks and draw individual lines
949 uint32_t i_start = 0;
950 uint32_t i_end = i_text_len;
952 // Set up black outlining of the text --
953 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
954 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
958 // ATSUBreakLine will automatically pick up any manual '\n's also
959 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
960 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
964 uint32_t i_actualSize;
966 // Come down far enough to fit the height of this line --
967 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
968 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
970 // Quartz uses an upside-down co-ordinate space -> y values decrease as
971 // you move down the page
974 // Set the outlining for this line to be dependant on the size of the line -
975 // make it about 5% of the ascent, with a minimum at 1.0
976 float f_thickness = FixedToFloat( ascent ) * 0.05;
977 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
979 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
981 // and now prepare for the next line by coming down far enough for our
983 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
984 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
992 while( i_end < i_text_len );
994 *pi_textblock_height = i_height - Fix2Long( y );
995 CGContextFlush( p_context );
997 ATSUDisposeTextLayout( p_textLayout );
1000 CGContextRelease( p_context );
1002 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
1007 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
1008 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
1010 offscreen_bitmap_t *p_offScreen = NULL;
1011 int i_textblock_height = 0;
1013 int i_width = p_filter->fmt_out.video.i_visible_width;
1014 int i_height = p_filter->fmt_out.video.i_visible_height;
1015 int i_text_align = p_region->i_align & 0x3;
1017 if( !psz_utf16_str )
1019 msg_Err( p_filter, "Invalid argument to RenderYUVA" );
1020 return VLC_EGENERIC;
1023 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
1024 i_runs, pi_run_lengths, pp_styles,
1025 i_width, i_height, &i_textblock_height );
1029 msg_Err( p_filter, "No offscreen buffer" );
1030 return VLC_EGENERIC;
1033 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
1035 int x, y, i_offset, i_pitch;
1036 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
1037 subpicture_region_t *p_region_tmp;
1039 // Create a new subpicture region
1040 memset( &fmt, 0, sizeof(video_format_t) );
1041 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1043 fmt.i_width = fmt.i_visible_width = i_width;
1044 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
1045 fmt.i_x_offset = fmt.i_y_offset = 0;
1046 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
1049 msg_Err( p_filter, "cannot allocate SPU region" );
1050 return VLC_EGENERIC;
1052 p_region->fmt = p_region_tmp->fmt;
1053 p_region->picture = p_region_tmp->picture;
1054 free( p_region_tmp );
1056 p_dst_y = p_region->picture.Y_PIXELS;
1057 p_dst_u = p_region->picture.U_PIXELS;
1058 p_dst_v = p_region->picture.V_PIXELS;
1059 p_dst_a = p_region->picture.A_PIXELS;
1060 i_pitch = p_region->picture.A_PITCH;
1062 i_offset = VERTICAL_MARGIN *i_pitch;
1063 for( y=0; y<i_textblock_height; y++)
1065 for( x=0; x<i_width; x++)
1067 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
1068 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
1069 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
1070 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
1072 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
1073 802 * i_blue + 4096 + 131072 ) >> 13, 235);
1074 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
1075 3598 * i_blue + 4096 + 1048576) >> 13, 240);
1076 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
1077 -585 * i_blue + 4096 + 1048576) >> 13, 240);
1079 p_dst_y[ i_offset + x ] = i_y;
1080 p_dst_u[ i_offset + x ] = i_u;
1081 p_dst_v[ i_offset + x ] = i_v;
1082 p_dst_a[ i_offset + x ] = i_alpha;
1084 i_offset += i_pitch;
1087 free( p_offScreen->p_data );
1088 free( p_offScreen );