Changeset 213

Show
Ignore:
Timestamp:
12/28/06 03:10:10 (2 years ago)
Author:
astrange
Message:

Outside-stroke and shadowed subtitles

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/TextSubCodec.c

    r211 r213  
    1414// basically random numbers i picked, font size is a factor of the diagonal of the movie window, border is a factor of that 
    1515#define FontSizeRatio .04 
    16 #define BorderSizeRatio FontSizeRatio * .045 
     16#define BorderSizeRatio FontSizeRatio * .09 
     17// in pixels 
     18#define ShadowDistance 2.8 
    1719 
    1820// Constants 
     
    2931        CGColorSpaceRef         colorSpace; 
    3032         
    31         ATSUStyle                               textStyle
     33        ATSUStyle                               textStyle, shadowStyle
    3234        ATSUTextLayout                  textLayout; 
    3335        //Ptr                     textBuffer; 
     
    278280        TextSubDecompressRecord *myDrp = (TextSubDecompressRecord *)drp->userDecompressRecord; 
    279281        float diagonalLength = sqrtf(myDrp->width*myDrp->width+myDrp->height*myDrp->height); 
    280         ATSUTextMeasurement lineWidth = Long2Fix(myDrp->width), lineHeight = Long2Fix(20)
     282        ATSUTextMeasurement lineWidth = Long2Fix(myDrp->width), lineHeight = Long2Fix(20), ShadowDistFix = FloatToFixed(ShadowDistance)
    281283//      char *dataPtr = (char *)drp->codecData; 
    282284//      ICMDataProcRecordPtr dataProc = drp->dataProcRecord.dataProc ? &drp->dataProcRecord : NULL; 
     
    289291                ATSUAttributeTag tags[] = {kATSUSizeTag, kATSURGBAlphaColorTag, kATSUStyleRenderingOptionsTag, kATSUFontTag}; 
    290292                ByteCount                sizes[] = {sizeof(Fixed), sizeof(ATSURGBAlphaColor), sizeof(ATSStyleRenderingOptions), sizeof(ATSUFontID)}; 
    291                 Fixed                    size = X2Fix(diagonalLength * FontSizeRatio); ATSURGBAlphaColor white = {1,1,1,1}
     293                Fixed                    size = X2Fix(diagonalLength * FontSizeRatio); ATSURGBAlphaColor white = {1,1,1,1}, black = {0,0,0,1}
    292294                Boolean                  trueval = TRUE; ATSUFontID fid; ATSStyleRenderingOptions rend = kATSStyleApplyAntiAliasing; 
    293                 ATSUAttributeValuePtr vals[] = {&size,&white,&rend,&fid}; 
     295                ATSUAttributeValuePtr vals[] = {&size, &white, &rend,&fid}; 
    294296                ATSUCreateStyle(&glob->textStyle); 
    295297                ATSUFindFontFromName(SubFontName,strlen(SubFontName),kFontFullName,kFontNoPlatform,kFontNoScript,kFontNoLanguage,&fid); 
    296298                ATSUSetAttributes(glob->textStyle,5, tags, sizes, vals); 
     299         
     300                ATSUAttributeTag btags[] = {kATSURGBAlphaColorTag}; 
     301                ByteCount                bsizes[] = {sizeof(ATSURGBAlphaColor)}; 
     302                ATSUAttributeValuePtr bvals[] = {&black}; 
     303                 
     304                ATSUCreateAndCopyStyle(glob->textStyle,&glob->shadowStyle); 
     305                ATSUSetAttributes(glob->shadowStyle,1, btags, bsizes, bvals); 
    297306        } 
    298307         
     
    346355         
    347356        CGContextClearRect(c, CGRectMake(0, 0, myDrp->width, myDrp->height)); 
    348         CGContextSetRGBStrokeColor(c, 0,0,0,1); 
    349         CGContextSetTextDrawingMode(c, kCGTextFillStroke); 
    350         CGContextSetLineWidth(c, diagonalLength * BorderSizeRatio); 
    351357 
    352358        for (i = breakCount; i >= 0; i--) { 
    353359                int end = breaks[i+1]; 
    354360                if (end == kATSUToTextEnd) end=sublen; 
     361                 
     362                // we draw shadow, then stroke text, then fill text 
     363                // this has several problems relating to hinting and line height and etc. 
     364                // ideally we'd draw the shadow of every line first (rather than shadow, text, next shadow, next text) 
     365                // and do a combined outside-stroke-then-fill operation on every line afterwards 
     366                // but ATSUI only has fill-then-stroke :( 
     367                CGContextSetTextDrawingMode(c, kCGTextFill); 
     368 
     369                ATSUSetRunStyle(glob->textLayout,glob->shadowStyle,kATSUFromTextBeginning,kATSUToTextEnd); 
     370                 
     371                ATSUDrawText(glob->textLayout, breaks[i], end-breaks[i], Long2Fix(0) + ShadowDistFix, lineHeight - ShadowDistFix); 
     372                 
     373                ATSUSetRunStyle(glob->textLayout,glob->textStyle,kATSUFromTextBeginning,kATSUToTextEnd); 
     374                 
     375                CGContextSetRGBStrokeColor(c, 0,0,0,1); 
     376                CGContextSetTextDrawingMode(c, kCGTextStroke); 
     377                CGContextSetLineWidth(c, diagonalLength * BorderSizeRatio); 
     378                 
    355379                ATSUDrawText(glob->textLayout, breaks[i], end-breaks[i], Long2Fix(0), lineHeight); 
    356  
     380                 
    357381                ATSUTextMeasurement ascent, descent; ByteCount unused; 
    358382                ATSUGetLineControl(glob->textLayout, breaks[i], kATSULineAscentTag, sizeof(ATSUTextMeasurement), &ascent, &unused); 
    359383                ATSUGetLineControl(glob->textLayout, breaks[i], kATSULineDescentTag, sizeof(ATSUTextMeasurement), &descent, &unused); 
     384                 
     385                CGContextSetTextDrawingMode(c, kCGTextFill); 
     386 
     387                ATSUDrawText(glob->textLayout, breaks[i], end-breaks[i], Long2Fix(0), lineHeight); 
     388 
     389//              CGContextSetRGBFillColor(c, 0,0,0,1); 
     390//              CGContextSetTextDrawingMode(c, kCGTextFill); 
     391 
    360392                lineHeight += ascent + descent; 
    361393        }