axmol/cocos/platform/linux/CCDevice-linux.cpp

509 lines
15 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2011 Laschweinski
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
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-01-31 09:10:18 +08:00
2014-09-10 08:41:49 +08:00
#include "platform/CCPlatformConfig.h"
2014-01-31 09:10:18 +08:00
#if CC_TARGET_PLATFORM == CC_PLATFORM_LINUX
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCDevice.h"
#include <X11/Xlib.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <fontconfig/fontconfig.h>
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
#include "ft2build.h"
#include FT_FREETYPE_H
using namespace std;
// as FcFontMatch is quite an expensive call, cache the results of getFontFile
static std::map<std::string, std::string> fontCache;
struct LineBreakGlyph {
FT_UInt glyphIndex;
int paintPosition;
int glyphWidth;
int bearingX;
int kerning;
int horizAdvance;
};
struct LineBreakLine {
LineBreakLine() : lineWidth(0) {}
std::vector<LineBreakGlyph> glyphs;
int lineWidth;
void reset() {
glyphs.clear();
lineWidth = 0;
}
void calculateWidth() {
lineWidth = 0;
if ( glyphs.empty() == false ) {
lineWidth = glyphs.at(glyphs.size() - 1).paintPosition + glyphs.at(glyphs.size() - 1).glyphWidth;
}
}
};
NS_CC_BEGIN
int Device::getDPI()
{
static int dpi = -1;
if (dpi == -1)
{
Display *dpy;
char *displayname = NULL;
int scr = 0; /* Screen number */
dpy = XOpenDisplay (displayname);
/*
* there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
*
* dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
* = N pixels / (M inch / 25.4)
* = N * 25.4 pixels / M inch
*/
double xres = ((((double) DisplayWidth(dpy,scr)) * 25.4) /
((double) DisplayWidthMM(dpy,scr)));
dpi = (int) (xres + 0.5);
//printf("dpi = %d\n", dpi);
2013-11-09 22:02:45 +08:00
XCloseDisplay (dpy);
}
return dpi;
}
void Device::setAccelerometerEnabled(bool isEnabled)
{
}
void Device::setAccelerometerInterval(float interval)
{
}
class BitmapDC
{
public:
BitmapDC() {
libError = FT_Init_FreeType( &library );
FcInit();
_data = NULL;
reset();
}
~BitmapDC() {
FT_Done_FreeType(library);
FcFini();
reset();
}
void reset() {
iMaxLineWidth = 0;
iMaxLineHeight = 0;
textLines.clear();
}
int utf8(char **p)
{
if ((**p & 0x80) == 0x00)
{
int a = *((*p)++);
return a;
}
if ((**p & 0xE0) == 0xC0)
{
int a = *((*p)++) & 0x1F;
int b = *((*p)++) & 0x3F;
return (a << 6) | b;
}
if ((**p & 0xF0) == 0xE0)
{
int a = *((*p)++) & 0x0F;
int b = *((*p)++) & 0x3F;
int c = *((*p)++) & 0x3F;
return (a << 12) | (b << 6) | c;
}
if ((**p & 0xF8) == 0xF0)
{
int a = *((*p)++) & 0x07;
int b = *((*p)++) & 0x3F;
int c = *((*p)++) & 0x3F;
int d = *((*p)++) & 0x3F;
return (a << 18) | (b << 12) | (c << 8) | d;
}
return 0;
}
bool isBreakPoint(FT_UInt currentCharacter, FT_UInt previousCharacter) {
if ( previousCharacter == '-' || previousCharacter == '/' || previousCharacter == '\\' ) {
// we can insert a line break after one of these characters
return true;
}
return false;
}
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight) {
const char* pText = sText;
textLines.clear();
iMaxLineWidth = 0;
FT_UInt unicode;
FT_UInt prevCharacter = 0;
FT_UInt glyphIndex = 0;
FT_UInt prevGlyphIndex = 0;
FT_Vector delta;
LineBreakLine currentLine;
int currentPaintPosition = 0;
int lastBreakIndex = -1;
bool hasKerning = FT_HAS_KERNING( face );
while ((unicode=utf8((char**)&pText))) {
if (unicode == '\n') {
currentLine.calculateWidth();
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
textLines.push_back(currentLine);
currentLine.reset();
prevGlyphIndex = 0;
prevCharacter = 0;
lastBreakIndex = -1;
currentPaintPosition = 0;
continue;
}
if ( isBreakPoint(unicode, prevCharacter) ) {
lastBreakIndex = currentLine.glyphs.size() - 1;
}
glyphIndex = FT_Get_Char_Index(face, unicode);
if (FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT)) {
return false;
}
2014-07-17 18:42:23 +08:00
if (iswspace(unicode)) {
currentPaintPosition += face->glyph->metrics.horiAdvance >> 6;
prevGlyphIndex = glyphIndex;
prevCharacter = unicode;
lastBreakIndex = currentLine.glyphs.size();
continue;
}
LineBreakGlyph glyph;
glyph.glyphIndex = glyphIndex;
glyph.glyphWidth = face->glyph->metrics.width >> 6;
glyph.bearingX = face->glyph->metrics.horiBearingX >> 6;
glyph.horizAdvance = face->glyph->metrics.horiAdvance >> 6;
glyph.kerning = 0;
if (prevGlyphIndex != 0 && hasKerning) {
FT_Get_Kerning(face, prevGlyphIndex, glyphIndex, FT_KERNING_DEFAULT, &delta);
glyph.kerning = delta.x >> 6;
}
if (iMaxWidth > 0 && currentPaintPosition + glyph.bearingX + glyph.kerning + glyph.glyphWidth > iMaxWidth) {
int glyphCount = currentLine.glyphs.size();
if ( lastBreakIndex >= 0 && lastBreakIndex < glyphCount && currentPaintPosition + glyph.bearingX + glyph.kerning + glyph.glyphWidth - currentLine.glyphs.at(lastBreakIndex).paintPosition < iMaxWidth ) {
// we insert a line break at our last break opportunity
std::vector<LineBreakGlyph> tempGlyphs;
std::vector<LineBreakGlyph>::iterator it = currentLine.glyphs.begin();
std::advance(it, lastBreakIndex);
tempGlyphs.insert(tempGlyphs.begin(), it, currentLine.glyphs.end());
currentLine.glyphs.erase(it, currentLine.glyphs.end());
currentLine.calculateWidth();
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
textLines.push_back(currentLine);
currentLine.reset();
currentPaintPosition = 0;
for ( it = tempGlyphs.begin(); it != tempGlyphs.end(); it++ ) {
if ( currentLine.glyphs.empty() ) {
currentPaintPosition = -(*it).bearingX;
(*it).kerning = 0;
}
(*it).paintPosition = currentPaintPosition + (*it).bearingX + (*it).kerning;
currentLine.glyphs.push_back((*it));
currentPaintPosition += (*it).kerning + (*it).horizAdvance;
}
} else {
// the current word is too big to fit into one line, insert line break right here
currentPaintPosition = 0;
glyph.kerning = 0;
currentLine.calculateWidth();
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
textLines.push_back(currentLine);
currentLine.reset();
}
prevGlyphIndex = 0;
prevCharacter = 0;
lastBreakIndex = -1;
} else {
prevGlyphIndex = glyphIndex;
prevCharacter = unicode;
}
if ( currentLine.glyphs.empty() ) {
currentPaintPosition = -glyph.bearingX;
}
glyph.paintPosition = currentPaintPosition + glyph.bearingX + glyph.kerning;
currentLine.glyphs.push_back(glyph);
currentPaintPosition += glyph.kerning + glyph.horizAdvance;
}
if ( currentLine.glyphs.empty() == false ) {
currentLine.calculateWidth();
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
textLines.push_back(currentLine);
}
return true;
}
/**
* compute the start pos of every line
*/
int computeLineStart(FT_Face face, Device::TextAlign eAlignMask, int line) {
int lineWidth = textLines.at(line).lineWidth;
if (eAlignMask == Device::TextAlign::CENTER || eAlignMask == Device::TextAlign::TOP || eAlignMask == Device::TextAlign::BOTTOM) {
return (iMaxLineWidth - lineWidth) / 2;
} else if (eAlignMask == Device::TextAlign::RIGHT || eAlignMask == Device::TextAlign::TOP_RIGHT || eAlignMask == Device::TextAlign::BOTTOM_RIGHT) {
return (iMaxLineWidth - lineWidth);
}
// left or other situation
return 0;
}
int computeLineStartY( FT_Face face, Device::TextAlign eAlignMask, int txtHeight, int borderHeight ){
int baseLinePos = ceilf(FT_MulFix( face->bbox.yMax, face->size->metrics.y_scale )/64.0f);
if (eAlignMask == Device::TextAlign::CENTER || eAlignMask == Device::TextAlign::LEFT || eAlignMask == Device::TextAlign::RIGHT) {
//vertical center
return (borderHeight - txtHeight) / 2 + baseLinePos;
} else if (eAlignMask == Device::TextAlign::BOTTOM_RIGHT || eAlignMask == Device::TextAlign::BOTTOM || eAlignMask == Device::TextAlign::BOTTOM_LEFT) {
//vertical bottom
return borderHeight - txtHeight + baseLinePos;
}
// top alignment
return baseLinePos;
}
std::string getFontFile(const char* family_name) {
std::string fontPath = family_name;
std::map<std::string, std::string>::iterator it = fontCache.find(family_name);
if ( it != fontCache.end() ) {
return it->second;
}
// check if the parameter is a font file shipped with the application
std::string lowerCasePath = fontPath;
std::transform(lowerCasePath.begin(), lowerCasePath.end(), lowerCasePath.begin(), ::tolower);
if ( lowerCasePath.find(".ttf") != std::string::npos ) {
fontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fontPath.c_str());
FILE *f = fopen(fontPath.c_str(), "r");
if ( f ) {
fclose(f);
fontCache.insert(std::pair<std::string, std::string>(family_name, fontPath));
return fontPath;
}
}
// use fontconfig to match the parameter against the fonts installed on the system
FcPattern *pattern = FcPatternBuild (0, FC_FAMILY, FcTypeString, family_name, (char *) 0);
FcConfigSubstitute(0, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
FcResult result;
FcPattern *font = FcFontMatch(0, pattern, &result);
if ( font ) {
FcChar8 *s = NULL;
if ( FcPatternGetString(font, FC_FILE, 0, &s) == FcResultMatch ) {
fontPath = (const char*)s;
FcPatternDestroy(font);
FcPatternDestroy(pattern);
fontCache.insert(std::pair<std::string, std::string>(family_name, fontPath));
return fontPath;
}
FcPatternDestroy(font);
}
FcPatternDestroy(pattern);
return family_name;
}
bool getBitmap(const char *text, int nWidth, int nHeight, Device::TextAlign eAlignMask, const char * pFontName, float fontSize) {
if (libError) {
return false;
}
FT_Face face;
std::string fontfile = getFontFile(pFontName);
if ( FT_New_Face(library, fontfile.c_str(), 0, &face) ) {
//no valid font found use default
if ( FT_New_Face(library, "/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 0, &face) ) {
return false;
}
}
//select utf8 charmap
if ( FT_Select_Charmap(face, FT_ENCODING_UNICODE) ) {
FT_Done_Face(face);
return false;
}
if ( FT_Set_Pixel_Sizes(face, fontSize, fontSize) ) {
FT_Done_Face(face);
return false;
}
if ( divideString(face, text, nWidth, nHeight) == false ) {
FT_Done_Face(face);
return false;
}
//compute the final line width
iMaxLineWidth = MAX(iMaxLineWidth, nWidth);
//compute the final line height
iMaxLineHeight = ceilf(FT_MulFix( face->bbox.yMax - face->bbox.yMin, face->size->metrics.y_scale )/64.0f);
int lineHeight = face->size->metrics.height>>6;
if ( textLines.size() > 0 ) {
iMaxLineHeight += (lineHeight * (textLines.size() -1));
}
int txtHeight = iMaxLineHeight;
iMaxLineHeight = MAX(iMaxLineHeight, nHeight);
2014-02-18 10:11:04 +08:00
_data = (unsigned char*)malloc(sizeof(unsigned char) * (iMaxLineWidth * iMaxLineHeight * 4));
memset(_data,0, iMaxLineWidth * iMaxLineHeight*4);
int iCurYCursor = computeLineStartY(face, eAlignMask, txtHeight, iMaxLineHeight);
int lineCount = textLines.size();
for (int line = 0; line < lineCount; line++) {
int iCurXCursor = computeLineStart(face, eAlignMask, line);
int glyphCount = textLines.at(line).glyphs.size();
for (int i = 0; i < glyphCount; i++) {
LineBreakGlyph glyph = textLines.at(line).glyphs.at(i);
if (FT_Load_Glyph(face, glyph.glyphIndex, FT_LOAD_RENDER)) {
continue;
}
FT_Bitmap& bitmap = face->glyph->bitmap;
int yoffset = iCurYCursor - (face->glyph->metrics.horiBearingY >> 6);
int xoffset = iCurXCursor + glyph.paintPosition;
for (int y = 0; y < bitmap.rows; ++y) {
int iY = yoffset + y;
if (iY>=iMaxLineHeight) {
//exceed the height truncate
break;
}
iY *= iMaxLineWidth;
int bitmap_y = y * bitmap.width;
for (int x = 0; x < bitmap.width; ++x) {
unsigned char cTemp = bitmap.buffer[bitmap_y + x];
if (cTemp == 0) {
continue;
}
int iX = xoffset + x;
int iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
*(int*) &_data[(iY + iX) * 4 + 0] = iTemp;
}
}
}
// step to next line
iCurYCursor += lineHeight;
}
// free face
FT_Done_Face(face);
return true;
}
public:
FT_Library library;
unsigned char *_data;
int libError;
std::vector<LineBreakLine> textLines;
int iMaxLineWidth;
int iMaxLineHeight;
};
static BitmapDC& sharedBitmapDC()
{
static BitmapDC s_BmpDC;
return s_BmpDC;
}
Data Device::getTextureDataForText(const char * text, const FontDefinition& textDefinition, TextAlign align, int &width, int &height, bool& hasPremultipliedAlpha)
{
Data ret;
do
{
BitmapDC &dc = sharedBitmapDC();
CC_BREAK_IF(! dc.getBitmap(text, textDefinition._dimensions.width, textDefinition._dimensions.height, align, textDefinition._fontName.c_str(), textDefinition._fontSize));
CC_BREAK_IF(! dc._data);
width = dc.iMaxLineWidth;
height = dc.iMaxLineHeight;
dc.reset();
ret.fastSet(dc._data,width * height * 4);
hasPremultipliedAlpha = true;
} while (0);
return ret;
}
2014-08-18 20:41:12 +08:00
void Device::setKeepScreenOn(bool value)
{
}
NS_CC_END
2014-01-31 09:10:18 +08:00
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_LINUX