View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.css;
16  
17  import static org.htmlunit.javascript.host.css.CSSStyleDeclaration.isLength;
18  
19  import org.htmlunit.util.StringUtils;
20  
21  /**
22   * A helper class for handling font attributes of {@link ComputedCSSStyleDeclaration}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  final class ComputedFont {
28  
29      // static final int FONT_STYLE_INDEX = 0;
30      // static final int FONT_WEIGHT_INDEX = 1;
31      // static final int FONT_STRETCH_INDEX = 2;
32  
33      static final int FONT_SIZE_INDEX = 3;
34      static final int LINE_HEIGHT_INDEX = 4;
35      static final int FONT_FAMILY_INDEX = 5;
36  
37      static String[] getDetails(final String font) {
38          String fontName = font;
39          while (fontName.contains("  ")) {
40              fontName = fontName.replace("  ", " ");
41          }
42          final String[] tokens = StringUtils.splitAtBlank(fontName);
43          if (tokens.length > 1) {
44              final String[] fontSizeDetails = getFontSizeDetails(tokens[tokens.length - 2]);
45              if (fontSizeDetails == null) {
46                  return null;
47              }
48              final String[] details = new String[6];
49              details[FONT_SIZE_INDEX] = fontSizeDetails[0];
50              details[LINE_HEIGHT_INDEX] = fontSizeDetails[1];
51              details[FONT_FAMILY_INDEX] = tokens[tokens.length - 1];
52              return details;
53          }
54          return null;
55      }
56  
57      /**
58       * Parses the specified font-size value into its font size and line height components.
59       *
60       * @param fontSize the font-size value to parse
61       * @return an array containing the font size and line height, or {@code null}
62       *         if the value is invalid
63       */
64      private static String[] getFontSizeDetails(final String fontSize) {
65          final int slash = fontSize.indexOf('/');
66          final String actualFontSize = slash == -1 ? fontSize : fontSize.substring(0, slash);
67          if (!isLength(actualFontSize)) {
68              return null;
69          }
70  
71          String actualLineHeight = slash == -1 ? "" : fontSize.substring(slash + 1);
72          if (actualLineHeight.isEmpty()) {
73              actualLineHeight = null;
74          }
75          else if (!isValidLineHeight(actualLineHeight)) {
76              return null;
77          }
78          return new String[] {actualFontSize, actualLineHeight};
79      }
80  
81      private static boolean isValidLineHeight(final String lineHeight) {
82          return isLength(lineHeight) || "normal".equals(lineHeight);
83      }
84  
85      private ComputedFont() {
86          // util class
87      }
88  
89  }