Browse Source

Merge pull request #330 from olliwang/CJK

Fixes line breaks for CJK characters.
shared-context
Mikko Mononen GitHub 8 years ago
parent
commit
84e848d1e9
1 changed files with 10 additions and 6 deletions
  1. +10
    -6
      src/nanovg.c

+ 10
- 6
src/nanovg.c View File

@@ -2549,6 +2549,7 @@ enum NVGcodepointType {
NVG_SPACE, NVG_SPACE,
NVG_NEWLINE, NVG_NEWLINE,
NVG_CHAR, NVG_CHAR,
NVG_CJK_CHAR,
}; };


int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows) int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows)
@@ -2616,7 +2617,10 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
type = NVG_NEWLINE; type = NVG_NEWLINE;
break; break;
default: default:
type = NVG_CHAR;
if (iter.codepoint >= 0x4E00 && iter.codepoint <= 0x9FFF)
type = NVG_CJK_CHAR;
else
type = NVG_CHAR;
break; break;
} }


@@ -2643,7 +2647,7 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
} else { } else {
if (rowStart == NULL) { if (rowStart == NULL) {
// Skip white space until the beginning of the line // Skip white space until the beginning of the line
if (type == NVG_CHAR) {
if (type == NVG_CHAR || type == NVG_CJK_CHAR) {
// The current char is the row so far // The current char is the row so far
rowStartX = iter.x; rowStartX = iter.x;
rowStart = iter.str; rowStart = iter.str;
@@ -2663,26 +2667,26 @@ int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, floa
float nextWidth = iter.nextx - rowStartX; float nextWidth = iter.nextx - rowStartX;


// track last non-white space character // track last non-white space character
if (type == NVG_CHAR) {
if (type == NVG_CHAR || type == NVG_CJK_CHAR) {
rowEnd = iter.next; rowEnd = iter.next;
rowWidth = iter.nextx - rowStartX; rowWidth = iter.nextx - rowStartX;
rowMaxX = q.x1 - rowStartX; rowMaxX = q.x1 - rowStartX;
} }
// track last end of a word // track last end of a word
if (ptype == NVG_CHAR && type == NVG_SPACE) {
if ((ptype == NVG_CHAR && type == NVG_SPACE) || type == NVG_CJK_CHAR) {
breakEnd = iter.str; breakEnd = iter.str;
breakWidth = rowWidth; breakWidth = rowWidth;
breakMaxX = rowMaxX; breakMaxX = rowMaxX;
} }
// track last beginning of a word // track last beginning of a word
if (ptype == NVG_SPACE && type == NVG_CHAR) {
if ((ptype == NVG_SPACE && type == NVG_CHAR) || type == NVG_CJK_CHAR) {
wordStart = iter.str; wordStart = iter.str;
wordStartX = iter.x; wordStartX = iter.x;
wordMinX = q.x0 - rowStartX; wordMinX = q.x0 - rowStartX;
} }


// Break to new line when a character is beyond break width. // Break to new line when a character is beyond break width.
if (type == NVG_CHAR && nextWidth > breakRowWidth) {
if ((type == NVG_CHAR || type == NVG_CJK_CHAR) && nextWidth > breakRowWidth) {
// The run length is too long, need to break to new line. // The run length is too long, need to break to new line.
if (breakEnd == rowStart) { if (breakEnd == rowStart) {
// The current word is longer than the row length, just break it from here. // The current word is longer than the row length, just break it from here.


Loading…
Cancel
Save