annotate font/font_layout.cc @ 15:38226842bac8

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