Debian Codegraph @

src/af/xap/xp/xap_Dlg_FontChooser.cpp

raw
1 /* AbiSource Application Framework
2 * Copyright (C) 1998 AbiSource, Inc.
3 * Copyright (C) 2009-2016 Hubert Figuiere
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301 USA.
19 */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "ut_string.h"
25 #include "ut_debugmsg.h"
26 #include "xap_Dlg_FontChooser.h"
27 #include "gr_Graphics.h"
28 #include "gr_Painter.h"
29
30 /*****************************************************************/
31
32 // your typographer's standard nonsense latin font phrase
33 #define PREVIEW_ENTRY_DEFAULT_STRING "Lorem ipsum dolor sit amet, consectetaur adipisicing..."
34
35 XAP_Dialog_FontChooser::XAP_Dialog_FontChooser(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id)
36 : XAP_Dialog_NonPersistent(pDlgFactory,id, "interface/dialogfont")
37 {
38 m_answer = a_CANCEL;
39 m_pGraphics = NULL;
40 m_pFontPreview = NULL;
41 m_bUnderline = false;
42 m_bOverline = false;
43 m_bStrikeout = false;
44 m_bTopline = false;
45 m_bBottomline = false;
46 m_bHidden = false;
47 m_bSuperScript = false;
48 m_bSubScript = false;
49 m_bChangedFontFamily = false;
50 m_bChangedTextTransform = false;
51 m_bChangedFontSize = false;
52 m_bChangedFontWeight = false;
53 m_bChangedFontStyle = false;
54 m_bChangedColor = false;
55 m_bChangedBGColor = false;
56 m_bChangedUnderline = false;
57 m_bChangedOverline = false;
58 m_bChangedStrikeOut = false;
59 m_bChangedTopline = false;
60 m_bChangedBottomline = false;
61 m_bChangedHidden = false;
62 m_bChangedSuperScript = false;
63 m_bChangedSubScript = false;
64
65 UT_UCS4_cloneString_char (&m_drawString, PREVIEW_ENTRY_DEFAULT_STRING);
66 }
67
68 XAP_Dialog_FontChooser::~XAP_Dialog_FontChooser(void)
69 {
70 FREEP(m_drawString);
71 DELETEP(m_pFontPreview);
72 }
73
74 void XAP_Dialog_FontChooser::setDrawString(const UT_UCSChar * str)
75 {
76 FREEP(m_drawString);
77 UT_sint32 len = UT_UCS4_strlen(str);
78 if(len <= 0)
79 {
80 UT_UCS4_cloneString_char (&m_drawString, PREVIEW_ENTRY_DEFAULT_STRING);
81 }
82 else
83 {
84 UT_UCS4_cloneString(&m_drawString, str);
85 }
86 }
87
88 void XAP_Dialog_FontChooser::setGraphicsContext(GR_Graphics * pGraphics)
89 {
90 m_pGraphics = pGraphics;
91 }
92
93 void XAP_Dialog_FontChooser::_createFontPreviewFromGC(GR_Graphics * gc,
94 UT_uint32 width,
95 UT_uint32 height)
96 {
97 UT_ASSERT(gc);
98
99 m_pFontPreview = new XAP_Preview_FontPreview(gc,m_sColorBackground.c_str());
100 UT_return_if_fail(m_pFontPreview);
101
102 m_pFontPreview->setWindowSize(width, height);
103 m_pFontPreview->setVecProperties( & m_mapProps);
104 }
105
106 void XAP_Dialog_FontChooser::addOrReplaceVecProp(const std::string & sProp, const std::string & sVal)
107 {
108 m_mapProps[sProp] = sVal;
109 }
110
111 /*!
112 * This method updates the drawing area in the dialog.
113 */
114 void XAP_Dialog_FontChooser::event_previewExposed(const UT_UCSChar * pszChars)
115 {
116 UT_UCSChar * pszNew = NULL;
117 if(!pszChars || UT_UCS4_strlen(pszChars) <= 0)
118 {
119 //FREEP(pszChars); // we should not g_free it here
120 UT_UCS4_cloneString_char (&pszNew, PREVIEW_ENTRY_DEFAULT_STRING);
121 if (!pszNew)
122 return;
123
124 m_pFontPreview->setDrawString(pszNew);
125 }
126 else
127 {
128 m_pFontPreview->setDrawString(pszChars);
129 }
130 m_pFontPreview->draw();
131
132 FREEP(pszNew);
133 }
134
135
136 /*!
137 * This method clears the drawing area in the dialog.
138 */
139 void XAP_Dialog_FontChooser::event_previewClear(void)
140 {
141 m_pFontPreview->clearScreen();
142 }
143
144 /*!
145 * This method returns a std::string of the value associated with the
146 * the property sProp.
147 */
148 std::string XAP_Dialog_FontChooser::getVal(const std::string & sProp) const
149 {
150 PropMap::const_iterator iter = m_mapProps.find(sProp);
151 if(iter == m_mapProps.end()) {
152 return "";
153 }
154 return iter->second;
155 }
156
157 /*!
158 * This method sets all the local properties from a vector of string
159 * Property - Value pairs.
160 * This method wipes out all the old values and clears all the bools
161 * assciated with them.
162 */
163 void XAP_Dialog_FontChooser::setAllPropsFromVec(const std::vector<std::string> & vProps)
164 {
165 UT_sint32 remCount = vProps.size();
166 if(remCount <= 0)
167 return;
168 // BAD BAD, we have wrong count
169 UT_ASSERT_HARMLESS(remCount % 2 == 0);
170 if(remCount % 2) {
171 remCount--;
172 }
173 m_mapProps.clear();
174 UT_sint32 i = 0;
175 for(i=0; i< remCount; i+=2)
176 {
177 m_mapProps.insert(std::make_pair(vProps[i],
178 vProps[i+1]));
179 }
180 //
181 // Do the Text decorations
182 //
183 const std::string sDecor = getVal("text-decoration");
184 m_bUnderline = (NULL != strstr(sDecor.c_str(),"underline"));
185 m_bOverline = (NULL != strstr(sDecor.c_str(),"overline"));
186 m_bStrikeout = (NULL != strstr(sDecor.c_str(),"line-through"));
187 m_bTopline = (NULL != strstr(sDecor.c_str(),"topline"));
188 m_bBottomline = (NULL != strstr(sDecor.c_str(),"bottomline"));
189
190 const std::string sDisplay = getVal("display");
191 m_bHidden = sDisplay != "none";
192
193 const std::string sPosition = getVal("text-position");
194 m_bSuperScript = sPosition == "superscript";
195 m_bSubScript = sPosition == "subscript";
196 }
197
198 void XAP_Dialog_FontChooser::setFontFamily(const std::string& sFontFamily)
199 {
200 m_sFontFamily = sFontFamily;
201 addOrReplaceVecProp("font-family",sFontFamily);
202 }
203
204 void XAP_Dialog_FontChooser::setTextTransform(const std::string& sTextTransform)
205 {
206 m_sTextTransform = sTextTransform;
207 addOrReplaceVecProp("text-transform",sTextTransform);
208 }
209
210 void XAP_Dialog_FontChooser::setFontSize(const std::string& sFontSize)
211 {
212 m_sFontSize = sFontSize;
213 addOrReplaceVecProp("font-size",sFontSize);
214 }
215
216 void XAP_Dialog_FontChooser::setFontWeight(const std::string& sFontWeight)
217 {
218 m_sFontWeight = sFontWeight;
219 addOrReplaceVecProp("font-weight",sFontWeight);
220 }
221
222 void XAP_Dialog_FontChooser::setFontStyle(const std::string& sFontStyle)
223 {
224 m_sFontStyle = sFontStyle;
225 addOrReplaceVecProp("font-style",sFontStyle);
226 }
227
228 void XAP_Dialog_FontChooser::setColor(const std::string& sColor)
229 {
230 m_sColor = sColor;
231 addOrReplaceVecProp("color",sColor);
232 }
233
234 void XAP_Dialog_FontChooser::setBGColor(const std::string& sBGColor)
235 {
236 m_sBGColor = sBGColor;
237 addOrReplaceVecProp("bgcolor",sBGColor);
238 }
239
240 void XAP_Dialog_FontChooser::setSuperScript(bool bSuperScript)
241 {
242 static char none[] = "superscript";
243 static char empty[] = "";
244
245 if(bSuperScript)
246 {
247 addOrReplaceVecProp("text-position",none);
248 }
249 else
250 {
251 addOrReplaceVecProp("text-position",empty);
252 }
253 m_bSuperScript = bSuperScript;
254 }
255
256 void XAP_Dialog_FontChooser::setSubScript(bool bSubScript)
257 {
258 static char none[] = "subscript";
259 static char empty[] = "";
260
261 if(bSubScript)
262 {
263 addOrReplaceVecProp("text-position",none);
264 }
265 else
266 {
267 addOrReplaceVecProp("text-position",empty);
268 }
269 m_bSubScript = bSubScript;
270 }
271
272 void XAP_Dialog_FontChooser::setHidden(bool bHidden)
273 {
274 static char none[] = "none";
275 static char empty[] = "";
276
277 if(bHidden)
278 {
279 addOrReplaceVecProp("display",none);
280 }
281 else
282 {
283 addOrReplaceVecProp("display",empty);
284 }
285 m_bHidden = bHidden;
286 }
287
288 void XAP_Dialog_FontChooser::setBackGroundColor(const gchar * pBackground)
289 {
290 m_sColorBackground = pBackground;
291 }
292
293 void XAP_Dialog_FontChooser::setFontDecoration(bool bUnderline, bool bOverline, bool bStrikeOut, bool bTopline, bool bBottomline)
294 {
295 m_bUnderline = bUnderline;
296 m_bOverline = bOverline;
297 m_bStrikeout = bStrikeOut;
298 m_bTopline = bTopline;
299 m_bBottomline = bBottomline;
300
301 static gchar s[50];
302 UT_String decors;
303 decors.clear();
304 if(bUnderline)
305 decors += "underline ";
306 if(bStrikeOut)
307 decors += "line-through ";
308 if(bOverline)
309 decors += "overline ";
310 if(bTopline)
311 decors += "topline ";
312 if(bBottomline)
313 decors += "bottomline ";
314 if(!bUnderline && !bStrikeOut && !bOverline && !bTopline && !bBottomline)
315 decors = "none";
316 sprintf(s,"%s",decors.c_str());
317 addOrReplaceVecProp("text-decoration",(const gchar *) s);
318 }
319
320 XAP_Dialog_FontChooser::tAnswer XAP_Dialog_FontChooser::getAnswer(void) const
321 {
322 return m_answer;
323 }
324
325 bool XAP_Dialog_FontChooser::getChangedFontFamily(std::string& szFontFamily) const
326 {
327 std::string fontFamily = getVal("font-family");
328 bool bchanged = didPropChange(m_sFontFamily, fontFamily);
329 bool useVal = (bchanged && !m_bChangedFontFamily);
330 if (useVal)
331 szFontFamily = fontFamily;
332 else
333 szFontFamily = m_sFontFamily;
334 return bchanged;
335 }
336
337 bool XAP_Dialog_FontChooser::getChangedTextTransform(std::string& szTextTransform) const
338 {
339 std::string textTransform = getVal("text-transform");
340 bool bchanged = didPropChange(m_sTextTransform, textTransform);
341 bool useVal = (bchanged && !m_bChangedTextTransform);
342 if (useVal)
343 szTextTransform = textTransform;
344 else
345 szTextTransform = m_sTextTransform;
346 return bchanged;
347 }
348
349 bool XAP_Dialog_FontChooser::getChangedFontSize(std::string& szFontSize) const
350 {
351 std::string fontSize = getVal("font-size");
352 bool bchanged = didPropChange(m_sFontSize, fontSize);
353 bool useVal = (bchanged && !m_bChangedFontSize);
354 if (useVal)
355 szFontSize = fontSize;
356 else
357 szFontSize = m_sFontSize;
358 return bchanged;
359 }
360
361 bool XAP_Dialog_FontChooser::getChangedFontWeight(std::string& szFontWeight) const
362 {
363 std::string fontWeight = getVal("font-weight");
364 bool bchanged = didPropChange(m_sFontWeight, fontWeight);
365 bool useVal = (bchanged && !m_bChangedFontWeight);
366 if (useVal)
367 szFontWeight = fontWeight;
368 else
369 szFontWeight = m_sFontWeight;
370 return bchanged;
371 }
372
373 bool XAP_Dialog_FontChooser::getChangedFontStyle(std::string& szFontStyle) const
374 {
375 std::string fontStyle = getVal("font-style");
376 bool bchanged = didPropChange(m_sFontStyle, fontStyle);
377 bool useVal = (bchanged && !m_bChangedFontStyle);
378 if (useVal)
379 szFontStyle = fontStyle;
380 else
381 szFontStyle = m_sFontStyle;
382 return bchanged;
383 }
384
385 bool XAP_Dialog_FontChooser::getChangedBGColor(std::string& szBGColor) const
386 {
387 std::string bgColor = getVal("bgcolor");
388 bool bchanged = didPropChange(m_sBGColor, bgColor);
389 bool useVal = (bchanged && !m_bChangedBGColor);
390 if (useVal)
391 szBGColor = bgColor;
392 else
393 szBGColor = m_sBGColor;
394 return bchanged;
395 }
396
397
398 bool XAP_Dialog_FontChooser::getChangedColor(std::string& szColor) const
399 {
400 std::string color = getVal("color");
401 bool bchanged = didPropChange(m_sColor, color);
402 bool useVal = (bchanged && !m_bChangedColor);
403 if (useVal)
404 szColor = color;
405 else
406 szColor = m_sColor;
407 return bchanged;
408 }
409
410 /*!
411 * Compare two prop values and gracefully handle the cases of NULL pointers
412 */
413 bool XAP_Dialog_FontChooser::didPropChange(const std::string & v1, const std::string & v2) const
414 {
415 if(v1.empty() && v2.empty())
416 return false;
417 else if(v1.empty() || v2.empty())
418 return true;
419 return v1 != v2;
420 }
421
422 bool XAP_Dialog_FontChooser::getChangedHidden(bool * pbHidden) const
423 {
424 if (pbHidden)
425 *pbHidden = m_bHidden;
426 return m_bChangedHidden;
427 }
428
429 bool XAP_Dialog_FontChooser::getChangedSuperScript(bool * pbSuperScript) const
430 {
431 if (pbSuperScript)
432 *pbSuperScript = m_bSuperScript;
433 return m_bChangedSuperScript;
434 }
435
436 bool XAP_Dialog_FontChooser::getChangedSubScript(bool * pbSubScript) const
437 {
438 if (pbSubScript)
439 *pbSubScript = m_bSubScript;
440 return m_bChangedSubScript;
441 }
442
443 bool XAP_Dialog_FontChooser::getChangedUnderline(bool * pbUnderline) const
444 {
445 if (pbUnderline)
446 *pbUnderline = m_bUnderline;
447 return m_bChangedUnderline;
448 }
449
450 bool XAP_Dialog_FontChooser::getChangedOverline(bool * pbOverline) const
451 {
452 if (pbOverline)
453 *pbOverline = m_bOverline;
454 return m_bChangedOverline;
455 }
456
457 bool XAP_Dialog_FontChooser::getChangedStrikeOut(bool * pbStrikeOut) const
458 {
459 if (pbStrikeOut)
460 *pbStrikeOut = m_bStrikeout;
461 return m_bChangedStrikeOut;
462 }
463
464 bool XAP_Dialog_FontChooser::getChangedTopline(bool * pbTopline) const
465 {
466 if (pbTopline)
467 *pbTopline = m_bTopline;
468 return m_bChangedTopline;
469 }
470
471 bool XAP_Dialog_FontChooser::getChangedBottomline(bool * pbBottomline) const
472 {
473 if (pbBottomline)
474 *pbBottomline = m_bBottomline;
475 return m_bChangedBottomline;
476 }
477
478 /////////////////////////////////////////////////////////////////////////
479
480 XAP_Preview_FontPreview::XAP_Preview_FontPreview(GR_Graphics * gc, const gchar * pszClrBackground)
481 : XAP_Preview(gc),
482 m_pFont(NULL),
483 m_iAscent(0),
484 m_iDescent(0),
485 m_iHeight(0)
486 {
487 if(pszClrBackground != NULL && strcmp(pszClrBackground,"transparent")!=0)
488 UT_parseColor(pszClrBackground,m_clrBackground);
489 else
490 UT_setColor(m_clrBackground,255,255,255);
491
492 }
493
494 XAP_Preview_FontPreview::~XAP_Preview_FontPreview()
495 {
496 }
497
498 void XAP_Preview_FontPreview::setVecProperties( const XAP_Dialog_FontChooser::PropMap * vFontProps)
499 {
500 m_mapProps = vFontProps;
501 }
502
503 /*!
504 * This method returns a pointer to the const char * value associated with the
505 * the property szProp. Stolen from ap_Dialog_Lists.
506 */
507 std::string XAP_Preview_FontPreview::getVal(const std::string & sProp) const
508 {
509 XAP_Dialog_FontChooser::PropMap::const_iterator iter = m_mapProps->find(sProp);
510 if(iter == m_mapProps->end()) {
511 return "";
512 }
513 return iter->second;
514 }
515
516 /*
517 *
518 * Finally draw the characters in the preview.
519 *
520 */
521 void XAP_Preview_FontPreview::draw(const UT_Rect *clip)
522 {
523 UT_UNUSED(clip);
524 //
525 // Get text decorations.
526 //
527 bool isUnder,isOver,isStrike;
528
529 const std::string sDecor = getVal("text-decoration");
530 if(!sDecor.empty())
531 {
532 isUnder = (NULL != strstr(sDecor.c_str(),"underline"));
533 isOver = (NULL != strstr(sDecor.c_str(),"overline"));
534 isStrike = (NULL != strstr(sDecor.c_str(),"line-through"));
535 }
536 else
537 {
538 isUnder = false;
539 isOver = false;
540 isStrike = false;
541 }
542
543 //
544 // Do foreground and background colors.
545 //
546 UT_RGBColor FGcolor(0,0,0);
547 const std::string sFGColor = getVal("color");
548 if(!sFGColor.empty())
549 UT_parseColor(sFGColor.c_str(),FGcolor);
550 UT_RGBColor BGcolor(m_clrBackground);
551 const std::string sBGColor = getVal("bgcolor");
552 if(!sBGColor.empty() && sBGColor != "transparent")
553 UT_parseColor(sBGColor.c_str(),BGcolor);
554 //
555 // Get the font and bold/italic- ness
556 //
557 //GR_Font * pFont;
558 std::string sFamily = getVal("font-family");
559 std::string sStyle = getVal("font-style");
560 std::string sVariant = getVal("font-variant");
561 std::string sStretch = getVal("font-stretch");
562 std::string sSize = getVal("font-size");
563 std::string sWeight = getVal("font-weight");
564
565 if(sFamily.empty())
566 sFamily = "Times New Roman";
567
568 if(sStyle.empty())
569 sStyle = "normal";
570
571 if(sVariant.empty())
572 sVariant = "normal";
573
574 if(sStretch.empty())
575 sStretch = "normal";
576
577 if(sSize.empty())
578 sSize="12pt";
579
580 if(sWeight.empty())
581 sWeight = "normal";
582
583 m_pFont = m_gc->findFont(sFamily.c_str(), sStyle.c_str(),
584 sVariant.c_str(), sWeight.c_str(),
585 sStretch.c_str(), sSize.c_str(),
586 NULL);
587
588 UT_ASSERT_HARMLESS(m_pFont);
589 if(!m_pFont)
590 {
591 clearScreen();
592 return;
593 }
594
595 m_gc->setFont(m_pFont);
596
597 m_iAscent = m_gc->getFontAscent(m_pFont);
598 m_iDescent = m_gc->getFontDescent(m_pFont);
599 m_iHeight = m_gc->getFontHeight(m_pFont);
600
601 //
602 // Clear the screen!
603 //
604 clearScreen();
605 //
606 // Calculate the draw coordinates position
607 //
608 UT_sint32 iWinWidth = m_gc->tlu(getWindowWidth());
609 UT_sint32 iWinHeight = m_gc->tlu(getWindowHeight());
610 UT_sint32 iTop = (iWinHeight - m_iHeight)/2;
611 UT_sint32 len = UT_UCS4_strlen(m_pszChars);
612 UT_sint32 twidth = m_gc->measureString(m_pszChars,0,len,NULL);
613 UT_sint32 iLeft = (iWinWidth - twidth)/2;
614 //
615 // Fill the background color
616 //
617 GR_Painter painter(m_gc);
618
619 if(!sBGColor.empty())
620 painter.fillRect(BGcolor,iLeft,iTop,twidth,m_iHeight);
621 //
622 // Do the draw chars at last!
623 //
624 m_gc->setColor(FGcolor);
625 painter.drawChars(m_pszChars, 0, len, iLeft, iTop);
626
627 //
628 // Do the decorations
629 //
630 if(isUnder)
631 {
632 UT_sint32 iDrop = iTop + m_iAscent + m_iDescent/3;
633 painter.drawLine(iLeft,iDrop,iLeft+twidth,iDrop);
634 }
635 if(isOver)
636 {
637 UT_sint32 iDrop = iTop + m_gc->tlu(1) + (UT_MAX(m_gc->tlu(10),m_iAscent) - m_gc->tlu(10))/8;
638 painter.drawLine(iLeft,iDrop,iLeft+twidth,iDrop);
639 }
640 if(isStrike)
641 {
642 UT_sint32 iDrop = iTop + m_iAscent * 2 /3;
643 painter.drawLine(iLeft,iDrop,iLeft+twidth,iDrop);
644 }
645
646 // bad hardcoded color, but this will probably [ <-this assumption is the bad thing :) ] never be different anyway
647 m_gc->setColor(UT_RGBColor(0,0,0));
648 painter.drawLine(0, 0, m_gc->tlu(getWindowWidth()), 0);
649 painter.drawLine(m_gc->tlu(getWindowWidth()) - m_gc->tlu(1), 0, m_gc->tlu(getWindowWidth()) - m_gc->tlu(1),
650 m_gc->tlu(getWindowHeight()));
651 painter.drawLine(m_gc->tlu(getWindowWidth()) - m_gc->tlu(1), m_gc->tlu(getWindowHeight()) - m_gc->tlu(1), 0,
652 m_gc->tlu(getWindowHeight()) - m_gc->tlu(1));
653 painter.drawLine(0, m_gc->tlu(getWindowHeight()) - m_gc->tlu(1), 0, 0);
654 }
655
656 void XAP_Preview_FontPreview::clearScreen(void)
657 {
658 UT_sint32 iWidth = m_gc->tlu(getWindowWidth());
659 UT_sint32 iHeight = m_gc->tlu(getWindowHeight());
660
661 GR_Painter painter(m_gc);
662
663 // clear the whole drawing area, except for the border
664 painter.fillRect(m_clrBackground, 0 + m_gc->tlu(1), 0 + m_gc->tlu(1), iWidth - m_gc->tlu(2), iHeight - m_gc->tlu(2));
665 }