0
|
1 /* layout2.cc
|
|
2 * テキストの禁則処理、レイアウトなどを行う
|
|
3 */
|
|
4 /*
|
|
5 * Copyright (c) 2004-2006 Kazunori "jagarl" Ueno
|
|
6 * All rights reserved.
|
|
7 *
|
|
8 * Redistribution and use in source and binary forms, with or without
|
|
9 * modification, are permitted provided that the following conditions
|
|
10 * are met:
|
|
11 * 1. Redistributions of source code must retain the above copyright
|
|
12 * notice, this list of conditions and the following disclaimer.
|
|
13 * 2. Redistributions in binary form must reproduce the above copyright
|
|
14 * notice, this list of conditions and the following disclaimer in the
|
|
15 * documentation and/or other materials provided with the distribution.
|
|
16 * 3. The name of the author may not be used to endorse or promote products
|
|
17 * derived from this software without specific prior written permission.
|
|
18 *
|
|
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29 */
|
|
30
|
|
31 #include<vector>
|
|
32 #include<map>
|
|
33 #include<iostream>
|
|
34
|
|
35 using namespace std;
|
|
36
|
|
37 #include"font.h"
|
|
38 #include"text.h"
|
|
39
|
|
40 const int line_skip = 1; // 行と行の間の間隔
|
|
41 const int ruby_textskip = 0; // 文字とルビの間の間隔
|
|
42 const int ruby_lineskip = 1; // ルビがあるときに行間に加える値
|
|
43 const double ruby_scale = 0.4; // ルビのスケール
|
|
44
|
|
45 class TextGlyphStreamHelper;
|
|
46
|
|
47 enum KinsokuType { KinsokuHead = 1, KinsokuTail = 2};
|
|
48 static int kinsoku_table1[] = {
|
|
49 /* 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 */
|
|
50 0,0,2,2,2,2,0,0, /* X 、。,.・: */
|
|
51 0,2,2,0,0,0,0,0, /* ;?!゛゜´`¨ */
|
|
52 0,0,0,0,0,0,0,0, /* ^ ̄_ヽヾゝゞ〃 */
|
|
53 0,0,0,0,2,0,0,0, /* 仝々〆〇ー―‐/ */
|
|
54 0,2,0,0,2,2,1,2, /* \〜‖|…‥‘’ */
|
|
55 1,2,1,2,1,2,1,2, /* “”()〔〕[] */
|
|
56 1,2,1,2,1,2,1,2, /* {}〈〉《》「」 */
|
|
57 1,2,1,2,0,0,0,0, /* 『』【】+−±× */
|
|
58 0,0,0,0,0,0,0,0, /* ÷=≠<>≦≧∞ */
|
|
59 0,0,0,0,0,0,0,0, /* ∴♂♀°′″℃¥ */
|
|
60 0,0,0,0,0,0,0,0, /* $¢£%#&*@ */
|
|
61 0,0,0,0,0,0,0,0, /* §☆★○●◎◇X */
|
|
62 0
|
|
63 };
|
|
64 static int kinsoku_table2[] = {
|
|
65 0,2,0,2,0,2,0,2,0,2,0,0,0,0,0,0, /* ぁあぃいぅうぇえぉおかがきぎく */
|
|
66 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ぐけげこごさざしじすずせぜそぞた */
|
|
67 0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0, /* だちぢっつづてでとどなにぬねのは */
|
|
68 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* ばぱひびぴふぶぷへべぺほぼぽまみ */
|
|
69 0,0,0,2,0,2,0,2,0,0,0,0,0,0,2,0, /* むめもゃやゅゆょよらりるれろゎわ */
|
|
70 0,0,0,0,0,2,2,0,0,0,0,0,0,0,0,0, /* ゐゑをんヴヵヶ */
|
|
71 0
|
|
72 };
|
|
73
|
|
74 inline int Kinsoku(int code) {
|
|
75 if ( (code&0xff80) == 0xa180) return kinsoku_table1[ (code&0xff) - 0xa0];
|
|
76 if ( (code&0xfe80) == 0xa480) return kinsoku_table2[ (code&0xff) - 0xa0]; /* code = 0xa400 / 0xa500 */
|
|
77 return 0;
|
|
78 }
|
|
79
|
|
80 class TextGlyphStreamHelper {
|
|
81 typedef TextStream::Iterator Iterator;
|
|
82 typedef TextGlyphStream::iterator iterator;
|
|
83
|
|
84 TextGlyphStream* data;
|
|
85
|
|
86 // information for rendering
|
|
87 unsigned char r, g, b;
|
|
88 XKFont::Face* face;
|
|
89 XKFont::Face* ruby_face;
|
|
90 XKFont::Font* font;
|
|
91
|
|
92 public:
|
|
93 int min_lineheight;
|
|
94 TextGlyphStreamHelper(XKFont::Font* font);
|
|
95 // helper functions
|
|
96 void Init(TextGlyphStream* data);
|
|
97 Iterator Add(int& x, Iterator begin, Iterator end, int max_x = 0);
|
|
98 Iterator AddRuby(int& x, Iterator begin, Iterator end);
|
|
99 int CharWidth(int code);
|
|
100 void SetGroup(iterator begin, iterator end);
|
|
101 void CalcHeight(int& ascent, int& descent, iterator begin, iterator end);
|
|
102 void AdjustPosition(int xstart_add, int xend_add, int y_add, iterator begin, iterator end);
|
|
103 };
|
|
104
|
|
105 TextGlyphStreamHelper::TextGlyphStreamHelper(XKFont::Font* __font) {
|
|
106 font = __font;
|
|
107 face = font->FaceLoad(1.0);
|
|
108 ruby_face = 0;
|
|
109 r = 255; g = 255; b = 255;
|
|
110 min_lineheight = font->vsize;
|
|
111 }
|
|
112
|
|
113 void TextGlyphStreamHelper::Init(TextGlyphStream* __data) {
|
|
114 r = 255; g = 255; b = 255;
|
|
115 face = font->FaceLoad(1.0);
|
|
116 data = __data;
|
|
117 data->clear();
|
|
118 data->font = font;
|
|
119 }
|
|
120
|
|
121 TextGlyphStreamHelper::Iterator
|
|
122 TextGlyphStreamHelper::Add(int& x, TextGlyphStreamHelper::Iterator begin, TextGlyphStreamHelper::Iterator end, int max_x) {
|
|
123 /* text を glyph に変換する */
|
|
124 TextGlyph gl;
|
|
125 Iterator it;
|
|
126 gl.x = x; gl.y = 0; gl.r = r; gl.g = g; gl.b = b; gl.flag = TextGlyph::Flag(0); gl.is_rev = false;
|
|
127 for (it = begin; it != end; it++) {
|
|
128 if (it->type != TextElem::glyph) {
|
|
129 if (it->type == TextElem::color) {
|
|
130 gl.r = r = it->impl.Color.r;
|
|
131 gl.g = g = it->impl.Color.g;
|
|
132 gl.b = b = it->impl.Color.b;
|
|
133 } else if (it->type == TextElem::size) {
|
50
|
134 delete face;
|
0
|
135 face = font->FaceLoad(it->impl.Size.scale);
|
|
136 } else if (it->type == TextElem::escape) {
|
|
137 x = gl.x;
|
|
138 return it;
|
|
139 }
|
|
140 continue;
|
|
141 }
|
|
142 try {
|
|
143 gl.glyph = face->GlyphLoad(it->impl.Glyph.code);
|
|
144 if (max_x > 0 && gl.x + gl.glyph->advance.x > max_x) {
|
|
145 x = gl.x;
|
|
146 return it;
|
|
147 }
|
|
148 if ( Kinsoku(it->impl.Glyph.code) == KinsokuTail)
|
|
149 gl.flag = TextGlyph::Flag(gl.flag | TextGlyph::Kinsoku);
|
|
150 else
|
|
151 gl.flag = TextGlyph::Flag(0);
|
|
152 data->push_back(gl);
|
|
153 gl.x += gl.glyph->advance.x;
|
|
154 } catch(...) {}
|
|
155 }
|
|
156 x = gl.x;
|
|
157 return it;
|
|
158 }
|
|
159
|
|
160 TextGlyphStreamHelper::Iterator TextGlyphStreamHelper::AddRuby(int& x, TextGlyphStreamHelper::Iterator sbegin, TextGlyphStreamHelper::Iterator send) {
|
|
161 Iterator it;
|
|
162 it = sbegin;
|
|
163 if (it == send) return it;
|
|
164 if (it->type != TextElem::escape || it->impl.Escape.type != TextElem::ruby_start) return sbegin;
|
|
165 it++;
|
|
166 /* まず、本文描画 */
|
|
167 int str_firstpos = data->size();
|
|
168 int str_width = 0;
|
|
169 it = Add(str_width, it, send);
|
|
170 if (it == send || it->type != TextElem::escape || it->impl.Escape.type != TextElem::ruby_startruby) {
|
|
171 // ありえないはずだが、取り合えずなにもしないで終了
|
|
172 cerr << "TextGlyphStream::AddRuby : invalid operation; fallback to the upeer level"<<endl;
|
|
173 data->erase(data->begin()+str_firstpos, data->end());
|
|
174 return sbegin+1;
|
|
175 }
|
|
176 it++;
|
|
177 int str_lastpos = data->size()-1;
|
|
178 TextGlyph& str_first = data->begin()[str_firstpos];
|
|
179 TextGlyph& str_last = data->back();
|
|
180 // 次に、フォントを取りかえてルビ描画
|
|
181 int ruby_firstpos = data->size();
|
|
182 XKFont::Face* save_font = face;
|
|
183 if (ruby_face == 0) ruby_face = font->FaceLoad(ruby_scale);
|
|
184 face = ruby_face;
|
|
185 int ruby_width = 0;
|
|
186 it = Add(ruby_width, it, send);
|
|
187 if (it->type != TextElem::escape || it->impl.Escape.type != TextElem::ruby_end) {
|
|
188 /* ありえないはずだが、取り合えずなにもしないで終了 */
|
|
189 cerr << "TextGlyphStream::AddRuby : invalid operation; fallback to the upeer level"<<endl;
|
|
190 data->erase(data->begin()+str_firstpos, data->end());
|
|
191 return sbegin+1;
|
|
192 }
|
|
193 it++;
|
|
194 face = save_font;
|
|
195 TextGlyph& ruby_first = (*data)[ruby_firstpos];
|
|
196 TextGlyph& ruby_last = data->back();
|
|
197
|
|
198 /* ルビを移動すべき高さを求める */
|
|
199 int dummy, str_ascent, ruby_descent;
|
|
200 CalcHeight(str_ascent, dummy, data->begin()+str_firstpos, data->begin()+ruby_firstpos);
|
|
201 CalcHeight(dummy, ruby_descent, data->begin()+ruby_firstpos, data->end());
|
|
202 int ruby_height = str_ascent + ruby_descent + ruby_textskip;
|
|
203
|
|
204 /* センタリングした場合の、ルビの左側、右側のマージン */
|
|
205 int leftmergin, rightmergin;
|
|
206 leftmergin = str_first.glyph->advance.x/2 - (ruby_first.glyph->advance.x+1)/2;
|
|
207 rightmergin = str_last.glyph->advance.x/2 - (ruby_last.glyph->advance.x+1)/2;
|
|
208
|
|
209 /* ルビ、本文の横方向の移動 */
|
|
210 int ruby_xstart_add = 0, ruby_xend_add = 0, str_xstart_add=0, str_xend_add = 0;
|
|
211 if (ruby_width+leftmergin+rightmergin <= str_width) { // ルビの方が小さい
|
|
212 ruby_xstart_add = leftmergin;
|
|
213 ruby_xend_add = str_width-rightmergin-ruby_width;
|
|
214 } else if (ruby_width <= str_width) { // マージンを減らす必要あり
|
|
215 leftmergin = (str_width-ruby_width)/2;
|
|
216 ruby_xstart_add = leftmergin;
|
|
217 ruby_xend_add = str_width-leftmergin-ruby_width;
|
|
218 } else { // ルビの方が大きい
|
|
219 int str_count = ruby_firstpos - str_firstpos;
|
|
220 str_xstart_add = ruby_width/str_count/2 - str_first.glyph->advance.x/2;
|
|
221 str_xend_add = (ruby_width-str_width) - (ruby_width/str_count/2-str_last.glyph->advance.x/2);
|
|
222 str_width = ruby_width;
|
|
223 }
|
|
224 AdjustPosition(str_xstart_add+x, str_xend_add+x, 0, data->begin()+str_firstpos, data->begin()+ruby_firstpos);
|
|
225 AdjustPosition(ruby_xstart_add+x, ruby_xend_add+x, -ruby_height, data->begin()+ruby_firstpos, data->end());
|
|
226
|
|
227 /* 本文が一文字ずつ表示されるように glyph の順番を入れかえ、グループ化 */
|
|
228 vector<TextGlyph> save;
|
|
229 save.assign(data->begin()+str_firstpos, data->end());
|
|
230 iterator it_str = save.begin();
|
|
231 iterator it_ruby = save.begin()+(ruby_firstpos-str_firstpos);
|
|
232 iterator dit = data->begin()+str_firstpos;
|
|
233 int str_count = it_ruby-it_str;
|
|
234 int ruby_count = save.end()-it_ruby;
|
|
235 int i,j = 0;
|
|
236 for (i=0; i<str_count; i++) {
|
|
237 iterator charstart = dit;
|
|
238 int jend = (i+1)*ruby_count/str_count;
|
|
239 for (; j<jend; j++) {
|
|
240 *dit++ = *it_ruby++;
|
|
241 }
|
|
242 *dit++ = *it_str++;
|
|
243 SetGroup(charstart, dit);
|
|
244 }
|
|
245 x += str_width;
|
|
246 return it;
|
|
247 }
|
|
248
|
|
249
|
|
250 void TextGlyphStreamHelper::SetGroup(TextGlyphStreamHelper::iterator begin, TextGlyphStreamHelper::iterator end) {
|
|
251 iterator it;
|
|
252 for (it = begin; it+1 != end; it++)
|
|
253 it->flag = TextGlyph::Flag(it->flag |TextGlyph::Group);
|
|
254 it->flag = TextGlyph::Flag(it->flag & ~TextGlyph::Group);
|
|
255 return;
|
|
256 }
|
|
257
|
|
258 void TextGlyphStreamHelper::AdjustPosition(int xstart_add, int xend_add, int y_add, TextGlyphStreamHelper::iterator begin, TextGlyphStreamHelper::iterator end) {
|
|
259 iterator it;
|
|
260 /* 文字数を数える */
|
|
261 int total_count = 0;
|
|
262 for (it = begin; it != end; it++) {
|
|
263 if (it->flag & TextGlyph::Group) continue;
|
|
264 total_count++;
|
|
265 }
|
|
266 /* 文字間のギャップを変更 */
|
|
267 int incr = 0;
|
|
268 if (total_count != 1) incr = (xend_add - xstart_add) * 256 / (total_count-1);
|
|
269 int cur = xstart_add * 256;
|
|
270 for (it = begin; it != end; it++) {
|
|
271 it->x += cur / 256;
|
|
272 it->y += y_add;
|
|
273 if (it->flag & TextGlyph::Group) continue;
|
|
274 cur += incr;
|
|
275 }
|
|
276 return;
|
|
277 }
|
|
278 void TextGlyphStreamHelper::CalcHeight(int& ascent_r, int& descent_r, TextGlyphStreamHelper::iterator begin, TextGlyphStreamHelper::iterator end) {
|
|
279 iterator it;
|
|
280 /* 最大の descent, ascent を計算 */
|
|
281 int ascent = 0;
|
|
282 int descent = 0;
|
|
283 for (it = begin; it != end; it++) {
|
|
284
|
|
285 int y_top = it->y - it->glyph->bitmap_top;
|
|
286 int y_bottom = it->y + it->glyph->bitmap.rows - it->glyph->bitmap_top;
|
|
287
|
|
288 if (descent < y_bottom) descent = y_bottom;
|
|
289 if (ascent < -y_top) ascent = -y_top;
|
|
290 }
|
|
291 ascent_r = ascent;
|
|
292 descent_r = descent;
|
|
293 return;
|
|
294 }
|
|
295
|
|
296 int TextGlyphStreamHelper::CharWidth(int code) {
|
|
297 try {
|
|
298 XKFont::Glyph* g = face->GlyphLoad(code);
|
|
299 return g->advance.x;
|
|
300 } catch(...) {
|
|
301 return 0;
|
|
302 }
|
|
303 }
|
|
304
|
|
305 class TextHorizLayout {
|
|
306 typedef TextStream::Iterator Iterator;
|
|
307
|
|
308 Iterator pos;
|
|
309 Iterator end;
|
|
310 TextGlyphStream* data;
|
|
311 TextGlyphStreamHelper helper;
|
|
312 int tab_width;
|
|
313 int cur_y;
|
|
314
|
|
315 void SetName(void);
|
|
316 void SetLineHead(void);
|
|
317 void MakeLine(int line_first, int width, vector<int>& lineheights);
|
|
318 public:
|
|
319 TextHorizLayout(XKFont::Font* font);
|
|
320 void Layout(TextStream& stream, TextGlyphStream& glyph, vector<int>& lineheights, int width);
|
|
321 };
|
|
322
|
|
323 TextHorizLayout::TextHorizLayout(XKFont::Font* font) :
|
|
324 helper(font), tab_width(0), cur_y(0) {
|
|
325 }
|
|
326
|
|
327 void TextHorizLayout::Layout(TextStream& stream, TextGlyphStream& glyph, vector<int>& lineheights, int width) {
|
|
328 pos = stream.container.begin();
|
|
329 end = stream.container.end();
|
|
330 data = &glyph;
|
|
331
|
|
332 helper.Init(data);
|
|
333 tab_width = 0;
|
|
334 cur_y = 0;
|
|
335 int prev_y = 0;
|
|
336 int line_start = glyph.size();
|
|
337 while(pos != end) {
|
|
338 /*
|
|
339 if (pos->type == TextElem::glyph) { int c = pos->impl.Glyph.code; char cc[3]={0,0,0};cc[0]=c>>8;cc[1]=c;cout<<"glyph "<<cc<<endl;}
|
|
340 if (pos->type == TextElem::escape) { cout<<"escape "<<pos->impl.Escape.type<<endl;}
|
|
341 */
|
|
342 SetName();
|
|
343 SetLineHead();
|
|
344 MakeLine(line_start, width, lineheights);
|
|
345 if (line_start != glyph.size()) {
|
|
346 data->back().flag = TextGlyph::Flag(data->back().flag | TextGlyph::PhraseEnd | TextGlyph::LineEnd);
|
|
347 }
|
|
348 prev_y = cur_y;
|
|
349 if (pos != end && pos->type == TextElem::escape && pos->impl.Escape.type == TextElem::ret) pos++;
|
|
350 line_start = glyph.size();
|
|
351 }
|
|
352 return;
|
|
353 }
|
|
354
|
|
355 void TextHorizLayout::SetName(void) {
|
|
356 Iterator it;
|
|
357
|
|
358 tab_width = 0;
|
|
359 /* 行頭が名前なら、処理開始 */
|
|
360 for (; pos != end; pos++) {
|
|
361 if (pos->type == TextElem::escape || pos->type == TextElem::glyph) break;
|
|
362 int x = 0;
|
|
363 helper.Add(x, pos, pos+1);
|
|
364 }
|
|
365
|
|
366 if (pos->type != TextElem::escape || pos->impl.Escape.type != TextElem::name_start) return;
|
|
367
|
|
368 /* 名前をセットし、行頭の「の分を含めてタブ幅を設定する */
|
|
369 pos++;
|
|
370 for (it = pos; it != end; it++) {
|
|
371 if (it->type == TextElem::escape && it->impl.Escape.type == TextElem::name_end) break;
|
|
372 }
|
|
373 if (it == end) return;
|
|
374 int line_firstpos = data->size();
|
|
375 pos = helper.Add(tab_width, pos, it);
|
|
376 pos++;
|
|
377 helper.SetGroup(data->begin() + line_firstpos, data->end());
|
|
378
|
|
379 // 行頭の「分を開ける
|
|
380 try {
|
|
381 tab_width += helper.CharWidth(0xa1d6); /* 「 */
|
|
382 } catch(...) {}
|
|
383
|
|
384 return;
|
|
385 };
|
|
386
|
|
387 void TextHorizLayout::SetLineHead(void) {
|
|
388
|
|
389 /* 行頭は 「などか? */
|
|
390
|
|
391 for (; pos != end; pos++) {
|
|
392 if (pos->type == TextElem::escape || pos->type == TextElem::glyph) break;
|
|
393 int x = 0;
|
|
394 helper.Add(x, pos, pos+1);
|
|
395 }
|
|
396 if (pos->type != TextElem::glyph || Kinsoku(pos->impl.Glyph.code) != KinsokuHead) return;
|
|
397
|
|
398 /* 「なので、処理する */
|
|
399 if (tab_width != 0) tab_width -= helper.CharWidth(pos->impl.Glyph.code);
|
|
400 int line_firstpos = data->size();
|
|
401 pos = helper.Add(tab_width, pos, pos+1);
|
|
402 return;
|
|
403 }
|
|
404
|
|
405 void TextHorizLayout::MakeLine(int line_start, int width, vector<int>& lineheights) {
|
|
406
|
|
407 int x = tab_width;
|
|
408 /* まず、全文字描画する */
|
|
409 while(pos != end) {
|
|
410 pos = helper.Add(x, pos, end);
|
|
411 if (pos->type == TextElem::escape && pos->impl.Escape.type == TextElem::ruby_start) {
|
|
412 pos = helper.AddRuby(x, pos, end);
|
|
413 }
|
|
414 if (pos != end && pos->type == TextElem::escape) {
|
|
415 if (pos->impl.Escape.type == TextElem::ret) break;
|
|
416 if (pos->impl.Escape.type != TextElem::ruby_start) pos++;
|
|
417 }
|
|
418 }
|
|
419 /* 行に分割していく */
|
|
420 TextGlyphStream::iterator it_start = data->begin() + line_start;
|
|
421 TextGlyphStream::iterator it_end = data->end();
|
|
422 TextGlyphStream::iterator it = it_start;
|
|
423
|
|
424 TextGlyphStream::iterator group_head = it_start;
|
|
425 int xstart = tab_width;
|
|
426 int xend = width;
|
|
427 while(it != it_end) {
|
|
428 // この行の終わりを決める
|
|
429 bool is_ruby = false;
|
|
430 TextGlyphStream::iterator it_line_start = it;
|
|
431 for (; it != it_end; it++) {
|
|
432 if (it->x + it->glyph->advance.x > xend) break;
|
|
433 if (it->flag & TextGlyph::Group) is_ruby = true;
|
|
434 if (!(it->flag & TextGlyph::Group)) group_head = it;
|
|
435 }
|
|
436 // 水平移動の大きさを決める。デフォルトでタブ位置まで戻す
|
|
437 int xadd_start = -xstart + tab_width;
|
|
438 int xadd_end = xadd_start;
|
|
439 // it == 次行の先頭なので、今行の末尾へ戻す
|
|
440 // ただし、 最低一文字の表示は保証
|
|
441 if (it != it_line_start && it != it_line_start+1 && it != it_end) it--;
|
|
442 if (it != it_end) {
|
|
443 // グループ化されている文字で終了したら、前の文字に戻す
|
|
444 if (it->flag & TextGlyph::Group) it = group_head;
|
|
445 // 次が行頭禁則文字ならこの行に入れる
|
|
446 if ( (it+1) != it_end && (it+1)->flag & TextGlyph::Kinsoku) it++;
|
|
447 // 移動する大きさを決める
|
|
448 // 行端ぞろえ、行末文字なら半文字分だけ突き出る
|
|
449 int glyph_xend = it->x + it->glyph->advance.x;
|
|
450 if (it != it_line_start && (it-1)->flag & TextGlyph::Group) { // グループ化文字の場合、1文字前も見る
|
|
451 if (glyph_xend < (it-1)->x + (it-1)->glyph->advance.x)
|
|
452 glyph_xend = (it-1)->x + (it-1)->glyph->advance.x;
|
|
453 }
|
|
454 xadd_end += xend - glyph_xend;
|
|
455 if (it->flag & TextGlyph::Kinsoku)
|
|
456 xadd_end += it->glyph->advance.x / 2;
|
|
457 }
|
|
458 if (it != it_end) {
|
|
459 it->flag = TextGlyph::Flag(it->flag | TextGlyph::LineEnd);
|
|
460 it++; // it == 次行の先頭へ
|
|
461 }
|
|
462 int ascent, descent;
|
|
463 helper.CalcHeight(ascent, descent, it_start, it);
|
|
464 if (ascent+descent < helper.min_lineheight) {
|
|
465 int dif = helper.min_lineheight-(ascent+descent);
|
|
466 ascent += dif/2;
|
|
467 descent += dif-(dif/2);
|
|
468 }
|
|
469 if (is_ruby) ascent+=ruby_lineskip;
|
|
470 helper.AdjustPosition(xadd_start, xadd_end, cur_y+ascent+1, it_start, it);
|
|
471 cur_y += ascent + descent + line_skip;
|
|
472 lineheights.push_back(ascent+descent+line_skip);
|
|
473
|
|
474 /* 次の行へ */
|
|
475 if (it != it_end) {
|
|
476 it_start = it;
|
|
477 group_head = it_start;
|
|
478 /* 1文字目がグループ化されていれば、グループの先頭文字にする */
|
|
479 xstart = it->x;
|
|
480 if (it->flag & TextGlyph::Group) {
|
|
481 TextGlyphStream::iterator jit;
|
|
482 for (jit = it; jit != it_end; jit++) {
|
|
483 if (xstart > jit->x) xstart = jit->x;
|
|
484 if (!(jit->flag & TextGlyph::Group)) break;
|
|
485 }
|
|
486 }
|
|
487 xend = it->x + width-tab_width;
|
|
488 }
|
|
489 }
|
|
490 return;
|
|
491 }
|
|
492
|
|
493 namespace XKFont {
|
|
494
|
|
495 HorizLayout::HorizLayout(const char* fontname, int size) {
|
|
496 font = new Font(fontname, size);
|
|
497 pimpl = new ::TextHorizLayout(font);
|
|
498 }
|
|
499
|
|
500 HorizLayout::~HorizLayout() {
|
|
501 delete pimpl;
|
|
502 delete font;
|
|
503 }
|
|
504
|
|
505 void HorizLayout::Layout(TextStream& stream, TextGlyphStream& glyph, vector<int>& lineheights, int width) {
|
|
506 pimpl->Layout(stream, glyph, lineheights, width);
|
|
507 };
|
|
508 TextGlyphStream HorizLayout::Layout(const char* str, int width, int r, int gc, int b) {
|
|
509 TextStream s;
|
|
510 s.SetColor(r,gc,b);
|
|
511 s.Add(str);
|
|
512 TextGlyphStream g;
|
|
513 vector<int> h;
|
|
514 Layout(s, g, h, width);
|
|
515 return g;
|
|
516 }
|
|
517
|
|
518 };
|
|
519
|
|
520 int TextGlyphStream::width(void) {
|
|
521 if (empty()) return 0;
|
|
522 iterator it;
|
|
523 int xmax = 0;
|
|
524 for (it=begin(); it!=end(); it++) {
|
|
525 int x = it->x + it->glyph->advance.x;
|
|
526 if (x > xmax) xmax = x;
|
|
527 }
|
|
528 return xmax + 1;
|
|
529 }
|
|
530
|
|
531 int TextGlyphStream::height(void) {
|
|
532 if (empty()) return 0;
|
|
533 iterator it;
|
|
534 int ymax = 0;
|
|
535 it = end();
|
|
536 while(1) {
|
|
537 it--;
|
|
538 int y = it->y + it->glyph->bitmap.rows - it->glyph->bitmap_top;
|
|
539 if (ymax < y) ymax = y;
|
|
540 if (it == begin()) break;
|
|
541 if (it->flag & TextGlyph::LineEnd) {
|
|
542 if (!(it->flag & TextGlyph::PhraseEnd)) break; // PhraseEnd は最後の文字
|
|
543 }
|
|
544 }
|
|
545 return ymax + 1;
|
|
546 }
|
|
547
|