Ignore:
Timestamp:
Mar 31, 2009, 2:09:07 PM (15 years ago)
Author:
イグトランス (egtra)
Message:

GDI+をコンパイルできるように修正。FontFamily, Penの追加。サンプルとして、Step 32のGDI+版を制作。
(#56)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/Drawing/Font.ab

    r635 r698  
    11Namespace System
    22Namespace Drawing
    3 
    43
    54/**
     
    87*/
    98
    10 Class FontFamily : End Class
    11 Class FontCollection : End Class
     9Class FontCollection
     10Public
     11    nativeCollection As *GpFontCollection
     12End Class
    1213
    1314Class Font
    1415    Implements System.IDisposable
    1516Public
    16 '    friend class Graphics
    17 
    18     Sub Font(/*IN*/ hdc As HDC)
     17    Static Function FromHDC(/*IN*/ hdc As HDC) As Font
    1918        Dim font = 0 As *GpFont
    20         lastResult = GdipCreateFontFromDC(hdc, font)
    21         SetNativeFont(font)
    22     End Sub
    23 
    24     Sub Font(/*IN*/ hdc As HDC, /*IN const*/ ByRef logfont As LOGFONTA)
     19        SetStatus(GdipCreateFontFromDC(hdc, font))
     20        FromHDC = New Font(font)
     21    End Function
     22
     23    Static Function FromLogFont(/*IN*/ hdc As HDC, /*IN const*/ ByRef logfont As LOGFONTA) As Font
    2524        Dim font = 0 As *GpFont
    26         lastResult =GdipCreateFontFromLogfontA(hdc, logfont, font)
    27         SetNativeFont(font)
    28     End Sub
    29 
    30     Sub Font(/*IN*/ hdc As HDC, /*IN const*/ ByRef logfont As LOGFONTW)
     25        SetStatus(GdipCreateFontFromLogfontA(hdc, logfont, font))
     26        FromLogFont = New Font(font)
     27    End Function
     28
     29    Static Function FromLogFont(/*IN*/ hdc As HDC, /*IN const*/ ByRef logfont As LOGFONTW) As Font
    3130        Dim font = 0 As *GpFont
    32         lastResult =GdipCreateFontFromLogfontW(hdc, logfont, font)
    33         SetNativeFont(font)
    34     End Sub
    35 
    36     Sub Font(/*IN*/ hdc As HDC, /*IN const*/ hfont As HFONT)
     31        SetStatus(GdipCreateFontFromLogfontW(hdc, logfont, font))
     32        FromLogFont = New Font(font)
     33    End Function
     34
     35    Static Function FromHFont(/*IN*/ hdc As HDC, /*IN const*/ hfont As HFONT) As Font
    3736        Dim font = 0 As *GpFont
    3837        If hfont <> 0 Then
    3938            Dim lf As LOGFONTA
    40             If GetObjectA(hfont, sizeof (LOGFONTA), lf) <> 0 Then
    41                 lastResult = GdipCreateFontFromLogfontA(hdc, lf, font)
     39            If GetObjectA(hfont, Len(lf), lf) <> 0 Then
     40                SetStatus(GdipCreateFontFromLogfontA(hdc, lf, font))
    4241            Else
    43                 lastResult = GdipCreateFontFromDC(hdc, font)
     42                SetStatus(GdipCreateFontFromDC(hdc, font))
    4443            End If
    4544        Else
    46             lastResult = GdipCreateFontFromDC(hdc, font)
    47         End If
    48         SetNativeFont(font)
     45            SetStatus(GdipCreateFontFromDC(hdc, font))
     46        End If
     47        FromHFont = New Font(font)
     48    End Function
     49
     50    Sub Font(f As *GpFont)
     51        nativeFont = f
    4952    End Sub
    5053
    5154    Sub Font(/*IN const*/ family As FontFamily, /*IN*/ emSize As Single)
    52         Font(family, emSize, FontStyleRegular, UnitPoint)
    53     End Sub
    54 
    55     Sub Font(/*IN const*/ family As FontFamily,
    56         /*IN*/ emSize As Single, /*IN*/ style As Long)
    57 
    58         Font(family, emSize, style, UnitPoint)
     55        initFont(family, emSize, FontStyle.Regular, GraphicsUnit.Point)
     56    End Sub
     57
     58    Sub Font(/*IN const*/ family As FontFamily, /*IN*/ emSize As Single, /*IN*/ style As FontStyle)
     59        initFont(family, emSize, style, GraphicsUnit.Point)
     60    End Sub
     61
     62    Sub Font(/*IN const*/ family As FontFamily, /*IN*/ emSize As Single, /*IN*/ unit As GraphicsUnit)
     63        initFont(family, emSize, FontStyle.Regular, unit)
    5964    End Sub
    6065
    6166    Sub Font(/*IN const*/ family As FontFamily, /*IN*/ emSize As Single,
    62         /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit)
    63 
    64         Dim font = 0 As *GpFont
    65         lastResult = GdipCreateFont(
    66             family.NativeFamily, emSize, style, unit, font)
    67         SetNativeFont(font)
     67        /*IN*/ style As FontStyle, /*IN*/ unit As GraphicsUnit)
     68
     69        initFont(family, emSize, style, unit)
    6870    End Sub
    6971
    7072    Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single)
    71         Font(familyName, emSize, FontStyleRegular, Unit.Point, ByVal 0)
     73        initFromName(familyName, emSize, FontStyle.Regular, GraphicsUnit.Point, Nothing)
    7274    End Sub
    7375
    7476    Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single)
    75         Font(familyName, emSize, FontStyleRegular, Unit.Point, ByVal 0)
     77        initFromName(ToWCStr(familyName), emSize, FontStyle.Regular, GraphicsUnit.Point, Nothing)
     78    End Sub
     79
     80    Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single, /*IN*/ style As FontStyle)
     81        initFromName(familyName, emSize, style, GraphicsUnit.Point, Nothing)
     82    End Sub
     83
     84    Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single, /*IN*/ style As FontStyle)
     85        initFromName(ToWCStr(familyName), emSize, style, GraphicsUnit.Point, Nothing)
     86    End Sub
     87
     88    Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single, /*IN*/ unit As GraphicsUnit)
     89        initFromName(familyName, emSize, FontStyle.Regular, unit, Nothing)
     90    End Sub
     91
     92    Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single, /*IN*/ unit As GraphicsUnit)
     93        initFromName(ToWCStr(familyName), emSize, FontStyle.Regular, unit, Nothing)
    7694    End Sub
    7795
    7896    Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single,
    79          /*IN*/ style As Long)
    80         Font(familyName, emSize, style, Unit.Point, ByVal 0)
     97         /*IN*/ style As FontStyle, /*IN*/ unit As GraphicsUnit)
     98
     99        initFromName(familyName, emSize, style, unit, Nothing)
    81100    End Sub
    82101
    83102    Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single,
    84          /*IN*/ style As Long)
    85         Font(familyName, emSize, style, Unit.Point, ByVal 0)
     103         /*IN*/ style As FontStyle, /*IN*/ unit As GraphicsUnit)
     104
     105        initFromName(ToWCStr(familyName), emSize, style, unit, Nothing)
    86106    End Sub
    87107
    88108    Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single,
    89          /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit)
    90         Font(familyName, emSize, style, unit, ByVal 0)
     109        /*IN*/ style As FontStyle, /*IN*/ unit As GraphicsUnit,
     110        /*IN const*/ fontCollection As FontCollection)
     111
     112        initFromName(familyName, emSize, style, unit, fontCollection)
    91113    End Sub
    92114
    93115    Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single,
    94          /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit)
    95         Font(familyName, emSize, style, unit, ByVal 0)
    96     End Sub
    97 
    98     Sub Font(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single,
    99         /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit,
    100         /*IN const*/ ByRef fontCollection As FontCollection)
    101 
    102         nativeFont = 0
    103 
    104         Dim family As FontFamily(familyName, fontCollection)
    105         Dim nativeFamily = family.NativeFamily As *GpFontFamily
    106 
    107         lastResult = family.GetLastStatus()
    108 
    109         If lastResult <> Ok Then
    110             nativeFamily = FontFamily.GenericSansSerif()->NativeFamily
    111             lastResult = FontFamily.GenericSansSerif()->lastResult
    112             If lastResult <> Ok Then
    113                 Exit Sub
    114             End If
    115         End If
    116 
    117         lastResult = GdipCreateFont(
    118             nativeFamily, emSize, style, unit, nativeFont)
    119 
    120         If lastResult <> Ok Then
    121             nativeFamily = FontFamily.GenericSansSerif()->NativeFamily
    122             lastResult = FontFamily.GenericSansSerif()->lastResult
    123             If lastResult <> Ok Then
    124                 Exit Sub
    125             End If
    126 
    127             lastResult = GdipCreateFont(
    128                 nativeFamily, emSize, style, unit, nativeFont)
    129         End If
    130     End Sub
    131 
    132     Sub Font(/*IN const*/ familyName As String, /*IN*/ emSize As Single,
    133         /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit,
     116        /*IN*/ style As FontStyle, /*IN*/ unit As GraphicsUnit,
    134117        /*IN const*/ fontCollection As FontCollection)
    135         Font(ToWCStr(familyName), emSize, style, unit, fontCollection)
    136     End Sub
    137 
    138     Const Function GetLogFontA(/*IN const*/ g As Graphics, /*OUT*/ ByRef lf As LOGFONTA) As Status
    139         Dim nativeGraphics As *GpGraphics
    140         If Not ActiveBasic.IsNothing(g) Then
    141             nativeGraphics = g.nativeGraphics
    142         End If
    143         Return SetStatus(GdipGetLogFontA(nativeFont, nativeGraphics, lf))
    144     End Function
    145 
    146     Const Function GetLogFontW(/*IN const*/ g As Graphics, /*OUT*/ ByRef lf As LOGFONTW) As Status
    147         Dim nativeGraphics As *GpGraphics
    148         If Not ActiveBasic.IsNothing(g) Then
    149             nativeGraphics = g.nativeGraphics
    150         End If
    151         Return SetStatus(GdipGetLogFontW(nativeFont, nativeGraphics, lf))
    152     End Function
    153 
    154     Const Function GetLogFont(/*IN const*/ g As Graphics, /*OUT*/ ByRef lf As LOGFONT) As Status
    155         Dim nativeGraphics As *GpGraphics
    156         If Not ActiveBasic.IsNothing(g) Then
    157             nativeGraphics = g.nativeGraphics
    158         End If
    159 #ifdef UNICODE
    160         Return SetStatus(GdipGetLogFontW(nativeFont, nativeGraphics, lf))
    161 #else
    162         Return SetStatus(GdipGetLogFontA(nativeFont, nativeGraphics, lf))
    163 #endif
    164     End Function
     118
     119        initFromName(ToWCStr(familyName), emSize, style, unit, fontCollection)
     120    End Sub
    165121
    166122    Const Function Clone() As Font
    167123        Dim cloneFont = 0 As *GpFont
    168124        SetStatus(GdipCloneFont(nativeFont, cloneFont))
    169         Return New Font(cloneFont, lastResult)
    170     End Function
     125        Clone = New Font(cloneFont)
     126    End Function
     127
     128    Sub Dispose()
     129        If nativeFont <> 0 Then
     130            GdipDeleteFont(nativeFont)
     131        End If
     132        nativeFont = 0
     133    End Sub
    171134
    172135    Sub ~Font()
    173         GdipDeleteFont(nativeFont)
    174     End Sub
    175 
    176     Const Function IsAvailable() As Boolean
    177         Return nativeFont <> 0
    178     End Function
    179 
    180     Const Function Style() As Long
    181         SetStatus(GdipGetFontStyle(nativeFont, Style))
    182     End Function
     136        Dispose()
     137    End Sub
     138
     139'   Const Function Style() As FontStyle
     140'       SetStatus(GdipGetFontStyle(nativeFont, Style))
     141'   End Function
    183142
    184143    Const Function Size() As Single
     
    186145    End Function
    187146
    188     Const Function SizeInPoints() As Single
    189 
    190     Const Function Unit() As GraphicsUnit
    191         SetStatus(GdipGetFontUnit(nativeFont, Unit))
    192     End Function
    193 
    194     Const Function LastStatus() As Status
    195         Return lastResult
    196     End Function
     147'   Const Function SizeInPoints() As Single
     148'   End Function
     149
     150'   Const Function Unit() As GraphicsUnit
     151'       SetStatus(GdipGetFontUnit(nativeFont, Unit))
     152'   End Function
    197153
    198154    Const Function Height() As Long
    199         Return GetHeight() As Long
     155        Height = GetHeight() As Long
    200156    End Function
    201157
     
    205161
    206162    Const Function GetHeight(/*IN const*/ g As Graphics) As Single
    207         Dim nativeGraphics As *GpGraphics
    208         If Not ActiveBasic.IsNothing(g) Then
    209             nativeGraphics = g.NativeGraphics
    210         End If
     163        Dim nativeGraphics = getNativeGraphics(g)
    211164        SetStatus(GdipGetFontHeight(nativeFont, nativeGraphics, GetHeight))
    212165    End Function
     
    216169    End Function
    217170
    218 '    Const Function FontFamily(/*OUT*/ ByRef family As FontFamily)
    219 '       If VarPtr(family) = 0 Then
    220 '           Return SetStatus(Status.InvalidParameter)
    221 '       End If
    222 '       Dim status = GdipGetFamily(nativeFont, family->nativeFamily)
    223 '       family->SetStatus(status)
    224 '       Return SetStatus(status)
     171'    Const Function FontFamily() As FontFamily
     172'       Dim nativeFamily As *GpFamily
     173'       SetStatus(GdipGetFamily(nativeFont, nativeFamily))
     174'       FontFamily = New FontFamily(nativeFamily)
    225175'   End Function
    226176
    227177    Const Function Bold() As Boolean
    228178        Dim lf As LOGFONT
    229         GetLogFont(0, lf)
     179        ToLogFont(lf)
    230180        Return lf.lfWeight > FW_BOLD
    231181    End Function
     
    233183    Const Function GdiCharSet() As Byte
    234184        Dim lf As LOGFONT
    235         GetLogFont(0, lf)
     185        ToLogFont(lf)
    236186        Return lf.lfCharSet
    237187    End Function
     
    239189    'Const Function GdiVerticalFont() As Boolean
    240190
    241     Const Function NativeFont() As *GpFont
    242         Return nativeFont
    243     End Function
    244 
    245191    'Const Function IsSystemFont() As Boolean
    246192
    247193    Const Function Italic() As Boolean
    248194        Dim lf As LOGFONT
    249         GetLogFont(0, lf)
    250         Return lf.lfItalic <> FALSE
     195        ToLogFont(lf)
     196        Italic = lf.lfItalic <> FALSE
    251197    End Function
    252198
    253199    Const Function Name() As String
    254 #ifdef UNICODE
    255         Dim lf As LOGFONTW
    256         GetLogFontW(0, lf)
    257 #else
    258         Dim lf As LOGFONTA
    259         GetLogFontA(0, lf)
    260 #endif
    261         Return lf.lfFaceName
    262     End Function
    263 
    264     'Const Function SizeInPoint() As Boolean
     200        Dim lf As LOGFONT
     201        ToLogFont(lf)
     202        Dim p = lf.lfFaceName As PCTSTR
     203        Name = New String(p)
     204    End Function
    265205
    266206    Const Function StrikeOut() As Boolean
    267207        Dim lf As LOGFONT
    268         GetLogFont(0, lf)
    269         Return lf.fdwStrikeOut <> FALSE
     208        ToLogFont(lf)
     209        StrikeOut = lf.lfStrikeOut <> FALSE
    270210    End Function
    271211
    272212    Const Function Style() As FontStyle
    273213        Dim lf As LOGFONT
    274         GetLogFont(0, lf)
    275         Return (((lf.lfWeight > FW_BOLD) And FontStyle.Bold) Or _
    276             ((lf.lfItatlic <> FALSE) And FontStyle.Italic) Or _
    277             ((lf.fdwStrikeOut <> FALSE) And FontStyle.Strickeout) Or _
    278             ((lf.fdwUnderline <> FALSE) And FontStyle.Underline)) As FontStyle
     214        ToLogFont(lf)
     215        Style = FontStyle.Regular
     216        If lf.lfWeight > FW_BOLD Then
     217            Style Or= FontStyle.Bold
     218        End If
     219        If lf.lfItalic <> FALSE Then
     220            Style Or= FontStyle.Italic
     221        End If
     222        If lf.lfStrikeOut <> FALSE Then
     223            Style Or= FontStyle.Strikeout
     224        End If
     225        If lf.lfUnderline <> FALSE Then
     226            Style Or= FontStyle.Underline
     227        End If
    279228    End Function
    280229
     
    283232    Const Function Underline() As Boolean
    284233        Dim lf As LOGFONT
    285         GetLogFont(0, lf)
    286         Return lf.fdwUnderline <> FALSE
     234        ToLogFont(lf)
     235        Underline = lf.lfUnderline <> FALSE
    287236    End Function
    288237
    289238    Override Function ToString() As String
    290         Return Name
     239        ToString = Name
    291240    End Function
    292241
    293242    Const Function ToHfont() As HFONT
    294243        Dim lf As LOGFONT
    295         GetLogFont(ByVal 0, lf)
    296         Return CreateFontIndirect(lf)
    297     End Function
    298 
    299     Const Sub ToLogFont(ByRef lf As LOGFONT)
    300         GetLogFont(ByVal 0, lf)
    301     End Sub
    302 
    303     Const Sub ToLogFont(ByRef lf As LOGFONT, g As Graphics)
    304         GetLogFont(g, lf)
    305     End Sub
     244        ToLogFont(lf)
     245        ToHfont = CreateFontIndirect(lf)
     246    End Function
     247
     248    Const Sub ToLogFont(/*OUT*/ ByRef lf As LOGFONTA, /*IN const*/ g As Graphics)
     249        Dim nativeGraphics = getNativeGraphics(g)
     250        SetStatus(GdipGetLogFontA(nativeFont, nativeGraphics, lf))
     251    End Sub
     252
     253    Const Sub ToLogFont(/*OUT*/ ByRef lf As LOGFONTW, /*IN const*/ g As Graphics)
     254        Dim nativeGraphics = getNativeGraphics(g)
     255        SetStatus(GdipGetLogFontW(nativeFont, nativeGraphics, lf))
     256    End Sub
     257
     258    Const Sub ToLogFont(ByRef lf As LOGFONTA)
     259        ToLogFont(lf, Nothing)
     260    End Sub
     261
     262    Const Sub ToLogFont(ByRef lf As LOGFONTW)
     263        ToLogFont(lf, Nothing)
     264    End Sub
     265
     266    Function NativeFont() As *GpFont
     267        NativeFont = nativeFont
     268    End Function
    306269
    307270Private
    308 '   Sub Font(ByRef f As Font)
    309 
    310 Protected
    311     Sub Font(f As *GpFont, status As Status)
    312         lastResult = status
    313         SetNativeFont(f)
    314     End Sub
    315 
    316     Sub SetNativeFont(f As *GpFont)
    317         nativeFont = f
    318     End Sub
    319 
    320     Const Function SetStatus(s As Status) As Status
    321         If s <> Status.Ok Then
    322             lastResult = s
    323             Return s
     271    Sub initFont(/*IN const*/ family As FontFamily, /*IN*/ emSize As Single,
     272        /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit)
     273
     274        If ActiveBasic.IsNothing(family) Then
     275            Throw New ArgumentNullException("family")
     276        End If
     277        SetStatus(GdipCreateFont(family.NativeFamily, emSize, style, unit, nativeFont))
     278    End Sub
     279
     280    Sub initFromName(/*IN const*/ familyName As PCWSTR, /*IN*/ emSize As Single,
     281        /*IN*/ style As Long, /*IN*/ unit As GraphicsUnit, fontCollection As FontCollection)
     282
     283        nativeFont = 0
     284        Dim family = Nothing As FontFamily
     285        Dim nativeFamily = family.NativeFamily
     286
     287        Try
     288            family = New FontFamily(familyName, fontCollection)
     289        Catch e As Exception
     290            nativeFamily = FontFamily.GenericSansSerif().NativeFamily
     291        End Try
     292
     293        Try
     294            SetStatus(GdipCreateFont(nativeFamily, emSize, style, unit, nativeFont))
     295        Catch e As Exception
     296            nativeFamily = FontFamily.GenericSansSerif().NativeFamily
     297            SetStatus(GdipCreateFont(nativeFamily, emSize, style, unit, nativeFont))
     298        End Try
     299    End Sub
     300
     301    Static Function getNativeGraphics(g As Graphics) As *GpGraphics
     302        If ActiveBasic.IsNothing(g) Then
     303            getNativeGraphics = 0
    324304        Else
    325             Return s
    326         End If
    327     End Function
    328 
    329 Protected
     305            getNativeGraphics = g.NativeGraphics
     306        End If
     307    End Function
     308
    330309    nativeFont As *GpFont
    331     /*mutable*/ lastResult As Status
    332310End Class
    333311
Note: See TracChangeset for help on using the changeset viewer.