Skip to content

Commit

Permalink
Created new FontParser
Browse files Browse the repository at this point in the history
  • Loading branch information
oswetto committed May 5, 2024
1 parent d2f6b2e commit 729af34
Show file tree
Hide file tree
Showing 13 changed files with 8,544 additions and 9,462 deletions.
393 changes: 205 additions & 188 deletions LoboCommon/src/main/java/org/loboevolution/html/CSSValues.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public Node adoptNode(final Node source) {
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "readonly node");
}


final NodeImpl node = (NodeImpl) source;
node.setOwnerDocument(this.document, true);
return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ public void setProperty(final String propertyName, final String vl, final String
if (Strings.isBlank(value)) value = "";
style.setProperty(lwPropertyName, value, priority);
} else {
value = HtmlValues.isUrl(value) ? value.trim() : value.trim().toLowerCase();
value = HtmlValues.isUrl(value) ? value.trim() :
(FONT_FAMILY.equals(propertyName) && value.contains("\"")) ? value.trim() :
value.trim().toLowerCase();

if (HtmlValues.isUnits(value)) {
if (BACKGROUND_POSITION.equals(propertyName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import org.loboevolution.css.CSSStyleDeclaration;
import org.loboevolution.css.CSSStyleRule;
import org.loboevolution.css.CSSStyleSheet;
import org.loboevolution.html.style.setter.BackgroundSetter;
import org.loboevolution.html.style.setter.BorderSetter2;
import org.loboevolution.html.style.setter.BorderStyleSetter;
import org.loboevolution.html.style.setter.FourCornersSetter;
import org.loboevolution.html.style.setter.*;
import org.loboevolution.net.NameValuePair;

import java.util.ArrayList;
Expand All @@ -45,7 +42,7 @@
/**
* <p>CSSStyleRuleImpl class.</p>
*/
public class CSSStyleRuleImpl extends AbstractCSSStyleRule implements CSSStyleRule {
public class CSSStyleRuleImpl extends AbstractCSSStyleRule implements CSSProperties, CSSStyleRule {

private final AbstractCSSRuleImpl abstractCSSRule;

Expand Down Expand Up @@ -97,20 +94,22 @@ public CSSStyleDeclaration getStyle() {

list.forEach(p -> {
switch (p.getName()) {
case CSSProperties.MARGIN:
case CSSProperties.BORDER_COLOR:
case CSSProperties.BORDER_WIDTH:
case CSSProperties.PADDING:
case MARGIN:
case BORDER_COLOR:
case BORDER_WIDTH:
case PADDING:
new FourCornersSetter(p.getName(), p.getName() + "-", "").changeValue(atomicReference.get(), p.getValue());
break;
case CSSProperties.BORDER_STYLE:
case BORDER_STYLE:
new BorderStyleSetter(p.getName(), p.getName() + "-", "-style").changeValue(atomicReference.get(), p.getValue());
break;
case CSSProperties.BORDER:
case BORDER:
new BorderSetter2(p.getName()).changeValue(atomicReference.get(), p.getValue());
break;
case CSSProperties.BACKGROUND:
case BACKGROUND:
new BackgroundSetter().changeValue(atomicReference.get(), p.getValue());
case FONT:
new FontSetter().changeValue(atomicReference.get(), p.getValue());
default:
break;
}
Expand Down
106 changes: 106 additions & 0 deletions LoboHTML/src/main/java/org/loboevolution/html/parser/FontParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* MIT License
*
* Copyright (c) 2014 - 2023 LoboEvolution
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Contact info: ivan.difrancesco@yahoo.it
*/

package org.loboevolution.html.parser;

import org.loboevolution.common.Strings;
import org.loboevolution.html.CSSValues;
import org.loboevolution.html.style.FontValues;
import org.loboevolution.html.style.HtmlValues;

/**
* <p>FontParser class.</p>
*/
public class FontParser {

public final static int FONT_STYLE_INDEX = 0;
public final static int FONT_VARIANT_INDEX = 1;
public final static int FONT_WEIGHT_INDEX = 2;
public final static int FONT_SIZE_INDEX = 3;
public final static int LINE_HEIGHT_INDEX = 4;
public final static int FONT_FAMILY_INDEX = 5;
public final static int FONT_STRETCH_INDEX = 6;

public String[] fontParser(final String font) {
final String[] tokens = font.replace(" / ", "/").split(" ");
if (tokens.length > 1) {
final String[] details = new String[7];
fontParser(tokens, details);
if (details[FONT_SIZE_INDEX] != null && details[FONT_FAMILY_INDEX] == null) {
details[FONT_FAMILY_INDEX] = font.indexOf('"') == -1 ? tokens[tokens.length - 1] : font.substring(font.indexOf('"'));
return details;
}
}
return null;
}

private void fontParser(final String[] tokens, String[] details) {
for (String token : tokens) {
if (FontValues.isFontStyle(token)) {
details[FONT_STYLE_INDEX] = token;
}

if (FontValues.isFontVariant(token)) {
details[FONT_VARIANT_INDEX] = token;
}

if (FontValues.isFontWeight(token)) {
details[FONT_WEIGHT_INDEX] = token;
}

if (FontValues.isFontStretch(token)) {
details[FONT_STRETCH_INDEX] = token;
}

final String[] fontSize = fontSizeParser(token);
if (fontSize != null) {
details[FONT_SIZE_INDEX] = fontSize[0];
details[LINE_HEIGHT_INDEX] = fontSize[1];
}
}
}


private String[] fontSizeParser(final String fontSize) {
final int slash = fontSize.indexOf('/');
final String actualFontSize = slash == -1 ? fontSize : fontSize.substring(0, slash);
if (!HtmlValues.isUnits(actualFontSize) && !actualFontSize.endsWith("%")) {
return null;
}

String actualLineHeight = slash == -1 ? "" : fontSize.substring(slash + 1);
if (Strings.isBlank(actualLineHeight)) {
actualLineHeight = null;
} else if (!isValidLineHeight(actualLineHeight)) {
return null;
}
return new String[]{actualFontSize, actualLineHeight};
}

private boolean isValidLineHeight(final String lineHeight) {
return HtmlValues.isUnits(lineHeight) || CSSValues.NORMAL.isEqual(lineHeight) || lineHeight.endsWith("%");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public String getBackgroundPosition() {
break;
case TOP:
case LEFT:
System.out.println(builder.indexOf("100%"));
if (builder.indexOf("100%") > -1) {
builder.insert(0, "0% ");
} else {
Expand Down Expand Up @@ -623,7 +622,7 @@ public String getFontSize() {
} else {
fontSize = Float.valueOf(FontValues.getFontSize(style.getFontSize(), window, null)).intValue();
}
return this.element.getParentNode() == null ? null : fontSize + "px";
return this.element.getParentNode() == null ? null : (fontSize < 7 ? 6 : fontSize) + "px";
}

/**
Expand Down Expand Up @@ -710,9 +709,21 @@ public String getFlexFlow() {
*/
@Override
public String getLineHeight() {
final HTMLElementImpl parent = (HTMLElementImpl) element.getParentElement();
final CSSStyleDeclaration style = element.getStyle();
final String lineHeight = style.getLineHeight();
return this.element.getParentNode() == null ? null : Strings.isCssBlank(lineHeight) ? CSSValues.NORMAL.getValue() : lineHeight;
int lineHeight;

if (Strings.isCssBlank(style.getLineHeight()) && parent != null && parent.getStyle().getLength() > 0) {
final CSSStyleDeclaration currentStyle = parent.getStyle();
lineHeight = FontValues.getPixelSize(currentStyle.getLineHeight(), element.getRenderState(), window, -1);
} else {
if (style.getLineHeight() == null || CSSValues.NORMAL.isEqual(style.getLineHeight())) {
return CSSValues.NORMAL.getValue();
} else {
lineHeight = Float.valueOf(FontValues.getFontSize(style.getLineHeight(), window, element.getRenderState())).intValue();
}
}
return this.element.getParentNode() == null ? null : lineHeight + "px";
}

/**
Expand Down
49 changes: 38 additions & 11 deletions LoboHTML/src/main/java/org/loboevolution/html/style/FontValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ private static Integer getFontUnderline(final String underline, final RenderStat
public static String getFontWeight(final String fontWeight, final RenderState parentRenderState, final boolean isBold) {
if (fontWeight == null) {
if (parentRenderState != null) {
if(parentRenderState.getFont().getAttributes().get(TextAttribute.WEIGHT) == null){
if (parentRenderState.getFont().getAttributes().get(TextAttribute.WEIGHT) == null) {
return CSSValues.BOLD400.getValue();
}

Expand All @@ -381,6 +381,10 @@ public static String getFontWeight(final String fontWeight, final RenderState pa
}
}

if (CSSValues.NORMAL.isEqual(fontWeight)) {
return CSSValues.BOLD400.getValue();
}

return fontWeight;
}

Expand Down Expand Up @@ -425,17 +429,40 @@ public static boolean isFontVariant(final String token) {
public static boolean isFontWeight(final String token) {
final CSSValues tok = CSSValues.get(token);
switch (tok) {
case BOLD:
case NORMAL:
case LIGHTER:
return true;
default:
try {
final int value = Integer.parseInt(token);
return value % 100 == 0 && value >= 100 && value <= 900;
} catch (final NumberFormatException nfe) {
case BOLD:
case NORMAL:
case LIGHTER:
return true;
default:
try {
final int value = Integer.parseInt(token);
return value % 100 == 0 && value >= 100 && value <= 900;
} catch (final NumberFormatException nfe) {
return false;
}
}
}

/**
* {@inheritDoc}
*
* Checks if is font stretch.
*/
public static boolean isFontStretch(final String token) {
final CSSValues tok = CSSValues.get(token);
switch (tok) {
case NORMAL:
case CONDENSED:
case EXPANDED:
case EXTRA_CONDENSED:
case EXTRA_EXPANDED:
case ULTRA_CONDENSED:
case ULTRA_EXPANDED:
case SEMI_CONDENSED:
case SEMI_EXPANDED:
return true;
default:
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,15 @@ public static int getPixelSize(final String spec, final RenderState renderState,
case "em":
final FontFactory FONT_FACTORY = FontFactory.getInstance();
final WindowImpl win = (WindowImpl) window;
if (win == null || win.getConfig() == null) {
return (int) Math.round(16.0f * Double.parseDouble(text));
}

final Font DEFAULT_FONT = FONT_FACTORY.getFont(FontValues.getDefaultFontKey(win.getConfig()));
final Font f = (renderState == null) ? DEFAULT_FONT : renderState.getFont();
final int fontSize = f.getSize();
return (int) Math.round(fontSize * Double.parseDouble(text));

case "rem":
final WindowImpl win2 = (WindowImpl) window;
final float fs = win2.getConfig() != null ? win2.getConfig().getFontSize() : 16.0f;
Expand Down

0 comments on commit 729af34

Please sign in to comment.