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_common.h>
33 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_filter.h>
38 #include <vlc_stream.h>
40 #include <vlc_input.h>
44 #include <Carbon/Carbon.h>
46 #define DEFAULT_FONT "Verdana"
47 #define DEFAULT_FONT_COLOR 0xffffff
48 #define DEFAULT_REL_FONT_SIZE 16
50 #define VERTICAL_MARGIN 3
51 #define HORIZONTAL_MARGIN 10
53 //////////////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////////////
56 static int Create ( vlc_object_t * );
57 static void Destroy( vlc_object_t * );
59 static int LoadFontsFromAttachments( filter_t *p_filter );
61 static int RenderText( filter_t *, subpicture_region_t *,
62 subpicture_region_t * );
63 static int RenderHtml( filter_t *, subpicture_region_t *,
64 subpicture_region_t * );
66 static int GetFontSize( filter_t *p_filter );
67 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region,
68 UniChar *psz_utfString, uint32_t i_text_len,
69 uint32_t i_runs, uint32_t *pi_run_lengths,
70 ATSUStyle *pp_styles );
71 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size,
72 uint32_t i_font_color,
73 bool b_bold, bool b_italic,
75 //////////////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////////////
79 // The preferred way to set font style information is for it to come from the
80 // subtitle file, and for it to be rendered with RenderHtml instead of
81 // RenderText. This module, unlike Freetype, doesn't provide any options to
82 // override the fallback font selection used when this style information is
85 set_shortname( N_("Mac Text renderer"));
86 set_description( N_("Quartz font renderer") );
87 set_category( CAT_VIDEO );
88 set_subcategory( SUBCAT_VIDEO_SUBPIC );
90 set_capability( "text renderer", 120 );
91 add_shortcut( "text" );
92 set_callbacks( Create, Destroy );
95 typedef struct font_stack_t font_stack_t;
100 uint32_t i_color; // ARGB
102 font_stack_t *p_next;
105 typedef struct offscreen_bitmap_t offscreen_bitmap_t;
106 struct offscreen_bitmap_t
109 int i_bitsPerChannel;
115 //////////////////////////////////////////////////////////////////////////////
116 // filter_sys_t: quartztext local data
117 //////////////////////////////////////////////////////////////////////////////
118 // This structure is part of the video output thread descriptor.
119 // It describes the freetype specific properties of an output thread.
120 //////////////////////////////////////////////////////////////////////////////
124 uint8_t i_font_opacity;
128 ATSFontContainerRef *p_fonts;
132 //////////////////////////////////////////////////////////////////////////////
133 // Create: allocates osd-text video thread output method
134 //////////////////////////////////////////////////////////////////////////////
135 // This function allocates and initializes a Clone vout method.
136 //////////////////////////////////////////////////////////////////////////////
137 static int Create( vlc_object_t *p_this )
139 filter_t *p_filter = (filter_t *)p_this;
142 // Allocate structure
143 p_filter->p_sys = p_sys = malloc( sizeof( filter_sys_t ) );
146 msg_Err( p_filter, "out of memory" );
149 p_sys->psz_font_name = strdup( DEFAULT_FONT );
150 p_sys->i_font_opacity = 255;
151 p_sys->i_font_color = DEFAULT_FONT_COLOR;
152 p_sys->i_font_size = GetFontSize( p_filter );
154 p_filter->pf_render_text = RenderText;
155 p_filter->pf_render_html = RenderHtml;
157 p_sys->p_fonts = NULL;
160 LoadFontsFromAttachments( p_filter );
165 //////////////////////////////////////////////////////////////////////////////
166 // Destroy: destroy Clone video thread output method
167 //////////////////////////////////////////////////////////////////////////////
168 // Clean up all data and library connections
169 //////////////////////////////////////////////////////////////////////////////
170 static void Destroy( vlc_object_t *p_this )
172 filter_t *p_filter = (filter_t *)p_this;
173 filter_sys_t *p_sys = p_filter->p_sys;
179 for( k = 0; k < p_sys->i_fonts; k++ )
181 ATSFontDeactivate( p_sys->p_fonts[k], NULL, kATSOptionFlagsDefault );
184 free( p_sys->p_fonts );
187 free( p_sys->psz_font_name );
191 //////////////////////////////////////////////////////////////////////////////
192 // Make any TTF/OTF fonts present in the attachments of the media file
193 // available to the Quartz engine for text rendering
194 //////////////////////////////////////////////////////////////////////////////
195 static int LoadFontsFromAttachments( filter_t *p_filter )
197 filter_sys_t *p_sys = p_filter->p_sys;
198 input_thread_t *p_input;
199 input_attachment_t **pp_attachments;
200 int i_attachments_cnt;
202 int rv = VLC_SUCCESS;
204 p_input = (input_thread_t *)vlc_object_find( p_filter, VLC_OBJECT_INPUT, FIND_PARENT );
208 if( VLC_SUCCESS != input_Control( p_input, INPUT_GET_ATTACHMENTS, &pp_attachments, &i_attachments_cnt ))
210 vlc_object_release(p_input);
215 p_sys->p_fonts = malloc( i_attachments_cnt * sizeof( ATSFontContainerRef ) );
216 if(! p_sys->p_fonts )
219 for( k = 0; k < i_attachments_cnt; k++ )
221 input_attachment_t *p_attach = pp_attachments[k];
225 if(( !strcmp( p_attach->psz_mime, "application/x-truetype-font" ) || // TTF
226 !strcmp( p_attach->psz_mime, "application/x-font-otf" ) ) && // OTF
227 ( p_attach->i_data > 0 ) &&
228 ( p_attach->p_data != NULL ) )
230 ATSFontContainerRef container;
232 if( noErr == ATSFontActivateFromMemory( p_attach->p_data,
234 kATSFontContextLocal,
235 kATSFontFormatUnspecified,
237 kATSOptionFlagsDefault,
240 p_sys->p_fonts[ p_sys->i_fonts++ ] = container;
244 vlc_input_attachment_Delete( p_attach );
246 free( pp_attachments );
248 vlc_object_release(p_input);
253 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
254 // Original version of these functions available on:
255 // http://developer.apple.com/documentation/Carbon/Conceptual/QuickDrawToQuartz2D/tq_color/chapter_4_section_3.html
257 #define kGenericRGBProfilePathStr "/System/Library/ColorSync/Profiles/Generic RGB Profile.icc"
259 static CMProfileRef OpenGenericProfile( void )
261 static CMProfileRef cached_rgb_prof = NULL;
263 // Create the profile reference only once
264 if( cached_rgb_prof == NULL )
267 CMProfileLocation loc;
269 loc.locType = cmPathBasedProfile;
270 strcpy( loc.u.pathLoc.path, kGenericRGBProfilePathStr );
272 err = CMOpenProfile( &cached_rgb_prof, &loc );
276 cached_rgb_prof = NULL;
280 if( cached_rgb_prof )
282 // Clone the profile reference so that the caller has
283 // their own reference, not our cached one.
284 CMCloneProfileRef( cached_rgb_prof );
287 return cached_rgb_prof;
290 static CGColorSpaceRef CreateGenericRGBColorSpace( void )
292 static CGColorSpaceRef p_generic_rgb_cs = NULL;
294 if( p_generic_rgb_cs == NULL )
296 CMProfileRef generic_rgb_prof = OpenGenericProfile();
298 if( generic_rgb_prof )
300 p_generic_rgb_cs = CGColorSpaceCreateWithPlatformColorSpace( generic_rgb_prof );
302 CMCloseProfile( generic_rgb_prof );
306 return p_generic_rgb_cs;
310 static char *EliminateCRLF( char *psz_string )
315 for( p = psz_string; p && *p; p++ )
317 if( ( *p == '\r' ) && ( *(p+1) == '\n' ) )
319 for( q = p + 1; *q; q++ )
328 // Convert UTF-8 string to UTF-16 character array -- internal Mac Endian-ness ;
329 // we don't need to worry about bidirectional text conversion as ATSUI should
330 // handle that for us automatically
331 static void ConvertToUTF16( const char *psz_utf8_str, uint32_t *pi_strlen, UniChar **ppsz_utf16_str )
333 CFStringRef p_cfString;
336 p_cfString = CFStringCreateWithCString( NULL, psz_utf8_str, kCFStringEncodingUTF8 );
337 i_string_length = CFStringGetLength( p_cfString );
340 *pi_strlen = i_string_length;
342 if( !*ppsz_utf16_str )
343 *ppsz_utf16_str = (UniChar *) calloc( i_string_length, sizeof( UniChar ) );
345 CFStringGetCharacters( p_cfString, CFRangeMake( 0, i_string_length ), *ppsz_utf16_str );
347 CFRelease( p_cfString );
350 // Renders a text subpicture region into another one.
351 // It is used as pf_add_string callback in the vout method by this module
352 static int RenderText( filter_t *p_filter, subpicture_region_t *p_region_out,
353 subpicture_region_t *p_region_in )
355 filter_sys_t *p_sys = p_filter->p_sys;
356 UniChar *psz_utf16_str = NULL;
357 uint32_t i_string_length;
359 int i_font_color, i_font_alpha, i_font_size;
363 p_sys->i_font_size = GetFontSize( p_filter );
366 if( !p_region_in || !p_region_out ) return VLC_EGENERIC;
367 psz_string = p_region_in->psz_text;
368 if( !psz_string || !*psz_string ) return VLC_EGENERIC;
370 if( VLC_SUCCESS == var_Get( p_filter, "scale", &val ))
373 if( p_region_in->p_style )
375 i_font_color = __MAX( __MIN( p_region_in->p_style->i_font_color, 0xFFFFFF ), 0 );
376 i_font_alpha = __MAX( __MIN( p_region_in->p_style->i_font_alpha, 255 ), 0 );
377 i_font_size = __MAX( __MIN( p_region_in->p_style->i_font_size, 255 ), 0 ) * i_scale / 1000;
381 i_font_color = p_sys->i_font_color;
382 i_font_alpha = 255 - p_sys->i_font_opacity;
383 i_font_size = p_sys->i_font_size;
386 if( !i_font_alpha ) i_font_alpha = 255 - p_sys->i_font_opacity;
388 ConvertToUTF16( EliminateCRLF( psz_string ), &i_string_length, &psz_utf16_str );
390 p_region_out->i_x = p_region_in->i_x;
391 p_region_out->i_y = p_region_in->i_y;
393 if( psz_utf16_str != NULL )
395 ATSUStyle p_style = CreateStyle( p_sys->psz_font_name, i_font_size,
396 (i_font_color & 0xffffff) |
397 ((i_font_alpha & 0xff) << 24),
398 false, false, false );
401 RenderYUVA( p_filter, p_region_out, psz_utf16_str, i_string_length,
402 1, &i_string_length, &p_style );
405 ATSUDisposeStyle( p_style );
406 free( psz_utf16_str );
413 static ATSUStyle CreateStyle( char *psz_fontname, int i_font_size, uint32_t i_font_color,
414 bool b_bold, bool b_italic, bool b_uline )
420 float f_red = (float)(( i_font_color & 0x00FF0000 ) >> 16) / 255.0;
421 float f_green = (float)(( i_font_color & 0x0000FF00 ) >> 8) / 255.0;
422 float f_blue = (float)( i_font_color & 0x000000FF ) / 255.0;
423 float f_alpha = ( 255.0 - (float)(( i_font_color & 0xFF000000 ) >> 24)) / 255.0;
426 Fixed font_size = IntToFixed( i_font_size );
427 ATSURGBAlphaColor font_color = { f_red, f_green, f_blue, f_alpha };
428 Boolean bold = b_bold;
429 Boolean italic = b_italic;
430 Boolean uline = b_uline;
432 ATSUAttributeTag tags[] = { kATSUSizeTag, kATSURGBAlphaColorTag, kATSUQDItalicTag,
433 kATSUQDBoldfaceTag, kATSUQDUnderlineTag, kATSUFontTag };
434 ByteCount sizes[] = { sizeof( Fixed ), sizeof( ATSURGBAlphaColor ), sizeof( Boolean ),
435 sizeof( Boolean ), sizeof( Boolean ), sizeof( ATSUFontID )};
436 ATSUAttributeValuePtr values[] = { &font_size, &font_color, &italic, &bold, &uline, &font };
438 i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
440 status = ATSUFindFontFromName( psz_fontname,
441 strlen( psz_fontname ),
448 if( status != noErr )
450 // If we can't find a suitable font, just do everything else
454 if( noErr == ATSUCreateStyle( &p_style ) )
456 if( noErr == ATSUSetAttributes( p_style, i_tag_cnt, tags, sizes, values ) )
460 ATSUDisposeStyle( p_style );
465 static int PushFont( font_stack_t **p_font, const char *psz_name, int i_size,
473 p_new = malloc( sizeof( font_stack_t ) );
477 p_new->p_next = NULL;
480 p_new->psz_name = strdup( psz_name );
482 p_new->psz_name = NULL;
484 p_new->i_size = i_size;
485 p_new->i_color = i_color;
493 font_stack_t *p_last;
495 for( p_last = *p_font;
497 p_last = p_last->p_next )
500 p_last->p_next = p_new;
505 static int PopFont( font_stack_t **p_font )
507 font_stack_t *p_last, *p_next_to_last;
509 if( !p_font || !*p_font )
512 p_next_to_last = NULL;
513 for( p_last = *p_font;
515 p_last = p_last->p_next )
517 p_next_to_last = p_last;
521 p_next_to_last->p_next = NULL;
525 free( p_last->psz_name );
531 static int PeekFont( font_stack_t **p_font, char **psz_name, int *i_size,
534 font_stack_t *p_last;
536 if( !p_font || !*p_font )
541 p_last=p_last->p_next )
544 *psz_name = p_last->psz_name;
545 *i_size = p_last->i_size;
546 *i_color = p_last->i_color;
551 static ATSUStyle GetStyleFromFontStack( filter_sys_t *p_sys,
552 font_stack_t **p_fonts, bool b_bold, bool b_italic,
555 ATSUStyle p_style = NULL;
557 char *psz_fontname = NULL;
558 uint32_t i_font_color = p_sys->i_font_color;
559 int i_font_size = p_sys->i_font_size;
561 if( VLC_SUCCESS == PeekFont( p_fonts, &psz_fontname, &i_font_size,
564 p_style = CreateStyle( psz_fontname, i_font_size, i_font_color,
565 b_bold, b_italic, b_uline );
570 static int HandleFontAttributes( xml_reader_t *p_xml_reader,
571 font_stack_t **p_fonts, int i_scale )
574 char *psz_fontname = NULL;
575 uint32_t i_font_color = 0xffffff;
576 int i_font_alpha = 0;
577 int i_font_size = 24;
579 // Default all attributes to the top font in the stack -- in case not
580 // all attributes are specified in the sub-font
581 if( VLC_SUCCESS == PeekFont( p_fonts,
586 psz_fontname = strdup( psz_fontname );
587 i_font_size = i_font_size * 1000 / i_scale;
589 i_font_alpha = (i_font_color >> 24) & 0xff;
590 i_font_color &= 0x00ffffff;
592 while ( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
594 char *psz_name = xml_ReaderName( p_xml_reader );
595 char *psz_value = xml_ReaderValue( p_xml_reader );
597 if( psz_name && psz_value )
599 if( !strcasecmp( "face", psz_name ) )
601 free( psz_fontname );
602 psz_fontname = strdup( psz_value );
604 else if( !strcasecmp( "size", psz_name ) )
606 if( ( *psz_value == '+' ) || ( *psz_value == '-' ) )
608 int i_value = atoi( psz_value );
610 if( ( i_value >= -5 ) && ( i_value <= 5 ) )
611 i_font_size += ( i_value * i_font_size ) / 10;
612 else if( i_value < -5 )
613 i_font_size = - i_value;
614 else if( i_value > 5 )
615 i_font_size = i_value;
618 i_font_size = atoi( psz_value );
620 else if( !strcasecmp( "color", psz_name ) &&
621 ( psz_value[0] == '#' ) )
623 i_font_color = strtol( psz_value + 1, NULL, 16 );
624 i_font_color &= 0x00ffffff;
626 else if( !strcasecmp( "alpha", psz_name ) &&
627 ( psz_value[0] == '#' ) )
629 i_font_alpha = strtol( psz_value + 1, NULL, 16 );
630 i_font_alpha &= 0xff;
636 rv = PushFont( p_fonts,
638 i_font_size * i_scale / 1000,
639 (i_font_color & 0xffffff) | ((i_font_alpha & 0xff) << 24) );
641 free( psz_fontname );
646 static int ProcessNodes( filter_t *p_filter,
647 xml_reader_t *p_xml_reader,
648 text_style_t *p_font_style,
653 uint32_t **ppi_run_lengths,
654 ATSUStyle **ppp_styles )
656 int rv = VLC_SUCCESS;
657 filter_sys_t *p_sys = p_filter->p_sys;
658 UniChar *psz_text_orig = psz_text;
659 font_stack_t *p_fonts = NULL;
663 char *psz_node = NULL;
665 bool b_italic = false;
667 bool b_uline = false;
669 if( VLC_SUCCESS == var_Get( p_filter, "scale", &val ))
674 rv = PushFont( &p_fonts,
675 p_font_style->psz_fontname,
676 p_font_style->i_font_size * i_scale / 1000,
677 (p_font_style->i_font_color & 0xffffff) |
678 ((p_font_style->i_font_alpha & 0xff) << 24) );
680 if( p_font_style->i_style_flags & STYLE_BOLD )
682 if( p_font_style->i_style_flags & STYLE_ITALIC )
684 if( p_font_style->i_style_flags & STYLE_UNDERLINE )
689 rv = PushFont( &p_fonts,
690 p_sys->psz_font_name,
692 p_sys->i_font_color );
694 if( rv != VLC_SUCCESS )
697 while ( ( xml_ReaderRead( p_xml_reader ) == 1 ) )
699 switch ( xml_ReaderNodeType( p_xml_reader ) )
701 case XML_READER_NONE:
703 case XML_READER_ENDELEM:
704 psz_node = xml_ReaderName( p_xml_reader );
708 if( !strcasecmp( "font", psz_node ) )
710 else if( !strcasecmp( "b", psz_node ) )
712 else if( !strcasecmp( "i", psz_node ) )
714 else if( !strcasecmp( "u", psz_node ) )
720 case XML_READER_STARTELEM:
721 psz_node = xml_ReaderName( p_xml_reader );
724 if( !strcasecmp( "font", psz_node ) )
725 rv = HandleFontAttributes( p_xml_reader, &p_fonts, i_scale );
726 else if( !strcasecmp( "b", psz_node ) )
728 else if( !strcasecmp( "i", psz_node ) )
730 else if( !strcasecmp( "u", psz_node ) )
732 else if( !strcasecmp( "br", psz_node ) )
734 uint32_t i_string_length;
736 ConvertToUTF16( "\n", &i_string_length, &psz_text );
737 psz_text += i_string_length;
742 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
744 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
746 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
748 if( *ppi_run_lengths )
749 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
751 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
753 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
758 case XML_READER_TEXT:
759 psz_node = xml_ReaderValue( p_xml_reader );
762 uint32_t i_string_length;
764 // Turn any multiple-whitespaces into single spaces
765 char *s = strpbrk( psz_node, "\t\r\n " );
768 int i_whitespace = strspn( s, "\t\r\n " );
770 if( i_whitespace > 1 )
773 strlen( s ) - i_whitespace + 1 );
776 s = strpbrk( s, "\t\r\n " );
779 ConvertToUTF16( psz_node, &i_string_length, &psz_text );
780 psz_text += i_string_length;
785 *ppp_styles = (ATSUStyle *) realloc( *ppp_styles, *pi_runs * sizeof( ATSUStyle ) );
787 *ppp_styles = (ATSUStyle *) malloc( *pi_runs * sizeof( ATSUStyle ) );
789 (*ppp_styles)[ *pi_runs - 1 ] = GetStyleFromFontStack( p_sys, &p_fonts, b_bold, b_italic, b_uline );
791 if( *ppi_run_lengths )
792 *ppi_run_lengths = (uint32_t *) realloc( *ppi_run_lengths, *pi_runs * sizeof( uint32_t ) );
794 *ppi_run_lengths = (uint32_t *) malloc( *pi_runs * sizeof( uint32_t ) );
796 (*ppi_run_lengths)[ *pi_runs - 1 ] = i_string_length;
804 *pi_len = psz_text - psz_text_orig;
806 while( VLC_SUCCESS == PopFont( &p_fonts ) );
811 static int RenderHtml( filter_t *p_filter, subpicture_region_t *p_region_out,
812 subpicture_region_t *p_region_in )
814 int rv = VLC_SUCCESS;
815 stream_t *p_sub = NULL;
817 xml_reader_t *p_xml_reader = NULL;
819 if( !p_region_in || !p_region_in->psz_html )
822 /* Reset the default fontsize in case screen metrics have changed */
823 p_filter->p_sys->i_font_size = GetFontSize( p_filter );
825 p_sub = stream_MemoryNew( VLC_OBJECT(p_filter),
826 (uint8_t *) p_region_in->psz_html,
827 strlen( p_region_in->psz_html ),
831 p_xml = xml_Create( p_filter );
834 bool b_karaoke = false;
836 p_xml_reader = xml_ReaderCreate( p_xml, p_sub );
839 /* Look for Root Node */
840 if( xml_ReaderRead( p_xml_reader ) == 1 )
842 char *psz_node = xml_ReaderName( p_xml_reader );
844 if( !strcasecmp( "karaoke", psz_node ) )
846 /* We're going to have to render the text a number
847 * of times to show the progress marker on the text.
849 var_SetBool( p_filter, "text-rerender", true );
852 else if( !strcasecmp( "text", psz_node ) )
858 /* Only text and karaoke tags are supported */
859 xml_ReaderDelete( p_xml, p_xml_reader );
873 uint32_t *pi_run_lengths = NULL;
874 ATSUStyle *pp_styles = NULL;
876 psz_text = (UniChar *) malloc( strlen( p_region_in->psz_html ) *
882 rv = ProcessNodes( p_filter, p_xml_reader,
883 p_region_in->p_style, psz_text, &i_len,
884 &i_runs, &pi_run_lengths, &pp_styles );
886 p_region_out->i_x = p_region_in->i_x;
887 p_region_out->i_y = p_region_in->i_y;
889 if(( rv == VLC_SUCCESS ) && ( i_len > 0 ))
891 RenderYUVA( p_filter, p_region_out, psz_text, i_len, i_runs,
892 pi_run_lengths, pp_styles);
895 for( k=0; k<i_runs; k++)
896 ATSUDisposeStyle( pp_styles[k] );
898 free( pi_run_lengths );
902 xml_ReaderDelete( p_xml, p_xml_reader );
906 stream_Delete( p_sub );
912 static CGContextRef CreateOffScreenContext( int i_width, int i_height,
913 offscreen_bitmap_t **pp_memory, CGColorSpaceRef *pp_colorSpace )
915 offscreen_bitmap_t *p_bitmap;
916 CGContextRef p_context = NULL;
918 p_bitmap = (offscreen_bitmap_t *) malloc( sizeof( offscreen_bitmap_t ));
921 p_bitmap->i_bitsPerChannel = 8;
922 p_bitmap->i_bitsPerPixel = 4 * p_bitmap->i_bitsPerChannel; // A,R,G,B
923 p_bitmap->i_bytesPerPixel = p_bitmap->i_bitsPerPixel / 8;
924 p_bitmap->i_bytesPerRow = i_width * p_bitmap->i_bytesPerPixel;
926 p_bitmap->p_data = calloc( i_height, p_bitmap->i_bytesPerRow );
928 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
929 *pp_colorSpace = CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB );
931 *pp_colorSpace = CreateGenericRGBColorSpace();
934 if( p_bitmap->p_data && *pp_colorSpace )
936 p_context = CGBitmapContextCreate( p_bitmap->p_data, i_width, i_height,
937 p_bitmap->i_bitsPerChannel, p_bitmap->i_bytesPerRow,
938 *pp_colorSpace, kCGImageAlphaPremultipliedFirst);
942 #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_1
943 // OS X 10.1 doesn't support weak linking of this call which is only available
944 // int 10.4 and later
945 if( CGContextSetAllowsAntialiasing != NULL )
947 CGContextSetAllowsAntialiasing( p_context, true );
951 *pp_memory = p_bitmap;
957 static offscreen_bitmap_t *Compose( int i_text_align, UniChar *psz_utf16_str, uint32_t i_text_len,
958 uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles,
959 int i_width, int i_height, int *pi_textblock_height )
961 offscreen_bitmap_t *p_offScreen = NULL;
962 CGColorSpaceRef p_colorSpace = NULL;
963 CGContextRef p_context = NULL;
965 p_context = CreateOffScreenContext( i_width, i_height, &p_offScreen, &p_colorSpace );
969 ATSUTextLayout p_textLayout;
970 OSStatus status = noErr;
972 status = ATSUCreateTextLayoutWithTextPtr( psz_utf16_str, 0, i_text_len, i_text_len,
974 (const UniCharCount *) pi_run_lengths,
977 if( status == noErr )
979 // Attach our offscreen Image Graphics Context to the text style
980 // and setup the line alignment (have to specify the line width
981 // also in order for our chosen alignment to work)
983 Fract alignment = kATSUStartAlignment;
984 Fixed line_width = Long2Fix( i_width - HORIZONTAL_MARGIN * 2 );
986 ATSUAttributeTag tags[] = { kATSUCGContextTag, kATSULineFlushFactorTag, kATSULineWidthTag };
987 ByteCount sizes[] = { sizeof( CGContextRef ), sizeof( Fract ), sizeof( Fixed ) };
988 ATSUAttributeValuePtr values[] = { &p_context, &alignment, &line_width };
990 int i_tag_cnt = sizeof( tags ) / sizeof( ATSUAttributeTag );
992 if( i_text_align == SUBPICTURE_ALIGN_RIGHT )
994 alignment = kATSUEndAlignment;
996 else if( i_text_align != SUBPICTURE_ALIGN_LEFT )
998 alignment = kATSUCenterAlignment;
1001 ATSUSetLayoutControls( p_textLayout, i_tag_cnt, tags, sizes, values );
1003 // let ATSUI deal with characters not-in-our-specified-font
1004 ATSUSetTransientFontMatching( p_textLayout, true );
1006 Fixed x = Long2Fix( HORIZONTAL_MARGIN );
1007 Fixed y = Long2Fix( i_height );
1009 // Set the line-breaks and draw individual lines
1010 uint32_t i_start = 0;
1011 uint32_t i_end = i_text_len;
1013 // Set up black outlining of the text --
1014 CGContextSetRGBStrokeColor( p_context, 0, 0, 0, 0.5 );
1015 CGContextSetTextDrawingMode( p_context, kCGTextFillStroke );
1019 // ATSUBreakLine will automatically pick up any manual '\n's also
1020 status = ATSUBreakLine( p_textLayout, i_start, line_width, true, (UniCharArrayOffset *) &i_end );
1021 if( ( status == noErr ) || ( status == kATSULineBreakInWord ) )
1025 uint32_t i_actualSize;
1027 // Come down far enough to fit the height of this line --
1028 ATSUGetLineControl( p_textLayout, i_start, kATSULineAscentTag,
1029 sizeof( Fixed ), &ascent, (ByteCount *) &i_actualSize );
1031 // Quartz uses an upside-down co-ordinate space -> y values decrease as
1032 // you move down the page
1035 // Set the outlining for this line to be dependent on the size of the line -
1036 // make it about 5% of the ascent, with a minimum at 1.0
1037 float f_thickness = FixedToFloat( ascent ) * 0.05;
1038 CGContextSetLineWidth( p_context, (( f_thickness > 1.0 ) ? 1.0 : f_thickness ));
1040 ATSUDrawText( p_textLayout, i_start, i_end - i_start, x, y );
1042 // and now prepare for the next line by coming down far enough for our
1044 ATSUGetLineControl( p_textLayout, i_start, kATSULineDescentTag,
1045 sizeof( Fixed ), &descent, (ByteCount *) &i_actualSize );
1053 while( i_end < i_text_len );
1055 *pi_textblock_height = i_height - Fix2Long( y );
1056 CGContextFlush( p_context );
1058 ATSUDisposeTextLayout( p_textLayout );
1061 CGContextRelease( p_context );
1063 if( p_colorSpace ) CGColorSpaceRelease( p_colorSpace );
1068 static int GetFontSize( filter_t *p_filter )
1070 return p_filter->fmt_out.video.i_height / DEFAULT_REL_FONT_SIZE;
1073 static int RenderYUVA( filter_t *p_filter, subpicture_region_t *p_region, UniChar *psz_utf16_str,
1074 uint32_t i_text_len, uint32_t i_runs, uint32_t *pi_run_lengths, ATSUStyle *pp_styles )
1076 offscreen_bitmap_t *p_offScreen = NULL;
1077 int i_textblock_height = 0;
1079 int i_width = p_filter->fmt_out.video.i_visible_width;
1080 int i_height = p_filter->fmt_out.video.i_visible_height;
1081 int i_text_align = p_region->i_align & 0x3;
1083 if( !psz_utf16_str )
1085 msg_Err( p_filter, "Invalid argument to RenderYUVA" );
1086 return VLC_EGENERIC;
1089 p_offScreen = Compose( i_text_align, psz_utf16_str, i_text_len,
1090 i_runs, pi_run_lengths, pp_styles,
1091 i_width, i_height, &i_textblock_height );
1095 msg_Err( p_filter, "No offscreen buffer" );
1096 return VLC_EGENERIC;
1099 uint8_t *p_dst_y,*p_dst_u,*p_dst_v,*p_dst_a;
1101 int x, y, i_offset, i_pitch;
1102 uint8_t i_y, i_u, i_v; // YUV values, derived from incoming RGB
1103 subpicture_region_t *p_region_tmp;
1105 // Create a new subpicture region
1106 memset( &fmt, 0, sizeof(video_format_t) );
1107 fmt.i_chroma = VLC_FOURCC('Y','U','V','A');
1109 fmt.i_width = fmt.i_visible_width = i_width;
1110 fmt.i_height = fmt.i_visible_height = i_textblock_height + VERTICAL_MARGIN * 2;
1111 fmt.i_x_offset = fmt.i_y_offset = 0;
1112 p_region_tmp = spu_CreateRegion( p_filter, &fmt );
1115 msg_Err( p_filter, "cannot allocate SPU region" );
1116 return VLC_EGENERIC;
1118 p_region->fmt = p_region_tmp->fmt;
1119 p_region->picture = p_region_tmp->picture;
1120 free( p_region_tmp );
1122 p_dst_y = p_region->picture.Y_PIXELS;
1123 p_dst_u = p_region->picture.U_PIXELS;
1124 p_dst_v = p_region->picture.V_PIXELS;
1125 p_dst_a = p_region->picture.A_PIXELS;
1126 i_pitch = p_region->picture.A_PITCH;
1128 i_offset = VERTICAL_MARGIN *i_pitch;
1129 for( y=0; y<i_textblock_height; y++)
1131 for( x=0; x<i_width; x++)
1133 int i_alpha = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel ];
1134 int i_red = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 1 ];
1135 int i_green = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 2 ];
1136 int i_blue = p_offScreen->p_data[ y * p_offScreen->i_bytesPerRow + x * p_offScreen->i_bytesPerPixel + 3 ];
1138 i_y = (uint8_t)__MIN(abs( 2104 * i_red + 4130 * i_green +
1139 802 * i_blue + 4096 + 131072 ) >> 13, 235);
1140 i_u = (uint8_t)__MIN(abs( -1214 * i_red + -2384 * i_green +
1141 3598 * i_blue + 4096 + 1048576) >> 13, 240);
1142 i_v = (uint8_t)__MIN(abs( 3598 * i_red + -3013 * i_green +
1143 -585 * i_blue + 4096 + 1048576) >> 13, 240);
1145 p_dst_y[ i_offset + x ] = i_y;
1146 p_dst_u[ i_offset + x ] = i_u;
1147 p_dst_v[ i_offset + x ] = i_v;
1148 p_dst_a[ i_offset + x ] = i_alpha;
1150 i_offset += i_pitch;
1153 free( p_offScreen->p_data );
1154 free( p_offScreen );