| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "FontSelectionAlgorithm.h" |
| 28 | |
| 29 | namespace WebCore { |
| 30 | |
| 31 | FontSelectionAlgorithm::FontSelectionAlgorithm(FontSelectionRequest request, const Vector<Capabilities>& capabilities, Optional<Capabilities> bounds) |
| 32 | : m_request(request) |
| 33 | , m_capabilities(capabilities) |
| 34 | { |
| 35 | ASSERT(!m_capabilities.isEmpty()); |
| 36 | if (bounds) |
| 37 | m_capabilitiesBounds = bounds.value(); |
| 38 | else { |
| 39 | for (auto& capabilities : m_capabilities) |
| 40 | m_capabilitiesBounds.expand(capabilities); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | auto FontSelectionAlgorithm::stretchDistance(Capabilities capabilities) const -> DistanceResult |
| 45 | { |
| 46 | auto width = capabilities.width; |
| 47 | ASSERT(width.isValid()); |
| 48 | if (width.includes(m_request.width)) |
| 49 | return { FontSelectionValue(), m_request.width }; |
| 50 | |
| 51 | if (m_request.width > normalStretchValue()) { |
| 52 | if (width.minimum > m_request.width) |
| 53 | return { width.minimum - m_request.width, width.minimum }; |
| 54 | ASSERT(width.maximum < m_request.width); |
| 55 | auto threshold = std::max(m_request.width, m_capabilitiesBounds.width.maximum); |
| 56 | return { threshold - width.maximum, width.maximum }; |
| 57 | } |
| 58 | |
| 59 | if (width.maximum < m_request.width) |
| 60 | return { m_request.width - width.maximum, width.maximum }; |
| 61 | ASSERT(width.minimum > m_request.width); |
| 62 | auto threshold = std::min(m_request.width, m_capabilitiesBounds.width.minimum); |
| 63 | return { width.minimum - threshold, width.minimum }; |
| 64 | } |
| 65 | |
| 66 | auto FontSelectionAlgorithm::styleDistance(Capabilities capabilities) const -> DistanceResult |
| 67 | { |
| 68 | auto slope = capabilities.slope; |
| 69 | auto requestSlope = m_request.slope.valueOr(normalItalicValue()); |
| 70 | ASSERT(slope.isValid()); |
| 71 | if (slope.includes(requestSlope)) |
| 72 | return { FontSelectionValue(), requestSlope }; |
| 73 | |
| 74 | if (requestSlope >= italicThreshold()) { |
| 75 | if (slope.minimum > requestSlope) |
| 76 | return { slope.minimum - requestSlope, slope.minimum }; |
| 77 | ASSERT(requestSlope > slope.maximum); |
| 78 | auto threshold = std::max(requestSlope, m_capabilitiesBounds.slope.maximum); |
| 79 | return { threshold - slope.maximum, slope.maximum }; |
| 80 | } |
| 81 | |
| 82 | if (requestSlope >= FontSelectionValue()) { |
| 83 | if (slope.maximum >= FontSelectionValue() && slope.maximum < requestSlope) |
| 84 | return { requestSlope - slope.maximum, slope.maximum }; |
| 85 | if (slope.minimum > requestSlope) |
| 86 | return { slope.minimum, slope.minimum }; |
| 87 | ASSERT(slope.maximum < FontSelectionValue()); |
| 88 | auto threshold = std::max(requestSlope, m_capabilitiesBounds.slope.maximum); |
| 89 | return { threshold - slope.maximum, slope.maximum }; |
| 90 | } |
| 91 | |
| 92 | if (requestSlope > -italicThreshold()) { |
| 93 | if (slope.minimum > requestSlope && slope.minimum <= FontSelectionValue()) |
| 94 | return { slope.minimum - requestSlope, slope.minimum }; |
| 95 | if (slope.maximum < requestSlope) |
| 96 | return { -slope.maximum, slope.maximum }; |
| 97 | ASSERT(slope.minimum > FontSelectionValue()); |
| 98 | auto threshold = std::min(requestSlope, m_capabilitiesBounds.slope.minimum); |
| 99 | return { slope.minimum - threshold, slope.minimum }; |
| 100 | } |
| 101 | |
| 102 | if (slope.maximum < requestSlope) |
| 103 | return { requestSlope - slope.maximum, slope.maximum }; |
| 104 | ASSERT(slope.minimum > requestSlope); |
| 105 | auto threshold = std::min(requestSlope, m_capabilitiesBounds.slope.minimum); |
| 106 | return { slope.minimum - threshold, slope.minimum }; |
| 107 | } |
| 108 | |
| 109 | auto FontSelectionAlgorithm::weightDistance(Capabilities capabilities) const -> DistanceResult |
| 110 | { |
| 111 | auto weight = capabilities.weight; |
| 112 | ASSERT(weight.isValid()); |
| 113 | if (weight.includes(m_request.weight)) |
| 114 | return { FontSelectionValue(), m_request.weight }; |
| 115 | |
| 116 | if (m_request.weight >= lowerWeightSearchThreshold() && m_request.weight <= upperWeightSearchThreshold()) { |
| 117 | if (weight.minimum > m_request.weight && weight.minimum <= upperWeightSearchThreshold()) |
| 118 | return { weight.minimum - m_request.weight, weight.minimum }; |
| 119 | if (weight.maximum < m_request.weight) |
| 120 | return { upperWeightSearchThreshold() - weight.maximum, weight.maximum }; |
| 121 | ASSERT(weight.minimum > upperWeightSearchThreshold()); |
| 122 | auto threshold = std::min(m_request.weight, m_capabilitiesBounds.weight.minimum); |
| 123 | return { weight.minimum - threshold, weight.minimum }; |
| 124 | } |
| 125 | if (m_request.weight < lowerWeightSearchThreshold()) { |
| 126 | if (weight.maximum < m_request.weight) |
| 127 | return { m_request.weight - weight.maximum, weight.maximum }; |
| 128 | ASSERT(weight.minimum > m_request.weight); |
| 129 | auto threshold = std::min(m_request.weight, m_capabilitiesBounds.weight.minimum); |
| 130 | return { weight.minimum - threshold, weight.minimum }; |
| 131 | } |
| 132 | ASSERT(m_request.weight >= upperWeightSearchThreshold()); |
| 133 | if (weight.minimum > m_request.weight) |
| 134 | return { weight.minimum - m_request.weight, weight.minimum }; |
| 135 | ASSERT(weight.maximum < m_request.weight); |
| 136 | auto threshold = std::max(m_request.weight, m_capabilitiesBounds.weight.maximum); |
| 137 | return { threshold - weight.maximum, weight.maximum }; |
| 138 | } |
| 139 | |
| 140 | FontSelectionValue FontSelectionAlgorithm::bestValue(const bool eliminated[], DistanceFunction computeDistance) const |
| 141 | { |
| 142 | Optional<DistanceResult> smallestDistance; |
| 143 | for (size_t i = 0, size = m_capabilities.size(); i < size; ++i) { |
| 144 | if (eliminated[i]) |
| 145 | continue; |
| 146 | auto distanceResult = (this->*computeDistance)(m_capabilities[i]); |
| 147 | if (!smallestDistance || distanceResult.distance < smallestDistance.value().distance) |
| 148 | smallestDistance = distanceResult; |
| 149 | } |
| 150 | return smallestDistance.value().value; |
| 151 | } |
| 152 | |
| 153 | void FontSelectionAlgorithm::filterCapability(bool eliminated[], DistanceFunction computeDistance, CapabilitiesRange inclusionRange) |
| 154 | { |
| 155 | auto value = bestValue(eliminated, computeDistance); |
| 156 | for (size_t i = 0, size = m_capabilities.size(); i < size; ++i) { |
| 157 | eliminated[i] = eliminated[i] |
| 158 | || !(m_capabilities[i].*inclusionRange).includes(value); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | size_t FontSelectionAlgorithm::indexOfBestCapabilities() |
| 163 | { |
| 164 | Vector<bool, 256> eliminated(m_capabilities.size(), false); |
| 165 | filterCapability(eliminated.data(), &FontSelectionAlgorithm::stretchDistance, &Capabilities::width); |
| 166 | filterCapability(eliminated.data(), &FontSelectionAlgorithm::styleDistance, &Capabilities::slope); |
| 167 | filterCapability(eliminated.data(), &FontSelectionAlgorithm::weightDistance, &Capabilities::weight); |
| 168 | return eliminated.find(false); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |