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 //////////////////////////////////////////////////////////////////////////////
32 #include <vlc_block.h>
33 #include <vlc_filter.h>
34 #include <vlc_stream.h>
36 #include <vlc_input.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 LoadFontsFromAttachments( filter_t *p_filter );
57 static int RenderText( filter_t *, subpicture_region_t *,
58 subpicture_region_t * );
59 static int RenderHtml( filter_t *, subpicture_region_t *,
60 subpicture_region_t * );
62 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
63 UniChar *psz_utfString, uint32_t i_text_len,
64 uint32_t i_runs, uint32_t *pi_run_lengths,
65 ATSUStyle *pp_styles );
66 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size,
67 int i_font_color, int i_font_alpha,
68 vlc_bool_t b_bold, vlc_bool_t b_italic,
70 //////////////////////////////////////////////////////////////////////////////
72 //////////////////////////////////////////////////////////////////////////////
74 // The preferred way to set font style information is for it to come from the
75 // subtitle file, and for it to be rendered with RenderHtml instead of
76 // RenderText. This module, unlike Freetype, doesn't provide any options to
77 // override the fallback font selection used when this style information is
80 set_shortname( _("Mac Text renderer"));
81 set_description( _("Quartz font renderer") );
82 set_category( CAT_VIDEO );
83 set_subcategory( SUBCAT_VIDEO_SUBPIC );
85 set_capability( "text renderer", 120 );
86 add_shortcut( "text" );
87 set_callbacks( Create, Destroy );
90 typedef struct font_stack_t font_stack_t;
101 typedef struct offscreen_bitmap_t offscreen_bitmap_t;
102 struct offscreen_bitmap_t
105 int i_bitsPerChannel;
111 //////////////////////////////////////////////////////////////////////////////
112 // filter_sys_t: quartztext local data
113 //////////////////////////////////////////////////////////////////////////////
114 // This structure is part of the video output thread descriptor.
115 // It describes the freetype specific properties of an output thread.
116 //////////////////////////////////////////////////////////////////////////////
120 uint8_t i_font_opacity;
124 ATSFontContainerRef *p_fonts;
128 //////////////////////////////////////////////////////////////////////////////
129 // Create: allocates osd-text video thread output method
130 //////////////////////////////////////////////////////////////////////////////
131 // This function allocates and initializes a Clone vout method.
132 //////////////////////////////////////////////////////////////////////////////
133 static int Create( vlc_object_t *p_this )
135 filter_t *p_filter = (filter_t *)p_this;
138 // Allocate structure
139 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
142 msg_Err( p_filter, "out of memory" );
145 p_sys->psz_font_name = strdup( DEFAULT_FONT );
146 p_sys->i_font_opacity = 255;
147 p_sys->i_font_color = DEFAULT_FONT_COLOR;
148 p_sys->i_font_size = p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
150 p_filter->pf_render_text = RenderText;
151 p_filter->pf_render_html = RenderHtml;
153 p_sys->p_fonts = NULL;
156 LoadFontsFromAttachments( p_filter );
161 //////////////////////////////////////////////////////////////////////////////
162 // Destroy: destroy Clone video thread output method
163 //////////////////////////////////////////////////////////////////////////////
164 // Clean up all data and library connections
165 //////////////////////////////////////////////////////////////////////////////
166 static void Destroy( vlc_object_t *p_this )
168 filter_t *p_filter = (filter_t *)p_this;
169 filter_sys_t *p_sys = p_filter->p_sys;
175 for( k = 0; k < p_sys->i_fonts; k++ )
177 ATSFontDeactivate( p_sys->p_fonts[k], NULL, kATSOptionFlagsDefault );
180 free( p_sys->p_fonts );
183 if( p_sys->psz_font_name ) free( p_sys->psz_font_name );
188 //////////////////////////////////////////////////////////////////////////////
189 // Make any TTF/OTF fonts present in the attachments of the media file
190 // available to the Quartz engine for text rendering
191 //////////////////////////////////////////////////////////////////////////////
192 static int LoadFontsFromAttachments( filter_t *p_filter )
194 filter_sys_t *p_sys = p_filter->p_sys;
195 input_thread_t *p_input;
196 input_attachment_t **pp_attachments;
197 int i_attachments_cnt;
199 int rv = VLC_SUCCESS;
201 p_input = (input_thread_t *)vlc_object_find( p_filter, VLC_OBJECT_INPUT, FIND_PARENT );
205 if( VLC_SUCCESS != input_Control( p_input, INPUT_GET_ATTACHMENTS, &pp_attachments, &i_attachments_cnt ))
209 p_sys->p_fonts = malloc( i_attachments_cnt * sizeof( ATSFontContainerRef ) );
210 if(! p_sys->p_fonts )
213 for( k = 0; k < i_attachments_cnt; k++ )
215 input_attachment_t *p_attach = pp_attachments[k];
219 if(( !strcmp( p_attach->psz_mime, "application/x-truetype-font" ) || // TTF
220 !strcmp( p_attach->psz_mime, "application/x-font-otf" ) ) && // OTF
221 ( p_attach->i_data > 0 ) &&
222 ( p_attach->p_data != NULL ) )
224 ATSFontContainerRef container;
226 if( noErr == ATSFontActivateFromMemory( p_attach->p_data,
228 kATSFontContextLocal,
229 kATSFontFormatUnspecified,
231 kATSOptionFlagsDefault,
234 p_sys->p_fonts[ p_sys->i_fonts++ ] = container;
238 vlc_input_attachment_Delete( p_attach );
240 free( pp_attachments );
245 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
246 // Original version of these functions available on:
247 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
249 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
251 static CMProfileRef OpenGenericProfile( void )
253 static CMProfileRef cached_rgb_prof = NULL;
255 // Create the profile reference only once
256 if( cached_rgb_prof == NULL )
259 CMProfileLocation loc;
261 loc.locType = cmPathBasedProfile;
262 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
264 err = CMOpenProfile( &cached_rgb_prof, &loc );
268 cached_rgb_prof = NULL;
272 if( cached_rgb_prof )
274 // Clone the profile reference so that the caller has
275 // their own reference, not our cached one.
276 CMCloneProfileRef( cached_rgb_prof );
279 return cached_rgb_prof;
282 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
284 static CGColorSpaceRef p_generic_rgb_cs = NULL;
286 if( p_generic_rgb_cs == NULL )
288 CMProfileRef generic_rgb_prof = OpenGenericProfile();
290 if( generic_rgb_prof )
292 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
294 CMCloseProfile( generic_rgb_prof );
298 return p_generic_rgb_cs;
302 static char *EliminateCRLF( char *psz_string )
307 for( p = psz_string; p && *p; p++ )
309 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
311 for( q = p + 1; *q; q++ )
320 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
321 // we don't need to worry about bidirectional text conversion as ATSUI should
322 // handle that for us automatically
323 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
325 CFStringRef p_cfString;
328 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
329 i_string_length = CFStringGetLength( p_cfString );
332 *pi_strlen = i_string_length;
334 if( !*ppsz_utf16_str )
335 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
337 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
339 CFRelease( p_cfString );
342 // Renders a text subpicture region into another one.
343 // It is used as pf_add_string callback in the vout method by this module
344 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
345 subpicture_region_t *p_region_in )
347 filter_sys_t *p_sys = p_filter->p_sys;
348 UniChar *psz_utf16_str = NULL;
349 uint32_t i_string_length;
351 int i_font_color, i_font_alpha, i_font_size;
354 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
355 psz_string = p_region_in->psz_text;
356 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
358 if( p_region_in->p_style )
360 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
361 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
362 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 );
366 i_font_color = p_sys->i_font_color;
367 i_font_alpha = 255 - p_sys->i_font_opacity;
368 i_font_size = p_sys->i_font_size;
371 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
373 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
375 p_region_out->i_x = p_region_in->i_x;
376 p_region_out->i_y = p_region_in->i_y;
378 if( psz_utf16_str != NULL )
380 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
381 i_font_color, i_font_alpha,
382 VLC_FALSE, VLC_FALSE, VLC_FALSE );
385 RenderYUVA( p_filter, p_region_out, psz_utf16_str, i_string_length,
386 1, &i_string_length, &p_style );
389 ATSUDisposeStyle( p_style );
390 free( psz_utf16_str );
397 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size, int i_font_color, int i_font_alpha,
398 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
404 float f_red = (float)(( i_font_color & 0x00FF0000 ) >> 16) / 255.0;
405 float f_green = (float)(( i_font_color & 0x0000FF00 ) >> 8) / 255.0;
406 float f_blue = (float)( i_font_color & 0x000000FF ) / 255.0;
407 float f_alpha = ( 255.0 - (float)i_font_alpha) / 255.0;
410 Fixed font_size = IntToFixed( i_font_size );
411 ATSURGBAlphaColor font_color = { f_red, f_green, f_blue, f_alpha };
412 Boolean bold = b_bold;
413 Boolean italic = b_italic;
414 Boolean uline = b_uline;
416 ATSUAttributeTag tags[] = { kATSUSizeTag, kATSURGBAlphaColorTag, kATSUQDItalicTag,
417 kATSUQDBoldfaceTag, kATSUQDUnderlineTag, kATSUFontTag };
418 ByteCount sizes[] = { sizeof( Fixed ), sizeof( ATSURGBAlphaColor ), sizeof( Boolean ),
419 sizeof( Boolean ), sizeof( Boolean ), sizeof( ATSUFontID )};
420 ATSUAttributeValuePtr values[] = { &font_size, &font_color, &italic, &bold, &uline, &font };
422 i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
424 status = ATSUFindFontFromName( psz_fontname,
425 strlen( psz_fontname ),
432 if( status != noErr )
434 // If we can't find a suitable font, just do everything else
438 if( noErr == ATSUCreateStyle( &p_style ) )
440 if( noErr == ATSUSetAttributes( p_style, i_tag_cnt, tags, sizes, values ) )
444 ATSUDisposeStyle( p_style );
449 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
450 int i_color, int i_alpha )
457 p_new = malloc( sizeof( font_stack_t ) );
458 p_new->p_next = NULL;
461 p_new->psz_name = strdup( psz_name );
463 p_new->psz_name = NULL;
465 p_new->i_size = i_size;
466 p_new->i_color = i_color;
467 p_new->i_alpha = i_alpha;
475 font_stack_t *p_last;
477 for( p_last = *p_font;
479 p_last = p_last->p_next )
482 p_last->p_next = p_new;
487 static int PopFont( font_stack_t **p_font )
489 font_stack_t *p_last, *p_next_to_last;
491 if( !p_font || !*p_font )
494 p_next_to_last = NULL;
495 for( p_last = *p_font;
497 p_last = p_last->p_next )
499 p_next_to_last = p_last;
503 p_next_to_last->p_next = NULL;
507 free( p_last->psz_name );
513 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
514 int *i_color, int *i_alpha )
516 font_stack_t *p_last;
518 if( !p_font || !*p_font )
523 p_last=p_last->p_next )
526 *psz_name = p_last->psz_name;
527 *i_size = p_last->i_size;
528 *i_color = p_last->i_color;
529 *i_alpha = p_last->i_alpha;
534 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys, font_stack_t **p_fonts,
535 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
537 ATSUStyle p_style = NULL;
539 char *psz_fontname = NULL;
540 int i_font_color = p_sys->i_font_color;
541 int i_font_alpha = 0;
542 int i_font_size = p_sys->i_font_size;
544 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ) )
546 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color, i_font_alpha,
547 b_bold, b_italic, b_uline );
552 static void ProcessNodes( filter_t *p_filter, xml_reader_t *p_xml_reader,
553 text_style_t *p_font_style, UniChar *psz_text, int *pi_len,
554 uint32_t *pi_runs, uint32_t **ppi_run_lengths,
555 ATSUStyle **ppp_styles)
557 filter_sys_t *p_sys = p_filter->p_sys;
558 UniChar *psz_text_orig = psz_text;
559 font_stack_t *p_fonts = NULL;
561 char *psz_node = NULL;
563 vlc_bool_t b_italic = VLC_FALSE;
564 vlc_bool_t b_bold = VLC_FALSE;
565 vlc_bool_t b_uline = VLC_FALSE;
570 p_font_style->psz_fontname,
571 p_font_style->i_font_size,
572 p_font_style->i_font_color,
573 p_font_style->i_font_alpha );
575 if( p_font_style->i_style_flags & STYLE_BOLD )
577 if( p_font_style->i_style_flags & STYLE_ITALIC )
579 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
584 PushFont( &p_fonts, p_sys->psz_font_name, p_sys->i_font_size, p_sys->i_font_color, 0 );
587 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
589 switch ( xml_ReaderNodeType( p_xml_reader ) )
591 case XML_READER_NONE:
593 case XML_READER_ENDELEM:
594 psz_node = xml_ReaderName( p_xml_reader );
598 if( !strcasecmp( "font", psz_node ) )
600 else if( !strcasecmp( "b", psz_node ) )
602 else if( !strcasecmp( "i", psz_node ) )
603 b_italic = VLC_FALSE;
604 else if( !strcasecmp( "u", psz_node ) )
610 case XML_READER_STARTELEM:
611 psz_node = xml_ReaderName( p_xml_reader );
614 if( !strcasecmp( "font", psz_node ) )
616 char *psz_fontname = NULL;
617 int i_font_color = 0xffffff;
618 int i_font_alpha = 0;
619 int i_font_size = 24;
621 // Default all attributes to the top font in the stack -- in case not
622 // all attributes are specified in the sub-font
623 if( VLC_SUCCESS == PeekFont( &p_fonts, &psz_fontname, &i_font_size, &i_font_color, &i_font_alpha ))
625 psz_fontname = strdup( psz_fontname );
628 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
630 char *psz_name = xml_ReaderName ( p_xml_reader );
631 char *psz_value = xml_ReaderValue ( p_xml_reader );
633 if( psz_name && psz_value )
635 if( !strcasecmp( "face", psz_name ) )
637 if( psz_fontname ) free( psz_fontname );
638 psz_fontname = strdup( psz_value );
640 else if( !strcasecmp( "size", psz_name ) )
642 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
644 int i_value = atoi( psz_value );
646 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
647 i_font_size += ( i_value * i_font_size ) / 10;
648 else if( i_value < -5 )
649 i_font_size = - i_value;
650 else if( i_value > 5 )
651 i_font_size = i_value;
654 i_font_size = atoi( psz_value );
656 else if( !strcasecmp( "color", psz_name ) &&
657 ( psz_value[0] == '#' ) )
659 i_font_color = strtol( psz_value+1, NULL, 16 );
660 i_font_color &= 0x00ffffff;
662 else if( !strcasecmp( "alpha", psz_name ) &&
663 ( psz_value[0] == '#' ) )
665 i_font_alpha = strtol( psz_value+1, NULL, 16 );
666 i_font_alpha &= 0xff;
672 PushFont( &p_fonts, psz_fontname, i_font_size, i_font_color, i_font_alpha );
673 free( psz_fontname );
675 else if( !strcasecmp( "b", psz_node ) )
679 else if( !strcasecmp( "i", psz_node ) )
683 else if( !strcasecmp( "u", psz_node ) )
687 else if( !strcasecmp( "br", psz_node ) )
689 uint32_t i_string_length;
691 ConvertToUTF16( "\n", &i_string_length, &psz_text );
692 psz_text += i_string_length;
697 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
699 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
701 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
703 if( *ppi_run_lengths )
704 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
706 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
708 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
713 case XML_READER_TEXT:
714 psz_node = xml_ReaderValue( p_xml_reader );
717 uint32_t i_string_length;
719 // Turn any multiple-whitespaces into single spaces
720 char *s = strpbrk( psz_node, "\t\r\n " );
723 int i_whitespace = strspn( s, "\t\r\n " );
725 if( i_whitespace > 1 )
728 strlen( s ) - i_whitespace + 1 );
731 s = strpbrk( s, "\t\r\n " );
734 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
735 psz_text += i_string_length;
740 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
742 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
744 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
746 if( *ppi_run_lengths )
747 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
749 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
751 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
759 *pi_len = psz_text - psz_text_orig;
761 while( VLC_SUCCESS == PopFont( &p_fonts ) );
764 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
765 subpicture_region_t *p_region_in )
767 int rv = VLC_SUCCESS;
768 stream_t *p_sub = NULL;
770 xml_reader_t *p_xml_reader = NULL;
772 if( !p_region_in || !p_region_in->psz_html )
775 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
776 (uint8_t *) p_region_in->psz_html,
777 strlen( p_region_in->psz_html ),
781 p_xml = xml_Create( p_filter );
784 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
790 uint32_t *pi_run_lengths = NULL;
791 ATSUStyle *pp_styles = NULL;
793 psz_text = (UniChar *) calloc( strlen( p_region_in->psz_html ), sizeof( UniChar ) );
798 ProcessNodes( p_filter, p_xml_reader, p_region_in->p_style, psz_text,
799 &i_len, &i_runs, &pi_run_lengths, &pp_styles );
801 p_region_out->i_x = p_region_in->i_x;
802 p_region_out->i_y = p_region_in->i_y;
804 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs, pi_run_lengths, pp_styles);
806 for( k=0; k<i_runs; k++)
807 ATSUDisposeStyle( pp_styles[k] );
809 free( pi_run_lengths );
814 xml_ReaderDelete( p_xml, p_xml_reader );
818 stream_Delete( p_sub );
824 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
825 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
827 offscreen_bitmap_t *p_bitmap;
828 CGContextRef p_context = NULL;
830 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
833 p_bitmap->i_bitsPerChannel = 8;
834 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
835 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
836 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
838 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
840 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
841 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
843 *pp_colorSpace = CreateGenericRGBColorSpace();
846 if( p_bitmap->p_data && *pp_colorSpace )
848 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
849 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
850 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
854 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
855 // OS X 10.1 doesn't support weak linking of this call which is only available
856 // int 10.4 and later
857 if( CGContextSetAllowsAntialiasing != NULL )
859 CGContextSetAllowsAntialiasing( p_context, true );
863 *pp_memory = p_bitmap;
869 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
870 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
871 int i_width, int i_height, int *pi_textblock_height )
873 offscreen_bitmap_t *p_offScreen = NULL;
874 CGColorSpaceRef p_colorSpace = NULL;
875 CGContextRef p_context = NULL;
877 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
881 ATSUTextLayout p_textLayout;
882 OSStatus status = noErr;
884 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
886 (const UniCharCount *) pi_run_lengths,
889 if( status == noErr )
891 // Attach our offscreen Image Graphics Context to the text style
892 // and setup the line alignment (have to specify the line width
893 // also in order for our chosen alignment to work)
895 Fract alignment = kATSUStartAlignment;
896 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
898 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
899 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
900 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
902 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
904 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
906 alignment = kATSUEndAlignment;
908 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
910 alignment = kATSUCenterAlignment;
913 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
915 // let ATSUI deal with characters not-in-our-specified-font
916 ATSUSetTransientFontMatching( p_textLayout, true );
918 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
919 Fixed y = Long2Fix( i_height );
921 // Set the line-breaks and draw individual lines
922 uint32_t i_start = 0;
923 uint32_t i_end = i_text_len;
925 // Set up black outlining of the text --
926 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
927 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
931 // ATSUBreakLine will automatically pick up any manual '\n's also
932 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
933 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
937 uint32_t i_actualSize;
939 // Come down far enough to fit the height of this line --
940 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
941 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
943 // Quartz uses an upside-down co-ordinate space -> y values decrease as
944 // you move down the page
947 // Set the outlining for this line to be dependant on the size of the line -
948 // make it about 5% of the ascent, with a minimum at 1.0
949 float f_thickness = FixedToFloat( ascent ) * 0.05;
950 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
952 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
954 // and now prepare for the next line by coming down far enough for our
956 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
957 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
965 while( i_end < i_text_len );
967 *pi_textblock_height = i_height - Fix2Long( y );
968 CGContextFlush( p_context );
970 ATSUDisposeTextLayout( p_textLayout );
973 CGContextRelease( p_context );
975 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
980 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
981 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
983 offscreen_bitmap_t *p_offScreen = NULL;
984 int i_textblock_height = 0;
986 int i_width = p_filter->fmt_out.video.i_visible_width;
987 int i_height = p_filter->fmt_out.video.i_visible_height;
988 int i_text_align = p_region->i_align & 0x3;
992 msg_Err( p_filter, "Invalid argument to RenderYUVA" );
996 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
997 i_runs, pi_run_lengths, pp_styles,
998 i_width, i_height, &i_textblock_height );
1002 msg_Err( p_filter, "No offscreen buffer" );
1003 return VLC_EGENERIC;
1006 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
1008 int x, y, i_offset, i_pitch;
1009 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
1010 subpicture_region_t *p_region_tmp;
1012 // Create a new subpicture region
1013 memset( &fmt, 0, sizeof(video_format_t) );
1014 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1016 fmt.i_width = fmt.i_visible_width = i_width;
1017 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
1018 fmt.i_x_offset = fmt.i_y_offset = 0;
1019 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
1022 msg_Err( p_filter, "cannot allocate SPU region" );
1023 return VLC_EGENERIC;
1025 p_region->fmt = p_region_tmp->fmt;
1026 p_region->picture = p_region_tmp->picture;
1027 free( p_region_tmp );
1029 p_dst_y = p_region->picture.Y_PIXELS;
1030 p_dst_u = p_region->picture.U_PIXELS;
1031 p_dst_v = p_region->picture.V_PIXELS;
1032 p_dst_a = p_region->picture.A_PIXELS;
1033 i_pitch = p_region->picture.A_PITCH;
1035 i_offset = VERTICAL_MARGIN *i_pitch;
1036 for( y=0; y<i_textblock_height; y++)
1038 for( x=0; x<i_width; x++)
1040 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
1041 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
1042 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
1043 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
1045 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
1046 802 * i_blue + 4096 + 131072 ) >> 13, 235);
1047 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
1048 3598 * i_blue + 4096 + 1048576) >> 13, 240);
1049 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
1050 -585 * i_blue + 4096 + 1048576) >> 13, 240);
1052 p_dst_y[ i_offset + x ] = i_y;
1053 p_dst_u[ i_offset + x ] = i_u;
1054 p_dst_v[ i_offset + x ] = i_v;
1055 p_dst_a[ i_offset + x ] = i_alpha;
1057 i_offset += i_pitch;
1060 free( p_offScreen->p_data );
1061 free( p_offScreen );