Home | History | Annotate | Download | only in wx
      1 /*
      2  * Copyright (C) 2007 Kevin Ollivier.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "Font.h"
     28 
     29 #include "FontFallbackList.h"
     30 #include "GlyphBuffer.h"
     31 #include "GraphicsContext.h"
     32 #include "IntRect.h"
     33 #include "NotImplemented.h"
     34 #include "SimpleFontData.h"
     35 
     36 #if OS(WINDOWS)
     37 #include "UniscribeController.h"
     38 #endif
     39 
     40 #include <wx/dcclient.h>
     41 #include "fontprops.h"
     42 #include "non-kerned-drawing.h"
     43 
     44 namespace WebCore {
     45 
     46 bool Font::canReturnFallbackFontsForComplexText()
     47 {
     48 #if OS(WINDOWS)
     49     return true;
     50 #else
     51     return false;
     52 #endif
     53 }
     54 
     55 void Font::drawGlyphs(GraphicsContext* graphicsContext, const SimpleFontData* font, const GlyphBuffer& glyphBuffer,
     56                       int from, int numGlyphs, const FloatPoint& point) const
     57 {
     58     // prepare DC
     59     Color color = graphicsContext->fillColor();
     60 
     61     // We can't use wx drawing methods on Win/Linux because they automatically kern text
     62     // so we've created a function with platform dependent drawing implementations that
     63     // will hopefully be folded into wx once the API has solidified.
     64     // see platform/wx/wxcode/<platform> for the implementations.
     65     drawTextWithSpacing(graphicsContext, font, color, glyphBuffer, from, numGlyphs, point);
     66 }
     67 
     68 FloatRect Font::selectionRectForComplexText(const TextRun& run, const IntPoint& point, int h, int from, int to) const
     69 {
     70 #if OS(WINDOWS)
     71     UniscribeController it(this, run);
     72     it.advance(from);
     73     float beforeWidth = it.runWidthSoFar();
     74     it.advance(to);
     75     float afterWidth = it.runWidthSoFar();
     76 
     77     // Using roundf() rather than ceilf() for the right edge as a compromise to ensure correct caret positioning
     78     if (run.rtl()) {
     79         it.advance(run.length());
     80         float totalWidth = it.runWidthSoFar();
     81         return FloatRect(point.x() + floorf(totalWidth - afterWidth), point.y(), roundf(totalWidth - beforeWidth) - floorf(totalWidth - afterWidth), h);
     82     }
     83 
     84     return FloatRect(point.x() + floorf(beforeWidth), point.y(), roundf(afterWidth) - floorf(beforeWidth), h);
     85 #else
     86     notImplemented();
     87     return FloatRect();
     88 #endif
     89 }
     90 
     91 void Font::drawComplexText(GraphicsContext* context, const TextRun& run, const FloatPoint& point, int from, int to) const
     92 {
     93 #if OS(WINDOWS)
     94     // This glyph buffer holds our glyphs + advances + font data for each glyph.
     95     GlyphBuffer glyphBuffer;
     96 
     97     float startX = point.x();
     98     UniscribeController controller(this, run);
     99     controller.advance(from);
    100     float beforeWidth = controller.runWidthSoFar();
    101     controller.advance(to, &glyphBuffer);
    102 
    103     // We couldn't generate any glyphs for the run.  Give up.
    104     if (glyphBuffer.isEmpty())
    105         return;
    106 
    107     float afterWidth = controller.runWidthSoFar();
    108 
    109     if (run.rtl()) {
    110         controller.advance(run.length());
    111         startX += controller.runWidthSoFar() - afterWidth;
    112     } else
    113         startX += beforeWidth;
    114 
    115     // Draw the glyph buffer now at the starting point returned in startX.
    116     FloatPoint startPoint(startX, point.y());
    117     drawGlyphBuffer(context, glyphBuffer, run, startPoint);
    118 #else
    119     notImplemented();
    120 #endif
    121 }
    122 
    123 
    124 float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>* fallbackFonts) const
    125 {
    126 #if OS(WINDOWS)
    127     UniscribeController controller(this, run, fallbackFonts);
    128     controller.advance(run.length());
    129     return controller.runWidthSoFar();
    130 #else
    131     notImplemented();
    132     return 0;
    133 #endif
    134 }
    135 
    136 int Font::offsetForPositionForComplexText(const TextRun& run, int x, bool includePartialGlyphs) const
    137 {
    138 #if OS(WINDOWS)
    139     UniscribeController controller(this, run);
    140     return controller.offsetForPosition(x, includePartialGlyphs);
    141 #else
    142     notImplemented();
    143     return 0;
    144 #endif
    145 }
    146 }
    147