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 //////////////////////////////////////////////////////////////////////////////
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_stream.h>
39 #include <vlc_input.h>
43 #include <Carbon/Carbon.h>
45 #define DEFAULT_FONT "Verdana"
46 #define DEFAULT_FONT_COLOR 0xffffff
47 #define DEFAULT_REL_FONT_SIZE 16
49 #define VERTICAL_MARGIN 3
50 #define HORIZONTAL_MARGIN 10
52 //////////////////////////////////////////////////////////////////////////////
54 //////////////////////////////////////////////////////////////////////////////
55 static int Create ( vlc_object_t * );
56 static void Destroy( vlc_object_t * );
58 static int LoadFontsFromAttachments( filter_t *p_filter );
60 static int RenderText( filter_t *, subpicture_region_t *,
61 subpicture_region_t * );
62 static int RenderHtml( filter_t *, subpicture_region_t *,
63 subpicture_region_t * );
65 static int GetFontSize( filter_t *p_filter );
66 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
67 UniChar *psz_utfString, uint32_t i_text_len,
68 uint32_t i_runs, uint32_t *pi_run_lengths,
69 ATSUStyle *pp_styles );
70 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size,
71 uint32_t i_font_color,
72 vlc_bool_t b_bold, vlc_bool_t b_italic,
74 //////////////////////////////////////////////////////////////////////////////
76 //////////////////////////////////////////////////////////////////////////////
78 // The preferred way to set font style information is for it to come from the
79 // subtitle file, and for it to be rendered with RenderHtml instead of
80 // RenderText. This module, unlike Freetype, doesn't provide any options to
81 // override the fallback font selection used when this style information is
84 set_shortname( _("Mac Text renderer"));
85 set_description( _("Quartz font renderer") );
86 set_category( CAT_VIDEO );
87 set_subcategory( SUBCAT_VIDEO_SUBPIC );
89 set_capability( "text renderer", 120 );
90 add_shortcut( "text" );
91 set_callbacks( Create, Destroy );
94 typedef struct font_stack_t font_stack_t;
99 uint32_t i_color; // ARGB
101 font_stack_t *p_next;
104 typedef struct offscreen_bitmap_t offscreen_bitmap_t;
105 struct offscreen_bitmap_t
108 int i_bitsPerChannel;
114 //////////////////////////////////////////////////////////////////////////////
115 // filter_sys_t: quartztext local data
116 //////////////////////////////////////////////////////////////////////////////
117 // This structure is part of the video output thread descriptor.
118 // It describes the freetype specific properties of an output thread.
119 //////////////////////////////////////////////////////////////////////////////
123 uint8_t i_font_opacity;
127 ATSFontContainerRef *p_fonts;
131 //////////////////////////////////////////////////////////////////////////////
132 // Create: allocates osd-text video thread output method
133 //////////////////////////////////////////////////////////////////////////////
134 // This function allocates and initializes a Clone vout method.
135 //////////////////////////////////////////////////////////////////////////////
136 static int Create( vlc_object_t *p_this )
138 filter_t *p_filter = (filter_t *)p_this;
141 // Allocate structure
142 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
145 msg_Err( p_filter, "out of memory" );
148 p_sys->psz_font_name = strdup( DEFAULT_FONT );
149 p_sys->i_font_opacity = 255;
150 p_sys->i_font_color = DEFAULT_FONT_COLOR;
151 p_sys->i_font_size = GetFontSize( p_filter );
153 p_filter->pf_render_text = RenderText;
154 p_filter->pf_render_html = RenderHtml;
156 p_sys->p_fonts = NULL;
159 LoadFontsFromAttachments( p_filter );
164 //////////////////////////////////////////////////////////////////////////////
165 // Destroy: destroy Clone video thread output method
166 //////////////////////////////////////////////////////////////////////////////
167 // Clean up all data and library connections
168 //////////////////////////////////////////////////////////////////////////////
169 static void Destroy( vlc_object_t *p_this )
171 filter_t *p_filter = (filter_t *)p_this;
172 filter_sys_t *p_sys = p_filter->p_sys;
178 for( k = 0; k < p_sys->i_fonts; k++ )
180 ATSFontDeactivate( p_sys->p_fonts[k], NULL, kATSOptionFlagsDefault );
183 free( p_sys->p_fonts );
186 free( p_sys->psz_font_name );
190 //////////////////////////////////////////////////////////////////////////////
191 // Make any TTF/OTF fonts present in the attachments of the media file
192 // available to the Quartz engine for text rendering
193 //////////////////////////////////////////////////////////////////////////////
194 static int LoadFontsFromAttachments( filter_t *p_filter )
196 filter_sys_t *p_sys = p_filter->p_sys;
197 input_thread_t *p_input;
198 input_attachment_t **pp_attachments;
199 int i_attachments_cnt;
201 int rv = VLC_SUCCESS;
203 p_input = (input_thread_t *)vlc_object_find( p_filter, VLC_OBJECT_INPUT, FIND_PARENT );
207 if( VLC_SUCCESS != input_Control( p_input, INPUT_GET_ATTACHMENTS, &pp_attachments, &i_attachments_cnt ))
209 vlc_object_release(p_input);
214 p_sys->p_fonts = malloc( i_attachments_cnt * sizeof( ATSFontContainerRef ) );
215 if(! p_sys->p_fonts )
218 for( k = 0; k < i_attachments_cnt; k++ )
220 input_attachment_t *p_attach = pp_attachments[k];
224 if(( !strcmp( p_attach->psz_mime, "application/x-truetype-font" ) || // TTF
225 !strcmp( p_attach->psz_mime, "application/x-font-otf" ) ) && // OTF
226 ( p_attach->i_data > 0 ) &&
227 ( p_attach->p_data != NULL ) )
229 ATSFontContainerRef container;
231 if( noErr == ATSFontActivateFromMemory( p_attach->p_data,
233 kATSFontContextLocal,
234 kATSFontFormatUnspecified,
236 kATSOptionFlagsDefault,
239 p_sys->p_fonts[ p_sys->i_fonts++ ] = container;
243 vlc_input_attachment_Delete( p_attach );
245 free( pp_attachments );
247 vlc_object_release(p_input);
252 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
253 // Original version of these functions available on:
254 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
256 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
258 static CMProfileRef OpenGenericProfile( void )
260 static CMProfileRef cached_rgb_prof = NULL;
262 // Create the profile reference only once
263 if( cached_rgb_prof == NULL )
266 CMProfileLocation loc;
268 loc.locType = cmPathBasedProfile;
269 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
271 err = CMOpenProfile( &cached_rgb_prof, &loc );
275 cached_rgb_prof = NULL;
279 if( cached_rgb_prof )
281 // Clone the profile reference so that the caller has
282 // their own reference, not our cached one.
283 CMCloneProfileRef( cached_rgb_prof );
286 return cached_rgb_prof;
289 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
291 static CGColorSpaceRef p_generic_rgb_cs = NULL;
293 if( p_generic_rgb_cs == NULL )
295 CMProfileRef generic_rgb_prof = OpenGenericProfile();
297 if( generic_rgb_prof )
299 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
301 CMCloseProfile( generic_rgb_prof );
305 return p_generic_rgb_cs;
309 static char *EliminateCRLF( char *psz_string )
314 for( p = psz_string; p && *p; p++ )
316 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
318 for( q = p + 1; *q; q++ )
327 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
328 // we don't need to worry about bidirectional text conversion as ATSUI should
329 // handle that for us automatically
330 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
332 CFStringRef p_cfString;
335 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
336 i_string_length = CFStringGetLength( p_cfString );
339 *pi_strlen = i_string_length;
341 if( !*ppsz_utf16_str )
342 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
344 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
346 CFRelease( p_cfString );
349 // Renders a text subpicture region into another one.
350 // It is used as pf_add_string callback in the vout method by this module
351 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
352 subpicture_region_t *p_region_in )
354 filter_sys_t *p_sys = p_filter->p_sys;
355 UniChar *psz_utf16_str = NULL;
356 uint32_t i_string_length;
358 int i_font_color, i_font_alpha, i_font_size;
362 p_sys->i_font_size = GetFontSize( p_filter );
365 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
366 psz_string = p_region_in->psz_text;
367 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
369 if( VLC_SUCCESS == var_Get( p_filter, "scale", &val ))
372 if( p_region_in->p_style )
374 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
375 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
376 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 ) * i_scale / 1000;
380 i_font_color = p_sys->i_font_color;
381 i_font_alpha = 255 - p_sys->i_font_opacity;
382 i_font_size = p_sys->i_font_size;
385 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
387 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
389 p_region_out->i_x = p_region_in->i_x;
390 p_region_out->i_y = p_region_in->i_y;
392 if( psz_utf16_str != NULL )
394 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
395 (i_font_color & 0xffffff) |
396 ((i_font_alpha & 0xff) << 24),
397 VLC_FALSE, VLC_FALSE, VLC_FALSE );
400 RenderYUVA( p_filter, p_region_out, psz_utf16_str, i_string_length,
401 1, &i_string_length, &p_style );
404 ATSUDisposeStyle( p_style );
405 free( psz_utf16_str );
412 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size, uint32_t i_font_color,
413 vlc_bool_t b_bold, vlc_bool_t b_italic, vlc_bool_t b_uline )
419 float f_red = (float)(( i_font_color & 0x00FF0000 ) >> 16) / 255.0;
420 float f_green = (float)(( i_font_color & 0x0000FF00 ) >> 8) / 255.0;
421 float f_blue = (float)( i_font_color & 0x000000FF ) / 255.0;
422 float f_alpha = ( 255.0 - (float)(( i_font_color & 0xFF000000 ) >> 24)) / 255.0;
425 Fixed font_size = IntToFixed( i_font_size );
426 ATSURGBAlphaColor font_color = { f_red, f_green, f_blue, f_alpha };
427 Boolean bold = b_bold;
428 Boolean italic = b_italic;
429 Boolean uline = b_uline;
431 ATSUAttributeTag tags[] = { kATSUSizeTag, kATSURGBAlphaColorTag, kATSUQDItalicTag,
432 kATSUQDBoldfaceTag, kATSUQDUnderlineTag, kATSUFontTag };
433 ByteCount sizes[] = { sizeof( Fixed ), sizeof( ATSURGBAlphaColor ), sizeof( Boolean ),
434 sizeof( Boolean ), sizeof( Boolean ), sizeof( ATSUFontID )};
435 ATSUAttributeValuePtr values[] = { &font_size, &font_color, &italic, &bold, &uline, &font };
437 i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
439 status = ATSUFindFontFromName( psz_fontname,
440 strlen( psz_fontname ),
447 if( status != noErr )
449 // If we can't find a suitable font, just do everything else
453 if( noErr == ATSUCreateStyle( &p_style ) )
455 if( noErr == ATSUSetAttributes( p_style, i_tag_cnt, tags, sizes, values ) )
459 ATSUDisposeStyle( p_style );
464 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
472 p_new = malloc( sizeof( font_stack_t ) );
476 p_new->p_next = NULL;
479 p_new->psz_name = strdup( psz_name );
481 p_new->psz_name = NULL;
483 p_new->i_size = i_size;
484 p_new->i_color = i_color;
492 font_stack_t *p_last;
494 for( p_last = *p_font;
496 p_last = p_last->p_next )
499 p_last->p_next = p_new;
504 static int PopFont( font_stack_t **p_font )
506 font_stack_t *p_last, *p_next_to_last;
508 if( !p_font || !*p_font )
511 p_next_to_last = NULL;
512 for( p_last = *p_font;
514 p_last = p_last->p_next )
516 p_next_to_last = p_last;
520 p_next_to_last->p_next = NULL;
524 free( p_last->psz_name );
530 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
533 font_stack_t *p_last;
535 if( !p_font || !*p_font )
540 p_last=p_last->p_next )
543 *psz_name = p_last->psz_name;
544 *i_size = p_last->i_size;
545 *i_color = p_last->i_color;
550 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys,
551 font_stack_t **p_fonts, vlc_bool_t b_bold, vlc_bool_t b_italic,
554 ATSUStyle p_style = NULL;
556 char *psz_fontname = NULL;
557 uint32_t i_font_color = p_sys->i_font_color;
558 int i_font_size = p_sys->i_font_size;
560 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size,
563 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color,
564 b_bold, b_italic, b_uline );
569 static int HandleFontAttributes( xml_reader_t *p_xml_reader,
570 font_stack_t **p_fonts, int i_scale )
573 char *psz_fontname = NULL;
574 uint32_t i_font_color = 0xffffff;
575 int i_font_alpha = 0;
576 int i_font_size = 24;
578 // Default all attributes to the top font in the stack -- in case not
579 // all attributes are specified in the sub-font
580 if( VLC_SUCCESS == PeekFont( p_fonts,
585 psz_fontname = strdup( psz_fontname );
586 i_font_size = i_font_size * 1000 / i_scale;
588 i_font_alpha = (i_font_color >> 24) & 0xff;
589 i_font_color &= 0x00ffffff;
591 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
593 char *psz_name = xml_ReaderName( p_xml_reader );
594 char *psz_value = xml_ReaderValue( p_xml_reader );
596 if( psz_name && psz_value )
598 if( !strcasecmp( "face", psz_name ) )
600 free( psz_fontname );
601 psz_fontname = strdup( psz_value );
603 else if( !strcasecmp( "size", psz_name ) )
605 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
607 int i_value = atoi( psz_value );
609 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
610 i_font_size += ( i_value * i_font_size ) / 10;
611 else if( i_value < -5 )
612 i_font_size = - i_value;
613 else if( i_value > 5 )
614 i_font_size = i_value;
617 i_font_size = atoi( psz_value );
619 else if( !strcasecmp( "color", psz_name ) &&
620 ( psz_value[0] == '#' ) )
622 i_font_color = strtol( psz_value + 1, NULL, 16 );
623 i_font_color &= 0x00ffffff;
625 else if( !strcasecmp( "alpha", psz_name ) &&
626 ( psz_value[0] == '#' ) )
628 i_font_alpha = strtol( psz_value + 1, NULL, 16 );
629 i_font_alpha &= 0xff;
635 rv = PushFont( p_fonts,
637 i_font_size * i_scale / 1000,
638 (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24) );
640 free( psz_fontname );
645 static int ProcessNodes( filter_t *p_filter,
646 xml_reader_t *p_xml_reader,
647 text_style_t *p_font_style,
652 uint32_t **ppi_run_lengths,
653 ATSUStyle **ppp_styles )
655 int rv = VLC_SUCCESS;
656 filter_sys_t *p_sys = p_filter->p_sys;
657 UniChar *psz_text_orig = psz_text;
658 font_stack_t *p_fonts = NULL;
662 char *psz_node = NULL;
664 vlc_bool_t b_italic = VLC_FALSE;
665 vlc_bool_t b_bold = VLC_FALSE;
666 vlc_bool_t b_uline = VLC_FALSE;
668 if( VLC_SUCCESS == var_Get( p_filter, "scale", &val ))
673 rv = PushFont( &p_fonts,
674 p_font_style->psz_fontname,
675 p_font_style->i_font_size * i_scale / 1000,
676 (p_font_style->i_font_color & 0xffffff) |
677 ((p_font_style->i_font_alpha & 0xff) << 24) );
679 if( p_font_style->i_style_flags & STYLE_BOLD )
681 if( p_font_style->i_style_flags & STYLE_ITALIC )
683 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
688 rv = PushFont( &p_fonts,
689 p_sys->psz_font_name,
691 p_sys->i_font_color );
693 if( rv != VLC_SUCCESS )
696 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
698 switch ( xml_ReaderNodeType( p_xml_reader ) )
700 case XML_READER_NONE:
702 case XML_READER_ENDELEM:
703 psz_node = xml_ReaderName( p_xml_reader );
707 if( !strcasecmp( "font", psz_node ) )
709 else if( !strcasecmp( "b", psz_node ) )
711 else if( !strcasecmp( "i", psz_node ) )
712 b_italic = VLC_FALSE;
713 else if( !strcasecmp( "u", psz_node ) )
719 case XML_READER_STARTELEM:
720 psz_node = xml_ReaderName( p_xml_reader );
723 if( !strcasecmp( "font", psz_node ) )
724 rv = HandleFontAttributes( p_xml_reader, &p_fonts, i_scale );
725 else if( !strcasecmp( "b", psz_node ) )
727 else if( !strcasecmp( "i", psz_node ) )
729 else if( !strcasecmp( "u", psz_node ) )
731 else if( !strcasecmp( "br", psz_node ) )
733 uint32_t i_string_length;
735 ConvertToUTF16( "\n", &i_string_length, &psz_text );
736 psz_text += i_string_length;
741 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
743 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
745 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
747 if( *ppi_run_lengths )
748 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
750 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
752 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
757 case XML_READER_TEXT:
758 psz_node = xml_ReaderValue( p_xml_reader );
761 uint32_t i_string_length;
763 // Turn any multiple-whitespaces into single spaces
764 char *s = strpbrk( psz_node, "\t\r\n " );
767 int i_whitespace = strspn( s, "\t\r\n " );
769 if( i_whitespace > 1 )
772 strlen( s ) - i_whitespace + 1 );
775 s = strpbrk( s, "\t\r\n " );
778 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
779 psz_text += i_string_length;
784 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
786 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
788 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
790 if( *ppi_run_lengths )
791 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
793 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
795 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
803 *pi_len = psz_text - psz_text_orig;
805 while( VLC_SUCCESS == PopFont( &p_fonts ) );
810 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
811 subpicture_region_t *p_region_in )
813 int rv = VLC_SUCCESS;
814 stream_t *p_sub = NULL;
816 xml_reader_t *p_xml_reader = NULL;
818 if( !p_region_in || !p_region_in->psz_html )
821 /* Reset the default fontsize in case screen metrics have changed */
822 p_filter->p_sys->i_font_size = GetFontSize( p_filter );
824 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
825 (uint8_t *) p_region_in->psz_html,
826 strlen( p_region_in->psz_html ),
830 p_xml = xml_Create( p_filter );
833 vlc_bool_t b_karaoke = VLC_FALSE;
835 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
838 /* Look for Root Node */
839 if( xml_ReaderRead( p_xml_reader ) == 1 )
841 char *psz_node = xml_ReaderName( p_xml_reader );
843 if( !strcasecmp( "karaoke", psz_node ) )
845 /* We're going to have to render the text a number
846 * of times to show the progress marker on the text.
848 var_SetBool( p_filter, "text-rerender", VLC_TRUE );
849 b_karaoke = VLC_TRUE;
851 else if( !strcasecmp( "text", psz_node ) )
853 b_karaoke = VLC_FALSE;
857 /* Only text and karaoke tags are supported */
858 xml_ReaderDelete( p_xml, p_xml_reader );
872 uint32_t *pi_run_lengths = NULL;
873 ATSUStyle *pp_styles = NULL;
875 psz_text = (UniChar *) malloc( strlen( p_region_in->psz_html ) *
881 rv = ProcessNodes( p_filter, p_xml_reader,
882 p_region_in->p_style, psz_text, &i_len,
883 &i_runs, &pi_run_lengths, &pp_styles );
885 p_region_out->i_x = p_region_in->i_x;
886 p_region_out->i_y = p_region_in->i_y;
888 if(( rv == VLC_SUCCESS ) && ( i_len > 0 ))
890 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs,
891 pi_run_lengths, pp_styles);
894 for( k=0; k<i_runs; k++)
895 ATSUDisposeStyle( pp_styles[k] );
897 free( pi_run_lengths );
901 xml_ReaderDelete( p_xml, p_xml_reader );
905 stream_Delete( p_sub );
911 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
912 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
914 offscreen_bitmap_t *p_bitmap;
915 CGContextRef p_context = NULL;
917 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
920 p_bitmap->i_bitsPerChannel = 8;
921 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
922 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
923 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
925 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
927 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
928 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
930 *pp_colorSpace = CreateGenericRGBColorSpace();
933 if( p_bitmap->p_data && *pp_colorSpace )
935 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
936 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
937 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
941 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
942 // OS X 10.1 doesn't support weak linking of this call which is only available
943 // int 10.4 and later
944 if( CGContextSetAllowsAntialiasing != NULL )
946 CGContextSetAllowsAntialiasing( p_context, true );
950 *pp_memory = p_bitmap;
956 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
957 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
958 int i_width, int i_height, int *pi_textblock_height )
960 offscreen_bitmap_t *p_offScreen = NULL;
961 CGColorSpaceRef p_colorSpace = NULL;
962 CGContextRef p_context = NULL;
964 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
968 ATSUTextLayout p_textLayout;
969 OSStatus status = noErr;
971 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
973 (const UniCharCount *) pi_run_lengths,
976 if( status == noErr )
978 // Attach our offscreen Image Graphics Context to the text style
979 // and setup the line alignment (have to specify the line width
980 // also in order for our chosen alignment to work)
982 Fract alignment = kATSUStartAlignment;
983 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
985 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
986 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
987 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
989 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
991 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
993 alignment = kATSUEndAlignment;
995 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
997 alignment = kATSUCenterAlignment;
1000 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
1002 // let ATSUI deal with characters not-in-our-specified-font
1003 ATSUSetTransientFontMatching( p_textLayout, true );
1005 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
1006 Fixed y = Long2Fix( i_height );
1008 // Set the line-breaks and draw individual lines
1009 uint32_t i_start = 0;
1010 uint32_t i_end = i_text_len;
1012 // Set up black outlining of the text --
1013 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
1014 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
1018 // ATSUBreakLine will automatically pick up any manual '\n's also
1019 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
1020 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
1024 uint32_t i_actualSize;
1026 // Come down far enough to fit the height of this line --
1027 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
1028 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
1030 // Quartz uses an upside-down co-ordinate space -> y values decrease as
1031 // you move down the page
1034 // Set the outlining for this line to be dependant on the size of the line -
1035 // make it about 5% of the ascent, with a minimum at 1.0
1036 float f_thickness = FixedToFloat( ascent ) * 0.05;
1037 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
1039 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
1041 // and now prepare for the next line by coming down far enough for our
1043 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
1044 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
1052 while( i_end < i_text_len );
1054 *pi_textblock_height = i_height - Fix2Long( y );
1055 CGContextFlush( p_context );
1057 ATSUDisposeTextLayout( p_textLayout );
1060 CGContextRelease( p_context );
1062 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
1067 static int GetFontSize( filter_t *p_filter )
1069 return p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
1072 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
1073 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
1075 offscreen_bitmap_t *p_offScreen = NULL;
1076 int i_textblock_height = 0;
1078 int i_width = p_filter->fmt_out.video.i_visible_width;
1079 int i_height = p_filter->fmt_out.video.i_visible_height;
1080 int i_text_align = p_region->i_align & 0x3;
1082 if( !psz_utf16_str )
1084 msg_Err( p_filter, "Invalid argument to RenderYUVA" );
1085 return VLC_EGENERIC;
1088 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
1089 i_runs, pi_run_lengths, pp_styles,
1090 i_width, i_height, &i_textblock_height );
1094 msg_Err( p_filter, "No offscreen buffer" );
1095 return VLC_EGENERIC;
1098 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
1100 int x, y, i_offset, i_pitch;
1101 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
1102 subpicture_region_t *p_region_tmp;
1104 // Create a new subpicture region
1105 memset( &fmt, 0, sizeof(video_format_t) );
1106 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1108 fmt.i_width = fmt.i_visible_width = i_width;
1109 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
1110 fmt.i_x_offset = fmt.i_y_offset = 0;
1111 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
1114 msg_Err( p_filter, "cannot allocate SPU region" );
1115 return VLC_EGENERIC;
1117 p_region->fmt = p_region_tmp->fmt;
1118 p_region->picture = p_region_tmp->picture;
1119 free( p_region_tmp );
1121 p_dst_y = p_region->picture.Y_PIXELS;
1122 p_dst_u = p_region->picture.U_PIXELS;
1123 p_dst_v = p_region->picture.V_PIXELS;
1124 p_dst_a = p_region->picture.A_PIXELS;
1125 i_pitch = p_region->picture.A_PITCH;
1127 i_offset = VERTICAL_MARGIN *i_pitch;
1128 for( y=0; y<i_textblock_height; y++)
1130 for( x=0; x<i_width; x++)
1132 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
1133 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
1134 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
1135 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
1137 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
1138 802 * i_blue + 4096 + 131072 ) >> 13, 235);
1139 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
1140 3598 * i_blue + 4096 + 1048576) >> 13, 240);
1141 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
1142 -585 * i_blue + 4096 + 1048576) >> 13, 240);
1144 p_dst_y[ i_offset + x ] = i_y;
1145 p_dst_u[ i_offset + x ] = i_u;
1146 p_dst_v[ i_offset + x ] = i_v;
1147 p_dst_a[ i_offset + x ] = i_alpha;
1149 i_offset += i_pitch;
1152 free( p_offScreen->p_data );
1153 free( p_offScreen );