1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <FontFeaturesDialog.hxx>
#include <vcl/font/FeatureParser.hxx>
#include <svtools/colorcfg.hxx>
#include <svx/dialmgr.hxx>
#include <unordered_set>
using namespace css;
namespace cui
{
FontFeaturesDialog::FontFeaturesDialog(weld::Window* pParent, OUString const& rFontName)
: GenericDialogController(pParent, "cui/ui/fontfeaturesdialog.ui", "FontFeaturesDialog")
, m_sFontName(rFontName)
, m_xContentWindow(m_xBuilder->weld_scrolled_window("contentWindow"))
, m_xContentGrid(m_xBuilder->weld_container("contentGrid"))
, m_xPreviewWindow(new weld::CustomWeld(*m_xBuilder, "preview", m_aPreviewWindow))
{
svtools::ColorConfig aColorConfig;
Color aFillColor(aColorConfig.GetColorValue(svtools::DOCCOLOR).nColor);
m_aPreviewWindow.SetBackColor(aFillColor);
initialize();
}
FontFeaturesDialog::~FontFeaturesDialog() {}
static void makeEnumComboBox(weld::ComboBox& rNameBox,
vcl::font::FeatureDefinition const& rFeatureDefinition)
{
for (vcl::font::FeatureParameter const& rParameter : rFeatureDefinition.getEnumParameters())
rNameBox.append_text(rParameter.getDescription());
}
void FontFeaturesDialog::initialize()
{
ScopedVclPtrInstance<VirtualDevice> aVDev(*Application::GetDefaultDevice(),
DeviceFormat::DEFAULT, DeviceFormat::DEFAULT);
aVDev->SetOutputSizePixel(Size(10, 10));
vcl::Font aFont = aVDev->GetFont();
aFont.SetFamilyName(m_sFontName);
aVDev->SetFont(aFont);
std::vector<vcl::font::Feature> rFontFeatures;
if (!aVDev->GetFontFeatures(rFontFeatures))
return;
std::unordered_set<sal_uInt32> aDoneFeatures;
std::vector<vcl::font::Feature> rFilteredFontFeatures;
for (vcl::font::Feature const& rFontFeature : rFontFeatures)
{
sal_uInt32 nFontFeatureCode = rFontFeature.m_aID.m_aFeatureCode;
if (aDoneFeatures.find(nFontFeatureCode) != aDoneFeatures.end())
continue;
aDoneFeatures.insert(nFontFeatureCode);
rFilteredFontFeatures.push_back(rFontFeature);
}
fillGrid(rFilteredFontFeatures);
updateFontPreview();
}
void FontFeaturesDialog::fillGrid(std::vector<vcl::font::Feature> const& rFontFeatures)
{
vcl::font::FeatureParser aParser(m_sFontName);
std::unordered_map<sal_uInt32, sal_uInt32> aExistingFeatures = aParser.getFeaturesMap();
sal_Int32 i = 0;
for (vcl::font::Feature const& rFontFeature : rFontFeatures)
{
sal_uInt32 nFontFeatureCode = rFontFeature.m_aID.m_aFeatureCode;
vcl::font::FeatureDefinition aDefinition;
if (rFontFeature.m_aDefinition)
aDefinition = rFontFeature.m_aDefinition;
if (!aDefinition)
aDefinition = { nFontFeatureCode, nullptr };
m_aFeatureItems.emplace_back(m_xContentGrid.get());
sal_uInt32 nValue = 0;
if (aExistingFeatures.find(nFontFeatureCode) != aExistingFeatures.end())
nValue = aExistingFeatures.at(nFontFeatureCode);
FontFeatureItem& aCurrentItem = m_aFeatureItems.back();
aCurrentItem.m_aFeatureCode = nFontFeatureCode;
sal_Int32 nGridPositionX = (i % 2) * 2;
sal_Int32 nGridPositionY = i / 2;
aCurrentItem.m_xContainer->set_grid_left_attach(nGridPositionX);
aCurrentItem.m_xContainer->set_grid_top_attach(nGridPositionY);
Link<weld::ComboBox&, void> aComboBoxSelectHandler
= LINK(this, FontFeaturesDialog, ComboBoxSelectedHdl);
Link<weld::ToggleButton&, void> aCheckBoxToggleHandler
= LINK(this, FontFeaturesDialog, CheckBoxToggledHdl);
if (aDefinition.getType() == vcl::font::FeatureParameterType::ENUM)
{
aCurrentItem.m_xText->set_label(aDefinition.getDescription());
aCurrentItem.m_xText->show();
makeEnumComboBox(*aCurrentItem.m_xCombo, aDefinition);
aCurrentItem.m_xCombo->set_active(nValue);
aCurrentItem.m_xCombo->connect_changed(aComboBoxSelectHandler);
aCurrentItem.m_xCombo->show();
}
else
{
aCurrentItem.m_xCheck->set_active(nValue > 0);
aCurrentItem.m_xCheck->set_label(aDefinition.getDescription());
aCurrentItem.m_xCheck->connect_toggled(aCheckBoxToggleHandler);
aCurrentItem.m_xCheck->show();
}
i++;
}
Size aSize(m_xContentWindow->get_preferred_size());
Size aMaxSize(std::min<int>(aSize.Width(), m_xContentGrid->get_approximate_digit_width() * 100),
std::min<int>(aSize.Height(), m_xContentGrid->get_text_height() * 20));
m_xContentWindow->set_size_request(aMaxSize.Width(), aMaxSize.Height());
}
void FontFeaturesDialog::updateFontPreview()
{
vcl::Font rPreviewFont = m_aPreviewWindow.GetFont();
vcl::Font rPreviewFontCJK = m_aPreviewWindow.GetCJKFont();
vcl::Font rPreviewFontCTL = m_aPreviewWindow.GetCTLFont();
OUString sNewFontName = createFontNameWithFeatures();
rPreviewFont.SetFamilyName(sNewFontName);
rPreviewFontCJK.SetFamilyName(sNewFontName);
rPreviewFontCTL.SetFamilyName(sNewFontName);
m_aPreviewWindow.SetFont(rPreviewFont, rPreviewFontCJK, rPreviewFontCTL);
}
IMPL_LINK_NOARG(FontFeaturesDialog, CheckBoxToggledHdl, weld::ToggleButton&, void)
{
updateFontPreview();
}
IMPL_LINK_NOARG(FontFeaturesDialog, ComboBoxSelectedHdl, weld::ComboBox&, void)
{
updateFontPreview();
}
OUString FontFeaturesDialog::createFontNameWithFeatures()
{
OUString sResultFontName;
OUStringBuffer sNameSuffix;
bool bFirst = true;
for (FontFeatureItem& rItem : m_aFeatureItems)
{
if (rItem.m_xCheck->get_visible())
{
if (rItem.m_xCheck->get_active())
{
if (!bFirst)
sNameSuffix.append(OUString(vcl::font::FeatureSeparator));
else
bFirst = false;
sNameSuffix.append(vcl::font::featureCodeAsString(rItem.m_aFeatureCode));
}
}
else if (rItem.m_xCombo->get_visible() && rItem.m_xText->get_visible())
{
int nSelection = rItem.m_xCombo->get_active();
if (nSelection > 0)
{
if (!bFirst)
sNameSuffix.append(OUString(vcl::font::FeatureSeparator));
else
bFirst = false;
sNameSuffix.append(vcl::font::featureCodeAsString(rItem.m_aFeatureCode));
sNameSuffix.append("=");
sNameSuffix.append(OUString::number(nSelection));
}
}
}
sResultFontName = vcl::font::trimFontNameFeatures(m_sFontName);
if (!sNameSuffix.isEmpty())
sResultFontName += OUString(vcl::font::FeaturePrefix) + sNameSuffix.makeStringAndClear();
return sResultFontName;
}
short FontFeaturesDialog::run()
{
short nResult = GenericDialogController::run();
if (nResult == RET_OK)
{
m_sResultFontName = createFontNameWithFeatures();
}
return nResult;
}
} // end svx namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|