Changeset View
Changeset View
Standalone View
Standalone View
kcms/fonts/kxftconfig.cpp
| Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Line(s) | |||||
| 112 | 112 | | |||
| 113 | static time_t getTimeStamp(const QString &item) | 113 | static time_t getTimeStamp(const QString &item) | ||
| 114 | { | 114 | { | ||
| 115 | KDE_struct_stat info; | 115 | KDE_struct_stat info; | ||
| 116 | 116 | | |||
| 117 | return !item.isNull() && 0 == KDE_lstat(QFile::encodeName(item), &info) ? info.st_mtime : 0; | 117 | return !item.isNull() && 0 == KDE_lstat(QFile::encodeName(item), &info) ? info.st_mtime : 0; | ||
| 118 | } | 118 | } | ||
| 119 | 119 | | |||
| 120 | // | | |||
| 121 | // Obtain location of config file to use. | | |||
| 122 | QString getConfigFile() | | |||
| 123 | { | | |||
| 124 | FcStrList *list = FcConfigGetConfigFiles(FcConfigGetCurrent()); | | |||
| 125 | QStringList files; | | |||
| 126 | FcChar8 *file; | | |||
| 127 | QString home(dirSyntax(QDir::homePath())); | | |||
| 128 | | ||||
| 129 | while ((file = FcStrListNext(list))) { | | |||
| 130 | QString f((const char *)file); | | |||
| 131 | | ||||
| 132 | if (fExists(f) && 0 == f.indexOf(home)) { | | |||
| 133 | files.append(f); | | |||
| 134 | } | | |||
| 135 | } | | |||
| 136 | FcStrListDone(list); | | |||
| 137 | | ||||
| 138 | // | | |||
| 139 | // Go through list of files, looking for the preferred one... | | |||
| 140 | if (files.count()) { | | |||
| 141 | QStringList::const_iterator it(files.begin()), | | |||
| 142 | end(files.end()); | | |||
| 143 | | ||||
| 144 | for (; it != end; ++it) | | |||
| 145 | if (-1 != (*it).indexOf(QRegExp("/\\.?fonts\\.conf$"))) { | | |||
| 146 | return *it; | | |||
| 147 | } | | |||
| 148 | return files.front(); // Just return the 1st one... | | |||
| 149 | } else { // Hmmm... no known files? | | |||
| 150 | if (FcGetVersion() >= 21000) { | | |||
| 151 | QString targetPath(KGlobal::dirs()->localxdgconfdir() + "fontconfig"); | | |||
| 152 | QDir target(targetPath); | | |||
| 153 | if (!target.exists()) { | | |||
| 154 | target.mkpath(targetPath); | | |||
| 155 | } | | |||
| 156 | return targetPath + "/fonts.conf"; | | |||
| 157 | } else { | | |||
| 158 | return home + "/.fonts.conf"; | | |||
| 159 | } | | |||
| 160 | } | | |||
| 161 | } | | |||
| 162 | | ||||
| 163 | static QString getEntry(QDomElement element, const char *type, unsigned int numAttributes, ...) | 120 | static QString getEntry(QDomElement element, const char *type, unsigned int numAttributes, ...) | ||
| 164 | { | 121 | { | ||
| 165 | if (numAttributes == element.attributes().length()) { | 122 | if (numAttributes == element.attributes().length()) { | ||
| 166 | va_list args; | 123 | va_list args; | ||
| 167 | unsigned int arg; | 124 | unsigned int arg; | ||
| 168 | bool ok = true; | 125 | bool ok = true; | ||
| 169 | 126 | | |||
| 170 | va_start(args, numAttributes); | 127 | va_start(args, numAttributes); | ||
| ▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Line(s) | 189 | { | |||
| 233 | qDebug() << "Using fontconfig file:" << m_file; | 190 | qDebug() << "Using fontconfig file:" << m_file; | ||
| 234 | reset(); | 191 | reset(); | ||
| 235 | } | 192 | } | ||
| 236 | 193 | | |||
| 237 | KXftConfig::~KXftConfig() | 194 | KXftConfig::~KXftConfig() | ||
| 238 | { | 195 | { | ||
| 239 | } | 196 | } | ||
| 240 | 197 | | |||
| 198 | // | ||||
| 199 | // Obtain location of config file to use. | ||||
| 200 | QString KXftConfig::getConfigFile() | ||||
| 201 | { | ||||
| 202 | FcStrList *list = FcConfigGetConfigFiles(FcConfigGetCurrent()); | ||||
| 203 | QStringList localFiles; | ||||
| 204 | FcChar8 *file; | ||||
| 205 | QString home(dirSyntax(QDir::homePath())); | ||||
| 206 | | ||||
| 207 | m_globalFiles.clear(); | ||||
| 208 | | ||||
| 209 | while ((file = FcStrListNext(list))) { | ||||
| 210 | QString f((const char *)file); | ||||
| 211 | | ||||
| 212 | if (fExists(f) && 0 == f.indexOf(home)) { | ||||
| 213 | localFiles.append(f); | ||||
| 214 | } else { | ||||
| 215 | m_globalFiles.append(f); | ||||
| 216 | } | ||||
| 217 | } | ||||
| 218 | FcStrListDone(list); | ||||
| 219 | | ||||
| 220 | // | ||||
| 221 | // Go through list of localFiles, looking for the preferred one... | ||||
| 222 | if (localFiles.count()) { | ||||
| 223 | QStringList::const_iterator it(localFiles.begin()), | ||||
| 224 | end(localFiles.end()); | ||||
| 225 | | ||||
| 226 | for (; it != end; ++it) | ||||
| 227 | if (-1 != (*it).indexOf(QRegExp("/\\.?fonts\\.conf$"))) { | ||||
| 228 | return *it; | ||||
| 229 | } | ||||
| 230 | return localFiles.front(); // Just return the 1st one... | ||||
| 231 | } else { // Hmmm... no known localFiles? | ||||
| 232 | if (FcGetVersion() >= 21000) { | ||||
| 233 | QString targetPath(KGlobal::dirs()->localxdgconfdir() + "fontconfig"); | ||||
| 234 | QDir target(targetPath); | ||||
| 235 | if (!target.exists()) { | ||||
| 236 | target.mkpath(targetPath); | ||||
| 237 | } | ||||
| 238 | return targetPath + "/fonts.conf"; | ||||
| 239 | } else { | ||||
| 240 | return home + "/.fonts.conf"; | ||||
| 241 | } | ||||
| 242 | } | ||||
| 243 | } | ||||
| 244 | | ||||
| 241 | bool KXftConfig::reset() | 245 | bool KXftConfig::reset() | ||
| 242 | { | 246 | { | ||
| 243 | bool ok = false; | 247 | bool ok = false; | ||
| 244 | 248 | | |||
| 245 | m_madeChanges = false; | 249 | m_madeChanges = false; | ||
| 246 | m_hint.reset(); | 250 | m_hint.reset(); | ||
| 247 | m_hinting.reset(); | 251 | m_hinting.reset(); | ||
| 248 | m_excludeRange.reset(); | 252 | m_excludeRange.reset(); | ||
| 249 | m_excludePixelRange.reset(); | 253 | m_excludePixelRange.reset(); | ||
| 250 | m_subPixel.reset(); | 254 | m_subPixel.reset(); | ||
| 251 | m_antiAliasing.reset(); | 255 | m_antiAliasing.reset(); | ||
| 256 | m_antiAliasingHasLocalConfig = false; | ||||
| 252 | 257 | | |||
| 253 | QFile f(m_file); | 258 | QStringList::const_iterator it(m_globalFiles.begin()), | ||
| 254 | 259 | end(m_globalFiles.end()); | |||
| 255 | if (f.open(QIODevice::ReadOnly)) { | 260 | for (; it != end; ++it) { | ||
| 256 | m_time = getTimeStamp(m_file); | 261 | ok |= parseConfigFile(*it); | ||
| 257 | ok = true; | | |||
| 258 | m_doc.clear(); | | |||
| 259 | | ||||
| 260 | if (m_doc.setContent(&f)) { | | |||
| 261 | readContents(); | | |||
| 262 | } | | |||
| 263 | f.close(); | | |||
| 264 | } else { | | |||
| 265 | ok = !fExists(m_file) && dWritable(getDir(m_file)); | | |||
| 266 | } | 262 | } | ||
| 267 | 263 | | |||
| 268 | if (m_doc.documentElement().isNull()) { | 264 | AntiAliasing globalAntialiasing = m_antiAliasing; | ||
| 269 | m_doc.appendChild(m_doc.createElement("fontconfig")); | 265 | m_antiAliasing.state = AntiAliasing::NotSet; | ||
| 270 | } | | |||
| 271 | 266 | | |||
| 272 | if (ok) { | 267 | ok |= parseConfigFile(m_file); | ||
| 273 | // | | |||
| 274 | // Check exclude range values - i.e. size and pixel size... | | |||
| 275 | // If "size" range is set, ensure "pixelsize" matches... | | |||
| 276 | if (!equal(0, m_excludeRange.from) || !equal(0, m_excludeRange.to)) { | | |||
| 277 | double pFrom = (double)point2Pixel(m_excludeRange.from), | | |||
| 278 | pTo = (double)point2Pixel(m_excludeRange.to); | | |||
| 279 | 268 | | |||
| 280 | if (!equal(pFrom, m_excludePixelRange.from) || !equal(pTo, m_excludePixelRange.to)) { | 269 | if (m_antiAliasing.state == AntiAliasing::NotSet) { | ||
| 281 | m_excludePixelRange.from = pFrom; | 270 | m_antiAliasing = globalAntialiasing; | ||
| 282 | m_excludePixelRange.to = pTo; | 271 | } else { | ||
| 283 | m_madeChanges = true; | 272 | m_antiAliasingHasLocalConfig = true; | ||
| 284 | } | | |||
| 285 | } else if (!equal(0, m_excludePixelRange.from) || !equal(0, m_excludePixelRange.to)) { | | |||
| 286 | // "pixelsize" set, but not "size" !!! | | |||
| 287 | m_excludeRange.from = (int)pixel2Point(m_excludePixelRange.from); | | |||
| 288 | m_excludeRange.to = (int)pixel2Point(m_excludePixelRange.to); | | |||
| 289 | m_madeChanges = true; | | |||
| 290 | } | | |||
| 291 | } | 273 | } | ||
| 292 | 274 | | |||
| 293 | return ok; | 275 | return ok; | ||
| 294 | } | 276 | } | ||
| 295 | 277 | | |||
| 296 | bool KXftConfig::apply() | 278 | bool KXftConfig::apply() | ||
| 297 | { | 279 | { | ||
| 298 | bool ok = true; | 280 | bool ok = true; | ||
| ▲ Show 20 Lines • Show All 214 Lines • ▼ Show 20 Line(s) | 494 | case Hint::None: | |||
| 513 | return "hintnone"; | 495 | return "hintnone"; | ||
| 514 | case Hint::Slight: | 496 | case Hint::Slight: | ||
| 515 | return "hintslight"; | 497 | return "hintslight"; | ||
| 516 | case Hint::Full: | 498 | case Hint::Full: | ||
| 517 | return "hintfull"; | 499 | return "hintfull"; | ||
| 518 | } | 500 | } | ||
| 519 | } | 501 | } | ||
| 520 | 502 | | |||
| 503 | bool KXftConfig::parseConfigFile(const QString& filename) | ||||
| 504 | { | ||||
| 505 | bool ok = false; | ||||
| 506 | | ||||
| 507 | QFile f(filename); | ||||
| 508 | | ||||
| 509 | if (f.open(QIODevice::ReadOnly)) { | ||||
| 510 | m_time = getTimeStamp(filename); | ||||
| 511 | ok = true; | ||||
| 512 | m_doc.clear(); | ||||
| 513 | | ||||
| 514 | if (m_doc.setContent(&f)) { | ||||
| 515 | readContents(); | ||||
| 516 | } | ||||
| 517 | f.close(); | ||||
| 518 | } else { | ||||
| 519 | ok = !fExists(filename) && dWritable(getDir(filename)); | ||||
| 520 | } | ||||
| 521 | | ||||
| 522 | if (m_doc.documentElement().isNull()) { | ||||
| 523 | m_doc.appendChild(m_doc.createElement("fontconfig")); | ||||
| 524 | } | ||||
| 525 | | ||||
| 526 | if (ok) { | ||||
| 527 | // | ||||
| 528 | // Check exclude range values - i.e. size and pixel size... | ||||
| 529 | // If "size" range is set, ensure "pixelsize" matches... | ||||
| 530 | if (!equal(0, m_excludeRange.from) || !equal(0, m_excludeRange.to)) { | ||||
| 531 | double pFrom = (double)point2Pixel(m_excludeRange.from), | ||||
| 532 | pTo = (double)point2Pixel(m_excludeRange.to); | ||||
| 533 | | ||||
| 534 | if (!equal(pFrom, m_excludePixelRange.from) || !equal(pTo, m_excludePixelRange.to)) { | ||||
| 535 | m_excludePixelRange.from = pFrom; | ||||
| 536 | m_excludePixelRange.to = pTo; | ||||
| 537 | m_madeChanges = true; | ||||
| 538 | } | ||||
| 539 | } else if (!equal(0, m_excludePixelRange.from) || !equal(0, m_excludePixelRange.to)) { | ||||
| 540 | // "pixelsize" set, but not "size" !!! | ||||
| 541 | m_excludeRange.from = (int)pixel2Point(m_excludePixelRange.from); | ||||
| 542 | m_excludeRange.to = (int)pixel2Point(m_excludePixelRange.to); | ||||
| 543 | m_madeChanges = true; | ||||
| 544 | } | ||||
| 545 | } | ||||
| 546 | | ||||
| 547 | return ok; | ||||
| 548 | } | ||||
| 549 | | ||||
| 521 | void KXftConfig::readContents() | 550 | void KXftConfig::readContents() | ||
| 522 | { | 551 | { | ||
| 523 | QDomNode n = m_doc.documentElement().firstChild(); | 552 | QDomNode n = m_doc.documentElement().firstChild(); | ||
| 524 | 553 | | |||
| 525 | while (!n.isNull()) { | 554 | while (!n.isNull()) { | ||
| 526 | QDomElement e = n.toElement(); | 555 | QDomElement e = n.toElement(); | ||
| 527 | 556 | | |||
| 528 | if (!e.isNull()) { | 557 | if (!e.isNull()) { | ||
| 529 | if ("match" == e.tagName()) { | 558 | if ("match" == e.tagName()) { | ||
| 530 | QString str; | 559 | QString str; | ||
| 531 | 560 | | |||
| 532 | switch (e.childNodes().count()) { | 561 | int childNodesCount = e.childNodes().count(); | ||
| 562 | QDomNode en = e.firstChild(); | ||||
| 563 | while (!en.isNull()) { | ||||
| 564 | if (en.isComment()) { | ||||
| 565 | childNodesCount--; | ||||
| 566 | } | ||||
| 567 | en = en.nextSibling(); | ||||
| 568 | } | ||||
| 569 | | ||||
| 570 | switch (childNodesCount) { | ||||
| 533 | case 1: | 571 | case 1: | ||
| 534 | if ("font" == e.attribute("target")) { | 572 | if ("font" == e.attribute("target") || "pattern" == e.attribute("target")) { | ||
| 535 | QDomElement ene = e.firstChild().toElement(); | 573 | QDomNode en = e.firstChild(); | ||
| 574 | while (!en.isNull()) { | ||||
| 575 | QDomElement ene = en.toElement(); | ||||
| 576 | | ||||
| 577 | while (ene.isComment()) { | ||||
| 578 | ene = ene.nextSiblingElement(); | ||||
| 579 | } | ||||
| 536 | 580 | | |||
| 537 | if (!ene.isNull() && "edit" == ene.tagName()) { | 581 | if (!ene.isNull() && "edit" == ene.tagName()) { | ||
| 538 | if (!(str = getEntry(ene, "const", 2, "name", "rgba", "mode", | 582 | if (!(str = getEntry(ene, "const", 2, "name", "rgba", "mode", | ||
| 539 | "assign")).isNull()) { | 583 | "assign")).isNull() | ||
| 584 | || (m_subPixel.type == SubPixel::NotSet && !(str = getEntry(ene, "const", 2, "name", "rgba", "mode", | ||||
| 585 | "append")).isNull())) { | ||||
| 540 | m_subPixel.node = n; | 586 | m_subPixel.node = n; | ||
| 541 | m_subPixel.type = strToType(str.toLatin1()); | 587 | m_subPixel.type = strToType(str.toLatin1()); | ||
| 542 | } else if (!(str = getEntry(ene, "const", 2, "name", "hintstyle", "mode", | 588 | } else if (!(str = getEntry(ene, "const", 2, "name", "hintstyle", "mode", | ||
| 543 | "assign")).isNull()) { | 589 | "assign")).isNull() | ||
| 590 | || (m_hint.style == Hint::NotSet && !(str = getEntry(ene, "const", 2, "name", "hintstyle", "mode", | ||||
| 591 | "append")).isNull())) { | ||||
| 544 | m_hint.node = n; | 592 | m_hint.node = n; | ||
| 545 | m_hint.style = strToStyle(str.toLatin1()); | 593 | m_hint.style = strToStyle(str.toLatin1()); | ||
| 546 | } else if (!(str = getEntry(ene, "bool", 2, "name", "hinting", "mode", | 594 | } else if (!(str = getEntry(ene, "bool", 2, "name", "hinting", "mode", | ||
| 547 | "assign")).isNull()) { | 595 | "assign")).isNull()) { | ||
| 548 | m_hinting.node = n; | 596 | m_hinting.node = n; | ||
| 549 | m_hinting.set = str.toLower() != "false"; | 597 | m_hinting.set = str.toLower() != "false"; | ||
| 550 | } else if (!(str = getEntry(ene, "bool", 2, "name", "antialias", "mode", | 598 | } else if (!(str = getEntry(ene, "bool", 2, "name", "antialias", "mode", | ||
| 551 | "assign")).isNull()) { | 599 | "assign")).isNull() | ||
| 600 | || (m_antiAliasing.state == AntiAliasing::NotSet && !(str = getEntry(ene, "bool", 2, "name", "antialias", "mode", | ||||
| 601 | "append")).isNull())) { | ||||
| 552 | m_antiAliasing.node = n; | 602 | m_antiAliasing.node = n; | ||
| 553 | m_antiAliasing.state = str.toLower() != "false" ? | 603 | m_antiAliasing.state = str.toLower() != "false" ? | ||
| 554 | AntiAliasing::Enabled : AntiAliasing::Disabled; | 604 | AntiAliasing::Enabled : AntiAliasing::Disabled; | ||
| 555 | } | 605 | } | ||
| 556 | } | 606 | } | ||
| 607 | | ||||
| 608 | en = en.nextSibling(); | ||||
| 609 | } | ||||
| 557 | } | 610 | } | ||
| 558 | break; | 611 | break; | ||
| 559 | case 3: // CPD: Is target "font" or "pattern" ???? | 612 | case 3: // CPD: Is target "font" or "pattern" ???? | ||
| 560 | if ("font" == e.attribute("target")) { | 613 | if ("font" == e.attribute("target")) { | ||
| 561 | bool foundFalse = false; | 614 | bool foundFalse = false; | ||
| 562 | QDomNode en = e.firstChild(); | 615 | QDomNode en = e.firstChild(); | ||
| 616 | while (en.isComment()) { | ||||
| 617 | en = en.nextSibling(); | ||||
| 618 | } | ||||
| 563 | QString family; | 619 | QString family; | ||
| 564 | double from = -1.0, | 620 | double from = -1.0, | ||
| 565 | to = -1.0, | 621 | to = -1.0, | ||
| 566 | pixelFrom = -1.0, | 622 | pixelFrom = -1.0, | ||
| 567 | pixelTo = -1.0; | 623 | pixelTo = -1.0; | ||
| 568 | 624 | | |||
| 569 | while (!en.isNull()) { | 625 | while (!en.isNull()) { | ||
| 570 | QDomElement ene = en.toElement(); | 626 | QDomElement ene = en.toElement(); | ||
| ▲ Show 20 Lines • Show All 199 Lines • ▼ Show 20 Line(s) | 789 | } else { | |||
| 770 | if (!m_antiAliasing.node.isNull()) { | 826 | if (!m_antiAliasing.node.isNull()) { | ||
| 771 | m_doc.documentElement().removeChild(range.node); | 827 | m_doc.documentElement().removeChild(range.node); | ||
| 772 | } | 828 | } | ||
| 773 | m_doc.documentElement().appendChild(matchNode); | 829 | m_doc.documentElement().appendChild(matchNode); | ||
| 774 | range.node = matchNode; | 830 | range.node = matchNode; | ||
| 775 | } | 831 | } | ||
| 776 | } | 832 | } | ||
| 777 | 833 | | |||
| 834 | bool KXftConfig::antiAliasingHasLocalConfig() const | ||||
| 835 | { | ||||
| 836 | return m_antiAliasingHasLocalConfig; | ||||
| 837 | } | ||||
| 838 | | ||||
| 778 | KXftConfig::AntiAliasing::State KXftConfig::getAntiAliasing() const | 839 | KXftConfig::AntiAliasing::State KXftConfig::getAntiAliasing() const | ||
| 779 | { | 840 | { | ||
| 780 | return m_antiAliasing.state; | 841 | return m_antiAliasing.state; | ||
| 781 | } | 842 | } | ||
| 782 | 843 | | |||
| 783 | void KXftConfig::setAntiAliasing(AntiAliasing::State state) | 844 | void KXftConfig::setAntiAliasing(AntiAliasing::State state) | ||
| 784 | { | 845 | { | ||
| 785 | if (state != m_antiAliasing.state) { | 846 | if (state != m_antiAliasing.state) { | ||
| ▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines | |||||