LibreOffice Module vcl (master)  1
OpenTypeFeatureDefinitonList.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  */
10 
12 #include <font/OpenTypeFeatureStrings.hrc>
13 
14 #include <rtl/character.hxx>
15 
16 #include <algorithm>
17 
18 namespace vcl
19 {
20 namespace font
21 {
23 
25 {
26  m_aFeatureDefinition.assign({
27  { featureCode("aalt"), STR_FONT_FEATURE_ID_AALT },
28  { featureCode("afrc"), STR_FONT_FEATURE_ID_AFRC },
29  { featureCode("alig"), STR_FONT_FEATURE_ID_ALIG },
30  { featureCode("c2pc"), STR_FONT_FEATURE_ID_C2PC },
31  { featureCode("c2sc"), STR_FONT_FEATURE_ID_C2SC },
32  { featureCode("calt"), STR_FONT_FEATURE_ID_CALT },
33  { featureCode("case"), STR_FONT_FEATURE_ID_CASE },
34  { featureCode("clig"), STR_FONT_FEATURE_ID_CLIG },
35  { featureCode("cpct"), STR_FONT_FEATURE_ID_CPCT },
36  { featureCode("cpsp"), STR_FONT_FEATURE_ID_CPSP },
37  { featureCode("cswh"), STR_FONT_FEATURE_ID_CSWH },
38  { featureCode("dcap"), STR_FONT_FEATURE_ID_DCAP },
39  { featureCode("dlig"), STR_FONT_FEATURE_ID_DLIG },
40  { featureCode("dnom"), STR_FONT_FEATURE_ID_DNOM },
41  { featureCode("dpng"), STR_FONT_FEATURE_ID_DPNG },
42  { featureCode("expt"), STR_FONT_FEATURE_ID_EXPT },
43  { featureCode("falt"), STR_FONT_FEATURE_ID_FALT },
44  { featureCode("frac"), STR_FONT_FEATURE_ID_FRAC,
45  std::vector<FeatureParameter>{ { 1, STR_FONT_FEATURE_ID_FRAC_PARAM_1 },
46  { 2, STR_FONT_FEATURE_ID_FRAC_PARAM_2 } } },
47  { featureCode("fwid"), STR_FONT_FEATURE_ID_FWID },
48  { featureCode("halt"), STR_FONT_FEATURE_ID_HALT },
49  { featureCode("hist"), STR_FONT_FEATURE_ID_HIST },
50  { featureCode("hkna"), STR_FONT_FEATURE_ID_HKNA },
51  { featureCode("hlig"), STR_FONT_FEATURE_ID_HLIG },
52  { featureCode("hngl"), STR_FONT_FEATURE_ID_HNGL },
53  { featureCode("hojo"), STR_FONT_FEATURE_ID_HOJO },
54  { featureCode("hwid"), STR_FONT_FEATURE_ID_HWID },
55  { featureCode("ital"), STR_FONT_FEATURE_ID_ITAL },
56  { featureCode("jalt"), STR_FONT_FEATURE_ID_JALT },
57  { featureCode("jp78"), STR_FONT_FEATURE_ID_JP78 },
58  { featureCode("jp83"), STR_FONT_FEATURE_ID_JP83 },
59  { featureCode("jp90"), STR_FONT_FEATURE_ID_JP90 },
60  { featureCode("jp04"), STR_FONT_FEATURE_ID_JP04 },
61  { featureCode("kern"), STR_FONT_FEATURE_ID_KERN },
62  { featureCode("lfbd"), STR_FONT_FEATURE_ID_LFBD },
63  { featureCode("liga"), STR_FONT_FEATURE_ID_LIGA },
64  { featureCode("lnum"), STR_FONT_FEATURE_ID_LNUM },
65  { featureCode("mgrk"), STR_FONT_FEATURE_ID_MGRK },
66  { featureCode("nalt"), STR_FONT_FEATURE_ID_NALT },
67  { featureCode("nlck"), STR_FONT_FEATURE_ID_NLCK },
68  { featureCode("numr"), STR_FONT_FEATURE_ID_NUMR },
69  { featureCode("onum"), STR_FONT_FEATURE_ID_ONUM },
70  { featureCode("opbd"), STR_FONT_FEATURE_ID_OPBD },
71  { featureCode("ordn"), STR_FONT_FEATURE_ID_ORDN },
72  { featureCode("ornm"), STR_FONT_FEATURE_ID_ORNM },
73  { featureCode("palt"), STR_FONT_FEATURE_ID_PALT },
74  { featureCode("pcap"), STR_FONT_FEATURE_ID_PCAP },
75  { featureCode("pkna"), STR_FONT_FEATURE_ID_PKNA },
76  { featureCode("pnum"), STR_FONT_FEATURE_ID_PNUM },
77  { featureCode("pwid"), STR_FONT_FEATURE_ID_PWID },
78  { featureCode("qwid"), STR_FONT_FEATURE_ID_QWID },
79  { featureCode("rtbd"), STR_FONT_FEATURE_ID_RTBD },
80  { featureCode("ruby"), STR_FONT_FEATURE_ID_RUBY },
81  { featureCode("salt"), STR_FONT_FEATURE_ID_SALT },
82  { featureCode("sinf"), STR_FONT_FEATURE_ID_SINF },
83  { featureCode("smcp"), STR_FONT_FEATURE_ID_SMCP },
84  { featureCode("smpl"), STR_FONT_FEATURE_ID_SMPL },
85  { featureCode("subs"), STR_FONT_FEATURE_ID_SUBS },
86  { featureCode("sups"), STR_FONT_FEATURE_ID_SUPS },
87  { featureCode("swsh"), STR_FONT_FEATURE_ID_SWSH },
88  { featureCode("titl"), STR_FONT_FEATURE_ID_TITL },
89  { featureCode("tnam"), STR_FONT_FEATURE_ID_TNAM },
90  { featureCode("tnum"), STR_FONT_FEATURE_ID_TNUM },
91  { featureCode("trad"), STR_FONT_FEATURE_ID_TRAD },
92  { featureCode("twid"), STR_FONT_FEATURE_ID_TWID },
93  { featureCode("unic"), STR_FONT_FEATURE_ID_UNIC },
94  { featureCode("valt"), STR_FONT_FEATURE_ID_VALT },
95  { featureCode("vhal"), STR_FONT_FEATURE_ID_VHAL },
96  { featureCode("vkna"), STR_FONT_FEATURE_ID_VKNA },
97  { featureCode("vkrn"), STR_FONT_FEATURE_ID_VKRN },
98  { featureCode("vpal"), STR_FONT_FEATURE_ID_VPAL },
99  { featureCode("vrt2"), STR_FONT_FEATURE_ID_VRT2 },
100  { featureCode("vrtr"), STR_FONT_FEATURE_ID_VRTR },
101  { featureCode("zero"), STR_FONT_FEATURE_ID_ZERO },
102  });
103 
104  for (size_t i = 0; i < m_aFeatureDefinition.size(); ++i)
105  {
106  m_aCodeToIndex.emplace(m_aFeatureDefinition[i].getCode(), i);
107  }
108 
109  m_aRequiredFeatures.assign({
110  featureCode("abvf"), featureCode("abvm"), featureCode("abvs"), featureCode("akhn"),
111  featureCode("blwf"), featureCode("blwm"), featureCode("blws"), featureCode("ccmp"),
112  featureCode("cfar"), featureCode("cjct"), featureCode("curs"), featureCode("dist"),
113  featureCode("dtls"), featureCode("fin2"), featureCode("fin3"), featureCode("fina"),
114  featureCode("flac"), featureCode("half"), featureCode("haln"), featureCode("init"),
115  featureCode("isol"), featureCode("ljmo"), featureCode("locl"), featureCode("ltra"),
116  featureCode("ltrm"), featureCode("mark"), featureCode("med2"), featureCode("medi"),
117  featureCode("mkmk"), featureCode("mset"), featureCode("nukt"), featureCode("pref"),
118  featureCode("pres"), featureCode("pstf"), featureCode("psts"), featureCode("rand"),
119  featureCode("rclt"), featureCode("rkrf"), featureCode("rlig"), featureCode("rphf"),
120  featureCode("rtla"), featureCode("rtlm"), featureCode("rvrn"), featureCode("size"),
121  featureCode("ssty"), featureCode("stch"), featureCode("tjmo"), featureCode("vatu"),
122  featureCode("vert"), featureCode("vjmo"),
123  });
124 }
125 
126 namespace
127 {
128 bool isCharacterVariantCode(sal_uInt32 nFeatureCode)
129 {
130  return sal_Char((sal_uInt32(nFeatureCode) >> 24) & 0xFF) == 'c'
131  && sal_Char((sal_uInt32(nFeatureCode) >> 16) & 0xFF) == 'v';
132 }
133 
134 bool isStylisticSetCode(sal_uInt32 nFeatureCode)
135 {
136  return sal_Char((sal_uInt32(nFeatureCode) >> 24) & 0xFF) == 's'
137  && sal_Char((sal_uInt32(nFeatureCode) >> 16) & 0xFF) == 's';
138 }
139 
140 OUString getNumericLowerPart(sal_uInt32 nFeatureCode)
141 {
142  sal_Char cChar1((sal_uInt32(nFeatureCode) >> 8) & 0xFF);
143  sal_Char cChar2((sal_uInt32(nFeatureCode) >> 0) & 0xFF);
144 
145  if (rtl::isAsciiDigit(static_cast<unsigned char>(cChar1))
146  && rtl::isAsciiDigit(static_cast<unsigned char>(cChar2)))
147  {
148  return OUStringChar(cChar1) + OUStringChar(cChar2);
149  }
150  return OUString();
151 }
152 
153 } // end anonymous namespace
154 
156 {
157  return isCharacterVariantCode(nFeatureCode) || isStylisticSetCode(nFeatureCode);
158 }
159 
162 {
163  FeatureDefinition aFeatureDefinition;
164  OUString sNumericPart = getNumericLowerPart(nFeatureCode);
165  if (!sNumericPart.isEmpty())
166  {
167  if (isCharacterVariantCode(nFeatureCode))
168  aFeatureDefinition = { nFeatureCode, STR_FONT_FEATURE_ID_CVXX, sNumericPart };
169  else if (isStylisticSetCode(nFeatureCode))
170  aFeatureDefinition = { nFeatureCode, STR_FONT_FEATURE_ID_SSXX, sNumericPart };
171  }
172  return aFeatureDefinition;
173 }
174 
176 {
177  if (isSpecialFeatureCode(nFeatureCode))
178  {
179  return handleSpecialFeatureCode(nFeatureCode);
180  }
181 
182  if (m_aCodeToIndex.find(nFeatureCode) != m_aCodeToIndex.end())
183  {
184  size_t nIndex = m_aCodeToIndex.at(nFeatureCode);
185  return m_aFeatureDefinition[nIndex];
186  }
187  return FeatureDefinition();
188 }
189 
191 {
192  return std::find(m_aRequiredFeatures.begin(), m_aRequiredFeatures.end(), nFeatureCode)
193  != m_aRequiredFeatures.end();
194 }
195 
196 } // end font namespace
197 } // end vcl namespace
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< sal_uInt32, size_t > m_aCodeToIndex
constexpr uint32_t featureCode(const char sFeature[4])
Definition: Feature.hxx:21
FeatureDefinition getDefinition(sal_uInt32 nFeatureCode)
char sal_Char
int i
static FeatureDefinition handleSpecialFeatureCode(sal_uInt32 nFeatureCode)