Skip to content

Commit a4729c1

Browse files
committed
AWTStyleDatabase: adapt to StyleDatabase change in core.
1 parent 82c8473 commit a4729c1

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
3+
Copyright (c) 2005-2019, Carlos Amengual.
4+
5+
SPDX-License-Identifier: BSD-3-Clause
6+
7+
Licensed under a BSD-style License. You can find the license here:
8+
https://css4j.github.io/LICENSE.txt
9+
10+
*/
11+
12+
package io.sf.carte.doc.style.css.awt;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertTrue;
17+
18+
import java.awt.Font;
19+
import java.io.IOException;
20+
import java.util.Locale;
21+
22+
import javax.xml.parsers.DocumentBuilderFactory;
23+
import javax.xml.parsers.ParserConfigurationException;
24+
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.w3c.dom.DOMException;
28+
import org.w3c.dom.Document;
29+
import org.w3c.dom.Element;
30+
import org.w3c.dom.Node;
31+
import org.w3c.dom.stylesheets.LinkStyle;
32+
33+
import io.sf.carte.doc.agent.HeadlessDeviceFactory;
34+
import io.sf.carte.doc.style.css.CSSDocument;
35+
import io.sf.carte.doc.style.css.CSSElement;
36+
import io.sf.carte.doc.style.css.CSSMediaException;
37+
import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet;
38+
import io.sf.carte.doc.style.css.om.FontFaceRule;
39+
import io.sf.carte.doc.style.css.om.TestCSSStyleSheetFactory;
40+
41+
public class AWTStyleDatabaseTest {
42+
43+
private CSSDocument cssdoc;
44+
private Node styleText;
45+
private AbstractCSSStyleSheet sheet;
46+
private AWTStyleDatabase styleDb;
47+
48+
@Before
49+
public void setUp() throws DOMException, ParserConfigurationException, CSSMediaException {
50+
TestCSSStyleSheetFactory factory = new TestCSSStyleSheetFactory();
51+
HeadlessDeviceFactory deviceFactory = new HeadlessDeviceFactory();
52+
styleDb = new AWTStyleDatabase();
53+
deviceFactory.setStyleDatabase("screen", styleDb);
54+
factory.setDeviceFactory(deviceFactory);
55+
DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance();
56+
Document doc = dbFac.newDocumentBuilder().getDOMImplementation().createDocument(null, "html", null);
57+
Element head = doc.createElement("head");
58+
Element style = doc.createElement("style");
59+
style.setAttribute("id", "styleId");
60+
style.setIdAttribute("id", true);
61+
style.setAttribute("type", "text/css");
62+
style.setAttribute("media", "screen");
63+
style.setTextContent(" ");
64+
doc.getDocumentElement().appendChild(head);
65+
head.appendChild(style);
66+
Element body = doc.createElement("body");
67+
body.setAttribute("id", "bodyId");
68+
body.setIdAttribute("id", true);
69+
doc.getDocumentElement().appendChild(body);
70+
cssdoc = factory.createCSSDocument(doc);
71+
cssdoc.setTargetMedium("screen");
72+
CSSElement cssStyle = cssdoc.getElementById("styleId");
73+
sheet = (AbstractCSSStyleSheet) ((LinkStyle) cssStyle).getSheet();
74+
styleText = cssStyle.getChildNodes().item(0);
75+
}
76+
77+
@Test
78+
public void testFontFaceRule() throws IOException {
79+
styleText.setNodeValue(
80+
"@font-face{font-family:'OpenSans Regular';src:url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype')}");
81+
FontFaceRule ffrule = (FontFaceRule) sheet.getCssRules().item(0);
82+
assertEquals(2, ffrule.getStyle().getLength());
83+
assertEquals(
84+
"@font-face {font-family: 'OpenSans Regular'; src: url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype'); }",
85+
ffrule.getCssText());
86+
assertEquals("url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype')",
87+
ffrule.getStyle().getPropertyValue("src"));
88+
//
89+
CSSElement body = cssdoc.getElementById("bodyId");
90+
body.getComputedStyle(null);
91+
assertTrue(styleDb.isFontFaceName("opensans regular"));
92+
Font font = styleDb.getFont("opensans regular");
93+
assertNotNull(font);
94+
assertEquals("Open Sans", font.getFamily(Locale.ROOT));
95+
assertEquals("Open Sans Regular", font.getFontName(Locale.ROOT));
96+
}
97+
98+
}

src/io/sf/carte/doc/style/css/awt/AWTStyleDatabase.java

+56
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111

1212
package io.sf.carte.doc.style.css.awt;
1313

14+
import java.awt.Font;
15+
import java.awt.FontFormatException;
1416
import java.awt.GraphicsConfiguration;
1517
import java.awt.GraphicsEnvironment;
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.util.HashMap;
21+
import java.util.Map;
1622

1723
import org.w3c.dom.DOMException;
1824
import org.w3c.dom.css.CSSPrimitiveValue;
1925

2026
import io.sf.carte.doc.style.css.AbstractStyleDatabase;
27+
import io.sf.carte.doc.style.css.ExtendedCSSFontFaceRule;
2128

2229
/**
2330
* CSS style database for use with AWT objects.
@@ -36,6 +43,8 @@ public class AWTStyleDatabase extends AbstractStyleDatabase {
3643
private float defaultWidth = 595f;
3744
private float defaultHeight = 842f;
3845

46+
private final Map<String,Font> fontfaceNames = new HashMap<String,Font>();
47+
3948
/**
4049
* Constructs a default style database with no graphics configuration.
4150
*/
@@ -94,6 +103,53 @@ protected boolean isFontFamilyAvailable(String fontFamily) {
94103
return false;
95104
}
96105

106+
@Override
107+
public boolean isFontFaceName(String requestedFamily) {
108+
return fontfaceNames.containsKey(requestedFamily);
109+
}
110+
111+
@Override
112+
protected boolean loadFontFace(String familyName, FontFormat format, InputStream is, ExtendedCSSFontFaceRule rule)
113+
throws IOException {
114+
int fontFormat;
115+
if (format == null || (fontFormat = fontFormatFromEnum(format)) == -1) {
116+
return false;
117+
}
118+
Font font;
119+
try {
120+
font = Font.createFont(fontFormat, is);
121+
} catch (FontFormatException e) {
122+
rule.getParentStyleSheet().getErrorHandler().fontFormatError(rule, e);
123+
return false;
124+
}
125+
fontfaceNames.put(familyName, font);
126+
return true;
127+
}
128+
129+
private int fontFormatFromEnum(FontFormat format) {
130+
int fontFormat;
131+
switch (format) {
132+
case TRUETYPE:
133+
case OPENTYPE:
134+
fontFormat = Font.TRUETYPE_FONT;
135+
break;
136+
default:
137+
fontFormat = -1;
138+
}
139+
return fontFormat;
140+
}
141+
142+
/**
143+
* Get a font that was loaded by a {@literal @}font-face rule.
144+
*
145+
* @param lcFamilyName the family name in lowercase.
146+
* @return the font, or <code>null</code> if no font with that family name (in
147+
* lowercase) has been loaded from a {@literal @}font-face rule.
148+
*/
149+
public Font getFont(String lcFamilyName) {
150+
return fontfaceNames.get(lcFamilyName);
151+
}
152+
97153
/**
98154
* Gets the GraphicsConfiguration for this style database.
99155
*

0 commit comments

Comments
 (0)