webengine/osswebengine/WebKit/win/WebKitGraphics.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2007 Apple Inc.  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 "WebKitGraphics.h"
       
    28 
       
    29 #include "WebKit.h"
       
    30 #include "WebKitDLL.h"
       
    31 
       
    32 #pragma warning(push, 0)
       
    33 #include <WebCore/CharacterNames.h>
       
    34 #include <WebCore/Font.h>
       
    35 #include <WebCore/FontDescription.h>
       
    36 #include <WebCore/GraphicsContext.h>
       
    37 #include <WebCore/PlatformString.h>
       
    38 #include <WebCore/StringTruncator.h>
       
    39 #include <WebCore/WebCoreTextRenderer.h>
       
    40 
       
    41 #include <CoreGraphics/CoreGraphics.h>
       
    42 #pragma warning(pop)
       
    43 
       
    44 #include <WebKitSystemInterface/WebKitSystemInterface.h>
       
    45 
       
    46 using namespace WebCore;
       
    47 
       
    48 static Font makeFont(const WebFontDescription& description)
       
    49 {
       
    50     AtomicString::init();
       
    51 
       
    52     String fontFamilyString(description.family, description.familyLength);
       
    53 
       
    54     FontDescription f;
       
    55     FontFamily family;
       
    56     family.setFamily(fontFamilyString);
       
    57     f.setFamily(family);
       
    58     f.setSpecifiedSize(description.size);
       
    59     f.setComputedSize(description.size);
       
    60     f.setItalic(description.italic);
       
    61     f.setBold(description.bold);
       
    62     f.setIsAbsoluteSize(true);
       
    63 
       
    64     Font font(f, 0, 0);
       
    65     font.update();
       
    66 
       
    67     return font;
       
    68 }
       
    69 
       
    70 void DrawTextAtPoint(CGContextRef cgContext, LPCTSTR text, int length, POINT point, const WebFontDescription& description, CGColorRef color, int underlinedIndex, bool drawAsPassword)
       
    71 {
       
    72     GraphicsContext context(cgContext);
       
    73 
       
    74     ASSERT(CGColorGetNumberOfComponents(color) == 4);   // this code assumes the CGColorRef has 4 components
       
    75     const CGFloat* components = CGColorGetComponents(color);
       
    76     Color textColor((int)(components[0] * 255), (int)(components[1] * 255), (int)(components[2] * 255), (int)(components[3] * 255));
       
    77 
       
    78     String drawString(text, length);
       
    79     if (drawAsPassword)
       
    80         drawString = drawString.impl()->secure(WebCore::bullet);
       
    81     WebCoreDrawTextAtPoint(context, drawString, point, makeFont(description), textColor, underlinedIndex);
       
    82 }
       
    83 
       
    84 void WebDrawText(WebTextRenderInfo* info)
       
    85 {
       
    86     if (!info || info->structSize != sizeof(WebTextRenderInfo) || !info->cgContext || !info->description)
       
    87         return;
       
    88 
       
    89     int oldFontSmoothingLevel = -1;
       
    90     if (info->overrideSmoothingLevel >= 0) {
       
    91         oldFontSmoothingLevel = wkGetFontSmoothingLevel();
       
    92         wkSetFontSmoothingLevel(info->overrideSmoothingLevel);
       
    93     }
       
    94 
       
    95     DrawTextAtPoint(info->cgContext, info->text, info->length, info->pt, *(info->description), info->color, info->underlinedIndex, info->drawAsPassword);
       
    96 
       
    97     if (info->overrideSmoothingLevel >= 0)
       
    98         wkSetFontSmoothingLevel(oldFontSmoothingLevel);
       
    99 }
       
   100 
       
   101 float TextFloatWidth(LPCTSTR text, int length, const WebFontDescription& description)
       
   102 {
       
   103     return WebCoreTextFloatWidth(String(text, length), makeFont(description));
       
   104 }
       
   105 
       
   106 void FontMetrics(const WebFontDescription& description, int* ascent, int* descent, int* lineSpacing)
       
   107 {
       
   108     if (!ascent && !descent && !lineSpacing)
       
   109         return;
       
   110 
       
   111     Font font(makeFont(description));
       
   112 
       
   113     if (ascent)
       
   114         *ascent = font.ascent();
       
   115 
       
   116     if (descent)
       
   117         *descent = font.descent();
       
   118 
       
   119     if (lineSpacing)
       
   120         *lineSpacing = font.lineSpacing();
       
   121 }
       
   122 
       
   123 void CenterTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
       
   124 {
       
   125     ASSERT(buffer);
       
   126 
       
   127     String result = StringTruncator::centerTruncate(String(text, length), width, makeFont(description), false);
       
   128     memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
       
   129     buffer[result.length()] = '\0';
       
   130 }
       
   131 
       
   132 void RightTruncateStringToWidth(LPCTSTR text, int length, const WebFontDescription& description, float width, WCHAR* buffer)
       
   133 {
       
   134     ASSERT(buffer);
       
   135 
       
   136     String result = StringTruncator::rightTruncate(String(text, length), width, makeFont(description), false);
       
   137     memcpy(buffer, result.characters(), result.length() * sizeof(UChar));
       
   138     buffer[result.length()] = '\0';
       
   139 }