Debian Codegraph @

src/af/xap/xp/xap_Draw_Symbol.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA.
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "ut_debugmsg.h"
25 #include "ut_assert.h"
26 #include "ut_types.h"
27 #include "ut_string.h"
28 #include "gr_Graphics.h"
29 #include "gr_CharWidths.h"
30 #include "gr_Painter.h"
31
32 #include "xap_Draw_Symbol.h"
33
34
35 static UT_UCSChar MaxWidthChar = 0;
36 static UT_UCSChar MaxHeightChar = 0;
37
38 XAP_Draw_Symbol::XAP_Draw_Symbol(GR_Graphics * gc)
39 : XAP_Preview(gc),
40 m_areagc(NULL),
41 m_pFont(NULL),
42 m_drawWidth(0),
43 m_drawHeight(0),
44 m_drawareaWidth(0),
45 m_drawareaHeight(0),
46 m_start_base(0),
47 m_start_nb_char(0),
48 m_CurrentSymbol(UCS_SPACE),
49 m_PreviousSymbol(UCS_SPACE),
50 m_vCharSet(256),
51 m_stFont("Symbol")
52 {
53 }
54
55 XAP_Draw_Symbol::~XAP_Draw_Symbol(void)
56 {
57 }
58
59 void XAP_Draw_Symbol::setWindowSize( UT_uint32 width, UT_uint32 height)
60 {
61 m_drawWidth = m_gc->tlu(width);
62 m_drawHeight = m_gc->tlu(height);
63 }
64
65 void XAP_Draw_Symbol::setAreaGc( GR_Graphics * gc)
66 {
67 m_areagc = gc;
68 }
69
70 void XAP_Draw_Symbol::setAreaSize( UT_uint32 width, UT_uint32 height)
71 {
72 m_drawareaWidth = m_areagc->tlu(width);
73 m_drawareaHeight = m_areagc->tlu(height);
74 setFontStringarea();
75 }
76
77 void XAP_Draw_Symbol::setFontString( void )
78 {
79 setFontToGC(m_gc, m_drawWidth / 32, m_drawHeight / 7);
80 }
81
82 void XAP_Draw_Symbol::setFontToGC(GR_Graphics *p_gc, UT_uint32 MaxWidthAllowable, UT_uint32 MaxHeightAllowable)
83 {
84 UT_ASSERT(p_gc);
85 UT_ASSERT(MaxWidthAllowable);
86 UT_ASSERT(MaxHeightAllowable);
87
88 GR_Font* font = NULL;
89
90
91 // Estimate Pointsize
92 UT_sint32 PointSize = 32;
93 UT_sint32 LastPointSize = -1;
94 UT_sint32 UpperPointSize = -1;
95 UT_sint32 LowerPointSize = 1;
96
97 // We try to find the Pointsize which is a bit smaller than
98 // MaxHeightAllowable and MaxWidthAllowable (W+HAllowable)
99 // we archieve that in two steps:
100 // 1. step: find a Pointsize which is bigger as W+HAllowable)
101 // 2. step: Iterate by taking halfe PointSize steps until we find
102 // the fitting one
103 while (1)
104 {
105 char temp[10];
106
107 sprintf(temp, "%ipt", PointSize);
108 font = p_gc->findFont(m_stFont.c_str(),
109 "normal", "",
110 "normal", "",
111 temp,
112 NULL);
113 /* findFont does a fuzzy match. If the font found doesn't
114 * have the same family name that we asked for, we retrieve
115 * the new name and we use it
116 */
117 if (font->getFamily())
118 m_stFont = font->getFamily();
119
120 p_gc->setFont(font);
121
122 p_gc->getCoverage(m_vCharSet);
123
124 if (PointSize == LastPointSize)
125 break;
126
127 if (!MaxWidthChar)
128 {
129 UT_uint32 MaxWidth = 0;
130 UT_uint32 MaxHeight = 0;
131
132 // this can take very long with large fonts
133 for (UT_sint32 i = m_start_base; i < m_vCharSet.size(); i += 2)
134 {
135 UT_UCSChar base = static_cast<UT_UCSChar>(m_vCharSet[i]);
136 UT_sint32 nb_chars = m_vCharSet[i + 1];
137
138 for (UT_sint32 j = m_start_base == i? m_start_nb_char: 0; j < nb_chars; j++)
139 {
140 UT_UCSChar c = base + j;
141 UT_uint32 width, height;
142 p_gc->getMaxCharacterDimension(&c, 1, width, height);
143 if (width > MaxWidth)
144 {
145 MaxWidth = width;
146 MaxWidthChar = c;
147 }
148
149 if (height > MaxHeight)
150 {
151 MaxHeight = height;
152 MaxHeightChar = c;
153 }
154 }
155 }
156 }
157
158
159 UT_uint32 width, height;
160 p_gc->getMaxCharacterDimension(&MaxWidthChar, 1, width, height);
161 UT_sint32 xdiff_temp = MaxWidthAllowable - width;
162 p_gc->getMaxCharacterDimension(&MaxHeightChar, 1, width, height);
163 UT_sint32 ydiff_temp = MaxHeightAllowable - height;
164
165 if (UpperPointSize < 0)
166 {
167 if (xdiff_temp < 0 || ydiff_temp < 0)
168 UpperPointSize = PointSize;
169 else if (PointSize > 72)
170 UpperPointSize = PointSize = 72;
171 else
172 PointSize *= 2;
173 }
174 if (UpperPointSize > 0)
175 { LastPointSize = PointSize;
176 if (xdiff_temp < 0 || ydiff_temp < 0)
177 UpperPointSize = PointSize;
178 else
179 LowerPointSize = PointSize;
180
181 PointSize = LowerPointSize + (UpperPointSize - LowerPointSize) / 2;
182 }
183 if (!PointSize)
184 {
185 UT_ASSERT (PointSize);
186 break;
187 }
188
189 }
190
191 }
192
193 const char* XAP_Draw_Symbol::getSelectedFont()
194 {
195 return m_stFont.c_str();
196 }
197
198 void XAP_Draw_Symbol::setSelectedFont(const char *font)
199 {
200 m_stFont = font;
201 m_CurrentSymbol = m_PreviousSymbol = UCS_SPACE;
202 m_start_base = 0;
203 m_start_nb_char = 0;
204 MaxWidthChar = 0;
205 MaxHeightChar = 0;
206
207 setFontString();
208 setFontStringarea();
209 }
210
211 void XAP_Draw_Symbol::setFontStringarea(void)
212 {
213 setFontToGC(m_areagc, m_drawareaWidth, m_drawareaHeight);
214 }
215
216 void XAP_Draw_Symbol::setRow (UT_uint32 row)
217 {
218 UT_uint32 chars_needed = row * 32;
219 UT_uint32 chars = 0;
220 for (UT_sint32 i = 0; i < m_vCharSet.size(); i += 2)
221 {
222 UT_sint32 numb = m_vCharSet[i + 1];
223 if (chars + numb > chars_needed)
224 {
225 m_start_base = i;
226 m_start_nb_char = chars_needed - chars;
227 break;
228 }
229 chars += numb;
230 }
231 draw ();
232 }
233
234 UT_uint32 XAP_Draw_Symbol::getSymbolRows (void)
235 {
236 UT_uint32 chars = 0;
237 for (UT_sint32 i = m_start_base; i < m_vCharSet.size(); i += 2)
238 {
239 chars += m_vCharSet[i + 1];
240 }
241
242 UT_uint32 rows = chars / 32 + (chars % 32? 1: 0);
243 return rows;
244 }
245
246 void XAP_Draw_Symbol::draw(const UT_Rect *clip)
247 {
248 UT_UNUSED(clip);
249 UT_ASSERT(m_gc);
250 UT_uint32 wwidth, wheight, /*yoff, xoff,*/ x, y;
251 UT_sint32 i;
252
253 GR_Painter painter(m_gc);
254
255 wwidth = m_drawWidth;
256 wheight = m_drawHeight;
257 UT_uint32 tmpw = wwidth / 32;
258 UT_uint32 tmph = wheight / 7;
259 // yoff = wheight / (2 * 7);
260 // xoff = wwidth / (2 * 32);
261 painter.clearArea(0, 0, wwidth, wheight);
262 int pos = 0;
263
264 for (i = m_start_base; i < m_vCharSet.size(); i += 2)
265 {
266 UT_UCSChar base = static_cast<UT_UCSChar>(m_vCharSet[i]);
267 UT_sint32 nb_chars = m_vCharSet[i + 1];
268
269 for (UT_UCSChar j = base + (m_start_base == i? m_start_nb_char: 0); j < base + nb_chars; ++j)
270 {
271 UT_sint32 w = m_gc->measureUnRemappedChar(j);
272
273 if(w != GR_CW_ABSENT)
274 {
275 x = (pos % 32) * tmpw + (tmpw - w) / 2;
276 y = pos / 32 * tmph;
277
278 painter.drawChars(&j, 0, 1, x, y);
279 }
280
281 ++pos;
282
283 if(pos > 32 * 7)
284 break;
285 }
286
287 if(pos > 32 * 7)
288 break;
289 }
290
291 y = 0;
292 for(i = 0; i <= 7; i++)
293 {
294 painter.drawLine(0, y, wwidth - m_areagc->tlu(1), y);
295 y += tmph;
296 }
297
298 x = 0;
299 for(i = 0; i <= 32; i++)
300 {
301 painter.drawLine(x, 0, x, wheight - m_areagc->tlu(1));
302 x += tmpw;
303 }
304 }
305
306 UT_UCSChar XAP_Draw_Symbol::calcSymbolFromCoords(UT_uint32 ix, UT_uint32 iy)
307 {
308 UT_sint32 index,count;
309 index = iy * 32 + ix;
310 count = 0;
311 UT_DEBUGMSG(("calcSymbolFromCoords(x = [%u], y = [%u]) =", ix, iy));
312
313 for (UT_sint32 i = m_start_base; i < m_vCharSet.size(); i += 2)
314 {
315 count = m_vCharSet[i + 1];
316 if (i == m_start_base && count > m_start_nb_char)
317 index += m_start_nb_char;
318 if (count > index)
319 {
320 UT_DEBUGMSG((" %u\n", static_cast<UT_uint32>(m_vCharSet[i] + index - count + m_vCharSet[i + 1])));
321 return static_cast<UT_UCSChar>(m_vCharSet[i] + index);
322 }
323 index -= count;
324 }
325
326 return static_cast<UT_UCSChar>(0);
327 }
328
329 UT_UCSChar XAP_Draw_Symbol::calcSymbol(UT_uint32 x, UT_uint32 y)
330 {
331 UT_uint32 width = m_drawWidth;
332 UT_uint32 height = m_drawHeight;
333 UT_uint32 ix;
334 UT_uint32 iy;
335
336 if (x > width || y > height)
337 return static_cast<UT_UCSChar>(0);
338
339 iy = m_gc->tlu(y) / (height / 7);
340 ix = m_gc->tlu(x) / (width / 32);
341 return calcSymbolFromCoords(ix, iy);
342 }
343
344 void XAP_Draw_Symbol::calculatePosition(UT_UCSChar c, UT_uint32 &x, UT_uint32 &y)
345 {
346 UT_uint32 index = 0;
347
348 for (UT_sint32 i = m_start_base; i < m_vCharSet.size(); i += 2)
349 {
350 UT_uint32 base = static_cast<UT_uint32>(m_vCharSet[i]);
351 UT_uint32 size = static_cast<UT_uint32>(m_vCharSet[i + 1]);
352
353 if (base + size > c)
354 {
355 index += c - (i == m_start_base? base + m_start_nb_char: base);
356 break;
357 }
358 else
359 index += size - (m_start_base == i? m_start_nb_char: 0);
360 }
361
362 x = index % 32;
363 y = index / 32;
364 UT_DEBUGMSG(("[%d] -> (%d, %d)\n", c, x, y));
365 }
366
367 void XAP_Draw_Symbol::setCurrent(UT_UCSChar c)
368 {
369 m_PreviousSymbol = m_CurrentSymbol;
370 m_CurrentSymbol = c;
371 drawarea(m_CurrentSymbol, m_PreviousSymbol);
372 }
373
374 void XAP_Draw_Symbol::drawarea(UT_UCSChar c, UT_UCSChar p)
375 //
376 // This function displays the symbol c into the Selected Area.
377 // It also highlights the selected symbol in the Symbol Table.
378 //
379 {
380 UT_ASSERT(m_areagc);
381 UT_uint32 wwidth,wheight,x,y,cx,cy,px,py /*,swidth,sheight*/;
382 UT_uint32 /*cx1,cy1,*/px1,py1;
383
384 GR_Painter areaPainter(m_areagc);
385 GR_Painter painter(m_gc);
386
387 wwidth = m_drawareaWidth;
388 wheight = m_drawareaHeight;
389
390 // Center the character
391 // Note: That's bogus. measureString will give us the horizontal advance of "c",
392 // but we need the bounding box of "c" instead. To get it, we should use with FreeType face->bbox,
393 // in windows we should use the (FIXME: find the right name, it was something as getCharABC(...)
394 UT_uint32 h1 = 0;
395 UT_sint32 w1 = m_areagc->measureUnRemappedChar(c, &h1);
396
397 areaPainter.clearArea(0, 0, wwidth, wheight);
398
399 if(w1 != GR_CW_ABSENT)
400 {
401 x = (m_drawareaWidth - w1) / 2;
402 y = (m_drawareaHeight - h1) / 2;
403 areaPainter.drawChars(&c, 0, 1, x, y);
404 }
405
406 //
407 // Calculate the cordinates of the current and previous symbol
408 // along with the widths of the appropriate boxes.
409 // swidth = m_drawWidth;
410 // sheight = m_drawHeight;
411 UT_uint32 tmpw = m_drawWidth / 32;
412 UT_uint32 tmph = m_drawHeight / 7;
413
414 calculatePosition(c, cx, cy);
415 UT_sint32 wc = m_gc->measureUnRemappedChar(c);
416
417 cx *= tmpw;
418 cy *= tmph;
419
420 // cx1 = cx + tmpw;
421 // cy1 = cy + tmph;
422
423 calculatePosition(p, px, py);
424 UT_sint32 wp = m_gc->measureUnRemappedChar(p);
425
426 px *= tmpw;
427 py *= tmph;
428
429 px1 = px + tmpw;
430 py1 = py + tmph;
431
432 // Redraw the Previous Character in black on White
433 painter.clearArea(px + m_areagc->tlu(1), py + m_areagc->tlu(1), tmpw - m_areagc->tlu(1), tmph - m_areagc->tlu(1));
434
435 if(wp != GR_CW_ABSENT)
436 {
437 painter.drawChars(&p, 0, 1, px + (tmpw - wp) / 2, py);
438 }
439
440 // Redraw only the white box boundaries
441 // you do not notice the missing of the
442 // selected box
443 painter.drawLine(px, py, px1, py);
444 painter.drawLine(px, py1, px1, py1);
445 painter.drawLine(px, py, px, py1);
446 painter.drawLine(px1, py, px1, py1);
447
448 // Redraw the Current Character in black on Blue
449 UT_RGBColor colour(128, 128, 192);
450 painter.fillRect(colour, cx + m_areagc->tlu(1), cy + m_areagc->tlu(1), tmpw - m_areagc->tlu(1), tmph - m_areagc->tlu(1));
451 if(wc != GR_CW_ABSENT)
452 {
453 painter.drawChars(&c, 0, 1, cx + (tmpw - wc) / 2, cy);
454 }
455 }
456
457 void XAP_Draw_Symbol::onLeftButtonDown(UT_sint32 x, UT_sint32 y)
458 {
459 setCurrent(calcSymbol(x, y));
460 }