2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 Laschweinski
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "platform/CCDevice.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fontconfig/fontconfig.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;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct LineBreakGlyph
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_UInt glyphIndex;
|
|
|
|
int paintPosition;
|
|
|
|
int glyphWidth;
|
|
|
|
|
|
|
|
int bearingX;
|
|
|
|
int kerning;
|
|
|
|
int horizAdvance;
|
|
|
|
};
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct LineBreakLine
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LineBreakLine() : lineWidth(0) {}
|
|
|
|
|
|
|
|
std::vector<LineBreakGlyph> glyphs;
|
|
|
|
int lineWidth;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void reset()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
glyphs.clear();
|
|
|
|
lineWidth = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void calculateWidth()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
lineWidth = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (glyphs.empty() == false)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LineBreakGlyph& glyph = glyphs.at(glyphs.size() - 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
lineWidth = glyph.paintPosition + max(glyph.glyphWidth, glyph.horizAdvance - glyph.bearingX);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
int Device::getDPI()
|
|
|
|
{
|
|
|
|
static int dpi = -1;
|
|
|
|
if (dpi == -1)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Display* dpy;
|
|
|
|
char* displayname = NULL;
|
|
|
|
int scr = 0; /* Screen number */
|
|
|
|
dpy = XOpenDisplay(displayname);
|
2019-11-23 20:27:39 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
double xres = ((((double)DisplayWidth(dpy, scr)) * 25.4) / ((double)DisplayWidthMM(dpy, scr)));
|
|
|
|
dpi = (int)(xres + 0.5);
|
|
|
|
// printf("dpi = %d\n", dpi);
|
|
|
|
XCloseDisplay(dpy);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return dpi;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::setAccelerometerEnabled(bool isEnabled) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::setAccelerometerInterval(float interval) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
BitmapDC()
|
|
|
|
{
|
|
|
|
libError = FT_Init_FreeType(&library);
|
2019-11-23 20:27:39 +08:00
|
|
|
FcInit();
|
|
|
|
_data = NULL;
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
~BitmapDC()
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Done_FreeType(library);
|
|
|
|
FcFini();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
iMaxLineWidth = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
iMaxLineHeight = 0;
|
|
|
|
textLines.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int utf8(char** p)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool isBreakPoint(FT_UInt currentCharacter, FT_UInt previousCharacter)
|
|
|
|
{
|
|
|
|
if (previousCharacter == '-' || previousCharacter == '/' || previousCharacter == '\\')
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// we can insert a line break after one of these characters
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
const char* pText = sText;
|
|
|
|
textLines.clear();
|
|
|
|
iMaxLineWidth = 0;
|
|
|
|
|
|
|
|
FT_UInt unicode;
|
2021-12-25 10:04:45 +08:00
|
|
|
FT_UInt prevCharacter = 0;
|
|
|
|
FT_UInt glyphIndex = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_UInt prevGlyphIndex = 0;
|
|
|
|
FT_Vector delta;
|
|
|
|
LineBreakLine currentLine;
|
|
|
|
|
|
|
|
int currentPaintPosition = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
int firstBreakIndex = -1;
|
|
|
|
int lastBreakIndex = -1;
|
|
|
|
bool hasKerning = FT_HAS_KERNING(face);
|
|
|
|
while ((unicode = utf8((char**)&pText)))
|
|
|
|
{
|
|
|
|
if (unicode == '\n')
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
currentLine.calculateWidth();
|
|
|
|
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
|
|
|
|
textLines.push_back(currentLine);
|
|
|
|
currentLine.reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
prevGlyphIndex = 0;
|
|
|
|
prevCharacter = 0;
|
|
|
|
firstBreakIndex = -1;
|
|
|
|
lastBreakIndex = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
currentPaintPosition = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (isBreakPoint(unicode, prevCharacter))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
lastBreakIndex = currentLine.glyphs.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyphIndex = FT_Get_Char_Index(face, unicode);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LineBreakGlyph glyph;
|
2021-12-25 10:04:45 +08:00
|
|
|
glyph.glyphIndex = glyphIndex;
|
|
|
|
glyph.glyphWidth = face->glyph->metrics.width >> 6;
|
|
|
|
glyph.bearingX = face->glyph->metrics.horiBearingX >> 6;
|
2019-11-23 20:27:39 +08:00
|
|
|
glyph.horizAdvance = face->glyph->metrics.horiAdvance >> 6;
|
2021-12-25 10:04:45 +08:00
|
|
|
glyph.kerning = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (prevGlyphIndex != 0 && hasKerning)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Get_Kerning(face, prevGlyphIndex, glyphIndex, FT_KERNING_DEFAULT, &delta);
|
|
|
|
glyph.kerning = delta.x >> 6;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iswspace(unicode))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
prevGlyphIndex = glyphIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
prevCharacter = unicode;
|
2019-11-23 20:27:39 +08:00
|
|
|
lastBreakIndex = currentLine.glyphs.size();
|
|
|
|
|
|
|
|
if (firstBreakIndex == -1)
|
|
|
|
firstBreakIndex = lastBreakIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
if (iswspace(prevCharacter))
|
|
|
|
lastBreakIndex = currentLine.glyphs.size();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iMaxWidth > 0 &&
|
|
|
|
currentPaintPosition + glyph.bearingX + glyph.kerning + glyph.glyphWidth > iMaxWidth)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
int glyphCount = currentLine.glyphs.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (lastBreakIndex >= 0 && lastBreakIndex < glyphCount &&
|
|
|
|
currentPaintPosition + glyph.bearingX + glyph.kerning + glyph.glyphWidth -
|
|
|
|
currentLine.glyphs.at(lastBreakIndex).paintPosition <
|
|
|
|
iMaxWidth)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// 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());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (firstBreakIndex == -1)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
currentLine.glyphs.erase(it, currentLine.glyphs.end());
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
it = currentLine.glyphs.begin();
|
|
|
|
std::advance(it, firstBreakIndex);
|
|
|
|
currentLine.glyphs.erase(it, currentLine.glyphs.end());
|
|
|
|
}
|
|
|
|
currentLine.calculateWidth();
|
|
|
|
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
|
|
|
|
textLines.push_back(currentLine);
|
|
|
|
currentLine.reset();
|
|
|
|
currentPaintPosition = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& glyph : tempGlyphs)
|
|
|
|
{
|
|
|
|
if (currentLine.glyphs.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
currentPaintPosition = -glyph.bearingX;
|
2021-12-25 10:04:45 +08:00
|
|
|
glyph.kerning = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
glyph.paintPosition = currentPaintPosition + glyph.bearingX + glyph.kerning;
|
|
|
|
currentLine.glyphs.push_back(glyph);
|
|
|
|
currentPaintPosition += glyph.kerning + glyph.horizAdvance;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// the current word is too big to fit into one line, insert line break right here
|
|
|
|
currentPaintPosition = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
glyph.kerning = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
currentLine.calculateWidth();
|
|
|
|
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
|
|
|
|
textLines.push_back(currentLine);
|
|
|
|
currentLine.reset();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
prevGlyphIndex = 0;
|
|
|
|
prevCharacter = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
firstBreakIndex = -1;
|
2021-12-25 10:04:45 +08:00
|
|
|
lastBreakIndex = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
prevGlyphIndex = glyphIndex;
|
2021-12-25 10:04:45 +08:00
|
|
|
prevCharacter = unicode;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (currentLine.glyphs.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
currentPaintPosition = -glyph.bearingX;
|
|
|
|
}
|
|
|
|
glyph.paintPosition = currentPaintPosition + glyph.bearingX + glyph.kerning;
|
|
|
|
currentLine.glyphs.push_back(glyph);
|
|
|
|
currentPaintPosition += glyph.kerning + glyph.horizAdvance;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (currentLine.glyphs.empty() == false)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
currentLine.calculateWidth();
|
|
|
|
iMaxLineWidth = max(iMaxLineWidth, currentLine.lineWidth);
|
|
|
|
textLines.push_back(currentLine);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* compute the start pos of every line
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
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)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return (iMaxLineWidth - lineWidth) / 2;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (eAlignMask == Device::TextAlign::RIGHT || eAlignMask == Device::TextAlign::TOP_RIGHT ||
|
|
|
|
eAlignMask == Device::TextAlign::BOTTOM_RIGHT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return (iMaxLineWidth - lineWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
// left or other situation
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int computeLineStartY(FT_Face face, Device::TextAlign eAlignMask, int txtHeight, int borderHeight)
|
|
|
|
{
|
|
|
|
int baseLinePos = ceilf(face->size->metrics.ascender / 64.0f);
|
|
|
|
if (eAlignMask == Device::TextAlign::CENTER || eAlignMask == Device::TextAlign::LEFT ||
|
|
|
|
eAlignMask == Device::TextAlign::RIGHT)
|
|
|
|
{
|
|
|
|
// vertical center
|
2019-11-23 20:27:39 +08:00
|
|
|
return (borderHeight - txtHeight) / 2 + baseLinePos;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (eAlignMask == Device::TextAlign::BOTTOM_RIGHT || eAlignMask == Device::TextAlign::BOTTOM ||
|
|
|
|
eAlignMask == Device::TextAlign::BOTTOM_LEFT)
|
|
|
|
{
|
|
|
|
// vertical bottom
|
2019-11-23 20:27:39 +08:00
|
|
|
return borderHeight - txtHeight + baseLinePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// top alignment
|
|
|
|
return baseLinePos;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string getFontFile(const char* family_name)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string fontPath = family_name;
|
|
|
|
|
|
|
|
std::map<std::string, std::string>::iterator it = fontCache.find(family_name);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (it != fontCache.end())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (lowerCasePath.find(".ttf") != std::string::npos)
|
|
|
|
{
|
|
|
|
fontPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fontPath);
|
2021-05-05 19:49:30 +08:00
|
|
|
auto fileStream = cocos2d::FileUtils::getInstance()->openFileStream(fontPath, FileStream::Mode::READ);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (fileStream)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
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
|
2021-12-25 10:04:45 +08:00
|
|
|
FcPattern* pattern = FcPatternBuild(0, FC_FAMILY, FcTypeString, family_name, (char*)0);
|
2019-11-23 20:27:39 +08:00
|
|
|
FcConfigSubstitute(0, pattern, FcMatchPattern);
|
|
|
|
FcDefaultSubstitute(pattern);
|
|
|
|
|
|
|
|
FcResult result;
|
2021-12-25 10:04:45 +08:00
|
|
|
FcPattern* font = FcFontMatch(0, pattern, &result);
|
|
|
|
if (font)
|
|
|
|
{
|
|
|
|
FcChar8* s = NULL;
|
|
|
|
if (FcPatternGetString(font, FC_FILE, 0, &s) == FcResultMatch)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool getBitmap(const char* text, const FontDefinition& textDefinition, Device::TextAlign eAlignMask)
|
|
|
|
{
|
|
|
|
if (libError)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Face face;
|
|
|
|
std::string fontfile = getFontFile(textDefinition._fontName.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
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))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// select utf8 charmap
|
|
|
|
if (FT_Select_Charmap(face, FT_ENCODING_UNICODE))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FT_Set_Pixel_Sizes(face, textDefinition._fontSize, textDefinition._fontSize))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (divideString(face, text, textDefinition._dimensions.width, textDefinition._dimensions.height) == false)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
FT_Done_Face(face);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// compute the final line width
|
2019-11-23 20:27:39 +08:00
|
|
|
iMaxLineWidth = MAX(iMaxLineWidth, textDefinition._dimensions.width);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// compute the final line height
|
|
|
|
int lineHeight = face->size->metrics.height >> 6;
|
|
|
|
int txtHeight = (lineHeight * textLines.size());
|
2019-11-23 20:27:39 +08:00
|
|
|
iMaxLineHeight = MAX(txtHeight, textDefinition._dimensions.height);
|
|
|
|
|
|
|
|
_data = (unsigned char*)malloc(sizeof(unsigned char) * (iMaxLineWidth * iMaxLineHeight * 4));
|
2021-12-25 10:04:45 +08:00
|
|
|
memset(_data, 0, iMaxLineWidth * iMaxLineHeight * 4);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
int iCurYCursor = computeLineStartY(face, eAlignMask, txtHeight, iMaxLineHeight);
|
|
|
|
|
|
|
|
int lineCount = textLines.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int line = 0; line < lineCount; line++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
int iCurXCursor = computeLineStart(face, eAlignMask, line);
|
|
|
|
|
|
|
|
int glyphCount = textLines.at(line).glyphs.size();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < glyphCount; i++)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LineBreakGlyph glyph = textLines.at(line).glyphs.at(i);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (FT_Load_Glyph(face, glyph.glyphIndex, FT_LOAD_RENDER))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_Bitmap& bitmap = face->glyph->bitmap;
|
2021-12-25 10:04:45 +08:00
|
|
|
int yoffset = iCurYCursor - (face->glyph->metrics.horiBearingY >> 6);
|
|
|
|
int xoffset = iCurXCursor + glyph.paintPosition;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int y = 0; y < bitmap.rows; ++y)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
int iY = yoffset + y;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iY >= iMaxLineHeight)
|
|
|
|
{
|
|
|
|
// exceed the height truncate
|
2019-11-23 20:27:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
iY *= iMaxLineWidth;
|
|
|
|
|
|
|
|
int bitmap_y = y * bitmap.width;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int x = 0; x < bitmap.width; ++x)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned char cTemp = bitmap.buffer[bitmap_y + x];
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cTemp == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int iX = xoffset + x;
|
2021-12-25 10:04:45 +08:00
|
|
|
// FIXME:wrong text color
|
|
|
|
int iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
|
|
|
|
*(int*)&_data[(iY + iX) * 4 + 0] = iTemp;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// step to next line
|
|
|
|
iCurYCursor += lineHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// free face
|
|
|
|
FT_Done_Face(face);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
FT_Library library;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned char* _data;
|
2019-11-23 20:27:39 +08:00
|
|
|
int libError;
|
|
|
|
std::vector<LineBreakLine> textLines;
|
|
|
|
int iMaxLineWidth;
|
|
|
|
int iMaxLineHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Data Device::getTextureDataForText(const char* text,
|
|
|
|
const FontDefinition& textDefinition,
|
|
|
|
TextAlign align,
|
|
|
|
int& width,
|
|
|
|
int& height,
|
|
|
|
bool& hasPremultipliedAlpha)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Data ret;
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
BitmapDC& dc = sharedBitmapDC();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_BREAK_IF(!dc.getBitmap(text, textDefinition, align));
|
|
|
|
CC_BREAK_IF(!dc._data);
|
|
|
|
width = dc.iMaxLineWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
height = dc.iMaxLineHeight;
|
|
|
|
dc.reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
ret.fastSet(dc._data, width * height * 4);
|
2019-11-23 20:27:39 +08:00
|
|
|
hasPremultipliedAlpha = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::setKeepScreenOn(bool /*value*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Device::vibrate(float /*duration*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_END
|