Changeset 1098
- Timestamp:
- 07/09/09 23:22:14 (13 months ago)
- Location:
- trunk/Subtitles
- Files:
-
- 2 modified
-
SubATSUIRenderer.h (modified) (1 diff)
-
SubATSUIRenderer.m (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Subtitles/SubATSUIRenderer.h
r1046 r1098 34 34 ATSUTextLayout layout; 35 35 float screenScaleX, screenScaleY, videoWidth, videoHeight; 36 BOOL drawTextBounds; 36 37 @public; 37 38 CGColorSpaceRef srgbCSpace; -
trunk/Subtitles/SubATSUIRenderer.m
r1086 r1098 25 25 #import "SubUtilities.h" 26 26 #import "Codecprintf.h" 27 #import "CommonUtils.h" 27 28 28 29 static float GetWinFontSizeScale(ATSFontRef font); … … 254 255 UCCreateTextBreakLocator(NULL, 0, kUCTextBreakLineMask, &breakLocator); 255 256 context = [[SubContext alloc] initWithNonSSAType:kSubTypeSRT delegate:self]; 257 258 drawTextBounds = CFPreferencesGetAppBooleanValue(CFSTR("DrawSubTextBounds"), PERIAN_PREF_DOMAIN, NULL); 256 259 } 257 260 … … 278 281 UCCreateTextBreakLocator(NULL, 0, kUCTextBreakLineMask, &breakLocator); 279 282 context = [[SubContext alloc] initWithHeaders:headers styles:styles delegate:self]; 283 284 drawTextBounds = CFPreferencesGetAppBooleanValue(CFSTR("DrawSubTextBounds"), PERIAN_PREF_DOMAIN, NULL); 280 285 } 281 286 … … 365 370 break; 366 371 } 367 } 368 369 if (font == kATSUInvalidFontID) font = ATSFontFindFromName((CFStringRef)@"Helvetica",kATSOptionFlagsDefault); // final fallback 370 } 372 } 373 } 374 375 if (font == kATSUInvalidFontID && ![name isEqualToString:@"Helvetica"]) 376 font = GetFontIDForSSAName(@"Helvetica"); // final fallback 371 377 372 378 [fontIDCache setValue:[NSNumber numberWithInt:font] forKey:[name lowercaseString]]; … … 467 473 CGColorRef color; 468 474 CGAffineTransform mat; 475 ATSFontRef oldFont; 469 476 470 477 #define bv() bval = *(int*)p; … … 504 511 case tag_fn: 505 512 sv(); 513 oldFont = spanEx->font; 506 514 spanEx->fontVertical = ParseFontVerticality(&sval) ? kATSUStronglyVertical : kATSUStronglyHorizontal; 507 515 spanEx->font = GetFontIDForSSAName(sval); 516 if (oldFont != spanEx->font) spanEx->platformSizeScale = GetWinFontSizeScale(spanEx->font); 508 517 UpdateFontNameSize(spanEx, screenScaleY); 509 518 break; … … 600 609 #pragma mark Rendering Helper Functions 601 610 611 // XXX see comment for GetTypographicRectangleForLayout 602 612 static ATSUTextMeasurement GetLineHeight(ATSUTextLayout layout, UniCharArrayOffset lpos) 603 613 { … … 618 628 } 619 629 630 // XXX some broken fonts have very wrong typographic values set, and so this occasionally gives nonsense 631 // it should be checked against the real pixel box (see #if 0 below), but for correct fonts it looks much better 620 632 static void GetTypographicRectangleForLayout(ATSUTextLayout layout, UniCharArrayOffset *breaks, ItemCount breakCount, Fixed extraHeight, Fixed *lX, Fixed *lY, Fixed *height, Fixed *width) 621 633 { … … 653 665 } 654 666 667 // Draw the text bounds on screen under the actual text 668 // Note that it almost never appears where it's supposed to be, and I'm not sure if that's my fault or ATSUI's 669 static void VisualizeLayoutLineHeights(CGContextRef c, ATSUTextLayout layout, UniCharArrayOffset *breaks, ItemCount breakCount, Fixed extraHeight, Fixed penX, Fixed penY, float screenHeight) 670 { 671 ATSTrapezoid trap = {0}; 672 Rect pixRect = {0}; 673 ItemCount trapCount; 674 int i; 675 676 CGContextSetLineWidth(c, 3.0); 677 678 for (i = breakCount; i >= 0; i--) { 679 UniCharArrayOffset end = breaks[i+1]; 680 681 ATSUMeasureTextImage(layout, breaks[i], end-breaks[i], 0, 0, &pixRect); 682 ATSUGetGlyphBounds(layout, 0, 0, breaks[i], end-breaks[i], kATSUseDeviceOrigins, 1, &trap, &trapCount); 683 684 CGContextSetRGBStrokeColor(c, 1,0,0,1); 685 CGContextBeginPath(c); 686 CGContextMoveToPoint(c, FixedToFloat(penX + trap.upperLeft.x), FixedToFloat(penY + trap.lowerLeft.y)); 687 CGContextAddLineToPoint(c, FixedToFloat(penX + trap.upperRight.x), FixedToFloat(penY + trap.lowerRight.y)); 688 CGContextAddLineToPoint(c, FixedToFloat(penX + trap.lowerRight.x), FixedToFloat(penY + trap.upperRight.y)); 689 CGContextAddLineToPoint(c, FixedToFloat(penX + trap.lowerLeft.x), FixedToFloat(penY + trap.upperLeft.y)); 690 CGContextClosePath(c); 691 CGContextStrokePath(c); 692 CGContextSetRGBStrokeColor(c, 0, 0, 1, 1); 693 CGContextStrokeRect(c, CGRectMake(FixedToFloat(penX) + pixRect.left, FixedToFloat(penY) + pixRect.top, pixRect.right - pixRect.left, pixRect.bottom - pixRect.top)); 694 695 penY += GetLineHeight(layout, breaks[i]) + extraHeight; 696 } 697 } 698 655 699 #if 0 656 700 static void GetImageBoundingBoxForLayout(ATSUTextLayout layout, UniCharArrayOffset *breaks, ItemCount breakCount, Fixed extraHeight, Fixed *lX, Fixed *lY, Fixed *height, Fixed *width) … … 674 718 largeRect.top = MIN(largeRect.top, rect.top); 675 719 largeRect.right = MAX(largeRect.right, rect.right); 676 }720 } 677 721 678 722 … … 1075 1119 breakbuffer = FindLineBreaks(layout, div, breakLocator, breakbuffer, &breakCount, breakingWidth, ubuffer, textLen); 1076 1120 1077 ATSUTextMeasurement imageWidth, imageHeight ;1121 ATSUTextMeasurement imageWidth, imageHeight, descent; 1078 1122 UniCharArrayOffset *breaks = breakbuffer; 1079 1123 1080 1124 if (div->positioned || div->alignV == kSubAlignmentMiddle) 1081 1125 GetTypographicRectangleForLayout(layout, breaks, breakCount, FloatToFixed(div->styleLine->outlineRadius), NULL, NULL, &imageHeight, &imageWidth); 1082 1126 1127 if (div->positioned || div->alignV == kSubAlignmentBottom) 1128 ATSUGetLineControl(layout, kATSUFromTextBeginning, kATSULineDescentTag, sizeof(ATSUTextMeasurement), &descent, NULL); 1129 1130 #if 0 1131 { 1132 ATSUTextMeasurement ascent, descent; 1133 1134 ATSUGetLineControl(layout, kATSUFromTextBeginning, kATSULineAscentTag, sizeof(ATSUTextMeasurement), &ascent, NULL); 1135 ATSUGetLineControl(layout, kATSUFromTextBeginning, kATSULineDescentTag, sizeof(ATSUTextMeasurement), &descent, NULL); 1136 1137 NSLog(@"\"%@\" descent %f ascent %f\n", div->text, FixedToFloat(descent), FixedToFloat(ascent)); 1138 } 1139 #endif 1140 1083 1141 if (!div->positioned) { 1084 1142 penX = FloatToFixed(NSMinX(marginRect)); … … 1087 1145 case kSubAlignmentBottom: default: 1088 1146 if (!bottomPen || resetPens) { 1089 ATSUTextMeasurement bottomLineDescent; 1090 ATSUGetLineControl(layout, kATSUFromTextBeginning, kATSULineDescentTag, sizeof(ATSUTextMeasurement), &bottomLineDescent, NULL); 1091 penY = FloatToFixed(NSMinY(marginRect)) + bottomLineDescent; 1147 penY = FloatToFixed(NSMinY(marginRect)) + descent; 1092 1148 } else penY = bottomPen; 1093 1149 … … 1110 1166 } 1111 1167 } else { 1112 ATSUTextMeasurement descent;1113 1168 penX = FloatToFixed(div->posX * screenScaleX); 1114 1169 penY = FloatToFixed((context->resY - div->posY) * screenScaleY); … … 1119 1174 } 1120 1175 1121 ATSUGetLineControl(layout, kATSUFromTextBeginning, kATSULineDescentTag, sizeof(ATSUTextMeasurement), &descent, NULL);1122 1123 1176 switch (div->alignV) { 1124 1177 case kSubAlignmentMiddle: penY -= imageHeight / 2; break; … … 1139 1192 } 1140 1193 1194 if (drawTextBounds) 1195 VisualizeLayoutLineHeights(c, layout, breaks, breakCount, FloatToFixed(div->styleLine->outlineRadius), penX, penY, cHeight); 1196 1141 1197 breakc.breakCount = breakCount; 1142 1198 breakc.breaks = breaks; … … 1306 1362 1307 1363 // Windows and OS X use different TrueType fields to measure text. 1308 // Some Windows fonts have one field set incorrectly , so we have to compensate.1364 // Some Windows fonts have one field set incorrectly(?), so we have to compensate. 1309 1365 // XXX This function doesn't read from the right fonts; if we're using italic variant, it should get the ATSFontRef for that 1366 // XXX^2 This should be cached 1310 1367 static float GetWinFontSizeScale(ATSFontRef font) 1311 1368 {
