Re: boundingRectWithSize gives wrong size

2019-09-11 Thread Gabriel Zachmann via Cocoa-dev
> 

I must confess that I made the mistake of calculating the size of the text 
using the attributed string,
but I did *not* assign the attributed string to the text layer, but instead I 
assigned the orig. plain string.
That way, the text layer must have used a different font for rendering the text,
different from the font that CTFramesetterSuggestFrameSizeWithConstraints() used
(as specified in the attributed string).
Argh.


So, for the record, in case other people might have a similar problem some time 
in the future, too,
I am enclosing my current version of the code below.


And thanks again! Your suggestions kept me experimenting.
Best regards, Gabriel


Encl.:

NSFont * font = [NSFont fontWithName: @"Andale Mono" size: fontSize_ ];
if ( ! font )
[self logMessage: @"Could not find font Andale Mono!" asError: YES];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] 
init];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSColor *color = [NSColor whiteColor];
NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  font, NSFontAttributeName,
  color, NSForegroundColorAttributeName,
  paragraphStyle, 
NSParagraphStyleAttributeName,
  nil];
NSAttributedString * mesg_attrstring = [[NSAttributedString alloc] 
initWithString: mesg
   
attributes: attrsDictionary];

textLayer_.string = mesg_attrstring;
textLayer_.fontSize = fontSize_;// not 
sure this is actually needed

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString( 
(__bridge CFAttributedStringRef) mesg_attrstring );
CFRange mesg_string_range;
CGSize string_rect = CTFramesetterSuggestFrameSizeWithConstraints( 
framesetter, CFRangeMake(0,0), NULL, drawRect_.size, & mesg_string_range );

textLayer_.bounds = CGRectMake( 0.0, 0.0, string_rect.width, 
string_rect.height );
textLayer_.shadowPath = CGPathCreateWithRect( textLayer_.bounds, NULL );
  // set drop shadow

[textLayer_ setNeedsDisplay];




smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: boundingRectWithSize gives wrong size

2019-09-10 Thread Richard Charles via Cocoa-dev
> On Sep 10, 2019, at 10:54 AM, Gabriel Zachmann  wrote:
> 
>> I have had good results with CTLineGetTypographicBounds().
> 
> This seems to be suitable only for single lines of text.
> But , usually, I've got 2 lines of text.
> 
> I looked at Core Text a bit further, but I could not find how to determine 
> the width/height of a two-line string.

For multiple lines you need to use a framesetter.

CTFramesetterCreateWithAttributedString()

CTFramesetterSuggestFrameSizeWithConstraints()

--Richard Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: boundingRectWithSize gives wrong size

2019-09-10 Thread Jens Alfke via Cocoa-dev


> On Sep 10, 2019, at 9:54 AM, Gabriel Zachmann  wrote:
> 
> The excess width is sometimes more, sometimes a bit less, I could not find a 
> pattern.
> I guess , it has something to do with the actual glyphs in the string.

How much bigger is it? By a fraction of a character width? Or is it more than 
that? The right edge of the rectangle is probably not based on the actual glyph 
outline, but on the character's advance width. These can be quite different, 
especially in italics or if there are swashes.

> In my case, mesg_string is (usually) two lines of text (one string with \n 
> somewhere in the middle), 

Why not measure the two lines separately, since you said single-line text works 
correctly?

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: boundingRectWithSize gives wrong size

2019-09-10 Thread Gabriel Zachmann via Cocoa-dev
Thanks a lot to both of you for responding, and sorry for my delay!
(In case you forget the context, I have enclosed my original question below.)


>>   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize: 
>> drawRect_.size

> What is drawRect_ set to when this is called?

Here is one case:
drawRect bounds = 0.00 , 0.00 , 2560.00 , 1440.00

After the call:
string rect bounds = 0.00 , 0.00 , 721.00 , 44.00

Actually, the bounds of drawRect_ are always the same (i.e., the size of the 
screen).

> With NSStringDrawingUsesLineFragmentOrigin, if the text is broken into lines, 
> it's not going to tell you the exact pixel width of the widest line.

I thought  CGRectIntegral() will take care of that, doesn't it?

> 
> In single-line mode (without NSStringDrawingUsesLineFragmentOrigin) I believe 
> you do get the exact dimensions of the single line of text.

In my case, mesg_string is (usually) two lines of text (one string with \n 
somewhere in the middle), 
but I tried it without NSStringDrawingUsesLineFragmentOrigin, to no avail.

>> 
>> I am trying to determine the size of a piece of text using 
>> boundingRectWithSize.
>> 
>> The problem is that this method does not return the correct width. (The 
>> height seems to be about right.) Sometimes, the width is only a little bit 
>> too wide, sometimes it is much too wide.
> 
> I have had good results with CTLineGetTypographicBounds().

This seems to be suitable only for single lines of text.
But , usually, I've got 2 lines of text.

I looked at Core Text a bit further, but I could not find how to determine the 
width/height of a two-line string.



I have experimented a bit further, and tried this code:

NSFont * font = [NSFont fontWithName: @"Andale Mono" size: 
textLayer_.fontSize ];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] 
init]; 
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  font, NSFontAttributeName,
  paragraphStyle, 
NSParagraphStyleAttributeName, nil];
const CGSize textSize = [mesg sizeWithAttributes: attrsDictionary]; 
 // mesg is a simple string, not attributed string
textLayer_.bounds = NSMakeRect( 0, 0, textSize.width, textSize.height );
textLayer_.shadowPath = CGPathCreateWithRect( textLayer_.bounds, NULL );
  // set drop shadow

Again, the box of the shadow is too big, relative to the width of the text.
(But the height is OK.)


The excess width is sometimes more, sometimes a bit less, I could not find a 
pattern.
I guess , it has something to do with the actual glyphs in the string.



Best, G.


PS:
Here is my original question, just for reference


> 
> I am trying to determine the size of a piece of text using 
> boundingRectWithSize.
> 
> The problem is that this method does not return the correct width.
> (The height seems to be about right.)
> Sometimes, the width is only a little bit too wide, sometimes it is much too 
> wide.
> And I could not find a pattern, it is not just a linear factor.
> 
> Below is the code snippet that I use.
> 
> I already did considerable googling but could not find a solution for my 
> problem.
> (Most posts say that I needed to use the correct options, so I included them, 
> to no avail.)
> 
> I would appreciate very much all kinds of pointers or suggestions.
> 
> Best regards, Gabriel
> 
> 
> 
>   NSMutableString * mesg = NULL;
> ...
>   textLayer_.string = mesg;
>   textLayer_.fontSize = fontSize_;
> 
>   NSStringDrawingContext * ctxt = [NSStringDrawingContext new];
>   ctxt.minimumScaleFactor = 1.0;
>   NSFont * font = [NSFont fontWithName: @"Andale Mono" size: 
> textLayer_.fontSize ];
>   NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] 
> init];
> 
>   paragraphStyle.lineBreakMode = NSLineBreakByClipping;
>   NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
> font, NSFontAttributeName,
> paragraphStyle, 
> NSParagraphStyleAttributeName, nil];
>   NSAttributedString * mesg_string = [[NSAttributedString alloc] 
> initWithString: mesg
>  
> attributes: attrsDictionary];
> 
>   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize: 
> drawRect_.size
>   options: 
> NSStringDrawingUsesLineFragmentOrigin |
>// 
> NSStringDrawingUsesDeviceMetrics |   // tried with and w/o
>
>   
> NSStringDrawingUsesFontLeading
>   context: ctxt ] );
> 
>   

Re: boundingRectWithSize gives wrong size

2019-08-28 Thread Jens Alfke via Cocoa-dev


> On Aug 28, 2019, at 12:33 PM, Gabriel Zachmann via Cocoa-dev 
>  wrote:
> 
>   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize: 
> drawRect_.size

What is drawRect_ set to when this is called?

It's been a while since I used this, but IIRC the output width will be the same 
as the input, with the height increased to account for the number of lines the 
string is broken into.

With NSStringDrawingUsesLineFragmentOrigin, if the text is broken into lines, 
it's not going to tell you the exact pixel width of the widest line.

In single-line mode (without NSStringDrawingUsesLineFragmentOrigin) I believe 
you do get the exact dimensions of the single line of text.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: boundingRectWithSize gives wrong size

2019-08-28 Thread Richard Charles via Cocoa-dev


> On Aug 28, 2019, at 1:33 PM, Gabriel Zachmann via Cocoa-dev 
>  wrote:
> 
> I am trying to determine the size of a piece of text using 
> boundingRectWithSize.
> 
> The problem is that this method does not return the correct width. (The 
> height seems to be about right.) Sometimes, the width is only a little bit 
> too wide, sometimes it is much too wide.

I have had good results with CTLineGetTypographicBounds().

--Richard Charles

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com