Skip to content

Commit

Permalink
styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Lopatin committed Dec 11, 2007
1 parent 2909d6f commit 2a8a198
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 51 deletions.
2 changes: 1 addition & 1 deletion trunk/include/crengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
*/
#define CR_ENGINE_VERSION "0.4.5"
#define CR_ENGINE_VERSION "0.4.9"

#include "crsetup.h"
#include "lvtypes.h"
Expand Down
11 changes: 6 additions & 5 deletions trunk/include/cssdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ enum css_vertical_align_t {

/// text-decoration property values
enum css_text_decoration_t {
css_td_inherit = -1,
css_td_none = 0,
css_td_underline = 1,
css_td_overline = 2,
// TODO: support multiple flags
css_td_inherit = 0,
css_td_none = 1,
css_td_underline = 2,
css_td_overline = 3,
css_td_line_through = 4,
css_td_blink = 8,
css_td_blink = 5,
};

/// font-style property values
Expand Down
3 changes: 2 additions & 1 deletion trunk/include/fb2def.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ XS_TAG1T( date )
XS_TAG1T( year )
XS_TAG1T( sequence )

XS_TAG1D( stylesheet, false, css_d_none, css_ws_normal )
XS_TAG1D( stylesheet, true, css_d_none, css_ws_normal )
XS_TAG1D( description, false, css_d_none, css_ws_normal )
XS_TAG2( title_info, "title-info" )
XS_TAG2( src_title_info, "src-title-info" )
Expand Down Expand Up @@ -122,6 +122,7 @@ XS_ATTR( href )
XS_ATTR( type )
XS_ATTR( mode )
XS_ATTR( price )
XS_ATTR( style )
XS_ATTR( currency )
XS_ATTR( version )
XS_ATTR( encoding )
Expand Down
6 changes: 3 additions & 3 deletions trunk/include/lvfntman.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class LVFont
/// draws text string
virtual void DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen ) = 0;
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags=0 ) = 0;
/// constructor
LVFont() { }
/// returns true if font is empty
Expand Down Expand Up @@ -138,7 +138,7 @@ class LVBaseFont : public LVFont
/// draws text string
virtual void DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen );
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags=0 );
};

/* C++ wrapper class */
Expand Down Expand Up @@ -294,7 +294,7 @@ class LVWin32DrawFont : public LVBaseWin32Font
/// draws text string
virtual void DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen );
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags=0 );

/** \brief get glyph image in 1 byte per pixel format
\param code is unicode character
Expand Down
40 changes: 27 additions & 13 deletions trunk/include/lvtextfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,28 @@
extern "C" {
#endif

#define LTEXT_ALIGN_LEFT 1 /**< \brief new left-aligned paragraph */
#define LTEXT_ALIGN_RIGHT 2 /**< \brief new right-aligned paragraph */
#define LTEXT_ALIGN_CENTER 3 /**< \brief new centered paragraph */
#define LTEXT_ALIGN_WIDTH 4 /**< \brief new justified paragraph */
// text flags
#define LTEXT_ALIGN_LEFT 0x0001 /**< \brief new left-aligned paragraph */
#define LTEXT_ALIGN_RIGHT 0x0002 /**< \brief new right-aligned paragraph */
#define LTEXT_ALIGN_CENTER 0x0003 /**< \brief new centered paragraph */
#define LTEXT_ALIGN_WIDTH 0x0004 /**< \brief new justified paragraph */

#define LTEXT_FLAG_NEWLINE 7 /**< \brief new line flags mask */
#define LTEXT_FLAG_OWNTEXT 8 /**< \brief store local copy of text instead of pointer */
#define LTEXT_FLAG_NEWLINE 0x0007 /**< \brief new line flags mask */
#define LTEXT_FLAG_OWNTEXT 0x0008 /**< \brief store local copy of text instead of pointer */

#define LTEXT_VALIGN_MASK 0x70 /**< \brief vertical align flags mask */
#define LTEXT_VALIGN_BASELINE 0x00 /**< \brief baseline vertical align */
#define LTEXT_VALIGN_SUB 0x10 /**< \brief subscript */
#define LTEXT_VALIGN_SUPER 0x20 /**< \brief superscript */
#define LTEXT_VALIGN_MASK 0x0070 /**< \brief vertical align flags mask */
#define LTEXT_VALIGN_BASELINE 0x0000 /**< \brief baseline vertical align */
#define LTEXT_VALIGN_SUB 0x0010 /**< \brief subscript */
#define LTEXT_VALIGN_SUPER 0x0020 /**< \brief superscript */

