English 中文(简体)
带有使用核心技术分流的分辨率
原标题:Bounding box of character with diacritics using CoreText
  • 时间:2012-05-08 20:02:36
  •  标签:
  • cocoa

我正试图利用各种技术,在MacOS X/iOS获得具有特征的捆绑箱。 我现在介绍我的所有尝试。 直到现在,如果我想要达到诸如“生态”等分属性的规模,该法典就失败了。

Using CTFontGetBoundingRectsForGlyphs

-(void) resizeLayer1:(CATextLayer *)l toString:(NSString *)string
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    string = l.string;
    NSUInteger len = [string length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.string, CFRangeMake(0, [l.string length]), characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont, l.fontSize, NULL, NULL);

    // allocate glyphs and bounding box arrays for holding the result
    // assuming that each character is only one glyph, which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(coreTextFont, characters, glyphs, len);

    // get bounding boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont, kCTFontDefaultOrientation, glyphs, bb, len);
    CFRelease(coreTextFont);

    l.position = CGPointMake(200.f, 100.f);

    l.bounds = bb[0];

    l.backgroundColor = CGColorCreateGenericRGB(0.f, .5f, .9f, 1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

<>Result> 这是一种工程,但随着《禁止酷刑和其他残忍、不人道或有辱人格的待遇或处罚公约》的颁布,一些dding正在继续。

Using CTFramesetterSuggestFrameSizeWithConstraints

-(void) resizeLayer2:(CATextLayer *)l toString:(NSString *)string
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    string = l.string;

    NSUInteger len = [string length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.string, CFRangeMake(0, [l.string length]), characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont, l.fontSize, NULL, NULL);

    CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString(attrStr, CFRangeMake(0, 0), (__bridge CFStringRef)string);
    CTTextAlignment alignment = kCTJustifiedTextAlignment;
    CTParagraphStyleSetting _settings[] = { {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} };
    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings) / sizeof(_settings[0]));
    CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTParagraphStyleAttributeName, paragraphStyle);
    CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, coreTextFont);
    CFRelease(paragraphStyle);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr);
    CFRelease(attrStr);

    CFRange range;
    CGFloat maxWidth  = CGFLOAT_MAX;
    CGFloat maxHeight = 10000.f;
    CGSize constraint = CGSizeMake(maxWidth, maxHeight);

    //  checking frame sizes
    CGSize coreTextSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, len), nil, constraint, &range); 

    // allocate glyphs and bounding box arrays for holding the result
    // assuming that each character is only one glyph, which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(coreTextFont, characters, glyphs, len);

    // get bounding boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont, kCTFontDefaultOrientation, glyphs, bb, len);
    CFRelease(coreTextFont);

    bb[0].origin = l.bounds.origin;
    coreTextSize.width = ceilf(coreTextSize.width);
    coreTextSize.height = ceilf(coreTextSize.height);
    bb[0].size = coreTextSize;


    l.position = CGPointMake(200.f, 100.f);


    // after setting the bounds the layer gets transparent
    l.bounds = bb[0];
    l.opaque = YES;
    return;    

    l.backgroundColor = CGColorCreateGenericRGB(0.f, .5f, .9f, 1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

<>Result> 脚架是正确的。 它对二恶英有问题:Ä没有 do,因为捆绑箱不够。

Using CTLineGetTypographicBounds

-(void) resizeLayer3:(CATextLayer *)l toString:(NSString *)string
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    string = l.string;


    NSUInteger len = [string length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.string, CFRangeMake(0, [l.string length]), characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont, l.fontSize, NULL, NULL);

    CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
    CFAttributedStringReplaceString(attrStr, CFRangeMake(0, 0), (__bridge CFStringRef)string);
    CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, coreTextFont);

    CTLineRef line = CTLineCreateWithAttributedString(attrStr);
    CGFloat ascent;
    CGFloat descent;
    CGFloat width = CTLineGetTypographicBounds(line, &ascent, &descent, NULL);
    CGFloat height = ascent+descent;
    CGSize coreTextSize = CGSizeMake(width,height);


    // get bounding boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CFRelease(coreTextFont);

    bb[0].origin = CGPointZero;
    coreTextSize.width = ceilf(coreTextSize.width);
    coreTextSize.height = ceilf(coreTextSize.height);
    bb[0].size = coreTextSize;


    l.position = CGPointMake(200.f, 100.f);


    // after setting the bounds the layer gets transparent
    l.bounds = bb[0];
    l.opaque = YES;
    return;    

    l.backgroundColor = CGColorCreateGenericRGB(0.f, .5f, .9f, 1.f);

    free(characters);
    free(bb);
}

