mirror of https://github.com/axmolengine/axmol.git
fixed #624: insert white space in front of \n
This commit is contained in:
parent
5ba5d08e47
commit
13a1629299
|
@ -28,11 +28,7 @@ public class Cocos2dxBitmap{
|
||||||
public static void createTextBitmap(String content, String fontName,
|
public static void createTextBitmap(String content, String fontName,
|
||||||
int fontSize, int alignment, int width, int height){
|
int fontSize, int alignment, int width, int height){
|
||||||
|
|
||||||
// Avoid error when content is ""
|
content = refactorString(content);
|
||||||
if (content.compareTo("") == 0){
|
|
||||||
content = " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
Paint paint = newPaint(fontName, fontSize, alignment);
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
||||||
|
@ -272,6 +268,40 @@ public class Cocos2dxBitmap{
|
||||||
return paint;
|
return paint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String refactorString(String str){
|
||||||
|
// Avoid error when content is ""
|
||||||
|
if (str.compareTo("") == 0){
|
||||||
|
return " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the font of "\n" is "" or "\n", insert " " in front of it.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
* "\nabc" -> " \nabc"
|
||||||
|
* "\nabc\n\n" -> " \nabc\n \n"
|
||||||
|
*/
|
||||||
|
StringBuilder strBuilder = new StringBuilder(str);
|
||||||
|
int start = 0;
|
||||||
|
int index = strBuilder.indexOf("\n");
|
||||||
|
while (index != -1){
|
||||||
|
if (index == 0 || strBuilder.charAt(index -1) == '\n'){
|
||||||
|
strBuilder.insert(start, " ");
|
||||||
|
start = index + 2;
|
||||||
|
} else {
|
||||||
|
start = index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start > strBuilder.length() || index == strBuilder.length()){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = strBuilder.indexOf("\n", start);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static void initNativeObject(Bitmap bitmap){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
byte[] pixels = getPixels(bitmap);
|
byte[] pixels = getPixels(bitmap);
|
||||||
if (pixels == null){
|
if (pixels == null){
|
||||||
|
|
|
@ -28,11 +28,7 @@ public class Cocos2dxBitmap{
|
||||||
public static void createTextBitmap(String content, String fontName,
|
public static void createTextBitmap(String content, String fontName,
|
||||||
int fontSize, int alignment, int width, int height){
|
int fontSize, int alignment, int width, int height){
|
||||||
|
|
||||||
// Avoid error when content is ""
|
content = refactorString(content);
|
||||||
if (content.compareTo("") == 0){
|
|
||||||
content = " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
Paint paint = newPaint(fontName, fontSize, alignment);
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
||||||
|
@ -272,6 +268,40 @@ public class Cocos2dxBitmap{
|
||||||
return paint;
|
return paint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String refactorString(String str){
|
||||||
|
// Avoid error when content is ""
|
||||||
|
if (str.compareTo("") == 0){
|
||||||
|
return " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the font of "\n" is "" or "\n", insert " " in front of it.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
* "\nabc" -> " \nabc"
|
||||||
|
* "\nabc\n\n" -> " \nabc\n \n"
|
||||||
|
*/
|
||||||
|
StringBuilder strBuilder = new StringBuilder(str);
|
||||||
|
int start = 0;
|
||||||
|
int index = strBuilder.indexOf("\n");
|
||||||
|
while (index != -1){
|
||||||
|
if (index == 0 || strBuilder.charAt(index -1) == '\n'){
|
||||||
|
strBuilder.insert(start, " ");
|
||||||
|
start = index + 2;
|
||||||
|
} else {
|
||||||
|
start = index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start > strBuilder.length() || index == strBuilder.length()){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = strBuilder.indexOf("\n", start);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static void initNativeObject(Bitmap bitmap){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
byte[] pixels = getPixels(bitmap);
|
byte[] pixels = getPixels(bitmap);
|
||||||
if (pixels == null){
|
if (pixels == null){
|
||||||
|
|
|
@ -28,11 +28,7 @@ public class Cocos2dxBitmap{
|
||||||
public static void createTextBitmap(String content, String fontName,
|
public static void createTextBitmap(String content, String fontName,
|
||||||
int fontSize, int alignment, int width, int height){
|
int fontSize, int alignment, int width, int height){
|
||||||
|
|
||||||
// Avoid error when content is ""
|
content = refactorString(content);
|
||||||
if (content.compareTo("") == 0){
|
|
||||||
content = " ";
|
|
||||||
}
|
|
||||||
|
|
||||||
Paint paint = newPaint(fontName, fontSize, alignment);
|
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||||
|
|
||||||
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
TextProperty textProperty = getTextWidthAndHeight(content, paint, width, height);
|
||||||
|
@ -272,6 +268,40 @@ public class Cocos2dxBitmap{
|
||||||
return paint;
|
return paint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String refactorString(String str){
|
||||||
|
// Avoid error when content is ""
|
||||||
|
if (str.compareTo("") == 0){
|
||||||
|
return " ";
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If the font of "\n" is "" or "\n", insert " " in front of it.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
* "\nabc" -> " \nabc"
|
||||||
|
* "\nabc\n\n" -> " \nabc\n \n"
|
||||||
|
*/
|
||||||
|
StringBuilder strBuilder = new StringBuilder(str);
|
||||||
|
int start = 0;
|
||||||
|
int index = strBuilder.indexOf("\n");
|
||||||
|
while (index != -1){
|
||||||
|
if (index == 0 || strBuilder.charAt(index -1) == '\n'){
|
||||||
|
strBuilder.insert(start, " ");
|
||||||
|
start = index + 2;
|
||||||
|
} else {
|
||||||
|
start = index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start > strBuilder.length() || index == strBuilder.length()){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
index = strBuilder.indexOf("\n", start);
|
||||||
|
}
|
||||||
|
|
||||||
|
return strBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private static void initNativeObject(Bitmap bitmap){
|
private static void initNativeObject(Bitmap bitmap){
|
||||||
byte[] pixels = getPixels(bitmap);
|
byte[] pixels = getPixels(bitmap);
|
||||||
if (pixels == null){
|
if (pixels == null){
|
||||||
|
|
Loading…
Reference in New Issue