1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 19:42:17 +02:00

CSS: font size

This commit is contained in:
Nikolay Pultsin 2012-05-10 11:09:34 +01:00
parent 591a338864
commit eb07593877
10 changed files with 126 additions and 37 deletions

View file

@ -26,20 +26,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.core.image.ZLImageData;
abstract public class ZLPaintContext {
public static final class Metrics {
public final int FontSize;
public final int FontXHeight;
public final int FullWidth;
public final int FullHeight;
public Metrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight) {
FontSize = fontSize;
FontXHeight = fontXHeight;
FullWidth = fullWidth;
FullHeight = fullHeight;
}
}
private final ArrayList<String> myFamilies = new ArrayList<String>();
protected ZLPaintContext() {

View file

@ -0,0 +1,55 @@
/*
* Copyright (C) 2007-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.zlibrary.text.model;
public final class ZLTextMetrics {
public final int FontSize;
public final int FontXHeight;
public final int FullWidth;
public final int FullHeight;
public ZLTextMetrics(int fontSize, int fontXHeight, int fullWidth, int fullHeight) {
FontSize = fontSize;
FontXHeight = fontXHeight;
FullWidth = fullWidth;
FullHeight = fullHeight;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof ZLTextMetrics)) {
return false;
}
final ZLTextMetrics oo = (ZLTextMetrics)o;
return
FontSize == oo.FontSize &&
FontXHeight == oo.FontXHeight &&
FullWidth == oo.FullWidth &&
FullHeight == oo.FullHeight;
}
@Override
public int hashCode() {
return FontSize + 13 * (FontXHeight + 13 * (FullHeight + 13 * FullWidth));
}
}

View file

@ -20,7 +20,6 @@
package org.geometerplus.zlibrary.text.model;
import org.geometerplus.zlibrary.core.util.ZLBoolean3;
import org.geometerplus.zlibrary.core.view.ZLPaintContext;
public final class ZLTextStyleEntry {
public interface Feature {
@ -87,7 +86,7 @@ public final class ZLTextStyleEntry {
myLengths[featureId] = new Length(size, unit);
}
private int fullSize(ZLPaintContext.Metrics metrics, int featureId) {
private int fullSize(ZLTextMetrics metrics, int featureId) {
switch (featureId) {
default:
case Feature.LENGTH_LEFT_INDENT:
@ -102,7 +101,7 @@ public final class ZLTextStyleEntry {
}
}
public int getLength(int featureId, ZLPaintContext.Metrics metrics) {
public int getLength(int featureId, ZLTextMetrics metrics) {
switch (myLengths[featureId].Unit) {
default:
case SizeUnit.PIXEL:

View file

@ -19,6 +19,8 @@
package org.geometerplus.zlibrary.text.view;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
public abstract class ZLTextStyle {
public final ZLTextStyle Base;
public final ZLTextHyperlink Hyperlink;
@ -29,7 +31,7 @@ public abstract class ZLTextStyle {
}
public abstract String getFontFamily();
public abstract int getFontSize();
public abstract int getFontSize(ZLTextMetrics metrics);
public abstract boolean isBold();
public abstract boolean isItalic();

View file

@ -1340,6 +1340,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
}
public void clearCaches() {
resetMetrics();
rebuildPaintInfo();
Application.getViewWidget().reset();
myCharWidth = -1;

View file

@ -25,17 +25,40 @@ import org.geometerplus.zlibrary.core.view.ZLView;
import org.geometerplus.zlibrary.core.view.ZLPaintContext;
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
import org.geometerplus.zlibrary.text.view.style.*;
abstract class ZLTextViewBase extends ZLView {
private ZLTextStyle myTextStyle;
private int myWordHeight = -1;
private ZLTextMetrics myMetrics;
ZLTextViewBase(ZLApplication application) {
super(application);
resetTextStyle();
}
protected void resetMetrics() {
myMetrics = null;
}
private ZLTextMetrics metrics() {
if (myMetrics == null) {
final ZLTextBaseStyle base = ZLTextStyleCollection.Instance().getBaseStyle();
myMetrics = new ZLTextMetrics(
base.getFontSize(),
// TODO: font X height
base.getFontSize() * 15 / 10,
// TODO: screen area width
100,
// TODO: screen area height
100
);
}
return myMetrics;
}
final int getWordHeight() {
if (myWordHeight == -1) {
final ZLTextStyle textStyle = myTextStyle;
@ -86,7 +109,7 @@ abstract class ZLTextViewBase extends ZLView {
myTextStyle = style;
myWordHeight = -1;
}
myContext.setFont(style.getFontFamily(), style.getFontSize(), style.isBold(), style.isItalic(), style.isUnderline(), style.isStrikeThrough());
myContext.setFont(style.getFontFamily(), style.getFontSize(metrics()), style.isBold(), style.isItalic(), style.isUnderline(), style.isStrikeThrough());
}
final void resetTextStyle() {

View file

@ -23,6 +23,7 @@ import org.geometerplus.zlibrary.core.options.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.text.model.ZLTextAlignmentType;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.text.view.ZLTextHyperlink;
@ -61,11 +62,15 @@ public class ZLTextBaseStyle extends ZLTextStyle {
return FontFamilyOption.getValue();
}
@Override
public int getFontSize() {
return FontSizeOption.getValue();
}
@Override
public int getFontSize(ZLTextMetrics metrics) {
return getFontSize();
}
@Override
public boolean isBold() {
return BoldOption.getValue();

View file

@ -19,13 +19,14 @@
package org.geometerplus.zlibrary.text.view.style;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.text.view.ZLTextHyperlink;
public abstract class ZLTextDecoratedStyle extends ZLTextStyle {
// fields to be cached
private String myFontFamily;
private int myFontSize;
private boolean myIsItalic;
private boolean myIsBold;
private boolean myIsUnderline;
@ -34,13 +35,15 @@ public abstract class ZLTextDecoratedStyle extends ZLTextStyle {
private boolean myIsNotCached = true;
private int myFontSize;
private ZLTextMetrics myMetrics;
protected ZLTextDecoratedStyle(ZLTextStyle base, ZLTextHyperlink hyperlink) {
super(base, (hyperlink != null) ? hyperlink : base.Hyperlink);
}
private void initCache() {
myFontFamily = getFontFamilyInternal();
myFontSize = getFontSizeInternal();
myIsItalic = isItalicInternal();
myIsBold = isBoldInternal();
myIsUnderline = isUnderlineInternal();
@ -50,6 +53,11 @@ public abstract class ZLTextDecoratedStyle extends ZLTextStyle {
myIsNotCached = false;
}
private void initMetricsCache(ZLTextMetrics metrics) {
myMetrics = metrics;
myFontSize = getFontSizeInternal(metrics);
}
@Override
public final String getFontFamily() {
if (myIsNotCached) {
@ -60,13 +68,13 @@ public abstract class ZLTextDecoratedStyle extends ZLTextStyle {
protected abstract String getFontFamilyInternal();
@Override
public final int getFontSize() {
if (myIsNotCached) {
initCache();
public final int getFontSize(ZLTextMetrics metrics) {
if (!metrics.equals(myMetrics)) {
initMetricsCache(metrics);
}
return myFontSize;
}
protected abstract int getFontSizeInternal();
protected abstract int getFontSizeInternal(ZLTextMetrics metrics);
@Override
public final boolean isItalic() {

View file

@ -21,9 +21,11 @@ package org.geometerplus.zlibrary.text.view.style;
import org.geometerplus.zlibrary.core.util.ZLBoolean3;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
import org.geometerplus.zlibrary.text.model.ZLTextStyleEntry;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
public class ZLTextExplicitlyDecoratedStyle extends ZLTextStyle implements ZLTextStyleEntry.Feature, ZLTextStyleEntry.FontModifier {
private final ZLTextStyleEntry myEntry;
@ -33,18 +35,24 @@ public class ZLTextExplicitlyDecoratedStyle extends ZLTextStyle implements ZLTex
}
public String getFontFamily() {
if (myEntry.isFeatureSupported(FONT_FAMILY)) {
// TODO: implement
}
return Base.getFontFamily();
}
public int getFontSize() {
public int getFontSize(ZLTextMetrics metrics) {
if (myEntry.isFeatureSupported(FONT_STYLE_MODIFIER)) {
if (myEntry.getFontModifier(FONT_MODIFIER_LARGER) == ZLBoolean3.B3_TRUE) {
return Base.Base.getFontSize() * 120 / 100;
return Base.Base.getFontSize(metrics) * 120 / 100;
}
if (myEntry.getFontModifier(FONT_MODIFIER_SMALLER) == ZLBoolean3.B3_TRUE) {
return Base.Base.getFontSize() * 100 / 120;
return Base.Base.getFontSize(metrics) * 100 / 120;
}
// TODO: implement
return Base.getFontSize();
}
if (myEntry.isFeatureSupported(LENGTH_FONT_SIZE)) {
return myEntry.getLength(LENGTH_FONT_SIZE, metrics);
}
return Base.getFontSize(metrics);
}
public boolean isBold() {

View file

@ -19,6 +19,8 @@
package org.geometerplus.zlibrary.text.view.style;
import org.geometerplus.zlibrary.text.model.ZLTextMetrics;
import org.geometerplus.zlibrary.text.view.ZLTextStyle;
import org.geometerplus.zlibrary.text.view.ZLTextHyperlink;
@ -37,8 +39,8 @@ class ZLTextPartiallyDecoratedStyle extends ZLTextDecoratedStyle {
}
@Override
protected int getFontSizeInternal() {
return Base.getFontSize() + myDecoration.FontSizeDeltaOption.getValue();
protected int getFontSizeInternal(ZLTextMetrics metrics) {
return Base.getFontSize(metrics) + myDecoration.FontSizeDeltaOption.getValue();
}
@Override