#define LTEXT_TD_UNDERLINE 0x0100 /**< \brief underlined text */
#define LTEXT_TD_OVERLINE 0x0200 /**< \brief overlined text */
#define LTEXT_TD_LINE_THROUGH 0x0400 /**< \brief striked through text */
#define LTEXT_TD_BLINK 0x0800 /**< \brief blinking text */
#define LTEXT_TD_MASK 0x0F00 /**< \brief text decoration mask */

#define LTEXT_SRC_IS_OBJECT 0x8000 /**< \brief superscript */

#define LTEXT_SRC_IS_OBJECT 0x8000 /**< \brief superscript */


/** \brief Source text line
Expand All @@ -58,6 +66,8 @@ typedef struct
lUInt16 margin; /**< \brief first line margin */
lUInt8 interval; /**< \brief line interval, *16 (16=normal, 32=double) */
lUInt8 reserved; /**< \brief reserved */
lUInt32 color; /**< \brief color */
lUInt32 bgcolor; /**< \brief background color */
lUInt32 flags; /**< \brief flags */
} src_text_fragment_t;

Expand Down Expand Up @@ -93,7 +103,7 @@ typedef struct
/// object flag
#define LTEXT_WORD_IS_OBJECT 0x80

#define LTEXT_BACKGROUND_MARK_FLAGS 0xFFFF0000l
//#define LTEXT_BACKGROUND_MARK_FLAGS 0xFFFF0000l

