2010-07-14 09:47:17 +00:00
|
|
|
/*
|
|
|
|
* QuartzTextLayout.h
|
|
|
|
*
|
|
|
|
* Original Code by Evan Jones on Wed Oct 02 2002.
|
|
|
|
* Contributors:
|
|
|
|
* Shane Caraveo, ActiveState
|
|
|
|
* Bernd Paradies, Adobe
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-02-21 04:53:09 +00:00
|
|
|
#ifndef QUARTZTEXTLAYOUT_H
|
|
|
|
#define QUARTZTEXTLAYOUT_H
|
2010-07-14 09:47:17 +00:00
|
|
|
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "QuartzTextStyle.h"
|
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
class QuartzTextLayout {
|
2010-07-14 09:47:17 +00:00
|
|
|
public:
|
2019-05-04 18:14:48 +00:00
|
|
|
/** Create a text layout for drawing. */
|
2022-01-04 23:07:50 +00:00
|
|
|
QuartzTextLayout(std::string_view sv, CFStringEncoding encoding, const QuartzTextStyle *r) {
|
|
|
|
encodingUsed = encoding;
|
2019-05-04 18:14:48 +00:00
|
|
|
const UInt8 *puiBuffer = reinterpret_cast<const UInt8 *>(sv.data());
|
2022-01-04 23:07:50 +00:00
|
|
|
CFStringRef str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encodingUsed, false);
|
2019-05-04 18:14:48 +00:00
|
|
|
if (!str) {
|
|
|
|
// Failed to decode bytes into string with given encoding so try
|
|
|
|
// MacRoman which should accept any byte.
|
2022-01-04 23:07:50 +00:00
|
|
|
encodingUsed = kCFStringEncodingMacRoman;
|
|
|
|
str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encodingUsed, false);
|
2019-05-04 18:14:48 +00:00
|
|
|
}
|
|
|
|
if (!str) {
|
2022-01-04 23:07:50 +00:00
|
|
|
return;
|
2019-05-04 18:14:48 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
stringLength = CFStringGetLength(str);
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2022-01-04 23:07:50 +00:00
|
|
|
CFMutableDictionaryRef stringAttribs = r->getCTStyle();
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
mString = ::CFAttributedStringCreate(NULL, str, stringAttribs);
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
mLine = ::CTLineCreateWithAttributedString(mString);
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
CFRelease(str);
|
2022-01-04 23:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~QuartzTextLayout() {
|
|
|
|
if (mString) {
|
|
|
|
CFRelease(mString);
|
|
|
|
mString = NULL;
|
|
|
|
}
|
|
|
|
if (mLine) {
|
|
|
|
CFRelease(mLine);
|
|
|
|
mLine = NULL;
|
|
|
|
}
|
2019-05-04 18:14:48 +00:00
|
|
|
}
|
2010-07-14 09:47:17 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
/** Draw the text layout into a CGContext at the specified position.
|
|
|
|
* @param gc The CGContext in which to draw the text.
|
|
|
|
* @param x The x axis position to draw the baseline in the current CGContext.
|
|
|
|
* @param y The y axis position to draw the baseline in the current CGContext. */
|
2022-01-04 23:07:50 +00:00
|
|
|
void draw(CGContextRef gc, double x, double y) {
|
2019-05-04 18:14:48 +00:00
|
|
|
if (!mLine)
|
2011-07-17 22:30:49 +00:00
|
|
|
return;
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
::CGContextSetTextMatrix(gc, CGAffineTransformMakeScale(1.0, -1.0));
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
// Set the text drawing position.
|
|
|
|
::CGContextSetTextPosition(gc, x, y);
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2011-07-17 22:30:49 +00:00
|
|
|
// And finally, draw!
|
|
|
|
::CTLineDraw(mLine, gc);
|
2019-05-04 18:14:48 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
float MeasureStringWidth() {
|
2011-07-17 22:30:49 +00:00
|
|
|
if (mLine == NULL)
|
|
|
|
return 0.0f;
|
2015-06-07 21:19:26 +00:00
|
|
|
|
|
|
|
return static_cast<float>(::CTLineGetTypographicBounds(mLine, NULL, NULL, NULL));
|
2011-07-17 22:30:49 +00:00
|
|
|
}
|
2015-06-07 21:19:26 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
CTLineRef getCTLine() {
|
|
|
|
return mLine;
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
CFIndex getStringLength() {
|
|
|
|
return stringLength;
|
|
|
|
}
|
2010-07-14 09:47:17 +00:00
|
|
|
|
2022-01-04 23:07:50 +00:00
|
|
|
CFStringEncoding getEncoding() {
|
|
|
|
return encodingUsed;
|
|
|
|
}
|
|
|
|
|
2010-07-14 09:47:17 +00:00
|
|
|
private:
|
2022-01-04 23:07:50 +00:00
|
|
|
CFAttributedStringRef mString = NULL;
|
|
|
|
CTLineRef mLine = NULL;
|
|
|
|
CFIndex stringLength = 0;
|
|
|
|
CFStringEncoding encodingUsed = kCFStringEncodingMacRoman;
|
2010-07-14 09:47:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|