<>Result> 脚架是正确的。 它对二恶英有问题:Ä没有 do,因为捆绑箱不够。

Using CTFontGetBoundingRectsForGlyphs

-(void) resizeLayer4:(CATextLayer *)l toString:(NSString *)string
{
    // need to set CGFont explicitly to convert font property to a CGFontRef
    CGFontRef layerFont = CGFontCreateWithFontName((CFStringRef)@"Helvetica");
    l.font = layerFont;

    string = l.string;

    NSUInteger len = [string length];

    // get characters from NSString
    UniChar *characters = (UniChar *)malloc(sizeof(UniChar)*len);
    CFStringGetCharacters((__bridge CFStringRef)l.string, CFRangeMake(0, [l.string length]), characters);

    // Get CTFontRef from CGFontRef
    CTFontRef coreTextFont = CTFontCreateWithGraphicsFont(layerFont, l.fontSize, NULL, NULL);

    // allocate glyphs and bounding box arrays for holding the result
    // assuming that each character is only one glyph, which is wrong
    CGGlyph *glyphs = (CGGlyph *)malloc(sizeof(CGGlyph)*len);
    CTFontGetGlyphsForCharacters(coreTextFont, characters, glyphs, len);

    CGPathRef glyphPath = CTFontCreatePathForGlyph(coreTextFont, glyphs[1], NULL);
    CGRect rect = CGPathGetBoundingBox(glyphPath);

    // get bounding boxes for glyphs
    CGRect *bb = (CGRect *)malloc(sizeof(CGRect)*len);
    CTFontGetBoundingRectsForGlyphs(coreTextFont, kCTFontDefaultOrientation, glyphs, bb, len);
    CFRelease(coreTextFont);

    l.position = CGPointMake(200.f, 100.f);

    l.bounds = rect;

    l.backgroundColor = CGColorCreateGenericRGB(0.f, .5f, .9f, 1.f);

    free(characters);
    free(glyphs);
    free(bb);
}

<>Result> 脚架是正确的。 由于二英法系本身是一种精细体,而其性质又是一种精细的含义,即Ä由两种精细体组成。 如何使用?

我是否有任何其他可能性被忽视,值得尝试?

EDIT

第三项选择是。 然而,我正面临一个不同的问题,即《禁止酷刑和其他残忍、不人道或有辱人格的待遇或处罚公约》文本第1行没有其分号。 第一条线不过是不够的。 这将意味着我在这里 bar开错误的树木。

最佳回答

在第一个例子中,你似乎忽视了这样的事实,即约束性服装的开端很可能是一种负面的发源。 退回的重新编号通常将<代码>y=0作为案文的基准处理。 因此,你在捆绑上 rec倒,这很可能也是层层在案文中被抵消的原因。 (didn ttries but consider so)

如果你对特定案文的束缚不感兴趣,但选择了包含各种案文的高度,你也不妨读到<条码>。

问题回答

Try CTLineGetImageBounds。 不过,请注意,这将包括延伸到基线以下的精华部分,例如与下级焦炭有关。

Brief explanation: Rounded shapes must extend beyond straight shapes in order to appear with the same visual weight. See lowercase b or d, for example, which have both a round and a straight shape at the baseline.





相关问题
Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

How to get a flash url in a webpage using a webframe?

As we know , When we load frame from webpage of safari, we will invoke the delegate methods of webkit informal protocol(WebFrameLoadDelegate): webView:didStartProvisionalLoadForFrame: webView:...

热门标签