C

Ceci est un fragment de code sourc C qui fait parti de PocketKanji :

/* Copyright (C) 2001 Ivan Kanis
 * http://juliva.com/ivan
 *
 * 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 (gpl.html); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <PalmOS.h>
#include <PalmCompatibility.h>

#include "common.h"

#include "font.h"
#include "kanjiconv.h"
#include "kjpad.h"
#include "util.h"

Font font; 

static void bitmapInit(UInt fontSize);
static void fontDrawShino(Kanji *kanji, UInt x, UInt y);
static void fontDrawNative(Kanji *kanji, UInt x, UInt y);

void fontInit()
{
        /* Open the font database
           try the shinome font first */
        font.pFontDb = DmOpenDatabaseByTypeCreator(DB_SHINO_FONT, DB_CREATOR,
                                                                                          dmModeReadWrite);
        if (font.pFontDb) {
                font.fontSize = SHINO_SIZE;
                bitmapInit(font.fontSize);
        } else {
                /* Display characters with Japanese ROM or maybe J-OS */
                FntSetFont (largeFont);
                /* I am assuming kanjis have identical width and height */
                font.fontSize = FntCharHeight();
        }
}

short fontGetSize()
{
        return font.fontSize;
}

void fontTerm()
{
        if (font.pBitmap) {
                MemPtrFree(font.pBitmap);
        }
        font.pBitmap = 0;
}

void fontDrawKanji(Kanji *kanji, UInt x, UInt y)
{
        if (font.pFontDb) {
                fontDrawShino(kanji, x, y);
        } else {
                fontDrawNative(kanji, x, y);
        }
}

/* Draw character using Japanese PalmOS or J-OS */
static void fontDrawNative(Kanji *kanji, UInt x, UInt y)
{
        uchar kanjiChar[2];

        kanjiChar[0] = kanji->c1;
        kanjiChar[1] = kanji->c2;
        euc2sjis(&kanjiChar[0], &kanjiChar[1]);
        WinDrawChars (kanjiChar, 2, x, y);
}

/* Draw characters using internal font */
static void fontDrawShino(Kanji *kanji, UInt x, UInt y)
{
        uchar *fontDb, *bits;
        VoidHand hFont;
        UInt row, col, i, j, actualBytesRow;
        Boolean bValid;
        
        bValid = TRUE;
        
        switch (kanji->c1) {
                case 0xa1:
                        row = 0;
                        break;
                case 0xa4:
                        row = 1;
                        break;
                case 0xa5:
                        row = 2;
                        break;
                default:
                        row = kanji->c1 - 0xb0 + 3;
                        if (row < 0 || row > 0x43) {
                                bValid = FALSE;
                        }
                                
                        break;
        }
        col = kanji->c2 - 0xa1;
        if (col < 0 || col > 0x5e) {
                bValid = FALSE;
        }
        if (bValid) {
                /* calculate the number of bytes in font, unpadded */
                actualBytesRow = font.fontSize / 8;
                bits = (uchar *)font.pBitmap + sizeof (BitmapType);
                hFont = DmQueryRecord(font.pFontDb, row);
                fontDb = MemHandleLock(hFont);
                fontDb += (col * actualBytesRow * font.fontSize);
                for (i = 0; i < font.fontSize; i++) {
                        for (j = 0; j < actualBytesRow; j++, bits++, fontDb++) {
                                *bits = *fontDb;
                        }
                        if (font.bFontPadding) {
                                bits++ ;
                        }
                }
                MemHandleUnlock(hFont);
                WinDrawBitmap (font.pBitmap, x, y);
        }
}

void fontDrawKanjiStr(binstr *str, UInt x, UInt *y, UInt width)
{

        Kanji kanji;
        UInt wrap, i;

        /* wrap is the value of x where we don't have enough space
           to draw another character */
        wrap= width - font.fontSize + 1;
        i = 0;
        while (i < str->len) {
                if (x > wrap) {
                        x = 0;
                        *y += (font.fontSize + FONT_LINE_SPACE);
                }
                kanji.c1 = str->pch[i];
                i++;
                kanji.c2 = str->pch[i];
                i++;
                fontDrawKanji(&kanji, x, *y);
                x += (font.fontSize + FONT_CHAR_SPACE);
        }
}

static void bitmapInit(UInt fontSize)
{
        UInt bitmapSize;
        UInt rowBytes;

        /* divide size by 8 */
        rowBytes = fontSize / 8; 
        /* rowBytes needs to be word aligned */
        if (rowBytes % 2) {
                /* make it an even number */
                rowBytes++;
                /* We are using padding */
                font.bFontPadding = TRUE;
        } else {
                font.bFontPadding = FALSE;
        }
        bitmapSize = sizeof (BitmapType) + (fontSize * rowBytes);
        font.pBitmap = MemPtrNew (bitmapSize);
        if (font.pBitmap) {
                MemSet (font.pBitmap, bitmapSize, 0);
                font.pBitmap->width = fontSize;
                font.pBitmap->height = fontSize;
                font.pBitmap->rowBytes = rowBytes;
        } else {
                error ("Not enough memory for font.", 0, 0);
        }
}

retour