/src/wt/src/Wt/ServerSideFontMetrics.C
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2014 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include "Wt/WException.h" |
8 | | #include "Wt/WFontMetrics.h" |
9 | | #include "Wt/WPaintDevice.h" |
10 | | #include "Wt/WPainter.h" |
11 | | |
12 | | #ifdef WT_HAS_WPDFIMAGE |
13 | | #include "Wt/WPdfImage.h" |
14 | | #endif // WT_HAS_WPDFIMAGE |
15 | | |
16 | | #include "ServerSideFontMetrics.h" |
17 | | |
18 | | namespace Wt { |
19 | | |
20 | | ServerSideFontMetrics::ServerSideFontMetrics() |
21 | 0 | { |
22 | | #ifdef WT_HAS_WPDFIMAGE |
23 | | img_.reset(new WPdfImage(100, 100)); |
24 | | painter_.reset(new WPainter(img_.get())); |
25 | | #endif // WT_HAS_WPDFIMAGE |
26 | 0 | } |
27 | | |
28 | | ServerSideFontMetrics::~ServerSideFontMetrics() |
29 | 0 | { } |
30 | | |
31 | | WFontMetrics ServerSideFontMetrics::fontMetrics(const WFont& font) |
32 | 0 | { |
33 | | #ifdef WT_HAS_WPDFIMAGE |
34 | | painter_->setFont(font); |
35 | | return painter_->device()->fontMetrics(); |
36 | | #else |
37 | 0 | throw WException("ServerSideFontMetrics not available"); |
38 | 0 | #endif // WT_HAS_WPDFIMAGE |
39 | 0 | } |
40 | | |
41 | | WTextItem |
42 | | ServerSideFontMetrics::measureText(const WFont& font, |
43 | | const WString& text, double maxWidth, |
44 | | bool wordWrap) |
45 | 0 | { |
46 | | #ifdef WT_HAS_WPDFIMAGE |
47 | | painter_->setFont(font); |
48 | | WTextItem t = painter_->device()->measureText(text, maxWidth, wordWrap); |
49 | | const double REL_ERROR = 1.02; |
50 | | return WTextItem(t.text(), t.width() * REL_ERROR, |
51 | | t.nextWidth() > 0 ? t.nextWidth() * REL_ERROR : t.nextWidth()); |
52 | | #else |
53 | 0 | throw WException("ServerSideFontMetrics not available"); |
54 | 0 | #endif // WT_HAS_WPDFIMAGE |
55 | 0 | } |
56 | | |
57 | | bool ServerSideFontMetrics::available() |
58 | 0 | { |
59 | | #ifdef WT_HAS_WPDFIMAGE |
60 | | return true; |
61 | | #else |
62 | 0 | return false; |
63 | 0 | #endif // WT_HAS_WPDFIMAGE |
64 | 0 | } |
65 | | |
66 | | } |