| 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 | #pragma once |
| 27 | |
| 28 | #include "CSSValueKeywords.h" |
| 29 | #include "FontSelectionAlgorithm.h" |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | inline bool isCSS21Weight(FontSelectionValue weight) |
| 34 | { |
| 35 | return weight == FontSelectionValue(100) |
| 36 | || weight == FontSelectionValue(200) |
| 37 | || weight == FontSelectionValue(300) |
| 38 | || weight == FontSelectionValue(400) |
| 39 | || weight == FontSelectionValue(500) |
| 40 | || weight == FontSelectionValue(600) |
| 41 | || weight == FontSelectionValue(700) |
| 42 | || weight == FontSelectionValue(800) |
| 43 | || weight == FontSelectionValue(900); |
| 44 | } |
| 45 | |
| 46 | inline bool isCSS21Weight(int weight) |
| 47 | { |
| 48 | return !((weight % 100) || weight < 100 || weight > 900); |
| 49 | } |
| 50 | |
| 51 | inline Optional<CSSValueID> fontWeightKeyword(FontSelectionValue weight) |
| 52 | { |
| 53 | if (weight == normalWeightValue()) |
| 54 | return CSSValueNormal; |
| 55 | if (weight == boldWeightValue()) |
| 56 | return CSSValueBold; |
| 57 | return WTF::nullopt; |
| 58 | } |
| 59 | |
| 60 | inline Optional<FontSelectionValue> fontWeightValue(CSSValueID value) |
| 61 | { |
| 62 | switch (value) { |
| 63 | case CSSValueNormal: |
| 64 | return normalWeightValue(); |
| 65 | case CSSValueBold: |
| 66 | case CSSValueBolder: |
| 67 | return boldWeightValue(); |
| 68 | case CSSValueLighter: |
| 69 | return lightWeightValue(); |
| 70 | default: |
| 71 | return WTF::nullopt; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | inline Optional<CSSValueID> fontStretchKeyword(FontSelectionValue stretch) |
| 76 | { |
| 77 | if (stretch == ultraCondensedStretchValue()) |
| 78 | return CSSValueUltraCondensed; |
| 79 | if (stretch == extraCondensedStretchValue()) |
| 80 | return CSSValueExtraCondensed; |
| 81 | if (stretch == condensedStretchValue()) |
| 82 | return CSSValueCondensed; |
| 83 | if (stretch == semiCondensedStretchValue()) |
| 84 | return CSSValueSemiCondensed; |
| 85 | if (stretch == normalStretchValue()) |
| 86 | return CSSValueNormal; |
| 87 | if (stretch == semiExpandedStretchValue()) |
| 88 | return CSSValueSemiExpanded; |
| 89 | if (stretch == expandedStretchValue()) |
| 90 | return CSSValueExpanded; |
| 91 | if (stretch == extraExpandedStretchValue()) |
| 92 | return CSSValueExtraExpanded; |
| 93 | if (stretch == ultraExpandedStretchValue()) |
| 94 | return CSSValueUltraExpanded; |
| 95 | return WTF::nullopt; |
| 96 | } |
| 97 | |
| 98 | inline Optional<FontSelectionValue> fontStretchValue(CSSValueID value) |
| 99 | { |
| 100 | switch (value) { |
| 101 | case CSSValueUltraCondensed: |
| 102 | return ultraCondensedStretchValue(); |
| 103 | case CSSValueExtraCondensed: |
| 104 | return extraCondensedStretchValue(); |
| 105 | case CSSValueCondensed: |
| 106 | return condensedStretchValue(); |
| 107 | case CSSValueSemiCondensed: |
| 108 | return semiCondensedStretchValue(); |
| 109 | case CSSValueNormal: |
| 110 | return normalStretchValue(); |
| 111 | case CSSValueSemiExpanded: |
| 112 | return semiExpandedStretchValue(); |
| 113 | case CSSValueExpanded: |
| 114 | return expandedStretchValue(); |
| 115 | case CSSValueExtraExpanded: |
| 116 | return extraExpandedStretchValue(); |
| 117 | case CSSValueUltraExpanded: |
| 118 | return ultraExpandedStretchValue(); |
| 119 | default: |
| 120 | return WTF::nullopt; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | inline Optional<CSSValueID> fontStyleKeyword(Optional<FontSelectionValue> style, FontStyleAxis axis) |
| 125 | { |
| 126 | if (!style || style.value() == normalItalicValue()) |
| 127 | return CSSValueNormal; |
| 128 | if (style.value() == italicValue()) |
| 129 | return axis == FontStyleAxis::ital ? CSSValueItalic : CSSValueOblique; |
| 130 | return WTF::nullopt; |
| 131 | } |
| 132 | |
| 133 | } |
| 134 | |