• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to obtain paths of laid-out text [SOLVED]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to obtain paths of laid-out text [SOLVED]


  • Subject: Re: How to obtain paths of laid-out text [SOLVED]
  • From: Graham Cox <email@hidden>
  • Date: Wed, 12 Nov 2014 17:41:14 +1100

I think I have a good solution. Code below. Turning off discontiguous layout didn't help, and I determined that the method was not being called multiple times for the same glyphs (it is called multiple times for different lines).

Essentially, "packed" glyphs (whatever they are) appear to be a relic of the past (and possibly in10.10 they are buggy and unmaintained), and the [NSBezierPath appendPackedGlyphs:], while not deprecated, is "not recommended". So using the new override method, it's easy to add individual glyphs which suits my code nicely anyway, because I also frequently need to get an array of paths for each glyph, and previously this was very long-winded. With the new override, it's trivially easy. Also, just casting CGGlyph to NSGlyph with this approach seems to work fine (though I would like to have some official confirmation of that).

Forgive the manual memory management - this is an update of an old class and I haven't moved it to ARC yet.

--Graham




- (void)	showCGGlyphs:(const CGGlyph*) glyphs
			positions:(const NSPoint*) positions
				count:(NSUInteger) glyphCount
				 font:(NSFont*) font
			   matrix:(NSAffineTransform*) textMatrix
		   attributes:(NSDictionary*) attributes
			inContext:(NSGraphicsContext*) graphicsContext
{
	NSMutableArray*		paths = [[NSMutableArray alloc] initWithCapacity:glyphCount];

	NSBezierPath*		temp;
	NSAffineTransform*	tfm;
	NSUInteger			n;

	for( n = 0; n < glyphCount; ++n )
	{
		temp = [[NSBezierPath alloc] init];

		[temp moveToPoint:positions[n]];
		[temp appendBezierPathWithGlyph:(NSGlyph)glyphs[n] inFont:font];

		// flip the glyph vertically around its location point

		tfm = [[NSAffineTransform alloc] init];

		[tfm translateXBy:0 yBy:positions[n].y];
		[tfm scaleXBy:1 yBy:-1];
		[tfm translateXBy:0 yBy:-positions[n].y];

		[temp transformUsingAffineTransform:tfm];

		[tfm release];

		// keep the paths as a list of individual glyphs

		[paths addObject:temp];

		// accumulate the glyph paths into a single path.

		[mPath appendBezierPath:temp];

		[temp release];
	}

	if( mPathsArray == nil )
		mPathsArray = paths;
	else
	{
		[mPathsArray addObjectsFromArray:paths];
		[paths release];
	}
}



> On 12 Nov 2014, at 8:38 am, Graham Cox <email@hidden> wrote:
>
>
>> On 12 Nov 2014, at 3:27 am, Kyle Sluder <email@hidden> wrote:
>>
>> On Fri, Nov 7, 2014, at 04:40 PM, Graham Cox wrote:
>>> In the past, I've obtained bezier paths from laid out text using a
>>> subclass of NSLayoutManager that overrode the deprecated
>>> -showPackedGlyphs:length:... method. This method had a nice smooth
>>> impedance match with [NSBezierPath appendPackedGlyphs:]. The method has
>>> been deprecated since 10.7, but was still working fine in 10.9. In 10.10,
>>> this approach broke. The method is still called, but the resulting glyph
>>> runs include all sorts of extraneous characters which seem to be relics
>>> of earlier layouts (possibly NSLayoutManager does more aggressive caching
>>> in 10.10).
>>
>> What are you actually trying to do?
>
> Get the bezier path(s) of some laid-out text. From this I do further operations on the paths to create special text effects.
>
>> That is, why and to what degree does
>> it matter when and how often -showPackedGlyphs: is called?
>
> That's a good question. I'm not sure. The way I was capturing the paths was to use this method to copy the glyph paths to a bezier path using -appendPackedGlyphs: I did assume the method was being called only once for a given piece of text, though if it is called more than once, I'm not sure of the consequences for the bezier path - would characters be appended to the end of the run (i.e. is there some sort of internal advancement for each appended glyph?) or would it just add a path on top of an existing one? The documentation for -appendPackedGlyphs isn't very enlightening.
>
> I'll look into this, it could be the issue.
>
>> Also, have you tried disabling discontiguous layout?
>
> No, I'll try that as well.
>
> But some basic questions remain:
>
> 1. What is the relationship between CGGlyph and NSGlyph?
>
> 2. Given that the old -showPackedGlyphs: method is deprecated, and its replacement -showCGGlyphs: doesn't seem to have a natural match to a NSBezierPath method, what is the correct general approach when using this newer method to capture paths?
>
> --Graham
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden


References: 
 >How to obtain paths of laid-out text (From: Graham Cox <email@hidden>)
 >Re: How to obtain paths of laid-out text (From: Kyle Sluder <email@hidden>)
 >Re: How to obtain paths of laid-out text (From: Graham Cox <email@hidden>)

  • Prev by Date: What Apple event functionality is still missing from Cocoa?
  • Next by Date: Re: updating NSTextField before spinner animates
  • Previous by thread: Re: How to obtain paths of laid-out text
  • Next by thread: NSSplitView divider position
  • Index(es):
    • Date
    • Thread