/** \brief Text formatter formatted line
*/
Expand Down Expand Up @@ -146,6 +156,8 @@ void lvtextAddSourceLine(
lvfont_handle font, /* handle of font to draw string */
const lChar16 * text, /* pointer to unicode text string */
lUInt32 len, /* number of chars in text, 0 for auto(strlen) */
lUInt32 color, /* text color */
lUInt32 bgcolor, /* background color */
lUInt32 flags, /* flags */
lUInt8 interval, /* interline space, *16 (16=single, 32=double) */
lUInt16 margin, /* first line margin */
Expand Down Expand Up @@ -211,6 +223,8 @@ class LFormattedText
void AddSourceLine(
const lChar16 * text, /* pointer to unicode text string */
lUInt32 len, /* number of chars in text, 0 for auto(strlen) */
lUInt32 color, /* text color */
lUInt32 bgcolor, /* background color */
LVFont * font, /* font to draw string */
lUInt32 flags=LTEXT_ALIGN_LEFT|LTEXT_FLAG_OWNTEXT,
lUInt8 interval=16, /* interline space, *16 (16=single, 32=double) */
Expand All @@ -221,7 +235,7 @@ class LFormattedText
{
lvtextAddSourceLine(m_pbuffer,
font, //font->GetHandle()
text, len,
text, len, color, bgcolor,
flags, interval, margin, object, (lUInt16)offset );
}
lUInt32 Format(lUInt16 width, lUInt16 page_height) { return lvtextResize( m_pbuffer, width, page_height ); }
Expand Down
25 changes: 15 additions & 10 deletions trunk/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,16 @@ void LVDocView::drawCoverTo( LVDrawBuf * drawBuf, lvRect & rc )
{
if ( rc.width()<130 || rc.height()<130)
return;
int base_font_size = 26;
int base_font_size = 24;
int w = rc.width();
if ( w<300 )
base_font_size = 24;
base_font_size = 22;
else if ( w<500 )
base_font_size = 28;
base_font_size = 24;
else if ( w<700 )
base_font_size = 32;
base_font_size = 26;
else
base_font_size = 36;
base_font_size = 32;
LVFontRef author_fnt( fontMan->GetFont( base_font_size, 600, true, css_ff_serif, lString8("Times New Roman")) );
LVFontRef title_fnt( fontMan->GetFont( base_font_size+4, 600, false, css_ff_serif, lString8("Times New Roman")) );
LVFontRef series_fnt( fontMan->GetFont( base_font_size-3, 300, true, css_ff_serif, lString8("Times New Roman")) );
Expand All @@ -416,9 +416,9 @@ void LVDocView::drawCoverTo( LVDrawBuf * drawBuf, lvRect & rc )
title = L"no title";
LFormattedText txform;
if ( !authors.empty() )
txform.AddSourceLine( authors.c_str(), authors.length(), author_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
txform.AddSourceLine( title.c_str(), title.length(), title_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
txform.AddSourceLine( series.c_str(), series.length(), series_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
txform.AddSourceLine( authors.c_str(), authors.length(), 0xFFFFFFFF, 0xFFFFFFFF, author_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
txform.AddSourceLine( title.c_str(), title.length(), 0xFFFFFFFF, 0xFFFFFFFF, title_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
txform.AddSourceLine( series.c_str(), series.length(), 0xFFFFFFFF, 0xFFFFFFFF, series_fnt.get(), LTEXT_ALIGN_CENTER, 20 );
int title_w = rc.width() - rc.width()/3;
int h = txform.Format( title_w, rc.height() ) + 16;

Expand Down Expand Up @@ -1189,7 +1189,7 @@ bool LVDocView::windowToDocPoint( lvPoint & pt )
int page_y = m_pages[page]->start;
pt.x -= rc->left;
pt.y -= rc->top;
CRLog::debug(" point page offset( %d, %d )", pt.x, pt.y );
//CRLog::debug(" point page offset( %d, %d )", pt.x, pt.y );
pt.y += page_y;
return true;
}
Expand All @@ -1208,7 +1208,7 @@ ldomXPointer LVDocView::getNodeByPoint( lvPoint pt )
{
if ( windowToDocPoint( pt ) ) {
ldomXPointer ptr = m_doc->createXPointer( pt );
CRLog::debug(" ptr (%d, %d) node=%08X offset=%d", pt.x, pt.y, (lUInt32)ptr.getNode(), ptr.getOffset() );
//CRLog::debug(" ptr (%d, %d) node=%08X offset=%d", pt.x, pt.y, (lUInt32)ptr.getNode(), ptr.getOffset() );
return ptr;
}
return ldomXPointer();
Expand Down Expand Up @@ -1678,6 +1678,11 @@ bool LVDocView::LoadDocument( LVStreamRef stream )
m_pos = 0;


lString16 docstyle = m_doc->createXPointer(L"/FictionBook/stylesheet").getText();
if ( !docstyle.empty() ) {
m_doc->getStyleSheet()->parse(UnicodeToUtf8(docstyle).c_str());
}

#if 0
{
LVStreamRef ostream = LVOpenFileStream( "test_save.fb2", LVOM_WRITE );
Expand Down
61 changes: 51 additions & 10 deletions trunk/src/lvfntman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,34 @@ struct LVFontGlyphCacheItem
return sizeof(LVFontGlyphCacheItem)
+ (bmp_width * bmp_height - 1) * sizeof(lUInt8);
}
static LVFontGlyphCacheItem * newItem( LVFontLocalGlyphCache * local_cache, lChar16 ch, FT_GlyphSlot slot )
static LVFontGlyphCacheItem * newItem( LVFontLocalGlyphCache * local_cache, lChar16 ch, FT_GlyphSlot slot, bool drawMonochrome )
{
FT_Bitmap* bitmap = &slot->bitmap;
lUInt8 w = (lUInt8)(bitmap->width);
lUInt8 h = (lUInt8)(bitmap->rows);
LVFontGlyphCacheItem * item = (LVFontGlyphCacheItem *)malloc( sizeof(LVFontGlyphCacheItem)
+ (w*h - 1)*sizeof(lUInt8) );
memcpy( item->bmp, bitmap->buffer, w*h );
if ( drawMonochrome ) {
lUInt8 mask = 0x80;
const lUInt8 * ptr = (const lUInt8 *)bitmap->buffer;
lUInt8 * dst = item->bmp;
int rowsize = ((w + 15) / 16) * 2;
for ( int y=0; y<h; y++ ) {
const lUInt8 * row = ptr;
mask = 0x80;
for ( int x=0; x<w; x++ ) {
*dst++ = (*row & mask) ? 0xFF : 00;
mask >>= 1;
if ( !mask && x!=w-1) {
mask = 0x80;
row++;
}
}
ptr += rowsize;
}
} else {
memcpy( item->bmp, bitmap->buffer, w*h );
}
item->ch = ch;
item->bmp_width = w;
item->bmp_height = h;
Expand Down Expand Up @@ -406,10 +426,11 @@ class LVFreeTypeFace : public LVFont
int _baseline;
LVFontGlyphWidthCache _wcache;
LVFontLocalGlyphCache _glyph_cache;
bool _drawMonochrome;
public:
LVFreeTypeFace( FT_Library library, LVFontGlobalGlyphCache * globalCache )
: _fontFamily(css_ff_sans_serif), _library(library), _face(NULL), _size(0), _hyphen_width(0), _baseline(0)
, _glyph_cache(globalCache)
, _glyph_cache(globalCache), _drawMonochrome(false)
{
}

Expand All @@ -418,8 +439,9 @@ class LVFreeTypeFace : public LVFont
Clear();
}

bool loadFromFile( const char * fname, int index, int size, css_font_family_t fontFamily )
bool loadFromFile( const char * fname, int index, int size, css_font_family_t fontFamily, bool monochrome )
{
_drawMonochrome = monochrome;
_fontFamily = fontFamily;
if ( fname )
_fileName = fname;
Expand Down Expand Up @@ -676,7 +698,7 @@ class LVFreeTypeFace : public LVFont
/// draws text string
virtual void DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen )
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags )
{
if ( len <= 0 || _face==NULL )
return;
Expand All @@ -697,6 +719,7 @@ class LVFreeTypeFace : public LVFont
lChar16 ch;
// measure character widths
bool isHyphen = false;
int x0 = x;
for ( i=0; i<=len; i++) {
if ( i==len && (!addHyphen || isHyphen) )
break;
Expand Down Expand Up @@ -726,15 +749,16 @@ class LVFreeTypeFace : public LVFont
LVFontGlyphCacheItem * item = _glyph_cache.get( ch );
if ( !item ) {

int rend_flags = FT_LOAD_RENDER | ( !_drawMonochrome ? FT_LOAD_TARGET_NORMAL : (FT_LOAD_TARGET_MONO) ); //|FT_LOAD_MONOCHROME|FT_LOAD_FORCE_AUTOHINT
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Glyph( _face, /* handle to face object */
ch_glyph_index, /* glyph index */
FT_LOAD_RENDER ); /* load flags, see below */
rend_flags ); /* load flags, see below */
if ( error ) {
continue; /* ignore errors */
}

item = LVFontGlyphCacheItem::newItem( &_glyph_cache, ch, _slot );
item = LVFontGlyphCacheItem::newItem( &_glyph_cache, ch, _slot, _drawMonochrome );
_glyph_cache.put( item );
}
if ( item && !isHyphen || i>=len-1 ) { // avoid soft hyphens inside text string
Expand All @@ -750,6 +774,23 @@ class LVFreeTypeFace : public LVFont
previous = ch_glyph_index;
}
}
if ( flags & LTEXT_TD_MASK ) {
// text decoration: underline, etc.
int h = _size > 30 ? 2 : 1;
lUInt32 cl = buf->GetTextColor();
if ( flags & LTEXT_TD_UNDERLINE || flags & LTEXT_TD_BLINK ) {
int liney = y + _baseline + h;
buf->FillRect( x0, liney, x, liney+h, cl );
}
if ( flags & LTEXT_TD_OVERLINE ) {
int liney = y + h;
buf->FillRect( x0, liney, x, liney+h, cl );
}
if ( flags & LTEXT_TD_LINE_THROUGH ) {
int liney = y + _size/2 - h/2;
buf->FillRect( x0, liney, x, liney+h, cl );
}
}
}

/// returns true if font is empty
Expand Down Expand Up @@ -888,7 +929,7 @@ class LVFreeTypeFontManager : public LVFontManager
//}

//printf("going to load font file %s\n", fname.c_str());
if (font->loadFromFile( pathname.c_str(), item->getDef()->getIndex(), size, family ) )
if (font->loadFromFile( pathname.c_str(), item->getDef()->getIndex(), size, family, false ) )
{
//fprintf(_log, " : loading from file %s : %s %d\n", item->getDef()->getName().c_str(),
// item->getDef()->getTypeFace().c_str(), item->getDef()->getSize() );
Expand Down Expand Up @@ -1380,7 +1421,7 @@ int LVFontDef::CalcMatch( const LVFontDef & def ) const

void LVBaseFont::DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen )
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags )
{
static lUInt8 glyph_buf[16384];
LVFont::glyph_info_t info;
Expand Down Expand Up @@ -1877,7 +1918,7 @@ lUInt16 LVWin32DrawFont::measureText(
/// draws text string
void LVWin32DrawFont::DrawTextString( LVDrawBuf * buf, int x, int y,
const lChar16 * text, int len,
lChar16 def_char, lUInt32 * palette, bool addHyphen )
lChar16 def_char, lUInt32 * palette, bool addHyphen, lUInt32 flags )
{
if (_hfont==NULL)
return;
Expand Down
Loading

0 comments on commit 2a8a198

Please sign in to comment.