From f2e3ec218bc49e358401164bf96d519d8fe36c71 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Tue, 15 May 2012 11:49:13 +0100 Subject: [PATCH] fixed px/pt font size processing --- .../fbreader/src/formats/css/StyleSheetTable.cpp | 6 +++++- .../zlibrary/text/src/model/ZLTextStyleEntry.h | 1 + .../zlibrary/text/model/ZLTextMetrics.java | 6 +++++- .../zlibrary/text/model/ZLTextStyleEntry.java | 14 ++++++++++---- .../zlibrary/text/view/ZLTextViewBase.java | 8 ++++++-- .../text/view/style/ZLTextStyleCollection.java | 8 +++++++- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp index 00f4b983d..9fbab5855 100644 --- a/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp +++ b/jni/NativeFormats/fbreader/src/formats/css/StyleSheetTable.cpp @@ -67,10 +67,14 @@ static bool parseLength(const std::string &toParse, short &size, ZLTextStyleEntr unit = ZLTextStyleEntry::SIZE_UNIT_EX_100; size = (short)(100 * ZLStringUtil::stringToDouble(toParse, 0)); return true; - } else { + } else if (ZLStringUtil::stringEndsWith(toParse, "px")) { unit = ZLTextStyleEntry::SIZE_UNIT_PIXEL; size = atoi(toParse.c_str()); return true; + } else if (ZLStringUtil::stringEndsWith(toParse, "pt")) { + unit = ZLTextStyleEntry::SIZE_UNIT_POINT; + size = atoi(toParse.c_str()); + return true; } return false; } diff --git a/jni/NativeFormats/zlibrary/text/src/model/ZLTextStyleEntry.h b/jni/NativeFormats/zlibrary/text/src/model/ZLTextStyleEntry.h index e0df3ff82..9728863c9 100644 --- a/jni/NativeFormats/zlibrary/text/src/model/ZLTextStyleEntry.h +++ b/jni/NativeFormats/zlibrary/text/src/model/ZLTextStyleEntry.h @@ -31,6 +31,7 @@ class ZLTextStyleEntry : public ZLTextParagraphEntry { public: enum SizeUnit { SIZE_UNIT_PIXEL, + SIZE_UNIT_POINT, SIZE_UNIT_EM_100, SIZE_UNIT_EX_100, SIZE_UNIT_PERCENT diff --git a/src/org/geometerplus/zlibrary/text/model/ZLTextMetrics.java b/src/org/geometerplus/zlibrary/text/model/ZLTextMetrics.java index ec03cab8d..67d219a09 100644 --- a/src/org/geometerplus/zlibrary/text/model/ZLTextMetrics.java +++ b/src/org/geometerplus/zlibrary/text/model/ZLTextMetrics.java @@ -20,12 +20,16 @@ package org.geometerplus.zlibrary.text.model; public final class ZLTextMetrics { + public final int DPI; + public final int DefaultFontSize; 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) { + public ZLTextMetrics(int dpi, int defaultFontSize, int fontSize, int fontXHeight, int fullWidth, int fullHeight) { + DPI = dpi; + DefaultFontSize = defaultFontSize; FontSize = fontSize; FontXHeight = fontXHeight; FullWidth = fullWidth; diff --git a/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java b/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java index 86b95f5b8..b6d7e50b8 100644 --- a/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java +++ b/src/org/geometerplus/zlibrary/text/model/ZLTextStyleEntry.java @@ -48,9 +48,10 @@ public final class ZLTextStyleEntry { public interface SizeUnit { byte PIXEL = 0; - byte EM_100 = 1; - byte EX_100 = 2; - byte PERCENT = 3; + byte POINT = 1; + byte EM_100 = 2; + byte EX_100 = 3; + byte PERCENT = 4; } private class Length { @@ -106,7 +107,12 @@ public final class ZLTextStyleEntry { switch (myLengths[featureId].Unit) { default: case SizeUnit.PIXEL: - return myLengths[featureId].Size; + return myLengths[featureId].Size * metrics.FontSize / metrics.DefaultFontSize; + // we understand "point" as "1/2 point" + case SizeUnit.POINT: + return myLengths[featureId].Size + * metrics.DPI * metrics.FontSize + / 72 / metrics.DefaultFontSize / 2; case SizeUnit.EM_100: return (myLengths[featureId].Size * metrics.FontSize + 50) / 100; case SizeUnit.EX_100: diff --git a/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java b/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java index 4de897c74..3dc30805e 100644 --- a/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java +++ b/src/org/geometerplus/zlibrary/text/view/ZLTextViewBase.java @@ -20,10 +20,11 @@ package org.geometerplus.zlibrary.text.view; import org.geometerplus.zlibrary.core.application.ZLApplication; +import org.geometerplus.zlibrary.core.filesystem.ZLFile; +import org.geometerplus.zlibrary.core.library.ZLibrary; import org.geometerplus.zlibrary.core.util.ZLColor; 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; @@ -45,8 +46,11 @@ abstract class ZLTextViewBase extends ZLView { private ZLTextMetrics metrics() { if (myMetrics == null) { - final ZLTextBaseStyle base = ZLTextStyleCollection.Instance().getBaseStyle(); + final ZLTextStyleCollection collection = ZLTextStyleCollection.Instance(); + final ZLTextBaseStyle base = collection.getBaseStyle(); myMetrics = new ZLTextMetrics( + ZLibrary.Instance().getDisplayDPI(), + collection.getDefaultFontSize(), base.getFontSize(), // TODO: font X height base.getFontSize() * 15 / 10, diff --git a/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java b/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java index 20dd0c4ba..71d50708f 100644 --- a/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java +++ b/src/org/geometerplus/zlibrary/text/view/style/ZLTextStyleCollection.java @@ -28,6 +28,7 @@ import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile; public class ZLTextStyleCollection { private static ZLTextStyleCollection ourInstance = null; + private int myDefaultFontSize; private ZLTextBaseStyle myBaseStyle; private final ZLTextStyleDecoration[] myDecorationMap = new ZLTextStyleDecoration[256]; @@ -46,6 +47,10 @@ public class ZLTextStyleCollection { ourInstance = null; } + public int getDefaultFontSize() { + return myDefaultFontSize; + } + public ZLTextBaseStyle getBaseStyle() { return myBaseStyle; } @@ -99,7 +104,8 @@ public class ZLTextStyleCollection { final String STYLE = "style"; if (BASE.equals(tag)) { - myCollection.myBaseStyle = new ZLTextBaseStyle(attributes.getValue("family"), intValue(attributes, "fontSize", 0)); + myCollection.myDefaultFontSize = intValue(attributes, "fontSize", 0); + myCollection.myBaseStyle = new ZLTextBaseStyle(attributes.getValue("family"), myCollection.myDefaultFontSize); } else if (STYLE.equals(tag)) { String idString = attributes.getValue("id"); String name = attributes.getValue("name");