2012-07-21 12:23:40 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2012-07-21 12:23:40 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCDevice.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
#include <Foundation/Foundation.h>
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#include <string>
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccTypes.h"
|
2016-06-15 10:43:15 +08:00
|
|
|
#include "platform/apple/CCDevice-apple.h"
|
2012-07-21 12:23:40 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2013-07-18 13:40:32 +08:00
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
static NSAttributedString* __attributedStringWithFontSize(NSMutableAttributedString* attributedString, CGFloat fontSize)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[attributedString beginEditing];
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
[attributedString enumerateAttribute:NSFontAttributeName
|
|
|
|
inRange:NSMakeRange(0, attributedString.length)
|
|
|
|
options:0
|
|
|
|
usingBlock:^(id value, NSRange range, BOOL* stop) {
|
|
|
|
NSFont* font = value;
|
|
|
|
font = [[NSFontManager sharedFontManager] convertFont:font toSize:fontSize];
|
|
|
|
|
|
|
|
[attributedString removeAttribute:NSFontAttributeName range:range];
|
|
|
|
[attributedString addAttribute:NSFontAttributeName value:font range:range];
|
|
|
|
}];
|
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
[attributedString endEditing];
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
return [[attributedString copy] autorelease];
|
|
|
|
}
|
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
int Device::getDPI()
|
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
NSScreen* screen = [NSScreen mainScreen];
|
|
|
|
NSDictionary* description = [screen deviceDescription];
|
|
|
|
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
|
2015-08-04 11:07:04 +08:00
|
|
|
CGSize displayPhysicalSize = CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
return ((displayPixelSize.width / displayPhysicalSize.width) * 25.4f);
|
2014-02-13 21:08:43 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled) {}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
void Device::setAccelerometerInterval(float interval) {}
|
2014-02-13 21:08:43 +08:00
|
|
|
|
2012-07-21 12:23:40 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2013-12-06 16:32:06 +08:00
|
|
|
int height;
|
|
|
|
int width;
|
2015-08-14 21:17:51 +08:00
|
|
|
bool hasAlpha;
|
|
|
|
bool isPremultipliedAlpha;
|
|
|
|
unsigned char* data;
|
2012-07-21 12:23:40 +08:00
|
|
|
} tImageInfo;
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
static NSSize _calculateStringSize(NSAttributedString* str,
|
|
|
|
id font,
|
|
|
|
CGSize* constrainSize,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow)
|
2015-12-25 11:44:11 +08:00
|
|
|
{
|
2015-12-25 17:18:15 +08:00
|
|
|
NSSize textRect = NSZeroSize;
|
2021-12-28 11:00:34 +08:00
|
|
|
textRect.width = constrainSize->width > 0 ? constrainSize->width : CGFLOAT_MAX;
|
|
|
|
textRect.height = constrainSize->height > 0 ? constrainSize->height : CGFLOAT_MAX;
|
|
|
|
|
|
|
|
if (overflow == 1)
|
|
|
|
{
|
|
|
|
if (!enableWrap)
|
|
|
|
{
|
|
|
|
textRect.width = CGFLOAT_MAX;
|
2016-06-15 10:43:15 +08:00
|
|
|
textRect.height = CGFLOAT_MAX;
|
2021-12-28 11:00:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
textRect.height = CGFLOAT_MAX;
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2015-12-25 17:18:15 +08:00
|
|
|
NSSize dim;
|
2015-12-29 18:31:20 +08:00
|
|
|
#ifdef __MAC_10_11
|
2021-12-28 11:00:34 +08:00
|
|
|
# if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
|
|
|
|
dim = [str boundingRectWithSize:textRect
|
|
|
|
options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin)context:nil]
|
|
|
|
.size;
|
|
|
|
# else
|
|
|
|
dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin)]
|
|
|
|
.size;
|
|
|
|
# endif
|
2015-12-29 18:31:20 +08:00
|
|
|
#else
|
2021-12-28 11:00:34 +08:00
|
|
|
dim = [str boundingRectWithSize:textRect options:(NSStringDrawingOptions)(NSStringDrawingUsesLineFragmentOrigin)]
|
|
|
|
.size;
|
2015-12-29 18:31:20 +08:00
|
|
|
#endif
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
dim.width = ceilf(dim.width);
|
2015-12-25 11:44:11 +08:00
|
|
|
dim.height = ceilf(dim.height);
|
2015-12-25 17:18:15 +08:00
|
|
|
|
2015-12-25 11:44:11 +08:00
|
|
|
return dim;
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
static NSSize _calculateRealSizeForString(NSAttributedString** str, id font, NSSize constrainSize, bool enableWrap)
|
2016-06-15 10:43:15 +08:00
|
|
|
{
|
|
|
|
CGRect actualSize = CGRectMake(0, 0, constrainSize.width + 1, constrainSize.height + 1);
|
2021-12-28 11:00:34 +08:00
|
|
|
int fontSize = [font pointSize];
|
|
|
|
fontSize = fontSize + 1;
|
2016-06-15 10:43:15 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
if (!enableWrap)
|
|
|
|
{
|
|
|
|
while (actualSize.size.width > constrainSize.width || actualSize.size.height > constrainSize.height)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
fontSize = fontSize - 1;
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fontSize < 0)
|
|
|
|
{
|
2016-08-29 15:23:01 +08:00
|
|
|
actualSize = CGRectMake(0, 0, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
NSMutableAttributedString* mutableString = [[*str mutableCopy] autorelease];
|
|
|
|
*str = __attributedStringWithFontSize(mutableString, fontSize);
|
2016-06-15 10:43:15 +08:00
|
|
|
|
2016-07-27 22:44:00 +08:00
|
|
|
#ifdef __MAC_10_11
|
2021-12-28 11:00:34 +08:00
|
|
|
# if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
|
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin
|
|
|
|
context:nil]
|
|
|
|
.size;
|
|
|
|
# else
|
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin]
|
|
|
|
.size;
|
|
|
|
# endif
|
2016-07-27 22:44:00 +08:00
|
|
|
#else
|
2021-12-28 11:00:34 +08:00
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin]
|
|
|
|
.size;
|
2016-07-27 22:44:00 +08:00
|
|
|
#endif
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fitSize.width == 0 || fitSize.height == 0)
|
|
|
|
continue;
|
2016-06-15 10:43:15 +08:00
|
|
|
actualSize.size = fitSize;
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
if (constrainSize.width <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
constrainSize.width = fitSize.width;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
if (constrainSize.height <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
constrainSize.height = fitSize.height;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fontSize <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (actualSize.size.height > constrainSize.height || actualSize.size.width > constrainSize.width)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
fontSize = fontSize - 1;
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fontSize < 0)
|
|
|
|
{
|
2016-08-29 15:23:01 +08:00
|
|
|
actualSize = CGRectMake(0, 0, 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
NSMutableAttributedString* mutableString = [[*str mutableCopy] autorelease];
|
|
|
|
*str = __attributedStringWithFontSize(mutableString, fontSize);
|
2016-07-27 22:44:00 +08:00
|
|
|
|
|
|
|
#ifdef __MAC_10_11
|
2021-12-28 11:00:34 +08:00
|
|
|
# if __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_11
|
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin
|
|
|
|
context:nil]
|
|
|
|
.size;
|
|
|
|
# else
|
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin]
|
|
|
|
.size;
|
|
|
|
# endif
|
2016-07-27 22:44:00 +08:00
|
|
|
#else
|
2021-12-28 11:00:34 +08:00
|
|
|
CGSize fitSize = [*str boundingRectWithSize:CGSizeMake(constrainSize.width, CGFLOAT_MAX)
|
|
|
|
options:NSStringDrawingUsesLineFragmentOrigin]
|
|
|
|
.size;
|
2016-07-27 22:44:00 +08:00
|
|
|
#endif
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
if (fitSize.width == 0 || fitSize.height == 0)
|
|
|
|
continue;
|
2016-06-15 10:43:15 +08:00
|
|
|
actualSize.size = fitSize;
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
if (constrainSize.width <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
constrainSize.width = fitSize.width;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
if (constrainSize.height <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
constrainSize.height = fitSize.height;
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fontSize <= 0)
|
|
|
|
{
|
2016-06-15 10:43:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CGSizeMake(actualSize.size.width, actualSize.size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSFont* _createSystemFont(const char* fontName, int size)
|
|
|
|
{
|
2021-12-28 11:00:34 +08:00
|
|
|
NSString* fntName = [NSString stringWithUTF8String:fontName];
|
|
|
|
fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
|
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
// font
|
2021-12-28 11:00:34 +08:00
|
|
|
NSFont* font = [NSFont fontWithName:fntName size:size];
|
|
|
|
|
|
|
|
if (font == nil)
|
|
|
|
{
|
2016-10-08 14:34:55 +08:00
|
|
|
font = [NSFont systemFontOfSize:size];
|
2016-06-15 10:43:15 +08:00
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
static CGFloat _calculateTextDrawStartHeight(ax::Device::TextAlign align, CGSize realDimensions, CGSize dimensions)
|
2016-06-15 10:43:15 +08:00
|
|
|
{
|
|
|
|
float startH = 0;
|
|
|
|
// vertical alignment
|
|
|
|
unsigned int vAlignment = ((int)align >> 4) & 0x0F;
|
2021-12-28 11:00:34 +08:00
|
|
|
switch (vAlignment)
|
|
|
|
{
|
|
|
|
// bottom
|
|
|
|
case 1:
|
|
|
|
startH = dimensions.height - realDimensions.height;
|
|
|
|
break;
|
|
|
|
// top
|
|
|
|
case 2:
|
|
|
|
startH = 0;
|
|
|
|
break;
|
|
|
|
// center
|
|
|
|
case 3:
|
|
|
|
startH = (dimensions.height - realDimensions.height) / 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2016-06-15 10:43:15 +08:00
|
|
|
}
|
|
|
|
return startH;
|
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
static bool _initWithString(const char* text,
|
|
|
|
Device::TextAlign align,
|
|
|
|
const char* fontName,
|
|
|
|
int size,
|
|
|
|
tImageInfo* info,
|
|
|
|
const Color3B* fontColor,
|
|
|
|
int fontAlpha,
|
|
|
|
bool enableWrap,
|
|
|
|
int overflow,
|
|
|
|
const FontStroke& stroke)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(text, "Invalid text");
|
|
|
|
AXASSERT(info, "Invalid info");
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
NSString* string = [NSString stringWithUTF8String:text];
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(!string);
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
id font = _createSystemFont(fontName, size);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(!font);
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
// color
|
|
|
|
NSColor* foregroundColor;
|
2021-12-28 11:00:34 +08:00
|
|
|
if (fontColor)
|
|
|
|
{
|
|
|
|
foregroundColor = [NSColor colorWithDeviceRed:fontColor->r / 255.0
|
|
|
|
green:fontColor->g / 255.0
|
|
|
|
blue:fontColor->b / 255.0
|
|
|
|
alpha:fontAlpha / 255.0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
foregroundColor = [NSColor whiteColor];
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2015-12-25 11:44:11 +08:00
|
|
|
// alignment
|
2021-12-28 11:00:34 +08:00
|
|
|
NSTextAlignment textAlign = FontUtils::_calculateTextAlignment(align);
|
|
|
|
NSMutableParagraphStyle* paragraphStyle = FontUtils::_calculateParagraphStyle(enableWrap, overflow);
|
2015-08-14 21:17:51 +08:00
|
|
|
[paragraphStyle setAlignment:textAlign];
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
// attribute
|
2021-12-28 11:00:34 +08:00
|
|
|
NSDictionary* tokenAttributesDict = [NSDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:foregroundColor, NSForegroundColorAttributeName, font, NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, nil];
|
|
|
|
NSAttributedString* stringWithAttributes =
|
|
|
|
[[[NSAttributedString alloc] initWithString:string attributes:tokenAttributesDict] autorelease];
|
|
|
|
|
2015-12-25 11:44:11 +08:00
|
|
|
CGSize dimensions = CGSizeMake(info->width, info->height);
|
2016-06-15 10:43:15 +08:00
|
|
|
|
|
|
|
NSSize realDimensions;
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2019-09-17 13:35:19 +08:00
|
|
|
if (overflow == 2)
|
2016-06-15 10:43:15 +08:00
|
|
|
realDimensions = _calculateRealSizeForString(&stringWithAttributes, font, dimensions, enableWrap);
|
2019-09-17 13:35:19 +08:00
|
|
|
else
|
2021-12-28 11:00:34 +08:00
|
|
|
realDimensions = _calculateStringSize(stringWithAttributes, font, &dimensions, enableWrap, overflow);
|
2016-06-15 10:43:15 +08:00
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
// Mac crashes if the width or height is 0
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(realDimensions.width <= 0 || realDimensions.height <= 0);
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
if (dimensions.width <= 0.f)
|
2015-07-29 16:11:27 +08:00
|
|
|
dimensions.width = realDimensions.width;
|
2019-09-17 13:35:19 +08:00
|
|
|
if (dimensions.height <= 0.f)
|
2021-12-28 11:00:34 +08:00
|
|
|
dimensions.height = realDimensions.height;
|
|
|
|
|
|
|
|
// Alignment
|
2016-06-15 10:43:15 +08:00
|
|
|
CGFloat xPadding = FontUtils::_calculateTextDrawStartWidth(align, realDimensions, dimensions);
|
2019-09-17 13:35:19 +08:00
|
|
|
|
2016-06-15 10:43:15 +08:00
|
|
|
CGFloat yPadding = _calculateTextDrawStartHeight(align, realDimensions, dimensions);
|
2019-09-17 13:35:19 +08:00
|
|
|
|
2015-12-25 11:44:11 +08:00
|
|
|
NSInteger POTWide = dimensions.width;
|
|
|
|
NSInteger POTHigh = dimensions.height;
|
2021-12-28 11:00:34 +08:00
|
|
|
NSRect textRect =
|
|
|
|
NSMakeRect(xPadding, POTHigh - dimensions.height + yPadding, realDimensions.width, realDimensions.height);
|
|
|
|
|
|
|
|
NSBitmapImageRep* offscreenRep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
|
|
|
pixelsWide:POTWide
|
|
|
|
pixelsHigh:POTHigh
|
|
|
|
bitsPerSample:8
|
|
|
|
samplesPerPixel:4
|
|
|
|
hasAlpha:YES
|
|
|
|
isPlanar:NO
|
|
|
|
colorSpaceName:NSDeviceRGBColorSpace
|
|
|
|
bitmapFormat:0
|
|
|
|
bytesPerRow:4 * POTWide
|
|
|
|
bitsPerPixel:32] autorelease];
|
2019-09-17 13:35:19 +08:00
|
|
|
|
|
|
|
NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
|
|
|
|
[NSGraphicsContext saveGraphicsState];
|
|
|
|
[NSGraphicsContext setCurrentContext:g];
|
2021-12-28 11:00:34 +08:00
|
|
|
|
|
|
|
if (stroke._strokeSize > 0)
|
|
|
|
{
|
|
|
|
NSColor* strokeColor = [NSColor colorWithDeviceRed:stroke._strokeColor.r / 255.0
|
|
|
|
green:stroke._strokeColor.g / 255.0
|
|
|
|
blue:stroke._strokeColor.b / 255.0
|
|
|
|
alpha:stroke._strokeAlpha / 255.0];
|
|
|
|
NSNumber* strokeSize = [NSNumber numberWithFloat:stroke._strokeSize / size * 100.0];
|
|
|
|
NSDictionary* tokenAttributesDict2 = [NSDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:foregroundColor, NSForegroundColorAttributeName, font, NSFontAttributeName,
|
|
|
|
paragraphStyle, NSParagraphStyleAttributeName, strokeSize,
|
|
|
|
NSStrokeWidthAttributeName, strokeColor, NSStrokeColorAttributeName, nil];
|
|
|
|
NSAttributedString* strokeString =
|
|
|
|
[[[NSAttributedString alloc] initWithString:string attributes:tokenAttributesDict2] autorelease];
|
2020-09-25 13:13:54 +08:00
|
|
|
[strokeString drawInRect:textRect];
|
|
|
|
}
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2015-08-14 21:17:51 +08:00
|
|
|
[stringWithAttributes drawInRect:textRect];
|
2019-09-17 13:35:19 +08:00
|
|
|
[NSGraphicsContext restoreGraphicsState];
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
auto data = (unsigned char*)[offscreenRep bitmapData]; // Use the same buffer to improve the performance.
|
2019-09-17 13:35:19 +08:00
|
|
|
|
2015-12-25 11:44:11 +08:00
|
|
|
NSUInteger textureSize = POTWide * POTHigh * 4;
|
2021-12-28 11:00:34 +08:00
|
|
|
auto dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize);
|
|
|
|
if (dataNew)
|
|
|
|
{
|
2015-08-14 21:17:51 +08:00
|
|
|
memcpy(dataNew, data, textureSize);
|
|
|
|
// output params
|
2021-12-28 11:00:34 +08:00
|
|
|
info->width = static_cast<int>(POTWide);
|
|
|
|
info->height = static_cast<int>(POTHigh);
|
|
|
|
info->data = dataNew;
|
|
|
|
info->hasAlpha = true;
|
2015-08-14 21:17:51 +08:00
|
|
|
info->isPremultipliedAlpha = true;
|
2021-12-28 11:00:34 +08:00
|
|
|
ret = true;
|
2015-08-14 21:17:51 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
Data Device::getTextureDataForText(const char* text,
|
|
|
|
const FontDefinition& textDefinition,
|
|
|
|
TextAlign align,
|
|
|
|
int& width,
|
|
|
|
int& height,
|
|
|
|
bool& hasPremultipliedAlpha)
|
2012-07-21 12:23:40 +08:00
|
|
|
{
|
2014-02-13 21:08:43 +08:00
|
|
|
Data ret;
|
2021-12-28 11:00:34 +08:00
|
|
|
do
|
|
|
|
{
|
2014-02-13 21:08:43 +08:00
|
|
|
tImageInfo info = {0};
|
2021-12-28 11:00:34 +08:00
|
|
|
info.width = textDefinition._dimensions.width;
|
|
|
|
info.height = textDefinition._dimensions.height;
|
2020-09-25 13:13:54 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
if (!_initWithString(text, align, textDefinition._fontName.c_str(), textDefinition._fontSize, &info,
|
|
|
|
&textDefinition._fontFillColor, textDefinition._fontAlpha, textDefinition._enableWrap,
|
|
|
|
textDefinition._overflow, textDefinition._stroke))
|
2014-02-13 21:08:43 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
height = (short)info.height;
|
2021-12-28 11:00:34 +08:00
|
|
|
width = (short)info.width;
|
|
|
|
ret.fastSet(info.data, width * height * 4);
|
2014-05-06 11:22:23 +08:00
|
|
|
hasPremultipliedAlpha = true;
|
2014-02-13 21:08:43 +08:00
|
|
|
} while (0);
|
2021-12-28 11:00:34 +08:00
|
|
|
|
2014-02-13 21:08:43 +08:00
|
|
|
return ret;
|
2012-07-21 12:23:40 +08:00
|
|
|
}
|
2014-08-18 20:41:12 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
void Device::setKeepScreenOn(bool value) {}
|
2015-06-25 17:36:57 +08:00
|
|
|
|
2021-12-28 11:00:34 +08:00
|
|
|
void Device::vibrate(float duration) {}
|
2014-08-18 20:41:12 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|