Skip to content

Commit

Permalink
organize font related code and rename field bitmap to bitmaps (#144)
Browse files Browse the repository at this point in the history
* create a separate file fonts.jl

* rename bitmap field of font types to bitmaps

* variable rename char_bitmap to bitmap

* rename extract_bitmap.jl to extract_bitmaps.jl

* variable rename font_bitmap_name to font_instance_name
  • Loading branch information
Sid-Bhatia-0 authored Apr 25, 2022
1 parent 116d55f commit d524fb4
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 266 deletions.
1 change: 1 addition & 0 deletions src/SimpleDraw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include("circle.jl")
include("rectangle.jl")
include("triangle.jl")
include("bitmap.jl")
include("fonts/fonts.jl")
include("character.jl")
include("text.jl")
include("visualize.jl")
Expand Down
64 changes: 0 additions & 64 deletions src/character.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
abstract type AbstractFont end

abstract type AbstractASCIIFont <: AbstractFont end

include("fonts/Terminus_32_16.jl")
include("fonts/Terminus_16_8.jl")

const FONTS = [
TERMINUS_32_16,
TERMINUS_16_8,
]

"""
get_height(font::AbstractFont)
Return the height of a glyph contained in the monospace font `font` along the i-axis (vertical-axis, 1st-axis).
See also [`get_width`](@ref).
# Examples
```julia-repl
julia> get_height(TERMINUS_32_16)
32
julia> get_height(TERMINUS_16_8)
16
```
"""
get_height(font::Terminus_32_16) = 32

"""
get_width(font::AbstractFont)
Return the width of a glyph contained in the monospace font `font` along the j-axis (horizontal-axis, 2nd-axis).
See also [`get_width`](@ref).
# Examples
```julia-repl
julia> get_width(TERMINUS_32_16)
16
julia> get_width(TERMINUS_16_8)
8
```
"""
get_width(font::Terminus_32_16) = 16

get_height(font::Terminus_16_8) = 16
get_width(font::Terminus_16_8) = 8

"""
struct Character{I <: Integer, C <: AbstractChar, F <: AbstractFont} <: AbstractShape
position::Point{I}
Expand Down Expand Up @@ -108,19 +57,6 @@ struct Character{I <: Integer, C <: AbstractChar, F <: AbstractFont} <: Abstract
font::F
end

has_char(font::AbstractASCIIFont, character) = isascii(character) && isprint(character)

function get_bitmap(font, character)
bitmap = font.bitmap

codepoint_begin = codepoint(' ')
k = codepoint(character) - codepoint_begin + one(codepoint_begin)

char_bitmap = @view bitmap[:, :, k]

return char_bitmap
end

get_i_min(shape::Character) = shape.position.i
get_i_max(shape::Character) = isprint(shape.character) ? shape.position.i + get_height(shape.font) - one(shape.position.i) : shape.position.i

Expand Down
10 changes: 5 additions & 5 deletions src/fonts/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fonts

`extract_bitmap.jl` is a helper script that parses a `.bdf` bitmap font file and generates a `.jl` file containing the bitmap for the font glyphs as a `BitArray{3}`.
`extract_bitmaps.jl` is a helper script that parses a `.bdf` bitmap font file and generates a `.jl` file containing the bitmaps for the font glyphs as a `BitArray{3}`.

The script is very limited at this point and can only process fonts where the width of the font is a multiple of 4.

Expand All @@ -9,18 +9,18 @@ The generated julia file for a font is then included in `/src/character.jl` (aft
Here is an example usage:

```julia-repl
julia> include("extract_bitmap.jl")
julia> include("extract_bitmaps.jl")
generate_julia_font_file (generic function with 1 method)
julia> include("extract_bitmap.jl");
julia> include("extract_bitmaps.jl");
julia> font_height = 16; font_width = 8; unicode_codepoints_to_take = 32:126;
julia> font_file = "../../fonts/terminus-font-4.49.1/ter-u16n.bdf";
julia> font_type_name = "Terminus_16_8"; font_bitmap_name = "TERMINUS_16_8";
julia> font_type_name = "Terminus_16_8"; font_instance_name = "TERMINUS_16_8";
julia> font_info = FontInfo(font_height, font_width, unicode_codepoints_to_take, font_file, font_type_name, font_bitmap_name)
julia> font_info = FontInfo(font_height, font_width, unicode_codepoints_to_take, font_file, font_type_name, font_instance_name)
FontInfo(16, 8, 32:126, "../../fonts/terminus-font-4.49.1/ter-u16n.bdf", "Terminus_16_8", "TERMINUS_16_8")
julia> generate_julia_font_file(font_info)
Expand Down
Loading

0 comments on commit d524fb4

Please sign in to comment.